List of usage examples for javax.xml.stream XMLInputFactory newInstance
public static XMLInputFactory newInstance() throws FactoryConfigurationError
From source file:com.graphhopper.reader.OSMInputFile.java
private void openXMLStream(InputStream in) throws XMLStreamException { XMLInputFactory factory = XMLInputFactory.newInstance(); parser = factory.createXMLStreamReader(bis, "UTF-8"); int event = parser.next(); if (event != XMLStreamConstants.START_ELEMENT || !parser.getLocalName().equalsIgnoreCase("osm")) { throw new IllegalArgumentException("File is not a valid OSM stream"); }// w w w .jav a 2 s . c o m // See https://wiki.openstreetmap.org/wiki/PBF_Format#Definition_of_the_OSMHeader_fileblock String timestamp = parser.getAttributeValue(null, "osmosis_replication_timestamp"); if (timestamp == null) timestamp = parser.getAttributeValue(null, "timestamp"); if (timestamp != null) { try { fileheader = new OSMFileHeader(); fileheader.setTag("timestamp", timestamp); } catch (Exception ex) { } } eof = false; }
From source file:net.bulletin.pdi.xero.step.support.XMLChunkerTest.java
/** * <p>This test is checking to see that, without any container elements, the chunking will produce one * document and that single document should be the whole of the input.</p> *//*from www . j a v a2s . co m*/ @Test public void testPullNextXmlChunk_withoutContainerElements() throws Exception { byte[] sampleXml = readSampleXml(); XMLChunker chunker = new XMLChunkerImpl( XMLInputFactory.newInstance().createXMLStreamReader(new ByteArrayInputStream(sampleXml)), // all in-memory new Stack<String>()); // --------------------------------- String actuals[] = new String[] { chunker.pullNextXmlChunk(), chunker.pullNextXmlChunk() }; // --------------------------------- // This will work through the chunks and check specific information it knows in the sample. { DocumentBuilder documentBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder(); XPath xPath = XPathFactory.newInstance().newXPath(); org.w3c.dom.Document doc = documentBuilder .parse(new ByteArrayInputStream(actuals[0].getBytes(CharEncoding.UTF_8))); NodeList artistNodeList = (NodeList) xPath.evaluate("/Response/Artists/Artist", doc, XPathConstants.NODESET); Assert.assertEquals(3, artistNodeList.getLength()); } Assert.assertNull("expected the last chunk to be null", actuals[1]); }
From source file:com.widowcrawler.exo.parse.Parser.java
public Sitemap parse(InputStream inputStream) throws XMLStreamException, SitemapParseException { final XMLStreamReader reader = XMLInputFactory.newInstance().createXMLStreamReader(inputStream, "utf-8"); final Sitemap retval = new Sitemap(new HashSet<>()); final Set<SitemapURL> sitemapURLs = new HashSet<>(); SitemapURL.Builder urlBuilder = null; String urlContent;/*from w w w .j av a 2 s .co m*/ reader.getEventType(); while (reader.hasNext()) { switch (state) { case START: reader.nextTag(); if (StringUtils.equalsIgnoreCase(reader.getLocalName(), URLSET_TAG_NAME)) { state = State.URLSET; } else if (StringUtils.equalsIgnoreCase(reader.getLocalName(), SITEMAPINDEX_TAG_NAME)) { state = State.SITEMAPINDEX; } else { String message = "Invalid root element. Must be either urlset or sitemapindex"; logger.error(message); throw new SitemapParseException(message); } break; case END: // consume all end tags if (reader.getEventType() != XMLStreamConstants.END_ELEMENT) { String message = decorate("There should be only one root element in each sitemap.xml", reader.getLocation()); logger.error(message); throw new SitemapParseException(message); } reader.next(); break; ///////////////////// // URLSET Hierarchy ///////////////////// case URLSET: // If we're done with the URLs, we're done overall if (reader.nextTag() == XMLStreamConstants.END_ELEMENT) { state = State.END; break; } // Check that we're entering into a <url> element if (!StringUtils.equalsIgnoreCase(reader.getLocalName(), URL_TAG_NAME)) { String message = "A <urlset> element can only contain <url> elements. Found: " + reader.getLocalName(); logger.error(message); throw new SitemapParseException(message); } urlBuilder = new SitemapURL.Builder(); state = State.URL; break; case URL: reader.nextTag(); if (reader.getEventType() == XMLStreamConstants.START_ELEMENT) { //logger.info("reader.getLocalName(): " + reader.getLocalName()); switch (StringUtils.lowerCase(reader.getLocalName())) { case LOC_TAG_NAME: state = State.URL_PROP_LOC; break; case LASTMOD_TAG_NAME: state = State.URL_PROP_LASTMOD; break; case CHANGEFREQ_TAG_NAME: state = State.URL_PROP_CHANGEFREQ; break; case PRIORITY_TAG_NAME: state = State.URL_PROP_PRIORITY; break; case MOBILE_TAG_NAME: state = State.URL_PROP_MOBILE; break; default: String message = "Unexpected tag in url: " + reader.getLocalName(); logger.error(message); throw new SitemapParseException(message); } } else if (reader.getEventType() == XMLStreamConstants.END_ELEMENT) { // we're done collecting the data for this URL assert urlBuilder != null; sitemapURLs.add(urlBuilder.build()); urlBuilder = new SitemapURL.Builder(); state = State.URLSET; } break; case URL_PROP_LOC: urlContent = reader.getElementText(); try { assert urlBuilder != null; urlBuilder.withLocation(new URL(StringUtils.trimToNull(urlContent))); } catch (MalformedURLException ex) { String message = String.format("Malformed URL found: %s", urlContent); logger.error(message); throw new SitemapParseException(message); } state = State.URL; break; case URL_PROP_LASTMOD: assert urlBuilder != null; urlBuilder.withLastModified(DateTime.parse(reader.getElementText())); state = State.URL; break; case URL_PROP_CHANGEFREQ: assert urlBuilder != null; urlBuilder.withChangeFrequency(ChangeFreq.valueOf(StringUtils.upperCase(reader.getElementText()))); state = State.URL; break; case URL_PROP_PRIORITY: assert urlBuilder != null; urlBuilder.withPriority(Double.valueOf(reader.getElementText())); state = State.URL; break; case URL_PROP_MOBILE: assert urlBuilder != null; urlBuilder.withIsMobileContent(true); // consume until "end tag" of self-closing tag // Also works if someone puts content in reader.getElementText(); state = State.URL; break; /////////////////////////// // SITEMAPINDEX Hierarchy /////////////////////////// case SITEMAPINDEX: // If we're done with all the Sitemaps, we're done overall if (reader.nextTag() == XMLStreamConstants.END_ELEMENT) { state = State.END; break; } state = State.SITEMAP; break; case SITEMAP: if (!StringUtils.equalsIgnoreCase(reader.getLocalName(), SITEMAP_TAG_NAME)) { throw new SitemapParseException("A <sitemapindex> element can only contain <sitemap> elements"); } reader.nextTag(); if (reader.getEventType() == XMLStreamConstants.START_ELEMENT) { switch (StringUtils.lowerCase(reader.getLocalName())) { case LOC_TAG_NAME: state = State.URL_PROP_LOC; break; case LASTMOD_TAG_NAME: state = State.URL_PROP_LASTMOD; break; default: throw new SitemapParseException("Unexpected tag in sitemap: " + reader.getLocalName()); } } else if (reader.getEventType() == XMLStreamConstants.END_ELEMENT) { // we're done collecting the data for this URL assert urlBuilder != null; sitemapURLs.add(urlBuilder.build()); urlBuilder = new SitemapURL.Builder(); state = State.URLSET; } case SITEMAP_PROP_LOC: urlContent = reader.getElementText(); try { URL sitemapURL = new URL(StringUtils.trimToNull(urlContent)); Sitemap temp = Retry.retry(() -> { try { return Exo.parse(sitemapURL.toString()); } catch (Exception ex) { throw new RuntimeException(ex); } }); retval.merge(temp); } catch (MalformedURLException ex) { String message = String.format("Malformed URL found: %s", urlContent); logger.error(message); throw new SitemapParseException(message); } catch (InterruptedException e) { logger.warn("Thread interrupted while (re)trying"); Thread.currentThread().interrupt(); } catch (RetryFailedException e) { String message = String.format("Failed to retrieve sitemap of sitemap index at %s", urlContent); logger.error(message); throw new SitemapParseException(message); } state = State.URL; break; case SITEMAP_PROP_LASTMOD: // Do nothing with this data for now reader.getElementText(); break; } //System.out.println(state); } return retval.merge(new Sitemap(sitemapURLs)); }
From source file:com.flexive.chemistry.webdav.TextDocumentResource.java
protected void processXmlProperties(InputStream in) { final XMLInputFactory factory = XMLInputFactory.newInstance(); try {//from ww w . j a v a 2 s .c o m final XMLStreamReader parser = factory.createXMLStreamReader(in); for (int event = parser.next(); event != XMLStreamConstants.END_DOCUMENT; event = parser.next()) { switch (event) { case XMLStreamConstants.START_ELEMENT: if ("property".equals(parser.getLocalName())) { processProperty(parser); } else if ("name".equals(parser.getLocalName())) { processName(parser); } } } } catch (XMLStreamException e) { throw new RuntimeException("Failed to replace content: " + e.getMessage(), e); } }
From source file:com.graphhopper.reader.osm.OSMInputFile.java
private void openXMLStream(InputStream in) throws XMLStreamException { XMLInputFactory factory = XMLInputFactory.newInstance(); parser = factory.createXMLStreamReader(in, "UTF-8"); int event = parser.next(); if (event != XMLStreamConstants.START_ELEMENT || !parser.getLocalName().equalsIgnoreCase("osm")) { throw new IllegalArgumentException("File is not a valid OSM stream"); }/*from w w w . j ava 2 s . c o m*/ // See https://wiki.openstreetmap.org/wiki/PBF_Format#Definition_of_the_OSMHeader_fileblock String timestamp = parser.getAttributeValue(null, "osmosis_replication_timestamp"); if (timestamp == null) timestamp = parser.getAttributeValue(null, "timestamp"); if (timestamp != null) { try { fileheader = new OSMFileHeader(); fileheader.setTag("timestamp", timestamp); } catch (Exception ex) { } } eof = false; }
From source file:com.evolveum.midpoint.prism.lex.dom.DomLexicalProcessor.java
@Override public void readObjectsIteratively(@NotNull ParserSource source, @NotNull ParsingContext parsingContext, RootXNodeHandler handler) throws SchemaException, IOException { InputStream is = source.getInputStream(); XMLStreamReader stream = null; try {// ww w. j av a 2s. co m stream = XMLInputFactory.newInstance().createXMLStreamReader(is); int eventType = stream.nextTag(); if (eventType != XMLStreamConstants.START_ELEMENT) { throw new SystemException("StAX Malfunction?"); } DOMConverter domConverter = new DOMConverter(); Map<String, String> rootNamespaceDeclarations = new HashMap<>(); QName objectsMarker = schemaRegistry.getPrismContext().getObjectsElementName(); if (objectsMarker != null && !QNameUtil.match(stream.getName(), objectsMarker)) { readSingleObjectIteratively(stream, rootNamespaceDeclarations, domConverter, handler); } for (int i = 0; i < stream.getNamespaceCount(); i++) { rootNamespaceDeclarations.put(stream.getNamespacePrefix(i), stream.getNamespaceURI(i)); } while (stream.hasNext()) { eventType = stream.next(); if (eventType == XMLStreamConstants.START_ELEMENT) { if (!readSingleObjectIteratively(stream, rootNamespaceDeclarations, domConverter, handler)) { return; } } } } catch (XMLStreamException ex) { String lineInfo = stream != null ? " on line " + stream.getLocation().getLineNumber() : ""; throw new SchemaException("Exception while parsing XML" + lineInfo + ": " + ex.getMessage(), ex); } finally { if (source.closeStreamAfterParsing()) { IOUtils.closeQuietly(is); } } }
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 *///ww w .j ava 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:edu.harvard.i2b2.fr.ws.FileRepositoryService.java
private OMElement buildOMElementFromString(String xmlString, String contentID) throws XMLStreamException { OMElement returnElement = null;/*from ww w. ja v a 2 s . com*/ try { StringReader strReader = new StringReader(xmlString); XMLInputFactory xif = XMLInputFactory.newInstance(); XMLStreamReader reader = xif.createXMLStreamReader(strReader); StAXOMBuilder builder = new StAXOMBuilder(reader); returnElement = builder.getDocumentElement(); OMFactory factory = OMAbstractFactory.getOMFactory(); OMNamespace omNs = factory.createOMNamespace("http://www.i2b2.org/xsd", "swa"); OMElement fileElement = factory.createOMElement("file", omNs, returnElement); fileElement.addAttribute("href", contentID, null); } catch (XMLStreamException ex) { log.error("Error while converting FR response PDO to OMElement", ex); throw ex; } return returnElement; }
From source file:edu.harvard.i2b2.eclipse.plugins.admin.utilities.ws.OntServiceDriver.java
/** * Function to convert Ont requestVdo to OMElement * /* w w w .j a v a2 s . c o m*/ * @param requestVdo String requestVdo to send to Ont web service * @return An OMElement containing the Ont web service requestVdo */ public static OMElement getOntPayLoad(String requestVdo) throws Exception { OMElement lineItem = null; try { StringReader strReader = new StringReader(requestVdo); XMLInputFactory xif = XMLInputFactory.newInstance(); XMLStreamReader reader = xif.createXMLStreamReader(strReader); StAXOMBuilder builder = new StAXOMBuilder(reader); lineItem = builder.getDocumentElement(); } catch (FactoryConfigurationError e) { // TODO Auto-generated catch block e.printStackTrace(); log.error(e.getMessage()); throw new Exception(e); } return lineItem; }
From source file:com.autonomy.aci.client.services.impl.AbstractStAXProcessor.java
/** * This constructor gets a new {@link XMLInputFactory} instance that is reused every time * {@link #process(com.autonomy.aci.client.transport.AciResponseInputStream)} is called, this * should be faster than creating a new instance every time this method is called. * <p>//from ww w . ja va2 s .c om * The properties are set to the following defaults if they are not specified as system properties: * <table summary=""> * <tr><th>Property</th><th>Default</th></tr> * <tr><td>XMLInputFactory.IS_NAMESPACE_AWARE</td><td><tt>false</tt></td></tr> * <tr><td>XMLInputFactory.IS_VALIDATING<tt>false</tt></td></tr> * <tr><td>XMLInputFactory.IS_COALESCING<tt>false</tt></td></tr> * <tr><td>XMLInputFactory.IS_REPLACING_ENTITY_REFERENCES<tt>true</tt></td></tr> * <tr><td>XMLInputFactory.IS_SUPPORTING_EXTERNAL_ENTITIES<tt>false</tt></td></tr> * <tr><td>XMLInputFactory.SUPPORT_DTD<tt>true</tt></td></tr> * </table> */ protected AbstractStAXProcessor() { // See if the various XMLInputFactory properties are set as system properties... namespaceAware = BooleanUtils.toBoolean( StringUtils.defaultString(System.getProperty(XMLInputFactory.IS_NAMESPACE_AWARE), "false")); validating = BooleanUtils .toBoolean(StringUtils.defaultString(System.getProperty(XMLInputFactory.IS_VALIDATING), "false")); coalescing = BooleanUtils .toBoolean(StringUtils.defaultString(System.getProperty(XMLInputFactory.IS_COALESCING), "false")); replacingEntityReferences = BooleanUtils.toBoolean(StringUtils .defaultString(System.getProperty(XMLInputFactory.IS_REPLACING_ENTITY_REFERENCES), "true")); supportingExternalEntities = BooleanUtils.toBoolean(StringUtils .defaultString(System.getProperty(XMLInputFactory.IS_SUPPORTING_EXTERNAL_ENTITIES), "false")); supportDtd = BooleanUtils .toBoolean(StringUtils.defaultString(System.getProperty(XMLInputFactory.SUPPORT_DTD), "true")); // Create the XMLStreamReader factory... xmlInputFactory = XMLInputFactory.newInstance(); }