Search in sources :

Example 1 with LineTokenizer

use of org.apache.tools.ant.util.LineTokenizer in project ant by apache.

the class Tokens method getCollection.

/**
 * Sort the contained elements.
 * @return a Collection of Resources.
 */
protected synchronized Collection<Resource> getCollection() {
    ResourceCollection rc = getResourceCollection();
    if (rc.isEmpty()) {
        return Collections.emptySet();
    }
    if (tokenizer == null) {
        tokenizer = new LineTokenizer();
    }
    try (ConcatResourceInputStream cat = new ConcatResourceInputStream(rc);
        InputStreamReader rdr = new InputStreamReader(cat, encoding == null ? Charset.defaultCharset() : Charset.forName(encoding))) {
        cat.setManagingComponent(this);
        List<Resource> result = new ArrayList<>();
        for (String s = tokenizer.getToken(rdr); s != null; s = tokenizer.getToken(rdr)) {
            // do not send the Project to the constructor of StringResource, since
            // the semantics of that constructor clearly state that property value
            // replacement takes place on the passed string value. We don't want
            // that to happen.
            final StringResource resource = new StringResource(s);
            resource.setProject(getProject());
            result.add(resource);
        }
        return result;
    } catch (IOException e) {
        throw new BuildException("Error reading tokens", e);
    }
}
Also used : ConcatResourceInputStream(org.apache.tools.ant.util.ConcatResourceInputStream) InputStreamReader(java.io.InputStreamReader) LineTokenizer(org.apache.tools.ant.util.LineTokenizer) Resource(org.apache.tools.ant.types.Resource) ArrayList(java.util.ArrayList) IOException(java.io.IOException) BuildException(org.apache.tools.ant.BuildException) ResourceCollection(org.apache.tools.ant.types.ResourceCollection)

Example 2 with LineTokenizer

use of org.apache.tools.ant.util.LineTokenizer in project ant by apache.

the class Translate method translateOneFile.

private void translateOneFile(File src, File dest) throws IOException {
    try (BufferedWriter out = new BufferedWriter(new OutputStreamWriter(Files.newOutputStream(dest.toPath()), destEncoding));
        BufferedReader in = new BufferedReader(new InputStreamReader(Files.newInputStream(src.toPath()), srcEncoding))) {
        LineTokenizer lineTokenizer = new LineTokenizer();
        lineTokenizer.setIncludeDelims(true);
        String line = lineTokenizer.getToken(in);
        while (line != null) {
            // 2003-02-21 new replace algorithm by tbee (tbee@tbee.org)
            // because it wasn't able to replace something like "@aaa;@bbb;"
            // is there a startToken
            // and there is still stuff following the startToken
            int startIndex = line.indexOf(startToken);
            while (startIndex >= 0 && (startIndex + startToken.length()) <= line.length()) {
                // the new value, this needs to be here
                // because it is required to calculate the next position to
                // search from at the end of the loop
                String replace = null;
                // we found a starttoken, is there an endtoken following?
                // start at token+tokenlength because start and end
                // token may be identical
                int endIndex = line.indexOf(endToken, startIndex + startToken.length());
                if (endIndex < 0) {
                    startIndex += 1;
                } else {
                    // grab the token
                    String token = line.substring(startIndex + startToken.length(), endIndex);
                    // If there is a white space or = or :, then
                    // it isn't to be treated as a valid key.
                    boolean validToken = true;
                    for (int k = 0; k < token.length() && validToken; k++) {
                        char c = token.charAt(k);
                        if (c == ':' || c == '=' || Character.isSpaceChar(c)) {
                            validToken = false;
                        }
                    }
                    if (!validToken) {
                        startIndex += 1;
                    } else {
                        // find the replace string
                        if (resourceMap.containsKey(token)) {
                            replace = resourceMap.get(token);
                        } else {
                            log("Replacement string missing for: " + token, Project.MSG_VERBOSE);
                            replace = startToken + token + endToken;
                        }
                        // generate the new line
                        line = line.substring(0, startIndex) + replace + line.substring(endIndex + endToken.length());
                        // set start position for next search
                        startIndex += replace.length();
                    }
                }
                // find next starttoken
                startIndex = line.indexOf(startToken, startIndex);
            }
            out.write(line);
            line = lineTokenizer.getToken(in);
        }
    }
}
Also used : InputStreamReader(java.io.InputStreamReader) BufferedReader(java.io.BufferedReader) LineTokenizer(org.apache.tools.ant.util.LineTokenizer) OutputStreamWriter(java.io.OutputStreamWriter) BufferedWriter(java.io.BufferedWriter)

Example 3 with LineTokenizer

use of org.apache.tools.ant.util.LineTokenizer in project ant by apache.

the class TokenFilter method read.

/**
 * Returns the next character in the filtered stream, only including
 * lines from the original stream which match all of the specified
 * regular expressions.
 *
 * @return the next character in the resulting stream, or -1
 * if the end of the resulting stream has been reached
 *
 * @exception IOException if the underlying stream throws an IOException
 * during reading
 */
public int read() throws IOException {
    if (tokenizer == null) {
        tokenizer = new LineTokenizer();
    }
    while (line == null || line.length() == 0) {
        line = tokenizer.getToken(in);
        if (line == null) {
            return -1;
        }
        for (Enumeration<Filter> e = filters.elements(); e.hasMoreElements(); ) {
            Filter filter = e.nextElement();
            line = filter.filter(line);
            if (line == null) {
                break;
            }
        }
        linePos = 0;
        if (line != null) {
            if (tokenizer.getPostToken().length() != 0) {
                if (delimOutput != null) {
                    line = line + delimOutput;
                } else {
                    line = line + tokenizer.getPostToken();
                }
            }
        }
    }
    int ch = line.charAt(linePos);
    linePos++;
    if (linePos == line.length()) {
        line = null;
    }
    return ch;
}
Also used : LineTokenizer(org.apache.tools.ant.util.LineTokenizer)

Aggregations

LineTokenizer (org.apache.tools.ant.util.LineTokenizer)3 InputStreamReader (java.io.InputStreamReader)2 BufferedReader (java.io.BufferedReader)1 BufferedWriter (java.io.BufferedWriter)1 IOException (java.io.IOException)1 OutputStreamWriter (java.io.OutputStreamWriter)1 ArrayList (java.util.ArrayList)1 BuildException (org.apache.tools.ant.BuildException)1 Resource (org.apache.tools.ant.types.Resource)1 ResourceCollection (org.apache.tools.ant.types.ResourceCollection)1 ConcatResourceInputStream (org.apache.tools.ant.util.ConcatResourceInputStream)1