Search in sources :

Example 1 with Scanner

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 + ")";
        }
    };
}
Also used : Exchange(org.apache.camel.Exchange) Scanner(java.util.Scanner) ExpressionAdapter(org.apache.camel.support.ExpressionAdapter)

Example 2 with Scanner

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);
    }
}
Also used : Scanner(java.util.Scanner) BufferedReader(java.io.BufferedReader) FileReader(java.io.FileReader) LinkedList(java.util.LinkedList)

Example 3 with Scanner

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);
}
Also used : Scanner(java.util.Scanner) IOException(java.io.IOException) File(java.io.File)

Example 4 with Scanner

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;
}
Also used : Scanner(java.util.Scanner) ReadableByteChannel(java.nio.channels.ReadableByteChannel) WrappedFile(org.apache.camel.WrappedFile) InputStream(java.io.InputStream) FileNotFoundException(java.io.FileNotFoundException) RuntimeCamelException(org.apache.camel.RuntimeCamelException) File(java.io.File) WrappedFile(org.apache.camel.WrappedFile)

Example 5 with 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);
}
Also used : Scanner(java.util.Scanner) ByteArrayInputStream(java.io.ByteArrayInputStream)

Aggregations

Scanner (java.util.Scanner)567 File (java.io.File)97 IOException (java.io.IOException)63 NoSuchElementException (java.util.NoSuchElementException)59 ArrayList (java.util.ArrayList)56 Test (org.junit.Test)53 InputStream (java.io.InputStream)51 ServiceBuilder (com.github.scribejava.core.builder.ServiceBuilder)49 OAuthRequest (com.github.scribejava.core.model.OAuthRequest)49 Response (com.github.scribejava.core.model.Response)49 InputMismatchException (java.util.InputMismatchException)47 FileNotFoundException (java.io.FileNotFoundException)42 OAuth20Service (com.github.scribejava.core.oauth.OAuth20Service)31 OAuth2AccessToken (com.github.scribejava.core.model.OAuth2AccessToken)29 FileInputStream (java.io.FileInputStream)27 HashMap (java.util.HashMap)26 Locale (java.util.Locale)23 OAuth1AccessToken (com.github.scribejava.core.model.OAuth1AccessToken)18 OAuth1RequestToken (com.github.scribejava.core.model.OAuth1RequestToken)18 OAuth10aService (com.github.scribejava.core.oauth.OAuth10aService)18