Example usage for java.io PushbackReader PushbackReader

List of usage examples for java.io PushbackReader PushbackReader

Introduction

In this page you can find the example usage for java.io PushbackReader PushbackReader.

Prototype

public PushbackReader(Reader in, int size) 

Source Link

Document

Creates a new pushback reader with a pushback buffer of the given size.

Usage

From source file:Main.java

public static void main(String[] args) {
    try {//from w ww.j  a  v  a2  s.c om
        String s = "from java2s.com";

        StringReader sr = new StringReader(s);

        // create a new PushBack reader based on our string reader
        PushbackReader pr = new PushbackReader(sr, 20);

        // read the first five chars
        for (int i = 0; i < 5; i++) {
            char c = (char) pr.read();
            System.out.println(c);
        }
        // try to reset
        pr.reset();

        pr.close();

    } catch (IOException ex) {
        ex.printStackTrace();
    }
}

From source file:net.sf.jabref.importer.fileformat.BibtexParser.java

public BibtexParser(Reader in) {
    Objects.requireNonNull(in);

    pushbackReader = new PushbackReader(in, BibtexParser.LOOKAHEAD);
}

From source file:com.abstratt.mdd.internal.frontend.textuml.TextUMLCompiler.java

public String findModelName(String toParse) {
    PushbackReader in = new PushbackReader(new StringReader(toParse), 1);
    Lexer lexer = new Lexer(in);
    Parser parser = new Parser(lexer);
    try {//from  ww  w  . j a  v  a2s  .  c o m
        Start parsed = parser.parse();
        final String[] packageIdentifier = { null };
        parsed.getPStart().apply(new DepthFirstAdapter() {
            @Override
            public void caseAStart(AStart node) {
                ((APackageHeading) node.getPackageHeading()).getQualifiedIdentifier()
                        .apply(new DepthFirstAdapter() {
                            @Override
                            public void caseTIdentifier(TIdentifier node) {
                                if (packageIdentifier[0] == null)
                                    packageIdentifier[0] = Util.stripEscaping(node.getText());
                            }
                        });
            }
        });
        return packageIdentifier[0];
    } catch (RuntimeException e) {
        throw e;
    } catch (Exception e) {
        // fall through
    } finally {
        try {
            in.close();
        } catch (IOException e) {
            // not interested in failures while closing
        }
    }
    return null;
}

From source file:com.abstratt.mdd.internal.frontend.textuml.TextUMLCompiler.java

public String format(String toFormat) {
    PushbackReader in = new PushbackReader(new StringReader(toFormat), 64 * 1024);
    Lexer lexer = new Lexer(in);
    Parser parser = new Parser(lexer);
    try {//from  w ww  . j ava 2  s .c om
        Start parsed = parser.parse();
        return new TextUMLFormatter().format(parsed.getPStart(), parser.ignoredTokens);
    } catch (RuntimeException e) {
        throw e;
    } catch (Exception e) {
        // fall through
    } finally {
        try {
            in.close();
        } catch (IOException e) {
            // not interested in failures while closing
        }
    }
    return toFormat;
}

From source file:com.bigdata.rdf.rio.ntriples.BigdataNTriplesParser.java

/**
 * Implementation of the <tt>parse(Reader, String)</tt> method defined in
 * the RDFParser interface./* w w  w.ja v a2  s . com*/
 * 
 * @param reader
 *        The Reader from which to read the data, must not be <tt>null</tt>.
 * @param baseURI
 *        The URI associated with the data in the Reader, must not be
 *        <tt>null</tt>.
 * @throws IOException
 *         If an I/O error occurred while data was read from the InputStream.
 * @throws RDFParseException
 *         If the parser has found an unrecoverable parse error.
 * @throws RDFHandlerException
 *         If the configured statement handler encountered an unrecoverable
 *         error.
 * @throws IllegalArgumentException
 *         If the supplied reader or base URI is <tt>null</tt>.
 */
public synchronized void parse(final Reader reader, final String baseURI)
        throws IOException, RDFParseException, RDFHandlerException {
    if (reader == null) {
        throw new IllegalArgumentException("Reader can not be 'null'");
    }
    if (baseURI == null) {
        throw new IllegalArgumentException("base URI can not be 'null'");
    }

    rdfHandler.startRDF();

    // We need pushback for '<<' versus '<'.
    this.reader = new PushbackReader(reader, 1/* size */);
    lineNo = 1;

    reportLocation(lineNo, 1);
    push(new State());
    try {
        int c = reader.read();
        c = skipWhitespace(c);

        while (c != -1) {
            if (c == '#') {
                // Comment, ignore
                c = skipLine(c);
            } else if (c == '\r' || c == '\n') {
                // Empty line, ignore
                c = skipLine(c);
            } else {
                c = parseTriple(c, false/* embedded */);
            }

            c = skipWhitespace(c);
        }
    } finally {
        clear();
    }

    rdfHandler.endRDF();
}

From source file:com.abstratt.mdd.internal.frontend.textuml.TextUMLCompiler.java

private Start parse(Reader source, IProblemTracker problems) throws CoreException {
    ProblemBuilder<Node> problemBuilder = new ProblemBuilder<Node>(problems, new SCCTextUMLSourceMiner());
    PushbackReader in = new PushbackReader(source, 64 * 1024);
    Lexer lexer = new Lexer(in);
    Parser parser = new Parser(lexer);
    try {//from www  . j a  va 2s.co m
        return parser.parse();
    } catch (ParserException e) {
        if (problems != null)
            problemBuilder.addProblem(
                    new SyntaxProblem("Found: '" + e.getToken().getText() + "'. " + e.getMessage()),
                    e.getToken());
    } catch (LexerException e) {
        if (problems != null) {
            SyntaxProblem problem = new SyntaxProblem(e.getMessage());
            problem.setAttribute(IProblem.LINE_NUMBER, SCCTextUMLSourceMiner.parseLineNumber(e.getMessage()));
            problemBuilder.addProblem(problem, null);
        }
    } catch (IOException e) {
        IStatus status = new Status(IStatus.ERROR, TextUMLConstants.PLUGIN_ID, 0,
                "Error reading source unit: " + source.toString(), e);
        throw new CoreException(status);
    } finally {
        try {
            in.close();
        } catch (IOException e) {
            // not interested in failures while closing
        }
    }
    return null;
}

From source file:org.deegree.framework.xml.XMLFragment.java

/**
 * Initializes the <code>XMLFragment</code> with the content from the given <code>Reader</code>. Sets the SystemId,
 * too.//from  www  .j a v a  2s. com
 * 
 * @param reader
 * @param systemId
 *            can not be null. This string should represent a URL that is related to the passed reader. If this URL
 *            is not available or unknown, the string should contain the value of XMLFragment.DEFAULT_URL
 * @throws SAXException
 * @throws IOException
 * @throws NullPointerException
 */
public void load(Reader reader, String systemId) throws SAXException, IOException {

    PushbackReader pbr = new PushbackReader(reader, 1024);
    int c = pbr.read();
    if (c != 65279 && c != 65534) {
        // no BOM! push char back into reader
        pbr.unread(c);
    }

    InputSource source = new InputSource(pbr);
    if (systemId == null) {
        throw new NullPointerException("'systemId' must not be null!");
    }
    setSystemId(systemId);
    DocumentBuilder builder = XMLTools.getDocumentBuilder();
    Document doc = builder.parse(source);
    setRootElement(doc.getDocumentElement());
}

From source file:org.eclipse.rdf4j.rio.turtle.TurtleParser.java

/**
 * Implementation of the <tt>parse(Reader, String)</tt> method defined in
 * the RDFParser interface./* ww w  .ja  va 2s  .  c  o  m*/
 *
 * @param reader
 *            The Reader from which to read the data, must not be
 *            <tt>null</tt>.
 * @param baseURI
 *            The URI associated with the data in the Reader, must not be
 *            <tt>null</tt>.
 * @throws IOException
 *             If an I/O error occurred while data was read from the
 *             InputStream.
 * @throws RDFParseException
 *             If the parser has found an unrecoverable parse error.
 * @throws RDFHandlerException
 *             If the configured statement handler encountered an
 *             unrecoverable error.
 * @throws IllegalArgumentException
 *             If the supplied reader or base URI is <tt>null</tt>.
 */
public synchronized void parse(Reader reader, String baseURI)
        throws IOException, RDFParseException, RDFHandlerException {
    clear();

    try {
        if (reader == null) {
            throw new IllegalArgumentException("Reader must not be 'null'");
        }
        if (baseURI == null) {
            throw new IllegalArgumentException("base URI must not be 'null'");
        }

        if (rdfHandler != null) {
            rdfHandler.startRDF();
        }

        // Start counting lines at 1:
        lineNumber = 1;

        // Allow at most 8 characters to be pushed back:
        this.reader = new PushbackReader(reader, 8);

        // Store normalized base URI
        setBaseURI(baseURI);

        reportLocation();

        int c = skipWSC();

        while (c != -1) {
            parseStatement();
            c = skipWSC();
        }
    } finally {
        clear();
    }

    if (rdfHandler != null) {
        rdfHandler.endRDF();
    }
}

From source file:org.openrdf.rio.turtle.TurtleParser.java

/**
 * Implementation of the <tt>parse(Reader, String)</tt> method defined in the
 * RDFParser interface./*from w  w w . j  av a  2 s  . c o  m*/
 * 
 * @param reader
 *        The Reader from which to read the data, must not be <tt>null</tt>.
 * @param baseURI
 *        The URI associated with the data in the Reader, must not be
 *        <tt>null</tt>.
 * @throws IOException
 *         If an I/O error occurred while data was read from the InputStream.
 * @throws RDFParseException
 *         If the parser has found an unrecoverable parse error.
 * @throws RDFHandlerException
 *         If the configured statement handler encountered an unrecoverable
 *         error.
 * @throws IllegalArgumentException
 *         If the supplied reader or base URI is <tt>null</tt>.
 */
public synchronized void parse(Reader reader, String baseURI)
        throws IOException, RDFParseException, RDFHandlerException {
    if (reader == null) {
        throw new IllegalArgumentException("Reader must not be 'null'");
    }
    if (baseURI == null) {
        throw new IllegalArgumentException("base URI must not be 'null'");
    }

    if (rdfHandler != null) {
        rdfHandler.startRDF();
    }

    // Start counting lines at 1:
    lineNumber = 1;

    // Allow at most 8 characters to be pushed back:
    this.reader = new PushbackReader(reader, 8);

    // Store normalized base URI
    setBaseURI(baseURI);

    reportLocation();

    try {
        int c = skipWSC();

        while (c != -1) {
            parseStatement();
            c = skipWSC();
        }
    } finally {
        clear();
    }

    if (rdfHandler != null) {
        rdfHandler.endRDF();
    }
}

From source file:phex.utils.RSSParser.java

public RSSParser(Reader reader) {
    this.reader = new PushbackReader(reader, 6);
}