List of usage examples for javax.xml.stream XMLStreamConstants END_ELEMENT
int END_ELEMENT
To view the source code for javax.xml.stream XMLStreamConstants END_ELEMENT.
Click Source Link
From source file:de.huxhorn.sulky.plist.impl.PropertyListReader.java
private Object readValue(XMLStreamReader reader) throws XMLStreamException { int type = reader.getEventType(); if (XMLStreamConstants.START_ELEMENT == type && TRUE_NODE.equals(reader.getLocalName())) { reader.nextTag();/*from ww w . ja v a 2 s .co m*/ reader.require(XMLStreamConstants.END_ELEMENT, null, TRUE_NODE); reader.nextTag(); return Boolean.TRUE; } if (XMLStreamConstants.START_ELEMENT == type && FALSE_NODE.equals(reader.getLocalName())) { reader.nextTag(); reader.require(XMLStreamConstants.END_ELEMENT, null, FALSE_NODE); reader.nextTag(); return Boolean.FALSE; } if (XMLStreamConstants.START_ELEMENT == type && REAL_NODE.equals(reader.getLocalName())) { String text = StaxUtilities.readSimpleTextNodeIfAvailable(reader, null, REAL_NODE); return Double.parseDouble(text); } if (XMLStreamConstants.START_ELEMENT == type && INTEGER_NODE.equals(reader.getLocalName())) { String text = StaxUtilities.readSimpleTextNodeIfAvailable(reader, null, INTEGER_NODE); return Long.parseLong(text); } if (XMLStreamConstants.START_ELEMENT == type && STRING_NODE.equals(reader.getLocalName())) { return StaxUtilities.readSimpleTextNodeIfAvailable(reader, null, STRING_NODE); } if (XMLStreamConstants.START_ELEMENT == type && DATA_NODE.equals(reader.getLocalName())) { return readData(reader); } if (XMLStreamConstants.START_ELEMENT == type && DATE_NODE.equals(reader.getLocalName())) { return readDate(reader); } if (XMLStreamConstants.START_ELEMENT == type && ARRAY_NODE.equals(reader.getLocalName())) { return readArray(reader); } if (XMLStreamConstants.START_ELEMENT == type && DICT_NODE.equals(reader.getLocalName())) { return readDict(reader); } throw new RuntimeException("Unexpected XML-Node: " + reader.getLocalName()); }
From source file:net.bulletin.pdi.xero.step.support.XMLChunkerImpl.java
private String pullNextXmlChunkFromTopElementOnStack(XMLChunkerState data) throws KettleException { Stack<String> elementStack = data.getElementStack(); XMLStreamReader xmlStreamReader = data.getXmlStreamReader(); int elementStackDepthOnEntry = elementStack.size(); StringWriter stringWriter = new StringWriter(); try {/*from w ww .jav a 2 s.com*/ XMLStreamWriter xmlStreamWriter = data.getXmlOutputFactory().createXMLStreamWriter(stringWriter); xmlStreamWriter.writeStartDocument(CharEncoding.UTF_8, "1.0"); // put the current element on because presumably it's the open element for the one // that is being looked for. XmlReaderToWriter.write(xmlStreamReader, xmlStreamWriter); while (xmlStreamReader.hasNext() & elementStack.size() >= elementStackDepthOnEntry) { switch (xmlStreamReader.next()) { case XMLStreamConstants.END_DOCUMENT: break; // handled below explicitly. case XMLStreamConstants.END_ELEMENT: elementStack.pop(); XmlReaderToWriter.write(xmlStreamReader, xmlStreamWriter); break; case XMLStreamConstants.START_ELEMENT: elementStack.push(xmlStreamReader.getLocalName()); XmlReaderToWriter.write(xmlStreamReader, xmlStreamWriter); break; default: XmlReaderToWriter.write(xmlStreamReader, xmlStreamWriter); break; } } xmlStreamWriter.writeEndDocument(); xmlStreamWriter.close(); } catch (Exception e) { throw new KettleException("unable to process a chunk of the xero xml stream", e); } return stringWriter.toString(); }
From source file:edu.cmu.lti.oaqa.knn4qa.collection_reader.YahooAnswersStreamParser.java
private ParsedQuestion fetchNext() throws XMLStreamException { ParsedQuestion parsed = null;/*from w ww . ja v a 2 s .c o m*/ String question = ""; String questDetail = ""; String questUri = ""; String bestAnsw = ""; ArrayList<String> answers = new ArrayList<String>(); boolean bStart = false; String tagContent = null; while (mReader.hasNext()) { switch (mReader.next()) { case XMLStreamConstants.START_ELEMENT: switch (mReader.getLocalName()) { case "vespaadd": bStart = true; break; } break; case XMLStreamConstants.CHARACTERS: tagContent = mReader.getText(); break; case XMLStreamConstants.END_ELEMENT: switch (mReader.getLocalName()) { case "vespaadd": return new ParsedQuestion(question, questDetail, questUri, answers, bestAnsw, mDoCleanUp); case "uri": questUri = tagContent; break; case "subject": question = tagContent; break; case "content": questDetail = tagContent; break; case "bestanswer": bestAnsw = tagContent; break; case "answer_item": answers.add(tagContent); break; } break; } } return null; }
From source file:edu.indiana.d2i.htrc.portal.HTRCAgentClient.java
private Map<String, JobDetailsBean> parseJobDetailBeans(InputStream stream) throws XMLStreamException { Map<String, JobDetailsBean> res = new TreeMap<String, JobDetailsBean>(); XMLStreamReader parser = factory.createXMLStreamReader(stream); while (parser.hasNext()) { int event = parser.next(); if (event == XMLStreamConstants.START_ELEMENT) { if (parser.hasName()) { if (parser.getLocalName().equals("job_status")) { // one job status JobDetailsBean detail = new JobDetailsBean(); Map<String, String> jobParams = new HashMap<String, String>(); Map<String, String> results = new HashMap<String, String>(); int innerEvent = parser.next(); while (true) { if (innerEvent == XMLStreamConstants.END_ELEMENT && parser.getLocalName().equals("job_status")) { break; }//from w w w . j a v a2s .c o m if (innerEvent == XMLStreamConstants.START_ELEMENT && parser.hasName()) { // single tag if (parser.getLocalName().equals("job_name")) { detail.setJobTitle(parser.getElementText()); } else if (parser.getLocalName().equals("user")) { detail.setUserName(parser.getElementText()); } else if (parser.getLocalName().equals("algorithm")) { detail.setAlgorithmName(parser.getElementText()); } else if (parser.getLocalName().equals("job_id")) { detail.setJobId(parser.getElementText()); } else if (parser.getLocalName().equals("date")) { detail.setLastUpdatedDate(parser.getElementText()); } // parameters if (parser.hasName() && parser.getLocalName().equals(JobDetailsBean.ONEPARAM)) { String name, value; name = value = ""; for (int i = 0; i < 3; i++) { if (parser.getAttributeName(i).toString().equals("name")) name = parser.getAttributeValue(i); if (parser.getAttributeName(i).toString().equals("value")) value = parser.getAttributeValue(i); } jobParams.put(name, value); } // status if (parser.hasName() && parser.getLocalName().equals(JobDetailsBean.STATUS)) { String status = parser.getAttributeValue(0); detail.setJobStatus(status); } // results if (parser.hasName() && parser.getLocalName().equals(JobDetailsBean.RESULT)) { String name = parser.getAttributeValue(0); String value = parser.getElementText(); results.put(name, value); } // message if (parser.hasName() && parser.getLocalName().equals(JobDetailsBean.MESSAGE)) { detail.setMessage(parser.getElementText()); } // saved or unsaved if (parser.hasName() && parser.getLocalName().equals(JobDetailsBean.SAVEDORNOT)) { detail.setJobSavedStr(parser.getElementText()); } } innerEvent = parser.next(); } detail.setJobParams(jobParams); detail.setResults(results); res.put(detail.getJobId(), detail); } } } } return res; }
From source file:StAXEventTreeViewer.java
public void buildTree(DefaultTreeModel treeModel, DefaultMutableTreeNode current, File file) throws XMLStreamException, FileNotFoundException { XMLInputFactory inputFactory = XMLInputFactory.newInstance(); XMLEventReader reader = inputFactory.createXMLEventReader(new FileInputStream(file)); while (reader.hasNext()) { XMLEvent event = reader.nextEvent(); switch (event.getEventType()) { case XMLStreamConstants.START_DOCUMENT: StartDocument startDocument = (StartDocument) event; DefaultMutableTreeNode version = new DefaultMutableTreeNode(startDocument.getVersion()); current.add(version);//from w ww . j a v a 2 s . c o m current.add(new DefaultMutableTreeNode(startDocument.isStandalone())); current.add(new DefaultMutableTreeNode(startDocument.standaloneSet())); current.add(new DefaultMutableTreeNode(startDocument.encodingSet())); current.add(new DefaultMutableTreeNode(startDocument.getCharacterEncodingScheme())); break; case XMLStreamConstants.START_ELEMENT: StartElement startElement = (StartElement) event; QName elementName = startElement.getName(); DefaultMutableTreeNode element = new DefaultMutableTreeNode(elementName.getLocalPart()); current.add(element); current = element; if (!elementName.getNamespaceURI().equals("")) { String prefix = elementName.getPrefix(); if (prefix.equals("")) { prefix = "[None]"; } DefaultMutableTreeNode namespace = new DefaultMutableTreeNode( "prefix=" + prefix + ",URI=" + elementName.getNamespaceURI()); current.add(namespace); } for (Iterator it = startElement.getAttributes(); it.hasNext();) { Attribute attr = (Attribute) it.next(); DefaultMutableTreeNode attribute = new DefaultMutableTreeNode("Attribute (name=" + attr.getName().getLocalPart() + ",value=" + attr.getValue() + "')"); String attURI = attr.getName().getNamespaceURI(); if (!attURI.equals("")) { String attPrefix = attr.getName().getPrefix(); if (attPrefix.equals("")) { attPrefix = "[None]"; } attribute.add(new DefaultMutableTreeNode("prefix = " + attPrefix + ", URI = " + attURI)); } current.add(attribute); } break; case XMLStreamConstants.END_ELEMENT: current = (DefaultMutableTreeNode) current.getParent(); break; case XMLStreamConstants.CHARACTERS: Characters characters = (Characters) event; if (!characters.isIgnorableWhiteSpace() && !characters.isWhiteSpace()) { String data = characters.getData(); if (data.length() != 0) { current.add(new DefaultMutableTreeNode(characters.getData())); } } break; case XMLStreamConstants.DTD: DTD dtde = (DTD) event; current.add(new DefaultMutableTreeNode(dtde.getDocumentTypeDeclaration())); default: System.out.println(event.getClass().getName()); } } }
From source file:de.huxhorn.whistler.services.TagdefTagDefinitionService.java
public TagDefinition define(String tag) { if (tag.startsWith("#")) { tag = tag.substring(1);//from w w w . j av a 2 s . c o m } XMLStreamReader2 xmlStreamReader = null; try { BasicHttpParams params = new BasicHttpParams(); DefaultHttpClient httpclient = new DefaultHttpClient(params); URI uri = URIUtils.createURI("http", "api.tagdef.com", -1, "/one." + tag, null, null); HttpGet httpget = new HttpGet(uri); if (logger.isDebugEnabled()) logger.debug("HttpGet.uri={}", httpget.getURI()); HttpResponse response = httpclient.execute(httpget); HttpEntity entity = response.getEntity(); if (entity != null) { InputStream instream = entity.getContent(); /* <?xml version="1.0" encoding="UTF-8"?> <defs> <def> <text>#ff is the same as (short for) #followfriday.</text> <time>2009-05-08 00:00:00</time> <upvotes>38</upvotes> <downvotes>7</downvotes> <uri>http://tagdef.com/ff</uri> </def> </defs> <?xml version="1.0" encoding="UTF-8"?> <defs> <uri>http://tagdef.com/nonexisting</uri> </defs> */ xmlStreamReader = (XMLStreamReader2) WstxInputFactory.newInstance().createXMLStreamReader(instream); while (xmlStreamReader.hasNext()) { int type = xmlStreamReader.next(); if (type == XMLStreamConstants.START_ELEMENT) { String tagName = xmlStreamReader.getName().getLocalPart(); if ("def".equals(tagName)) { TagDefinition result = new TagDefinition(); result.setTag("#" + tag); while (xmlStreamReader.hasNext()) { type = xmlStreamReader.next(); if (type == XMLStreamConstants.START_ELEMENT) { tagName = xmlStreamReader.getName().getLocalPart(); if ("text".equals(tagName)) { result.setDefinition(xmlStreamReader.getElementText()); } else if ("uri".equals(tagName)) { result.setUrl(xmlStreamReader.getElementText()); } } else if (type == XMLStreamConstants.END_ELEMENT) { tagName = xmlStreamReader.getName().getLocalPart(); if ("def".equals(tagName)) { break; } } } return result; } } } /* BufferedReader reader = new BufferedReader(new InputStreamReader(instream, "UTF-8")); StringBuilder builder=new StringBuilder(); for(;;) { String line = reader.readLine(); if(line == null) { break; } if(builder.length() != 0) { builder.append("\n"); } builder.append(line); } if(logger.isInfoEnabled()) logger.info("Result: {}", builder); */ } } catch (IOException ex) { if (logger.isWarnEnabled()) logger.warn("Exception!", ex); } catch (URISyntaxException ex) { if (logger.isWarnEnabled()) logger.warn("Exception!", ex); } catch (XMLStreamException ex) { if (logger.isWarnEnabled()) logger.warn("Exception!", ex); } finally { if (xmlStreamReader != null) { try { xmlStreamReader.closeCompletely(); } catch (XMLStreamException e) { // ignore } } } return null; }
From source file:com.wavemaker.runtime.data.parser.BaseHbmParser.java
protected String nextInternal(String parentElementName, int numTries, String... elementNames) { int i = 1;//from w ww .j a va 2s.c o m try { for (int event = this.xmlReader.next(); event != XMLStreamConstants.END_DOCUMENT; event = this.xmlReader .next()) { switch (event) { case XMLStreamConstants.START_ELEMENT: this.currentText.delete(0, this.currentText.length()); this.currentElementName = this.xmlReader.getName().toString(); if (numTries > -1) { if (i == numTries) { return this.currentElementName; } else if (i > numTries) { return null; } } i++; if (elementNames != null) { for (String s : elementNames) { if (s.equals(this.currentElementName)) { return this.currentElementName; } } } break; case XMLStreamConstants.END_ELEMENT: String endElementName = this.xmlReader.getName().toString(); if (endElementName.equals(parentElementName)) { return null; } break; case XMLStreamConstants.CHARACTERS: this.currentText.append(this.xmlReader.getText()); break; case XMLStreamConstants.CDATA: break; } } } catch (XMLStreamException ex) { throw new WMRuntimeException(ex); } return null; }
From source file:net.bulletin.pdi.xero.step.support.XMLChunkerImpl.java
@Override public String pullNextXmlChunk() throws KettleException { Stack<String> elementStack = xmlChunkerState.getElementStack(); XMLStreamReader xmlStreamReader = xmlChunkerState.getXmlStreamReader(); try {//from w w w. ja v a2 s . c o m while (xmlStreamReader.hasNext()) { switch (xmlStreamReader.next()) { case XMLStreamConstants.END_DOCUMENT: return null; case XMLStreamConstants.END_ELEMENT: elementStack.pop(); break; case XMLStreamConstants.START_ELEMENT: elementStack.push(xmlStreamReader.getLocalName()); if (actualElementStackHasExpectedElements(xmlChunkerState)) { return pullNextXmlChunkFromTopElementOnStack(xmlChunkerState); } break; } } } catch (Exception e) { throw new KettleException("a problem has arisen reading the xero xml stream", e); } return null; }
From source file:StAXStreamTreeViewer.java
private void parseRestOfDocument(XMLStreamReader reader, DefaultMutableTreeNode current) throws XMLStreamException { while (reader.hasNext()) { int type = reader.next(); switch (type) { case XMLStreamConstants.START_ELEMENT: DefaultMutableTreeNode element = new DefaultMutableTreeNode(reader.getLocalName()); current.add(element);/*from ww w .j av a2 s.co m*/ current = element; if (reader.getNamespaceURI() != null) { String prefix = reader.getPrefix(); if (prefix == null) { prefix = "[None]"; } DefaultMutableTreeNode namespace = new DefaultMutableTreeNode( "prefix = '" + prefix + "', URI = '" + reader.getNamespaceURI() + "'"); current.add(namespace); } if (reader.getAttributeCount() > 0) { for (int i = 0; i < reader.getAttributeCount(); i++) { DefaultMutableTreeNode attribute = new DefaultMutableTreeNode( "Attribute (name = '" + reader.getAttributeLocalName(i) + "', value = '" + reader.getAttributeValue(i) + "')"); String attURI = reader.getAttributeNamespace(i); if (attURI != null) { String attPrefix = reader.getAttributePrefix(i); if (attPrefix == null || attPrefix.equals("")) { attPrefix = "[None]"; } DefaultMutableTreeNode attNamespace = new DefaultMutableTreeNode( "prefix=" + attPrefix + ",URI=" + attURI); attribute.add(attNamespace); } current.add(attribute); } } break; case XMLStreamConstants.END_ELEMENT: current = (DefaultMutableTreeNode) current.getParent(); break; case XMLStreamConstants.CHARACTERS: if (!reader.isWhiteSpace()) { DefaultMutableTreeNode data = new DefaultMutableTreeNode("CD:" + reader.getText()); current.add(data); } break; case XMLStreamConstants.DTD: DefaultMutableTreeNode dtd = new DefaultMutableTreeNode("DTD:" + reader.getText()); current.add(dtd); break; case XMLStreamConstants.SPACE: break; case XMLStreamConstants.COMMENT: DefaultMutableTreeNode comment = new DefaultMutableTreeNode(reader.getText()); current.add(comment); break; default: System.out.println(type); } } }
From source file:at.lame.hellonzb.parser.NzbParser.java
/** * This is the constructor of the class. * It parses the given XML file./*from ww w . j av a 2s.c o m*/ * * @param mainApp The main application object * @param file The file name of the nzb file to parse * @throws XMLStreamException * @throws IOException */ public NzbParser(HelloNzbCradle mainApp, String file) throws XMLStreamException, IOException, ParseException { this.mainApp = mainApp; DownloadFile currentFile = null; DownloadFileSegment currentSegment = null; boolean groupFlag = false; boolean segmentFlag = false; this.name = file.trim(); this.name = file.substring(0, file.length() - 4); this.downloadFiles = new Vector<DownloadFile>(); this.origTotalSize = 0; this.downloadedBytes = 0; // create XML parser String string = reformatInputStream(file); InputStream in = new ByteArrayInputStream(string.getBytes()); XMLInputFactory factory = XMLInputFactory.newInstance(); XMLStreamReader parser = factory.createXMLStreamReader(in); // parse nzb file with a Java XML parser while (parser.hasNext()) { switch (parser.getEventType()) { // parser has reached the end of the xml file case XMLStreamConstants.END_DOCUMENT: parser.close(); break; // parser has found a new element case XMLStreamConstants.START_ELEMENT: String elemName = parser.getLocalName().toLowerCase(); if (elemName.equals("file")) { currentFile = newDownloadFile(parser); boolean found = false; for (DownloadFile dlf : downloadFiles) if (dlf.getFilename().equals(currentFile.getFilename())) { found = true; break; } if (!found) downloadFiles.add(currentFile); } else if (elemName.equals("group")) groupFlag = true; else if (elemName.equals("segment")) { currentSegment = newDownloadFileSegment(parser, currentFile); currentFile.addSegment(currentSegment); segmentFlag = true; } break; // end of element case XMLStreamConstants.END_ELEMENT: groupFlag = false; segmentFlag = false; break; // get the elements value(s) case XMLStreamConstants.CHARACTERS: if (!parser.isWhiteSpace()) { if (groupFlag && (currentFile != null)) currentFile.addGroup(parser.getText()); else if (segmentFlag && (currentSegment != null)) currentSegment.setArticleId(parser.getText()); } break; // any other parser event? default: break; } parser.next(); } checkFileSegments(); this.origTotalSize = getCurrTotalSize(); }