use of java.util.Scanner in project camel by apache.
the class ExpressionBuilder method tokenizeExpression.
/**
* Returns a tokenize expression which will tokenize the string with the
* given token
*/
public static Expression tokenizeExpression(final Expression expression, final String token) {
return new ExpressionAdapter() {
public Object evaluate(Exchange exchange) {
String text = simpleExpression(token).evaluate(exchange, String.class);
Object value = expression.evaluate(exchange, Object.class);
Scanner scanner = ObjectHelper.getScanner(exchange, value);
scanner.useDelimiter(text);
return scanner;
}
@Override
public String toString() {
return "tokenize(" + expression + ", " + token + ")";
}
};
}
use of java.util.Scanner in project camel by apache.
the class FileDataSet method readSourceFile.
// Implementation methods
//-------------------------------------------------------------------------
private void readSourceFile() throws IOException {
List<Object> bodies = new LinkedList<>();
try (BufferedReader br = new BufferedReader(new FileReader(sourceFile))) {
Scanner scanner = new Scanner(br);
scanner.useDelimiter(delimiter);
while (scanner.hasNext()) {
String nextPayload = scanner.next();
if ((nextPayload != null) && (nextPayload.length() > 0)) {
bodies.add(nextPayload);
}
}
setDefaultBodies(bodies);
}
}
use of java.util.Scanner in project camel by apache.
the class FileStateRepository method loadStore.
/**
* Loads the given file store into the 1st level cache
*/
protected void loadStore() throws IOException {
// auto create starting directory if needed
if (!fileStore.exists()) {
LOG.debug("Creating filestore: {}", fileStore);
File parent = fileStore.getParentFile();
if (parent != null) {
parent.mkdirs();
}
boolean created = FileUtil.createNewFile(fileStore);
if (!created) {
throw new IOException("Cannot create filestore: " + fileStore);
}
}
LOG.trace("Loading to 1st level cache from state filestore: {}", fileStore);
cache.clear();
Scanner scanner = null;
try {
scanner = new Scanner(fileStore);
scanner.useDelimiter(STORE_DELIMITER);
while (scanner.hasNextLine()) {
String line = scanner.nextLine();
int separatorIndex = line.indexOf(KEY_VALUE_DELIMITER);
String key = line.substring(0, separatorIndex);
String value = line.substring(separatorIndex + KEY_VALUE_DELIMITER.length());
cache.put(key, value);
}
} catch (IOException e) {
throw ObjectHelper.wrapRuntimeCamelException(e);
} finally {
if (scanner != null) {
scanner.close();
}
}
LOG.debug("Loaded {} to the 1st level cache from state filestore: {}", cache.size(), fileStore);
}
use of java.util.Scanner in project camel by apache.
the class ObjectHelper method getScanner.
/**
* Creates a {@link Scanner} for scanning the given value.
*
* @param exchange the current exchange
* @param value the value, typically the message IN body
* @return the scanner, is newer <tt>null</tt>
*/
public static Scanner getScanner(Exchange exchange, Object value) {
if (value instanceof WrappedFile) {
WrappedFile<?> gf = (WrappedFile<?>) value;
Object body = gf.getBody();
if (body != null) {
// we have loaded the file content into the body so use that
value = body;
} else {
// generic file is just a wrapper for the real file so call again with the real file
return getScanner(exchange, gf.getFile());
}
}
String charset = exchange.getProperty(Exchange.CHARSET_NAME, String.class);
Scanner scanner = null;
if (value instanceof Readable) {
scanner = new Scanner((Readable) value);
} else if (value instanceof InputStream) {
scanner = charset == null ? new Scanner((InputStream) value) : new Scanner((InputStream) value, charset);
} else if (value instanceof File) {
try {
scanner = charset == null ? new Scanner((File) value) : new Scanner((File) value, charset);
} catch (FileNotFoundException e) {
throw new RuntimeCamelException(e);
}
} else if (value instanceof String) {
scanner = new Scanner((String) value);
} else if (value instanceof ReadableByteChannel) {
scanner = charset == null ? new Scanner((ReadableByteChannel) value) : new Scanner((ReadableByteChannel) value, charset);
}
if (scanner == null) {
// value is not a suitable type, try to convert value to a string
String text = exchange.getContext().getTypeConverter().convertTo(String.class, exchange, value);
if (text != null) {
scanner = new Scanner(text);
}
}
if (scanner == null) {
scanner = new Scanner("");
}
return scanner;
}
use of java.util.Scanner in project camel by apache.
the class GroupTokenIteratorTest method testGroupIteratorWithDifferentEncodingFromDefault.
public void testGroupIteratorWithDifferentEncodingFromDefault() throws Exception {
if (Charset.defaultCharset() == StandardCharsets.UTF_8) {
// can't think of test case where having default charset set to UTF-8 is affected
return;
}
byte[] buf = "£1\n£2\n".getBytes(StandardCharsets.UTF_8);
ByteArrayInputStream in = new ByteArrayInputStream(buf);
Scanner scanner = new Scanner(in, StandardCharsets.UTF_8.displayName());
scanner.useDelimiter("\n");
exchange.setProperty(Exchange.CHARSET_NAME, StandardCharsets.UTF_8.displayName());
GroupTokenIterator gi = new GroupTokenIterator(exchange, scanner, "\n", 1, false);
assertTrue(gi.hasNext());
assertEquals("£1", gi.next());
assertEquals("£2", gi.next());
assertFalse(gi.hasNext());
IOHelper.close(gi);
}
Aggregations