List of usage examples for javax.xml.stream XMLEventReader nextEvent
public XMLEvent nextEvent() throws XMLStreamException;
From source file:eu.delving.sip.xml.SourceConverter.java
public void parse(InputStream inputStream, OutputStream outputStream, Map<String, String> namespaces) throws StorageException { progressListener.prepareFor(totalRecords); // anonymousRecords = Integer.parseInt(System.getProperty(ANONYMOUS_RECORDS_PROPERTY, "0")); Path path = Path.create(); try {/*from w w w . jav a2 s .c o m*/ XMLEventReader in = inputFactory.createXMLEventReader(new StreamSource(inputStream, "UTF-8")); XMLEventWriter out = outputFactory.createXMLEventWriter(new OutputStreamWriter(outputStream, "UTF-8")); processEvents: while (!finished) { XMLEvent event = in.nextEvent(); switch (event.getEventType()) { case XMLEvent.START_DOCUMENT: out.add(eventFactory.createStartDocument()); out.add(eventFactory.createCharacters("\n")); List<Namespace> nslist = new ArrayList<Namespace>(); for (Map.Entry<String, String> entry : namespaces.entrySet()) { if (entry.getKey().isEmpty() || entry.getValue().trim().isEmpty()) continue; if (XSI_SCHEMA.equals(entry.getValue())) continue; nslist.add(eventFactory.createNamespace(entry.getKey(), entry.getValue())); } out.add(eventFactory.createStartElement("", "", ENVELOPE_TAG, null, nslist.iterator())); out.add(eventFactory.createCharacters("\n")); break; case XMLEvent.START_ELEMENT: boolean followsStart = start != null; start = event.asStartElement(); if (linesAvailable()) { eventBuffer.add(eventFactory.createCharacters("\n")); eventBuffer.add(eventFactory.createStartElement("", "", TEXT_TAG, null, null)); Iterator<String> walk = lines.iterator(); while (walk.hasNext()) { eventBuffer.add(eventFactory.createCharacters(walk.next())); if (walk.hasNext()) { eventBuffer.add(eventFactory.createEndElement("", "", TEXT_TAG)); eventBuffer.add(eventFactory.createCharacters("\n")); eventBuffer.add(eventFactory.createStartElement("", "", TEXT_TAG, null, null)); } } eventBuffer.add(eventFactory.createEndElement("", "", TEXT_TAG)); lines.clear(); } path = path.child(Tag.element(start.getName())); handleStartElement(path, followsStart); progressListener.setProgress(recordCount); break; case XMLEvent.END_ELEMENT: EndElement end = event.asEndElement(); if (recordEvents) { if (path.equals(recordRootPath)) { if (unique != null) { outputRecord(out); recordCount++; } else { clearEvents(); } } else { if (!uniqueElementPath.peek().isAttribute() && path.equals(uniqueElementPath)) { unique = StringUtils.join(lines, ""); } boolean addEndTag = true; if (linesAvailable()) { Iterator<String> walk = lines.iterator(); while (walk.hasNext()) { eventBuffer.add(eventFactory.createCharacters(walk.next())); if (walk.hasNext()) { eventBuffer.add(end); eventBuffer.add(eventFactory.createCharacters("\n")); handleStartElement(path, false); } } } else { if (eventBuffer.get(eventBuffer.size() - 1).isStartElement()) { eventBuffer.remove(eventBuffer.size() - 1); // remove the start event addEndTag = false; } } lines.clear(); if (addEndTag) { eventBuffer.add(end); eventBuffer.add(eventFactory.createCharacters("\n")); } } } start = null; path = path.parent(); break; case XMLEvent.END_DOCUMENT: out.add(eventFactory.createEndElement("", "", ENVELOPE_TAG)); out.add(eventFactory.createCharacters("\n")); out.add(eventFactory.createEndDocument()); out.flush(); break processEvents; case XMLEvent.CHARACTERS: case XMLEvent.CDATA: if (recordEvents) extractLines(event.asCharacters().getData()); break; } } } catch (CancelException e) { throw new StorageException("Cancelled", e); } catch (StorageException e) { throw e; } catch (Exception e) { throw new StorageException("Storage problem", e); } finally { if (uniqueRepeatCount > 0) { progressListener.getFeedback().alert(String.format("Uniqueness violations : " + uniqueRepeatCount)); } IOUtils.closeQuietly(inputStream); IOUtils.closeQuietly(outputStream); } }
From source file:sapience.injectors.stax.inject.StringBasedStaxStreamInjector.java
/** * The actual injection procedure// w w w .j av a2 s . c o m * @param in the input stream where the XML is coming from (will be closed in the end) * @param out the output stream where we write the annotated XML into (remains open) * @param refs a list of references * @throws IOException */ public void inject(InputStream in, OutputStream out, List<Reference> refs) throws IOException { StringBuilder pb; String characters = null; NamespaceContext context = null; int marked; current_path = new Stack<String>(); current_path.push("//"); try { XMLEventReader r = inFac.createXMLEventReader(in); XMLEventWriter w = outFac.createXMLEventWriter(out); XMLStreamWriter ws = outFac.createXMLStreamWriter(System.out); while (r.hasNext()) { XMLEvent e = r.nextEvent(); switch (e.getEventType()) { case XMLEvent.START_ELEMENT: StartElement se = (StartElement) e; context = se.getNamespaceContext(); if (elementReferences == null) { // process the namespaces in the references this.prepareReferences(refs, context); } // store location col = e.getLocation().getColumnNumber(); // add to current xpath current_path.add(generator.asXPathString((StartElement) e)); //XPathHelper.addCurrentElementToStack(current_path, se); // check if the current xpath is in our list of attribute references if (attributeReferences.size() > 0) { for (int i = 0; i < refs.size(); i++) { Stack<String> stack = attributeReferences.get(refs.get(i)); if (matcher.matches(current_path, stack, true, context)) { // yes, let's inject the reference (only valid for attributes here) this.handleAttribute(w, refs.get(i)); attributeReferences.remove(refs.get(i)); refs.remove(i); } } } w.add(e); break; case XMLEvent.END_ELEMENT: // before removing from stack, we check if the current path with added characters is a match (which means we have to add a new element now) if (characters != null) this.current_path.push(characters); if (elementReferences.size() > 0) { for (int i = 0; i < refs.size(); i++) { Stack<String> stack = elementReferences.get(refs.get(i)); if (matcher.matches(current_path, stack, true, context)) { // yes, let's inject the reference (only valid for attributes here) this.interceptingElement = refs.get(i); elementReferences.remove(refs.get(i)); refs.remove(i); } } } if (characters != null) { // clean up this.current_path.pop(); characters = null; } this.current_path.pop(); w.add(e); // if the intercepting is not null, the preceding element was a match, hence we inject some xml before writing a new element if (this.interceptingElement != null) { w.add(eventFac.createSpace("\n")); writeElementIntoStream(w, interceptingElement); } break; case XMLEvent.CHARACTERS: characters = generator.asXPathString((Characters) e); w.add(e); break; default: w.add(e); break; } } } catch (XPathExpressionException e) { if (logger.isLoggable(Level.SEVERE)) { logger.log(Level.SEVERE, "Not a valid XPath", e); } throw new IOException(e); } catch (XMLStreamException e) { if (logger.isLoggable(Level.SEVERE)) { logger.log(Level.SEVERE, "Failed to inject. Reason: " + e.getLocalizedMessage(), e); } throw new IOException(e); } finally { in.close(); } }
From source file:com.aionemu.gameserver.dataholders.loadingutils.XmlMerger.java
/** * Read all {@link javax.xml.stream.events.XMLEvent}'s from specified file * and write them onto the {@link javax.xml.stream.XMLEventWriter} * * @param file File to import/* w ww . ja v a 2 s. c o m*/ * @param skipRoot Skip-root flag * @param writer Destenation writer * @throws XMLStreamException On event reading/writing error. * @throws FileNotFoundException if the reading file does not exist, is a * directory rather than a regular file, or for some other reason cannot be * opened for reading. */ private void importFile(File file, boolean skipRoot, XMLEventWriter writer, Properties metadata) throws XMLStreamException, IOException { logger.debug("Appending file " + file); metadata.setProperty(file.getPath(), makeHash(file)); XMLEventReader reader = null; try { reader = inputFactory.createXMLEventReader(new FileReader(file)); QName firstTagQName = null; while (reader.hasNext()) { XMLEvent event = reader.nextEvent(); // skip start and end of document. if (event.isStartDocument() || event.isEndDocument()) { continue; } // skip all comments. if (event instanceof Comment) { continue; } // skip white-spaces and all ignoreable white-spaces. if (event.isCharacters()) { if (event.asCharacters().isWhiteSpace() || event.asCharacters().isIgnorableWhiteSpace()) { continue; } } // modify root-tag of imported file. if (firstTagQName == null && event.isStartElement()) { firstTagQName = event.asStartElement().getName(); if (skipRoot) { continue; } else { StartElement old = event.asStartElement(); event = eventFactory.createStartElement(old.getName(), old.getAttributes(), null); } } // if root was skipped - skip root end too. if (event.isEndElement() && skipRoot && event.asEndElement().getName().equals(firstTagQName)) { continue; } // finally - write tag writer.add(event); } } finally { if (reader != null) { try { reader.close(); } catch (Exception ignored) { } } } }
From source file:act.installer.pubchem.PubchemParser.java
/** * Incrementally parses a stream of XML events from a PubChem file, extracting the next available PC-Compound entry * as a Chemical object./*w w w .ja v a2s. com*/ * @param eventReader The xml event reader we are parsing the XML from * @return The constructed chemical * @throws XMLStreamException * @throws XPathExpressionException */ public Chemical extractNextChemicalFromXMLStream(XMLEventReader eventReader) throws XMLStreamException, JaxenException { Document bufferDoc = null; Element currentElement = null; StringBuilder textBuffer = null; /* With help from * http://stackoverflow.com/questions/7998733/loading-local-chunks-in-dom-while-parsing-a-large-xml-file-in-sax-java */ while (eventReader.hasNext()) { XMLEvent event = eventReader.nextEvent(); switch (event.getEventType()) { case XMLStreamConstants.START_ELEMENT: String eventName = event.asStartElement().getName().getLocalPart(); if (COMPOUND_DOC_TAG.equals(eventName)) { // Create a new document if we've found the start of a compound object. bufferDoc = documentBuilder.newDocument(); currentElement = bufferDoc.createElement(eventName); bufferDoc.appendChild(currentElement); } else if (currentElement != null) { // Wait until we've found a compound entry to start slurping up data. // Create a new child element and push down the current pointer when we find a new node. Element newElement = bufferDoc.createElement(eventName); currentElement.appendChild(newElement); currentElement = newElement; } // If we aren't in a PC-Compound tree, we just let the elements pass by. break; case XMLStreamConstants.CHARACTERS: if (currentElement == null) { // Ignore this event if we're not in a PC-Compound tree. continue; } Characters chars = event.asCharacters(); // Ignore only whitespace strings, which just inflate the size of the DOM. Text coalescing makes this safe. if (chars.isWhiteSpace()) { continue; } // Rely on the XMLEventStream to coalesce consecutive text events. Text textNode = bufferDoc.createTextNode(chars.getData()); currentElement.appendChild(textNode); break; case XMLStreamConstants.END_ELEMENT: if (currentElement == null) { // Ignore this event if we're not in a PC-Compound tree. continue; } eventName = event.asEndElement().getName().getLocalPart(); Node parentNode = currentElement.getParentNode(); if (parentNode instanceof Element) { currentElement = (Element) parentNode; } else if (parentNode instanceof Document && eventName.equals(COMPOUND_DOC_TAG)) { // We're back at the top of the node stack! Convert the buffered document into a Chemical. PubchemEntry entry = extractPCCompoundFeatures(bufferDoc); if (entry != null) { return entry.asChemical(); } else { // Skip this entry if we can't process it correctly by resetting the world and continuing on. bufferDoc = null; currentElement = null; } } else { // This should not happen, but is here as a sanity check. throw new RuntimeException(String.format("Parent of XML element %s is of type %d, not Element", currentElement.getTagName(), parentNode.getNodeType())); } break; // TODO: do we care about attributes or other XML structures? } } // Return null when we run out of chemicals, just like readLine(). return null; }
From source file:sapience.injectors.stax.inject.ModelBasedStaxStreamInjector.java
/** * If the reference is more then a simple attribute, we have to add new XML (subtree) to the stream. We transform * the reference into an InputStream and invoke another SAX parsing process for it. But the parsed events are added * to the main XMLEventWriter. /*from w w w .j a v a 2s. co m*/ * * @param w * @param string * @throws XMLStreamException * @throws XMLStreamException */ private void createEventsForElement(XMLEventWriter w, Reference ref) throws XMLStreamException { XMLEventReader r = null; try { StringBuilder target = new StringBuilder(ref.getTarget().toString()); NamespaceContext c = w.getNamespaceContext(); ByteArrayInputStream bais = new ByteArrayInputStream(target.toString().getBytes()); getXMLInputFactory().setProperty(XMLInputFactory.IS_NAMESPACE_AWARE, false); r = getXMLInputFactory().createXMLEventReader(bais); while (r.hasNext()) { XMLEvent e = r.nextEvent(); switch (e.getEventType()) { case XMLEvent.START_DOCUMENT: break; case XMLEvent.END_DOCUMENT: break; default: w.add(e); break; } } } finally { ; if (r != null) r.close(); } }
From source file:com.github.lindenb.jvarkit.tools.blast.BlastFilterJS.java
@Override protected Collection<Throwable> call(String inputName) throws Exception { final CompiledScript compiledScript; Unmarshaller unmarshaller;//www .ja v a 2 s .c om Marshaller marshaller; try { compiledScript = super.compileJavascript(); JAXBContext jc = JAXBContext.newInstance("gov.nih.nlm.ncbi.blast"); unmarshaller = jc.createUnmarshaller(); marshaller = jc.createMarshaller(); XMLInputFactory xmlInputFactory = XMLInputFactory.newFactory(); xmlInputFactory.setProperty(XMLInputFactory.IS_NAMESPACE_AWARE, Boolean.TRUE); xmlInputFactory.setProperty(XMLInputFactory.IS_COALESCING, Boolean.TRUE); xmlInputFactory.setProperty(XMLInputFactory.IS_REPLACING_ENTITY_REFERENCES, Boolean.TRUE); xmlInputFactory.setProperty(XMLInputFactory.IS_SUPPORTING_EXTERNAL_ENTITIES, Boolean.FALSE); marshaller.setProperty(Marshaller.JAXB_FRAGMENT, true); marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); PrintWriter pw = openFileOrStdoutAsPrintWriter(); XMLOutputFactory xof = XMLOutputFactory.newFactory(); xof.setProperty(XMLOutputFactory.IS_REPAIRING_NAMESPACES, Boolean.FALSE); XMLEventWriter w = xof.createXMLEventWriter(pw); StreamSource src = null; if (inputName == null) { LOG.info("Reading stdin"); src = new StreamSource(stdin()); } else { LOG.info("Reading file " + inputName); src = new StreamSource(new File(inputName)); } XMLEventReader r = xmlInputFactory.createXMLEventReader(src); XMLEventFactory eventFactory = XMLEventFactory.newFactory(); SimpleBindings bindings = new SimpleBindings(); while (r.hasNext()) { XMLEvent evt = r.peek(); switch (evt.getEventType()) { case XMLEvent.START_ELEMENT: { StartElement sE = evt.asStartElement(); Hit hit = null; JAXBElement<Hit> jaxbElement = null; if (sE.getName().getLocalPart().equals("Hit")) { jaxbElement = unmarshaller.unmarshal(r, Hit.class); hit = jaxbElement.getValue(); } else { w.add(r.nextEvent()); break; } if (hit != null) { bindings.put("hit", hit); boolean accept = super.evalJavaScriptBoolean(compiledScript, bindings); if (accept) { marshaller.marshal(jaxbElement, w); w.add(eventFactory.createCharacters("\n")); } } break; } case XMLEvent.SPACE: break; default: { w.add(r.nextEvent()); break; } } r.close(); } w.flush(); w.close(); pw.flush(); pw.close(); return RETURN_OK; } catch (Exception err) { return wrapException(err); } finally { } }
From source file:com.aionengine.gameserver.dataholders.loadingutils.XmlMerger.java
/** * This method processes the source file, replacing all of * the 'import' tags by the data from the relevant files. * * @throws XMLStreamException on event writing error. * @throws IOException if the destination file exists but is a directory rather than * a regular file, does not exist but cannot be created, * or cannot be opened for any other reason *///from w w w . j a v a2 s .c om private void doUpdate() throws XMLStreamException, IOException { XMLEventReader reader = null; XMLEventWriter writer = null; Properties metadata = new Properties(); try { writer = outputFactory.createXMLEventWriter(new BufferedWriter(new FileWriter(destFile, false))); reader = inputFactory.createXMLEventReader(new FileReader(sourceFile)); while (reader.hasNext()) { final XMLEvent xmlEvent = reader.nextEvent(); if (xmlEvent.isStartElement() && isImportQName(xmlEvent.asStartElement().getName())) { processImportElement(xmlEvent.asStartElement(), writer, metadata); continue; } if (xmlEvent.isEndElement() && isImportQName(xmlEvent.asEndElement().getName())) continue; if (xmlEvent instanceof Comment)// skip comments. continue; if (xmlEvent.isCharacters())// skip whitespaces. if (xmlEvent.asCharacters().isWhiteSpace() || xmlEvent.asCharacters().isIgnorableWhiteSpace())// skip whitespaces. continue; writer.add(xmlEvent); if (xmlEvent.isStartDocument()) { writer.add(eventFactory.createComment("\nThis file is machine-generated. DO NOT MODIFY IT!\n")); } } storeFileModifications(metadata, metaDataFile); } finally { if (writer != null) try { writer.close(); } catch (Exception ignored) { } if (reader != null) try { reader.close(); } catch (Exception ignored) { } } }
From source file:edu.jhu.hlt.concrete.ingesters.bolt.BoltForumPostIngester.java
/** * Move the iterator so that a call to nextEvent will return the beginning of a post tag. * * @param rdr//w w w .j av a 2 s . c o m * @throws XMLStreamException */ private void iterateToPosts(final XMLEventReader rdr) throws XMLStreamException { // Peek at the next element. XMLEvent fp = rdr.peek(); // If start element and part == "post", return. if (fp.isStartElement()) { StartElement se = fp.asStartElement(); if (se.getName().getLocalPart().equals(POST_LOCAL_NAME)) return; else // Churn through non-post start tags. this.handleNonPostStartElement(rdr); } else // Drop. rdr.nextEvent(); this.iterateToPosts(rdr); }
From source file:com.aionemu.gameserver.dataholders.loadingutils.XmlMerger.java
/** * This method processes the source file, replacing all of the 'import' tags * by the data from the relevant files.// w w w .ja va2 s . c om * * @throws XMLStreamException on event writing error. * @throws IOException if the destination file exists but is a directory * rather than a regular file, does not exist but cannot be created, or * cannot be opened for any other reason */ private void doUpdate() throws XMLStreamException, IOException { XMLEventReader reader = null; XMLEventWriter writer = null; Properties metadata = new Properties(); try { writer = outputFactory.createXMLEventWriter(new BufferedWriter(new FileWriter(destFile, false))); reader = inputFactory.createXMLEventReader(new FileReader(sourceFile)); while (reader.hasNext()) { final XMLEvent xmlEvent = reader.nextEvent(); if (xmlEvent.isStartElement() && isImportQName(xmlEvent.asStartElement().getName())) { processImportElement(xmlEvent.asStartElement(), writer, metadata); continue; } if (xmlEvent.isEndElement() && isImportQName(xmlEvent.asEndElement().getName())) { continue; } if (xmlEvent instanceof Comment)// skip comments. { continue; } if (xmlEvent.isCharacters())// skip whitespaces. { if (xmlEvent.asCharacters().isWhiteSpace() || xmlEvent.asCharacters().isIgnorableWhiteSpace())// skip // whitespaces. { continue; } } writer.add(xmlEvent); if (xmlEvent.isStartDocument()) { writer.add(eventFactory.createComment("\nThis file is machine-generated. DO NOT MODIFY IT!\n")); } } storeFileModifications(metadata, metaDataFile); } finally { if (writer != null) { try { writer.close(); } catch (Exception ignored) { } } if (reader != null) { try { reader.close(); } catch (Exception ignored) { } } } }
From source file:eu.peppol.document.PayloadParserTest.java
/** * Takes a file holding an SBD/SBDH with an ASiC archive in base64 as payload and extracts the ASiC archive in binary format, while * calculating the message digest./*from ww w.j a v a2 s. c o m*/ * * @throws Exception */ @Test public void parseSampleSbdWithAsic() throws Exception { InputStream resourceAsStream = PayloadParserTest.class.getClassLoader() .getResourceAsStream("sample-sbd-with-asic.xml"); assertNotNull(resourceAsStream); Path xmlFile = Files.createTempFile("unit-test", ".xml"); XMLEventReader xmlEventReader = XMLInputFactory.newInstance().createXMLEventReader(resourceAsStream, "UTF-8"); FileOutputStream outputStream = new FileOutputStream(xmlFile.toFile()); XMLEventWriter xmlEventWriter = XMLOutputFactory.newInstance().createXMLEventWriter(outputStream, "UTF-8"); Path asicFile = Files.createTempFile("unit-test", ".asice"); OutputStream asicOutputStream = Files.newOutputStream(asicFile); MessageDigest messageDigest = MessageDigest.getInstance("SHA-256"); DigestOutputStream digestOutputStream = new DigestOutputStream(asicOutputStream, messageDigest); Base64OutputStream base64OutputStream = new Base64OutputStream(digestOutputStream, false); boolean insideAsicElement = false; while (xmlEventReader.hasNext()) { XMLEvent xmlEvent = xmlEventReader.nextEvent(); switch (xmlEvent.getEventType()) { case XMLEvent.START_ELEMENT: String localPart = xmlEvent.asStartElement().getName().getLocalPart(); if ("asic".equals(localPart)) { insideAsicElement = true; } break; case XMLEvent.END_ELEMENT: localPart = xmlEvent.asEndElement().getName().getLocalPart(); if ("asic".equals(localPart)) { insideAsicElement = false; } break; case XMLEvent.CHARACTERS: // Whenever we are inside the ASiC XML element, spit // out the base64 encoded data into the base64 decoding output stream. if (insideAsicElement) { Characters characters = xmlEvent.asCharacters(); base64OutputStream.write(characters.getData().getBytes("UTF-8")); } break; } xmlEventWriter.add(xmlEvent); } asicOutputStream.close(); outputStream.close(); log.debug("Wrote xml output to: " + xmlFile); log.debug("Wrote ASiC to:" + asicFile); log.debug("Digest: " + new String(Base64.getEncoder().encode(messageDigest.digest()))); }