Example usage for javax.xml.stream XMLInputFactory createXMLEventReader

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

Introduction

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

Prototype

public abstract XMLEventReader createXMLEventReader(java.io.InputStream stream) throws XMLStreamException;

Source Link

Document

Create a new XMLEventReader from a java.io.InputStream

Usage

From source file:corpus.sinhala.crawler.blog.rss.RSSFeedParser.java

public String getBlogId() {
    Feed feed = null;// www  .  ja va2  s.  c  om
    String atomId = "";
    try {
        // First create a new XMLInputFactory
        XMLInputFactory inputFactory = XMLInputFactory.newInstance();
        // Setup a new eventReader
        InputStream in = read();
        XMLEventReader eventReader = inputFactory.createXMLEventReader(in);
        // Read the XML document
        int location = -1;
        while (eventReader.hasNext()) {
            XMLEvent event = eventReader.nextEvent();
            if (event.isStartElement()) {
                String localPart = event.asStartElement().getName().getLocalPart();
                //System.out.println(localPart);
                switch (localPart) {

                case CHANNEL:
                    event = eventReader.nextEvent();
                    atomId = getCharacterData(event, eventReader);
                    //System.out.println("Atom Id "+atomId );
                    return atomId.split("-")[1];
                }
            }
        }
    } catch (XMLStreamException e) {

        throw new RuntimeException(e);
    }

    return null;
}

From source file:org.eclipse.swordfish.core.configuration.xml.XmlToPropertiesTransformerImpl.java

public void loadConfiguration(URL path) {
    Assert.notNull(path);/* ww w. jav  a 2  s  . c om*/
    InputStream inputStream = null;
    try {
        inputStream = path.openStream();
        XMLInputFactory inputFactory = XMLInputFactory.newInstance();
        LinkedList<String> currentElements = new LinkedList<String>();
        XMLEventReader eventReader = inputFactory.createXMLEventReader(inputStream);
        Map<String, List<String>> props = new HashMap<String, List<String>>();
        // Read the XML document
        while (eventReader.hasNext()) {
            XMLEvent event = eventReader.nextEvent();
            if (event.isCharacters() && !event.asCharacters().isWhiteSpace()) {
                putElement(props, getQualifiedName(currentElements), event.asCharacters().getData());

            } else if (event.isStartElement()) {
                currentElements.add(event.asStartElement().getName().getLocalPart());
                for (Iterator attrIt = event.asStartElement().getAttributes(); attrIt.hasNext();) {
                    Attribute attribute = (Attribute) attrIt.next();
                    putElement(props, getQualifiedName(currentElements) + "[@" + attribute.getName() + "]",
                            attribute.getValue());

                }
            } else if (event.isAttribute()) {
            } else if (event.isEndElement()) {
                String lastElem = event.asEndElement().getName().getLocalPart();
                if (!currentElements.getLast().equals(lastElem)) {
                    throw new UnsupportedOperationException(lastElem + "," + currentElements.getLast());
                }
                currentElements.removeLast();
            }
        }
        properties = flattenProperties(props);
    } catch (Exception ex) {
        throw new SwordfishException(ex);
    } finally {
        if (inputStream != null) {
            try {
                inputStream.close();
            } catch (IOException ex) {
            }
        }
    }
}

From source file:lille.iagl.python_bucket.PythonBucketter.java

public PythonBucketter(String pathFileInput) {
    try {//  w  w  w . ja  v  a2s  .  c  om

        XMLInputFactory xmlIF = XMLInputFactory.newFactory();
        InputStream inputStream = new FileInputStream(pathFileInput);
        this.xmlSR = xmlIF.createXMLEventReader(inputStream);
        this.file_to_list();
        this.makeBucket();
    } catch (XMLStreamException | FileNotFoundException ex) {
        Logger.getLogger(PythonStackTraceRecognizer.class.getName()).log(Level.SEVERE, null, ex);
    }
}

From source file:com.joliciel.frenchTreebank.upload.TreebankXmlReader.java

void getNextEventReader() {
    if (eventReader == null && currentFileIndex < files.size()) {
        File file = files.get(currentFileIndex++);
        try {/*from  w  ww .  j  a  va  2  s.  c  om*/
            LOG.info("Reading file: " + file.getName());
            InputStream inputStream = new BufferedInputStream(new FileInputStream(file));
            XMLInputFactory factory = XMLInputFactory.newInstance();
            eventReader = factory.createXMLEventReader(inputStream);
        } catch (FileNotFoundException e) {
            LogUtils.logError(LOG, e);
            throw new RuntimeException(e);
        } catch (XMLStreamException e) {
            LogUtils.logError(LOG, e);
            throw new RuntimeException(e);
        }
        treebankFile = this.treebankService.newTreebankFile(file.getName());
    }
}

From source file:com.ggvaidya.scinames.model.Project.java

public static Project loadFromFile(File loadFromFile) throws IOException {
    Project project = null;/*from ww  w.  j  ava 2 s . c o  m*/

    XMLInputFactory factory = XMLInputFactory.newFactory();
    factory.setXMLReporter(new XMLReporter() {
        @Override
        public void report(String message, String errorType, Object relatedInformation, Location location)
                throws XMLStreamException {
            LOGGER.warning(errorType + " while loading project from XML file '" + loadFromFile + "': " + message
                    + " (related info: " + relatedInformation.toString() + ", location: " + location);
        }
    });

    try {
        XMLEventReader reader = factory.createXMLEventReader(
                new XmlStreamReader(new GZIPInputStream(new FileInputStream(loadFromFile))));

        project = ProjectXMLReader.read(reader);
        project.setFile(loadFromFile);
        project.lastModifiedProperty().saved();

        reader.close();

    } catch (XMLStreamException ex) {
        throw new IOException("Could not read project from XML file '" + loadFromFile + "': " + ex, ex);
    }

    return project;

    /*
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
            
    // Configure upcoming document builder.
    dbf.setIgnoringComments(true);
    dbf.setIgnoringElementContentWhitespace(true);
            
    Document docProject;
    try {
       DocumentBuilder db = dbf.newDocumentBuilder();
       docProject = db.parse(loadFromFile);
               
    } catch (SAXException ex) {
       throw new IOException("Could not load project XML file: " + ex);
               
    } catch (ParserConfigurationException ex) {
       throw new RuntimeException("Could not load XML parser configuration: " + ex);
            
    }
            
    // Load project.
    Project newProject;
    try {
       newProject = serializeFromDocument(docProject, loadFromFile);
    } catch(SAXException e) {
       throw new IOException("XML file loaded but project could not be read: " + e);
    } catch(IllegalStateException e) {
       throw new IOException("XML file contains errors in content: " + e);
    }
            
    return newProject;
    */
}

From source file:HTTPChunkLocator.java

@Override
public ChunkLocation[] getChunkLocations(final File file, final String virtualFsName)
        throws ChunkStorageException {
    final long inode = getInode(file);
    final long fileSizeInBytes = getFileSizeInBytes(file);

    final XMLInputFactory factory = XMLInputFactory.newInstance();
    XMLEventReader reader = null;
    try {/*  w w w  .  j  a v  a2 s .c  om*/
        final URI uri = new URI(endpoint.getScheme(), endpoint.getUserInfo(), endpoint.getHost(),
                endpoint.getPort(), endpoint.getPath(), "inum=" + inode, null);
        reader = factory.createXMLEventReader(openStream(uri));
        final List<ChunkLocation> parseChunks = parseChunks(reader, fileSizeInBytes);
        return parseChunks.toArray(new ChunkLocation[0]);
    } catch (final Exception e) {
        throw new ChunkStorageException(e);
    } finally {
        if (reader != null) {
            try {
                reader.close();
            } catch (final XMLStreamException e) {
                // ignore
            }
        }
    }
}

From source file:de.tudarmstadt.ukp.dkpro.core.io.tiger.TigerXmlReader.java

@Override
public void getNext(JCas aJCas) throws IOException, CollectionException {
    Resource res = nextFile();//from   www  . j a v  a2s  .c o  m
    initCas(aJCas, res);

    posMappingProvider.configure(aJCas.getCas());

    InputStream is = null;
    try {
        is = CompressionUtils.getInputStream(res.getLocation(), res.getInputStream());

        XMLInputFactory xmlInputFactory = XMLInputFactory.newInstance();
        XMLEventReader xmlEventReader = xmlInputFactory.createXMLEventReader(is);

        JAXBContext context = JAXBContext.newInstance(Meta.class, AnnotationDecl.class, TigerSentence.class);
        Unmarshaller unmarshaller = context.createUnmarshaller();

        JCasBuilder jb = new JCasBuilder(aJCas);

        XMLEvent e = null;
        while ((e = xmlEventReader.peek()) != null) {
            if (isStartElement(e, "s")) {
                readSentence(jb, unmarshaller.unmarshal(xmlEventReader, TigerSentence.class).getValue());
            } else {
                xmlEventReader.next();
            }

        }

        jb.close();

        // Can only do that after the builder is closed, otherwise the text is not yet set in
        // the
        // CAS and we get "null" for all token strings.
        if (pennTreeEnabled) {
            for (ROOT root : select(aJCas, ROOT.class)) {
                PennTree pt = new PennTree(aJCas, root.getBegin(), root.getEnd());
                PennTreeNode rootNode = PennTreeUtils.convertPennTree(root);
                pt.setPennTree(PennTreeUtils.toPennTree(rootNode));
                pt.addToIndexes();
            }
        }
    } catch (XMLStreamException ex1) {
        throw new IOException(ex1);
    } catch (JAXBException ex2) {
        throw new IOException(ex2);
    } finally {
        closeQuietly(is);
    }
}

From source file:edu.unc.lib.dl.services.TripleStoreManagerMulgaraImpl.java

/**
 * @param query/*  w  w  w.  ja va2s  . co m*/
 *           an ITQL command
 * @return the message returned by Mulgara
 * @throws RemoteException
 *            for communication failure
 */
public String storeCommand(String query) {
    String result = null;
    String response = this.sendTQL(query);
    if (response != null) {
        StringReader sr = new StringReader(response);
        XMLInputFactory factory = XMLInputFactory.newInstance();
        factory.setProperty(XMLInputFactory.IS_COALESCING, Boolean.TRUE);
        XMLEventReader r = null;
        try {
            boolean inMessage = false;
            StringBuffer message = new StringBuffer();
            r = factory.createXMLEventReader(sr);
            while (r.hasNext()) {
                XMLEvent e = r.nextEvent();
                if (e.isStartElement()) {
                    StartElement s = e.asStartElement();
                    if ("message".equals(s.getName().getLocalPart())) {
                        inMessage = true;
                    }
                } else if (e.isEndElement()) {
                    EndElement end = e.asEndElement();
                    if ("message".equals(end.getName().getLocalPart())) {
                        inMessage = false;
                    }
                } else if (inMessage && e.isCharacters()) {
                    message.append(e.asCharacters().getData());
                }
            }
            result = message.toString();
        } catch (XMLStreamException e) {
            e.printStackTrace();
        } finally {
            if (r != null) {
                try {
                    r.close();
                } catch (Exception ignored) {
                    log.error(ignored);
                }
            }

        }
        sr.close();
    }
    return result;
}

From source file:at.gv.egiz.slbinding.SLUnmarshaller.java

/**
 * @param source a StreamSource wrapping a Reader (!) for the marshalled Object
 * @return the unmarshalled Object//from  w  w  w.  ja v a 2  s.  c om
 * @throws XMLStreamException
 * @throws JAXBException
 */
public Object unmarshal(StreamSource source) throws XMLStreamException, JAXBException {
    Reader inputReader = source.getReader();

    /* Validate XML against XXE, XEE, and SSRF
     * 
     * This pre-validation step is required because com.sun.xml.stream.sjsxp-1.0.2 XML stream parser library does not 
     * support all XML parser features to prevent these types of attacks  
     */
    if (inputReader instanceof InputStreamReader) {
        try {
            //create copy of input stream
            InputStreamReader isReader = (InputStreamReader) inputReader;
            String encoding = isReader.getEncoding();
            byte[] backup = IOUtils.toByteArray(isReader, encoding);

            //validate input stream
            DOMUtils.validateXMLAgainstXXEAndSSRFAttacks(new ByteArrayInputStream(backup));

            //create new inputStreamReader for reak processing
            inputReader = new InputStreamReader(new ByteArrayInputStream(backup), encoding);

        } catch (XMLStreamException e) {
            log.error("XML data validation FAILED with msg: " + e.getMessage(), e);
            throw new XMLStreamException("XML data validation FAILED with msg: " + e.getMessage(), e);

        } catch (IOException e) {
            log.error("XML data validation FAILED with msg: " + e.getMessage(), e);
            throw new XMLStreamException("XML data validation FAILED with msg: " + e.getMessage(), e);

        }

    } else {
        log.error("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
        log.error(
                "Reader is not of type InputStreamReader -> can not make a copy of the InputStream --> extended XML validation is not possible!!! ");
        log.error("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");

    }

    /* !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
     * parse XML with original functionality
     * 
     * This code implements the the original mocca XML processing by using 
     *  com.sun.xml.stream.sjsxp-1.0.2 XML stream parser library. Currently, this library is required to get full 
     *  security-layer specific XML processing. However, this lib does not fully support XXE, XEE and SSRF
     *  prevention mechanisms (e.g.: XMLInputFactory.SUPPORT_DTD flag is not used)    
     * 
     * !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
     */
    XMLInputFactory inputFactory = XMLInputFactory.newInstance();

    //disallow DTD and external entities
    inputFactory.setProperty(XMLInputFactory.SUPPORT_DTD, false);
    inputFactory.setProperty("javax.xml.stream.isSupportingExternalEntities", false);

    XMLEventReader eventReader = inputFactory.createXMLEventReader(inputReader);
    RedirectEventFilter redirectEventFilter = new RedirectEventFilter();
    XMLEventReader filteredReader = inputFactory.createFilteredReader(eventReader, redirectEventFilter);

    Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
    ReportingValidationEventHandler validationEventHandler = new ReportingValidationEventHandler();
    unmarshaller.setEventHandler(validationEventHandler);

    unmarshaller.setListener(new RedirectUnmarshallerListener(redirectEventFilter));
    unmarshaller.setSchema(slSchema);

    Object object;
    try {
        log.trace("Before unmarshal().");
        object = unmarshaller.unmarshal(filteredReader);
        log.trace("After unmarshal().");
    } catch (UnmarshalException e) {
        if (log.isDebugEnabled()) {
            log.debug("Failed to unmarshal security layer message.", e);
        } else {
            log.info("Failed to unmarshal security layer message."
                    + (e.getMessage() != null ? " " + e.getMessage() : ""));
        }

        if (validationEventHandler.getErrorEvent() != null) {
            ValidationEvent errorEvent = validationEventHandler.getErrorEvent();
            if (e.getLinkedException() == null) {
                e.setLinkedException(errorEvent.getLinkedException());
            }
        }
        throw e;
    }

    return object;

}

From source file:com.streamsets.pipeline.lib.xml.StreamingXmlParser.java

public StreamingXmlParser(Reader reader, String recordElement, Map<String, String> namespaces,
        long initialPosition, boolean useFieldAttributesInsteadOfFields)
        throws IOException, XMLStreamException {
    this.reader = reader;
    this.useFieldAttributesInsteadOfFields = useFieldAttributesInsteadOfFields;
    if (Strings.isNullOrEmpty(recordElement)) {
        this.recordElement = Constants.ROOT_ELEMENT_PATH;
    } else {/*from   ww w. j a v  a  2  s.  c o m*/
        this.recordElement = recordElement;
    }
    XMLInputFactory factory = XMLInputFactory.newFactory();
    factory.setProperty("javax.xml.stream.isCoalescing", true);
    factory.setProperty("javax.xml.stream.isSupportingExternalEntities", false);
    factory.setProperty("javax.xml.stream.supportDTD", false);
    this.xmlEventReader = new XPathMatchingEventReader(factory.createXMLEventReader(reader), this.recordElement,
            namespaces);
    while (hasNext(xmlEventReader) && !peek(xmlEventReader).isEndDocument()
            && !peek(xmlEventReader).isStartElement()) {
        read(xmlEventReader);
    }
    if (recordElement == null || recordElement.isEmpty()) {
        StartElement startE = (StartElement) peek(xmlEventReader);
        this.recordElement = startE.getName().getLocalPart();
    } else {
        //consuming root
        StartElement startE = (StartElement) read(xmlEventReader);
        elementNameStack.addFirst(getNameAndTrackNs(startE.getName()));
    }
    if (initialPosition > 0) {
        //fastforward to initial position
        while (hasNext(xmlEventReader)
                && peek(xmlEventReader).getLocation().getCharacterOffset() < initialPosition) {
            read(xmlEventReader);
            fastForwardLeaseReader();
        }
        xmlEventReader.clearLastMatch();
    }
}