List of usage examples for javax.xml.stream.events XMLEvent asStartElement
public StartElement asStartElement();
From source file:edu.monash.merc.system.parser.xml.GPMWSXmlParser.java
public List<GPMEntryBean> parseGPMXml(String fileName, XMLInputFactory2 factory2) { xmlif2 = factory2;//from ww w . j av a 2 s. c o m logger.info("Starting to parse " + fileName); XMLEventReader2 xmlEventReader = null; List<GPMEntryBean> gpmEntryBeans = new ArrayList<GPMEntryBean>(); try { xmlEventReader = (XMLEventReader2) xmlif2.createXMLEventReader(new FileInputStream(fileName)); QName proteinQN = new QName(ELE_GPM_PROTEIN); QName enspQN = new QName(ATTR_GPM_ENSP); QName ensgQN = new QName(ATTR_GPM_ENSG); QName identiQN = new QName(ELE_GPM_IDENTIFICATION); QName besteQN = new QName(ATTR_GPM_IDEN_BESTE); QName samplesQN = new QName(ATTR_GPM_IDEN_SAMPLES); String ensg = null; String ensp = null; String beste = null; String samples = null; GPMEntryBean gpmEntryBean = null; while (xmlEventReader.hasNextEvent()) { //eventType = reader.next(); XMLEvent event = xmlEventReader.nextEvent(); if (event.isStartElement()) { StartElement element = event.asStartElement(); //protein entry if (element.getName().equals(proteinQN)) { //get the version attribute Attribute enspAttr = element.getAttributeByName(enspQN); Attribute ensgAttr = element.getAttributeByName(ensgQN); if (enspAttr != null) { ensp = enspAttr.getValue(); } if (ensgAttr != null) { ensg = ensgAttr.getValue(); } //create gpn entry bean gpmEntryBean = new GPMEntryBean(); //create gpm dbsource DBSourceBean gpmDbSourceBean = new DBSourceBean(); gpmDbSourceBean.setDbName(DbAcType.GPM.type()); gpmDbSourceBean.setPrimaryEvidences(true); //set the gpm dbsource gpmEntryBean.setPrimaryDbSourceBean(gpmDbSourceBean); //create a gene bean if (StringUtils.isNotBlank(ensg)) { GeneBean geneBean = new GeneBean(); geneBean.setEnsgAccession(ensg); gpmEntryBean.setGeneBean(geneBean); } //create an identified accession bean AccessionBean identAccessionBean = parseIdentifiedAccessionBean(ensp); gpmEntryBean.setIdentifiedAccessionBean(identAccessionBean); //create dbsource and accession entry bean List<DbSourceAcEntryBean> dbSourceAcEntryBeanList = parseDBSourceAcEntryBeans(ensp, ensg); gpmEntryBean.setDbSourceAcEntryBeans(dbSourceAcEntryBeanList); } //protein entry if (element.getName().equals(identiQN)) { Attribute besteAttr = element.getAttributeByName(besteQN); Attribute samplesAttr = element.getAttributeByName(samplesQN); if (besteAttr != null) { beste = besteAttr.getValue(); } if (samplesAttr != null) { samples = samplesAttr.getValue(); } //create pe ms prob evidence based on beste and ensp accesion PEEvidenceBean peMsProbEvidence = createMsProbEvidence(beste, ensp); //parse pe ms samples evidence PEEvidenceBean peMsSamplesEvidence = createMsSamplesEvidence(samples, ensp); if (peMsProbEvidence != null) { gpmEntryBean.setPeMsProbEvidenceBean(peMsProbEvidence); } if (peMsSamplesEvidence != null) { gpmEntryBean.setPeMsSamplesEvidenceBean(peMsSamplesEvidence); } } } //End of element if (event.isEndElement()) { EndElement endElement = event.asEndElement(); //hpa entry end if (endElement.getName().equals(proteinQN)) { //if gene is not null, the accession is not null and get some evidences for this gpm entry, then we add it to the list GeneBean geneBean = gpmEntryBean.getGeneBean(); AccessionBean identifiedAcBean = gpmEntryBean.getIdentifiedAccessionBean(); PEEvidenceBean peMsProbEvidence = gpmEntryBean.getPeMsProbEvidenceBean(); PEEvidenceBean peMsSampEvidence = gpmEntryBean.getPeMsProbEvidenceBean(); if (geneBean != null && identifiedAcBean != null) { if (peMsProbEvidence != null || peMsSampEvidence != null) { gpmEntryBeans.add(gpmEntryBean); } } } } } return gpmEntryBeans; } catch (Exception ex) { logger.error(ex); throw new DMXMLParserException(ex); } finally { if (xmlEventReader != null) { try { xmlEventReader.close(); } catch (Exception e) { //ignore whatever caught. } } } }
From source file:eu.delving.sip.base.Harvestor.java
private String saveRecords(HttpEntity fetchedRecords, XMLEventWriter out) throws IOException, XMLStreamException { InputStream inputStream = fetchedRecords.getContent(); XMLEventReader in = inputFactory.createXMLEventReader(new StreamSource(inputStream, "UTF-8")); Path path = Path.create(); StringBuilder tokenBuilder = null; StringBuilder errorBuilder = null; String tokenValue = null;// w w w . java2 s . c o m List<XMLEvent> recordEvents = new ArrayList<XMLEvent>(); boolean finished = false; while (!finished) { XMLEvent event = in.nextEvent(); switch (event.getEventType()) { case XMLEvent.START_ELEMENT: StartElement start = event.asStartElement(); path = path.child(Tag.element(start.getName().getLocalPart())); if (!recordEvents.isEmpty()) { recordEvents.add(event); } else if (path.equals(RECORD_ROOT)) { if (namespaceCollector != null) { namespaceCollector.gatherFrom(start); out.add(eventFactory.createStartElement("", "", HARVEST_TAG, null, namespaceCollector.iterator())); out.add(eventFactory.createCharacters("\n")); namespaceCollector = null; } recordEvents.add(eventFactory.createCharacters("\n")); // flag that record has started } else if (path.equals(RESUMPTION_TOKEN)) { tokenBuilder = new StringBuilder(); tokenValue = null; } else if (path.equals(ERROR)) { errorBuilder = new StringBuilder(); } else if (namespaceCollector != null) { namespaceCollector.gatherFrom(start); } break; case XMLEvent.END_ELEMENT: if (!recordEvents.isEmpty()) { if (path.equals(RECORD_ROOT)) { out.add(eventFactory.createStartElement("", "", RECORD_TAG, null, null)); for (XMLEvent saved : recordEvents) { out.add(saved); } out.add(eventFactory.createEndElement("", "", RECORD_TAG)); out.add(eventFactory.createCharacters("\n")); recordEvents.clear(); tokenBuilder = null; recordCount++; } else { recordEvents.add(event); recordEvents.add(eventFactory.createCharacters("\n")); } } else if (path.equals(RESUMPTION_TOKEN) && tokenBuilder != null) { tokenValue = tokenBuilder.toString(); tokenBuilder = null; } else if (path.equals(ERROR) && errorBuilder != null) { throw new IOException("OAI-PMH Error: " + errorBuilder); } path = path.parent(); break; case XMLEvent.END_DOCUMENT: finished = true; break; case XMLEvent.CHARACTERS: case XMLEvent.CDATA: if (!recordEvents.isEmpty()) { String string = event.asCharacters().getData().trim(); if (!string.isEmpty()) { recordEvents.add(eventFactory.createCharacters(string)); } } else if (tokenBuilder != null) { tokenBuilder.append(event.asCharacters().getData()); } else if (errorBuilder != null) { errorBuilder.append(event.asCharacters().getData()); } break; default: if (!recordEvents.isEmpty()) { recordEvents.add(event); } break; } } return tokenValue; }
From source file:com.adobe.acs.commons.it.build.ScrMetadataIT.java
private Descriptor parseScr(InputStream is, String name, boolean checkNs) throws Exception { Descriptor result = new Descriptor(); XMLEventReader reader = xmlInputFactory.createXMLEventReader(is); while (reader.hasNext()) { XMLEvent event = reader.nextEvent(); if (event.isStartElement()) { StartElement start = event.asStartElement(); String elementName = start.getName().getLocalPart(); if (elementName.equals("component")) { result.name = start.getAttributeByName(new QName("name")).getValue(); if (checkNs) { String scrUri = start.getName().getNamespaceURI(); if (!ALLOWED_SCR_NS_URIS.contains(scrUri)) { throw new Exception( String.format("Banned Namespace URI %s found for %s", scrUri, name)); }/* w ww. jav a 2s. c o m*/ } } else if (elementName.equals("property")) { String propName = start.getAttributeByName(new QName("name")).getValue(); Attribute value = start.getAttributeByName(new QName("value")); Attribute typeAttr = start.getAttributeByName(new QName("type")); String type = typeAttr == null ? "String" : typeAttr.getValue(); if (value != null) { result.properties.add(new Property(propName, value.getValue(), type)); } else { result.properties.add(new Property(propName, cleanText(reader.getElementText()), type)); } } } } return result; }
From source file:com.adobe.acs.commons.it.build.ScrMetadataIT.java
private Descriptor parseMetatype(InputStream is, String name) throws Exception { Descriptor result = new Descriptor(); XMLEventReader reader = xmlInputFactory.createXMLEventReader(is); while (reader.hasNext()) { XMLEvent event = reader.nextEvent(); if (event.isStartElement()) { StartElement start = event.asStartElement(); String elementName = start.getName().getLocalPart(); if (elementName.equals("Designate")) { Attribute pidAttribute = start.getAttributeByName(new QName("pid")); if (pidAttribute != null) { result.name = pidAttribute.getValue(); } else { pidAttribute = start.getAttributeByName(new QName("factoryPid")); if (pidAttribute != null) { result.name = pidAttribute.getValue(); }// w w w . j a v a2s.co m result.factory = true; } } else if (elementName.equals("AD")) { String propName = start.getAttributeByName(new QName("id")).getValue(); Attribute value = start.getAttributeByName(new QName("default")); Attribute typeAttr = start.getAttributeByName(new QName("type")); String type = typeAttr == null ? "String" : typeAttr.getValue(); if (value == null) { result.properties.add(new Property(propName, "", type)); } else { result.properties.add(new Property(propName, value.getValue(), type)); } } } } if (result.name == null) { throw new Exception("Could not identify pid for " + name); } return result; }
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//from w ww . jav a 2 s. co 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: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./*from w w w . j a va 2 s . co m*/ * @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:fr.openwide.talendalfresco.rest.client.ClientCommandBase.java
protected void handleResponseTopLevelEvent(XMLEvent event) { switch (event.getEventType()) { case XMLEvent.CHARACTERS: case XMLEvent.CDATA: if (singleLevelTextBuf != null) { singleLevelTextBuf.append(event.asCharacters().getData()); } // else element not meaningful break;//from w w w . j a v a 2s. c o m case XMLEvent.START_ELEMENT: StartElement startElement = event.asStartElement(); String elementName = startElement.getName().getLocalPart(); if (defaultResultElementSet.contains(elementName)) { // reinit buffer at start of meaningful elements singleLevelTextBuf = new StringBuffer(); } else if (RestConstants.TAG_CONTENT.equals(elementName)) { // switch to content mode isInCommandResponseContent = true; } else { singleLevelTextBuf = null; // not useful } break; case XMLEvent.END_ELEMENT: if (singleLevelTextBuf == null) { break; // element not meaningful } // TODO or merely put it in the map since the element has been tested at start EndElement endElement = event.asEndElement(); elementName = endElement.getName().getLocalPart(); if (defaultResultElementSet.contains(elementName)) { String value = singleLevelTextBuf.toString(); elementValueMap.put(elementName, value); // TODO test if it is code and it is not OK, break to error handling } // singleLevelTextBuf = new StringBuffer(); // no ! in start break; } }
From source file:lille.iagl.python_bucket.PythonBucketter.java
public void file_to_list() throws XMLStreamException { int eventType; XMLEvent event; this._posts = new HashMap<>(); this._post = null; while (this.xmlSR.hasNext()) { event = this.xmlSR.nextEvent(); eventType = event.getEventType(); switch (eventType) { case XMLEvent.START_ELEMENT: switch (event.asStartElement().getName().toString().toLowerCase()) { // Si on tombe sur un post, on sauvegarde l'ancien et on instancie un nouveau case "post": // reinitialisation des variables this._post = new Post(); this._stack = null; this._frame = null; break; case "postid": event = this.xmlSR.nextEvent(); this._post.setPostId(event.asCharacters().getData()); break; case "url": event = this.xmlSR.nextEvent(); this._post.setUrl(event.asCharacters().getData()); break; case "stack": this._stack = new StackTrace(); break; case "type": event = this.xmlSR.nextEvent(); this._stack.setType(event.asCharacters().getData()); break; case "message": event = this.xmlSR.nextEvent(); if (event.isEndElement()) { this._stack.setMessage(""); } else { this._stack.setMessage(event.asCharacters().getData()); }/* ww w. j a v a2 s . co m*/ break; case "frame": this._frame = new Frame(); break; case "file": event = this.xmlSR.nextEvent(); this._frame.setFile(event.asCharacters().getData()); break; case "line": event = this.xmlSR.nextEvent(); this._frame.setLine(event.asCharacters().getData()); break; case "method": event = this.xmlSR.nextEvent(); this._frame.setMethod(event.asCharacters().getData()); break; } break; case XMLEvent.END_ELEMENT: switch (event.asEndElement().getName().toString().toLowerCase()) { case "stack": if (this._stack != null) { this._post.addStackTrace(this._stack); insert_post(); this._post.getStacktraces().clear(); //this._post.addStackTrace(this._stack); } break; case "frame": if (this._frame != null) { this._stack.addFrame(this._frame); } break; // case "post": // if (this._post != null) { // insert_post(); // } // break; } case XMLEvent.END_DOCUMENT: default: break; } } }
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 *///w ww . java 2s.c o m 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: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 ww . j ava 2 s . co m*/ * * @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) { } } } }