Example usage for javax.xml.stream XMLInputFactory createXMLStreamReader

List of usage examples for javax.xml.stream XMLInputFactory createXMLStreamReader

Introduction

In this page you can find the example usage for javax.xml.stream XMLInputFactory createXMLStreamReader.

Prototype

public abstract XMLStreamReader createXMLStreamReader(java.io.InputStream stream) throws XMLStreamException;

Source Link

Document

Create a new XMLStreamReader from a java.io.InputStream

Usage

From source file:edu.cmu.lti.oaqa.knn4qa.collection_reader.YahooAnswersStreamParser.java

public YahooAnswersStreamParser(String fileName, boolean bDoCleanUp) throws IOException, XMLStreamException {
    mFile = new File(fileName);
    mDoCleanUp = bDoCleanUp;/*w w  w .j  av  a2  s. co  m*/

    final XMLInputFactory factory = XMLInputFactory.newInstance();
    factory.setProperty(XMLInputFactory.IS_COALESCING, true);
    mReader = factory.createXMLStreamReader(CompressUtils.createInputStream(fileName));
    mNextDoc = fetchNext();
}

From source file:com.adaptris.core.services.UseXmlCharsetAsEncodingService.java

@Override
public void doService(AdaptrisMessage msg) throws ServiceException {

    XMLInputFactory factory = createInputFactory();
    XMLStreamReader reader = null;
    try {//from  w  ww  .ja  v a2  s.  c o m
        try (InputStream in = msg.getInputStream()) {
            reader = factory.createXMLStreamReader(in);
            String encoding = reader.getCharacterEncodingScheme();
            if (!StringUtils.isBlank(encoding)) {
                msg.setContentEncoding(encoding);
            }
        }
    } catch (Exception e) {
        throw ExceptionHelper.wrapServiceException(e);
    } finally {
        closeQuietly(reader);
    }
}

From source file:fr.mobilit.neo4j.server.service.nantes.CycleRentTest.java

@BeforeClass
public void setUp() throws Exception {
    super.setUp(true);
    HttpClient client = new HttpClient();
    GetMethod get = null;//from   w w w .  ja v  a 2s.  c  o m
    try {
        // we do the http call and parse the xml response
        get = new GetMethod(CycleRentImpl.IMPORT_URL);
        client.executeMethod(get);
        javax.xml.stream.XMLInputFactory factory = javax.xml.stream.XMLInputFactory.newInstance();
        javax.xml.stream.XMLStreamReader parser = factory.createXMLStreamReader(get.getResponseBodyAsStream());
        ArrayList<String> currentXMLTags = new ArrayList<String>();
        int depth = 0;
        while (true) {
            int event = parser.next();
            if (event == javax.xml.stream.XMLStreamConstants.END_DOCUMENT) {
                break;
            }
            switch (event) {
            case javax.xml.stream.XMLStreamConstants.START_ELEMENT:
                currentXMLTags.add(depth, parser.getLocalName());
                String tagPath = currentXMLTags.toString();
                // here we have a match, so we construct the POI
                if (tagPath.equals("[carto, markers, marker]")) {
                    numberOfStations++;
                }
                depth++;
                break;
            case javax.xml.stream.XMLStreamConstants.END_ELEMENT:
                depth--;
                currentXMLTags.remove(depth);
                break;
            default:
                break;
            }
        }
    } catch (Exception e) {
        throw new RuntimeException(e.getMessage(), e.getCause());
    } finally {
        get.releaseConnection();
    }
}

From source file:de.tuebingen.uni.sfs.germanet.api.IliLoader.java

/**
 * Loads <code>IliRecords</code> from the specified stream into this
 * <code>IliLoader</code>'s <code>GermaNet</code> object.
 * @param inputStream the stream containing <code>IliRecords</code> data
 * @throws javax.xml.stream.XMLStreamException
 *//*from  ww  w .jav  a 2s.  co m*/
protected void loadILI(InputStream inputStream) throws XMLStreamException {
    XMLInputFactory factory = XMLInputFactory.newInstance();
    XMLStreamReader parser = factory.createXMLStreamReader(inputStream);
    int event;
    String nodeName;
    logger.debug("Loading input stream interLingualIndex_DE-EN.xml...");

    //Parse entire file, looking for ili record start elements
    while (parser.hasNext()) {
        event = parser.next();
        switch (event) {
        case XMLStreamConstants.START_DOCUMENT:
            namespace = parser.getNamespaceURI();
            break;
        case XMLStreamConstants.START_ELEMENT:
            nodeName = parser.getLocalName();
            if (nodeName.equals(GermaNet.XML_ILI_RECORD)) {
                IliRecord ili = processIliRecord(parser);
                germaNet.addIliRecord(ili);
            }
            break;
        }
    }
    parser.close();
    logger.debug("Done.");
}

From source file:de.tuebingen.uni.sfs.germanet.api.IliLoader.java

/**
 * Loads <code>IliRecords</code> from the specified file into this
 * <code>IliLoader</code>'s <code>GermaNet</code> object.
 * @param iliFile the file containing <code>IliRecords</code> data
 * @throws java.io.FileNotFoundException
 * @throws javax.xml.stream.XMLStreamException
 *//*from  w w w  .  j  ava  2s . c om*/
protected void loadILI(File iliFile) throws FileNotFoundException, XMLStreamException {
    InputStream in = new FileInputStream(iliFile);
    XMLInputFactory factory = XMLInputFactory.newInstance();
    XMLStreamReader parser = factory.createXMLStreamReader(in);
    int event;
    String nodeName;
    logger.debug("Loading " + iliFile.getName() + "...");

    //Parse entire file, looking for ili record start elements
    while (parser.hasNext()) {
        event = parser.next();
        switch (event) {
        case XMLStreamConstants.START_DOCUMENT:
            namespace = parser.getNamespaceURI();
            break;
        case XMLStreamConstants.START_ELEMENT:
            nodeName = parser.getLocalName();
            if (nodeName.equals(GermaNet.XML_ILI_RECORD)) {
                IliRecord ili = processIliRecord(parser);
                germaNet.addIliRecord(ili);
            }
            break;
        }
    }
    parser.close();
    logger.debug("Done.");
}

From source file:de.tuebingen.uni.sfs.germanet.api.WiktionaryLoader.java

/**
 * Loads <code>WiktionaryParaphrases</code> from the given streams into this
 * <code>WiktionaryLoader</code>'s <code>GermaNet</code> object.
 * @param inputStreams the list of streams containing <code>WiktionaryParaphrases</code> data
 * @param xmlNames the names of the streams
 * @throws javax.xml.stream.XMLStreamException
 */// w w  w . j  a  va  2  s  .  com
protected void loadWiktionary(List<InputStream> inputStreams, List<String> xmlNames) throws XMLStreamException {

    for (int i = 0; i < inputStreams.size(); i++) {
        if (xmlNames.get(i).startsWith("wiktionary")) {
            logger.debug("Loading input stream " + xmlNames.get(i) + "...");
            XMLInputFactory factory = XMLInputFactory.newInstance();
            XMLStreamReader parser = factory.createXMLStreamReader(inputStreams.get(i));
            int event;
            String nodeName;

            //Parse entire file, looking for Wiktionary paraphrase start elements
            while (parser.hasNext()) {
                event = parser.next();
                switch (event) {
                case XMLStreamConstants.START_DOCUMENT:
                    namespace = parser.getNamespaceURI();
                    break;
                case XMLStreamConstants.START_ELEMENT:
                    nodeName = parser.getLocalName();
                    if (nodeName.equals(GermaNet.XML_WIKTIONARY_PARAPHRASE)) {
                        WiktionaryParaphrase wiki = processWiktionaryParaphrase(parser);
                        germaNet.addWiktionaryParaphrase(wiki);
                    }
                    break;
                }
            }
            parser.close();
        }
    }

    logger.debug("Done.");

}

From source file:hudson.model.ExternalRun.java

/**
 * Instead of performing a build, accept the log and the return code from a
 * remote machine.//from   w w w  .j  a va  2  s  .co m
 *
 * <p> The format of the XML is:
 *
 * <pre><xmp>
 * <run>
 *  <log>...console output...</log>
 *  <result>exit code</result>
 * </run>
 * </xmp></pre>
 */
@SuppressWarnings({ "Since15" })
public void acceptRemoteSubmission(final Reader in) throws IOException {
    final long[] duration = new long[1];
    run(new Runner() {
        private String elementText(XMLStreamReader r) throws XMLStreamException {
            StringBuilder buf = new StringBuilder();
            while (true) {
                int type = r.next();
                if (type == CHARACTERS || type == CDATA) {
                    buf.append(r.getTextCharacters(), r.getTextStart(), r.getTextLength());
                } else {
                    return buf.toString();
                }
            }
        }

        public Result run(BuildListener listener) throws Exception {
            PrintStream logger = null;
            try {
                logger = new PrintStream(new DecodingStream(listener.getLogger()));

                XMLInputFactory xif = XMLInputFactory.newInstance();
                XMLStreamReader p = xif.createXMLStreamReader(in);

                p.nextTag(); // get to the <run>
                p.nextTag(); // get to the <log>

                charset = p.getAttributeValue(null, "content-encoding");
                while (p.next() != END_ELEMENT) {
                    int type = p.getEventType();
                    if (type == CHARACTERS || type == CDATA) {
                        logger.print(p.getText());
                    }
                }
                p.nextTag(); // get to <result>

                Result r = Integer.parseInt(elementText(p)) == 0 ? Result.SUCCESS : Result.FAILURE;

                p.nextTag(); // get to <duration> (optional)
                if (p.getEventType() == START_ELEMENT && p.getLocalName().equals("duration")) {
                    duration[0] = Long.parseLong(elementText(p));
                }

                return r;
            } finally {
                IOUtils.closeQuietly(logger);
            }
        }

        public void post(BuildListener listener) {
            // do nothing
        }

        public void cleanUp(BuildListener listener) {
            // do nothing
        }
    });

    if (duration[0] != 0) {
        super.duration = duration[0];
        // save the updated duration
        save();
    }
}

From source file:de.tuebingen.uni.sfs.germanet.api.WiktionaryLoader.java

/**
 * Loads <code>WiktionaryParaphrases</code> from the specified file into this
 * <code>WiktionaryLoader</code>'s <code>GermaNet</code> object.
 * @param wiktionaryFile the file containing <code>WiktionaryParaphrases</code> data
 * @throws java.io.FileNotFoundException
 * @throws javax.xml.stream.XMLStreamException
 *///from w w w. ja v  a2s  .c  o  m
protected void loadWiktionary(File wiktionaryFile) throws FileNotFoundException, XMLStreamException {
    wikiDir = wiktionaryFile;
    FilenameFilter filter = new WikiFilter(); //get only wiktionary files
    File[] wikiFiles = wikiDir.listFiles(filter);

    if (wikiFiles == null || wikiFiles.length == 0) {
        throw new FileNotFoundException(
                "Unable to load Wiktionary Paraphrases from \"" + this.wikiDir.getPath() + "\"");
    }

    for (File wikiFile : wikiFiles) {
        logger.debug("Loading " + wikiFile.getName() + "...");
        InputStream in = new FileInputStream(wikiFile);
        XMLInputFactory factory = XMLInputFactory.newInstance();
        XMLStreamReader parser = factory.createXMLStreamReader(in);
        int event;
        String nodeName;

        //Parse entire file, looking for Wiktionary paraphrase start elements
        while (parser.hasNext()) {
            event = parser.next();
            switch (event) {
            case XMLStreamConstants.START_DOCUMENT:
                namespace = parser.getNamespaceURI();
                break;
            case XMLStreamConstants.START_ELEMENT:
                nodeName = parser.getLocalName();
                if (nodeName.equals(GermaNet.XML_WIKTIONARY_PARAPHRASE)) {
                    WiktionaryParaphrase wiki = processWiktionaryParaphrase(parser);
                    germaNet.addWiktionaryParaphrase(wiki);
                }
                break;
            }
        }
        parser.close();
    }

    logger.debug("Done.");

}

From source file:com.wavemaker.runtime.data.parser.BaseHbmParser.java

protected BaseHbmParser(Reader reader) {
    if (reader == null) {
        throw new IllegalArgumentException("Reader cannot be null");
    }//from w  ww. j  a v  a  2s .c o m
    this.reader = reader;
    try {
        XMLInputFactory factory = XMLInputFactory.newInstance();
        setupEntityResolver(factory);
        this.xmlReader = factory.createXMLStreamReader(reader);
    } catch (XMLStreamException ex) {
        throw new WMRuntimeException(ex);
    }
}

From source file:org.openvpms.tools.data.loader.StaxArchetypeDataLoader.java

/**
 * Return the XMLReader for the specified file
 *
 * @param file the file// ww  w .jav  a2  s.c o m
 * @return XMLStreamReader
 * @throws FileNotFoundException if the file cannot be found
 * @throws XMLStreamException if the file cannot be read
 */
private XMLStreamReader getReader(File file) throws FileNotFoundException, XMLStreamException {
    FileInputStream stream = new FileInputStream(file);
    XMLInputFactory factory = XMLInputFactory.newInstance();

    return factory.createXMLStreamReader(stream);
}