List of usage examples for javax.xml.stream XMLStreamReader next
public int next() throws XMLStreamException;
From source file:org.activiti.dmn.converter.util.DmnXMLUtil.java
public static void parseChildElements(String elementName, DmnElement parentElement, XMLStreamReader xtr, Map<String, BaseChildElementParser> childParsers, DmnDefinition model) throws Exception { Map<String, BaseChildElementParser> localParserMap = new HashMap<String, BaseChildElementParser>( genericChildParserMap);/*from w ww . j a va 2 s . c om*/ if (childParsers != null) { localParserMap.putAll(childParsers); } boolean inExtensionElements = false; boolean readyWithChildElements = false; while (readyWithChildElements == false && xtr.hasNext()) { xtr.next(); if (xtr.isStartElement()) { if (ELEMENT_EXTENSIONS.equals(xtr.getLocalName())) { inExtensionElements = true; } else if (localParserMap.containsKey(xtr.getLocalName())) { BaseChildElementParser childParser = localParserMap.get(xtr.getLocalName()); //if we're into an extension element but the current element is not accepted by this parentElement then is read as a custom extension element if (inExtensionElements && !childParser.accepts(parentElement)) { DmnExtensionElement extensionElement = parseExtensionElement(xtr); parentElement.addExtensionElement(extensionElement); continue; } localParserMap.get(xtr.getLocalName()).parseChildElement(xtr, parentElement, model); } else if (inExtensionElements) { DmnExtensionElement extensionElement = parseExtensionElement(xtr); parentElement.addExtensionElement(extensionElement); } } else if (xtr.isEndElement()) { if (ELEMENT_EXTENSIONS.equals(xtr.getLocalName())) { inExtensionElements = false; } else if (elementName.equalsIgnoreCase(xtr.getLocalName())) { readyWithChildElements = true; } } } }
From source file:org.activiti.dmn.converter.util.DmnXMLUtil.java
public static DmnExtensionElement parseExtensionElement(XMLStreamReader xtr) throws Exception { DmnExtensionElement extensionElement = new DmnExtensionElement(); extensionElement.setName(xtr.getLocalName()); if (StringUtils.isNotEmpty(xtr.getNamespaceURI())) { extensionElement.setNamespace(xtr.getNamespaceURI()); }/* w ww. j a va 2 s . c o m*/ if (StringUtils.isNotEmpty(xtr.getPrefix())) { extensionElement.setNamespacePrefix(xtr.getPrefix()); } for (int i = 0; i < xtr.getAttributeCount(); i++) { DmnExtensionAttribute extensionAttribute = new DmnExtensionAttribute(); extensionAttribute.setName(xtr.getAttributeLocalName(i)); extensionAttribute.setValue(xtr.getAttributeValue(i)); if (StringUtils.isNotEmpty(xtr.getAttributeNamespace(i))) { extensionAttribute.setNamespace(xtr.getAttributeNamespace(i)); } if (StringUtils.isNotEmpty(xtr.getAttributePrefix(i))) { extensionAttribute.setNamespacePrefix(xtr.getAttributePrefix(i)); } extensionElement.addAttribute(extensionAttribute); } boolean readyWithExtensionElement = false; while (readyWithExtensionElement == false && xtr.hasNext()) { xtr.next(); if (xtr.isCharacters() || XMLStreamReader.CDATA == xtr.getEventType()) { if (StringUtils.isNotEmpty(xtr.getText().trim())) { extensionElement.setElementText(xtr.getText().trim()); } } else if (xtr.isStartElement()) { DmnExtensionElement childExtensionElement = parseExtensionElement(xtr); extensionElement.addChildElement(childExtensionElement); } else if (xtr.isEndElement() && extensionElement.getName().equalsIgnoreCase(xtr.getLocalName())) { readyWithExtensionElement = true; } } return extensionElement; }
From source file:org.activiti.dmn.xml.converter.DmnXMLConverter.java
public DmnDefinition convertToDmnModel(XMLStreamReader xtr) { DmnDefinition model = new DmnDefinition(); DmnElement parentElement = null;/*from w w w .j ava 2 s . c o m*/ try { while (xtr.hasNext()) { try { xtr.next(); } catch (Exception e) { LOGGER.debug("Error reading XML document", e); throw new DmnXMLException("Error reading XML", e); } if (xtr.isStartElement() == false) { continue; } if (ELEMENT_DEFINITIONS.equals(xtr.getLocalName())) { model.setId(xtr.getAttributeValue(null, ATTRIBUTE_ID)); model.setName(xtr.getAttributeValue(null, ATTRIBUTE_NAME)); model.setNamespace(MODEL_NAMESPACE); parentElement = model; } else if (ELEMENT_DECISION.equals(xtr.getLocalName())) { Decision decision = new Decision(); model.addDrgElement(decision); decision.setId(xtr.getAttributeValue(null, ATTRIBUTE_ID)); decision.setName(xtr.getAttributeValue(null, ATTRIBUTE_NAME)); parentElement = decision; } else if (ELEMENT_DECISION_TABLE.equals(xtr.getLocalName())) { DecisionTable currentDecisionTable = new DecisionTable(); currentDecisionTable.setId(xtr.getAttributeValue(null, ATTRIBUTE_ID)); if (xtr.getAttributeValue(null, ATTRIBUTE_HIT_POLICY) != null) { currentDecisionTable .setHitPolicy(HitPolicy.valueOf(xtr.getAttributeValue(null, ATTRIBUTE_HIT_POLICY))); } else { currentDecisionTable.setHitPolicy(HitPolicy.FIRST); } model.getDrgElements().get(model.getDrgElements().size() - 1) .setDecisionTable(currentDecisionTable); model.setCurrentDecisionTable(currentDecisionTable); parentElement = currentDecisionTable; } else if (ELEMENT_OUTPUT_CLAUSE.equals(xtr.getLocalName())) { OutputClause outputClause = new OutputClause(); model.getCurrentDecisionTable().addOutput(outputClause); outputClause.setId(xtr.getAttributeValue(null, ATTRIBUTE_ID)); outputClause.setLabel(xtr.getAttributeValue(null, ATTRIBUTE_LABEL)); outputClause.setName(xtr.getAttributeValue(null, ATTRIBUTE_NAME)); outputClause.setTypeRef(xtr.getAttributeValue(null, ATTRIBUTE_TYPE_REF)); parentElement = outputClause; } else if (ELEMENT_DESCRIPTION.equals(xtr.getLocalName())) { parentElement.setDescription(xtr.getElementText()); } else if (ELEMENT_EXTENSIONS.equals(xtr.getLocalName())) { while (xtr.hasNext()) { xtr.next(); if (xtr.isStartElement()) { DmnExtensionElement extensionElement = DmnXMLUtil.parseExtensionElement(xtr); parentElement.addExtensionElement(extensionElement); } else if (xtr.isEndElement()) { if (ELEMENT_EXTENSIONS.equals(xtr.getLocalName())) { break; } } } } else if (convertersToDmnMap.containsKey(xtr.getLocalName())) { BaseDmnXMLConverter converter = convertersToDmnMap.get(xtr.getLocalName()); converter.convertToDmnModel(xtr, model); } } processDmnElements(model.getCurrentDecisionTable()); } catch (DmnXMLException e) { throw e; } catch (Exception e) { LOGGER.error("Error processing DMN document", e); throw new DmnXMLException("Error processing DMN document", e); } return model; }
From source file:org.apache.axiom.om.impl.builder.StAXBuilder.java
/** * Method discard.// ww w. jav a 2 s .c o m * * @param element * @throws OMException */ public void discard(OMElement element) throws OMException { if (element.isComplete() || !cache) { throw new OMException(); } try { // We simply cannot use the parser instance from the builder for this case // it is not safe to assume that the parser inside the builder will be in // sync with the parser of the element in question // Note 1 - however calling getXMLStreamReaderWithoutCaching sets off two flags // the cache flag for this builder and the parserAccessed flag. These flags will be // reset later in this procedure int event = 0; XMLStreamReader elementParser = element.getXMLStreamReaderWithoutCaching(); do { event = elementParser.next(); } while (!(event == XMLStreamConstants.END_ELEMENT && element.getLocalName().equals(elementParser.getLocalName()))); //at this point we are safely at the end_element event of the element we discarded lastNode = element.getPreviousOMSibling(); // resetting the flags - see Note 1 above cache = true; parserAccessed = false; if (lastNode != null) { // if the last node is not an element, we are in trouble because leaf nodes // (such as text) cannot build themselves. worst the lastchild of the // currentparent is still the removed node! we have to correct it OMContainerEx ex = ((OMContainerEx) lastNode.getParent()); ex.setLastChild(lastNode); if (!(lastNode instanceof OMContainerEx)) { ex.buildNext(); } else { ((OMNodeEx) lastNode).setNextOMSibling(null); } } else { OMElement parent = (OMElement) element.getParent(); if (parent == null) { throw new OMException(); } ((OMContainerEx) parent).setFirstChild(null); lastNode = parent; } } catch (OMException e) { throw e; } catch (Exception e) { throw new OMException(e); } // when an element is discarded the element index that was incremented //at creation needs to be decremented ! elementLevel--; }
From source file:org.apache.axiom.om.impl.serialize.StreamingOMSerializer.java
protected void serializeNode(XMLStreamReader reader, XMLStreamWriter writer, boolean startAtNext) throws XMLStreamException { // TODO We get the StAXWriter at this point and uses it hereafter // assuming that this is the only entry point to this class. // If there can be other classes calling methodes of this we might // need to change methode signatures to OMOutputer // If not startAtNext, then seed the processing with the current element. boolean useCurrentEvent = !startAtNext; while (reader.hasNext() || useCurrentEvent) { int event = 0; OMDataSource ds = null;//w ww .jav a 2 s . com if (useCurrentEvent) { event = reader.getEventType(); useCurrentEvent = false; } else { event = reader.next(); } // If the reader is exposing a DataSourc // for this event, then simply serialize the // DataSource if (reader instanceof OMStAXWrapper) { ds = ((OMStAXWrapper) reader).getDataSource(); } if (ds != null) { ds.serialize(writer); } else { switch (event) { case START_ELEMENT: serializeElement(reader, writer); depth++; break; case ATTRIBUTE: serializeAttributes(reader, writer); break; case CHARACTERS: if (dataHandlerReader != null && dataHandlerReader.isBinary()) { serializeDataHandler(); break; } // Fall through case SPACE: serializeText(reader, writer); break; case COMMENT: serializeComment(reader, writer); break; case CDATA: serializeCData(reader, writer); break; case PROCESSING_INSTRUCTION: serializeProcessingInstruction(reader, writer); break; case END_ELEMENT: serializeEndElement(writer); depth--; break; case START_DOCUMENT: depth++; //if a start document is found then increment the depth break; case END_DOCUMENT: if (depth != 0) depth--; //for the end document - reduce the depth try { serializeEndElement(writer); } catch (Exception e) { //TODO: log exceptions } } } if (depth == 0) { break; } } }
From source file:org.apache.axiom.util.stax.XMLStreamReaderUtils.java
/** * Get a {@link DataHandler} for the binary data encoded in an element. The method supports * base64 encoded character data as well as optimized binary data through the * {@link DataHandlerReader} extension.//from www. j av a 2s .c om * <p> * <em>Precondition</em>: the reader is on a {@link XMLStreamConstants#START_ELEMENT} * <p> * <em>Postcondition</em>: the reader is on the corresponding * {@link XMLStreamConstants#END_ELEMENT} * * @param reader the stream to read the data from * @return the binary data from the element */ public static DataHandler getDataHandlerFromElement(XMLStreamReader reader) throws XMLStreamException { int event = reader.next(); if (event == XMLStreamConstants.END_ELEMENT) { // This means that the element is actually empty -> return empty DataHandler return new DataHandler(new EmptyDataSource("application/octet-stream")); } else if (event != XMLStreamConstants.CHARACTERS) { throw new XMLStreamException("Expected a CHARACTER event"); } DataHandlerReader dhr = getDataHandlerReader(reader); if (dhr != null && dhr.isBinary()) { DataHandler dh = dhr.getDataHandler(); reader.next(); return dh; } else { WritableBlob blob = new MemoryBlob(); Writer out = new Base64DecodingOutputStreamWriter(blob.getOutputStream()); try { writeTextTo(reader, out); // Take into account that in non coalescing mode, there may be additional // CHARACTERS events loop: while (true) { switch (reader.next()) { case XMLStreamConstants.CHARACTERS: writeTextTo(reader, out); break; case XMLStreamConstants.END_ELEMENT: break loop; default: throw new XMLStreamException("Expected a CHARACTER event"); } } out.close(); } catch (IOException ex) { throw new XMLStreamException("Error during base64 decoding", ex); } return new DataHandler(new BlobDataSource(blob, "application/octet-string")); } }
From source file:org.apache.axiom.util.stax.XMLStreamReaderUtilsTest.java
private void testGetDataHandlerFromElementWithZeroLength(boolean useDHR) throws Exception { XMLStreamReader reader = StAXUtils.createXMLStreamReader(new StringReader("<test/>")); if (useDHR) { // To have an XMLStreamReader that uses the DataHandlerReader extension, we wrap // the parser using an XOPDecodingStreamReader (even if the document doesn't contain // any xop:Include). reader = new XOPDecodingStreamReader(reader, null); }/*w w w .j ava 2 s . com*/ try { reader.next(); // Check precondition assertTrue(reader.isStartElement()); DataHandler dh = XMLStreamReaderUtils.getDataHandlerFromElement(reader); // Check postcondition assertTrue(reader.isEndElement()); assertEquals(-1, dh.getInputStream().read()); } finally { reader.close(); } }
From source file:org.apache.axiom.util.stax.XMLStreamReaderUtilsTest.java
private void testGetDataHandlerFromElementWithUnexpectedContent(boolean useDHR) throws Exception { XMLStreamReader reader = StAXUtils.createXMLStreamReader(new StringReader("<test>\n<child/>\n</test>")); if (useDHR) { reader = new XOPDecodingStreamReader(reader, null); }//from www . ja va 2s .c o m try { reader.next(); // Check precondition assertTrue(reader.isStartElement()); try { XMLStreamReaderUtils.getDataHandlerFromElement(reader); fail("Expected XMLStreamException"); } catch (XMLStreamException ex) { // Expected } } finally { reader.close(); } }
From source file:org.apache.axiom.util.stax.XMLStreamReaderUtilsTest.java
private void testGetDataHandlerFromElementNonCoalescing(boolean useDHR) throws Exception { // We generate base64 that is sufficiently large to force the parser to generate // multiple CHARACTER events StringBuffer buffer = new StringBuffer("<test>"); Base64EncodingStringBufferOutputStream out = new Base64EncodingStringBufferOutputStream(buffer); byte[] data = new byte[65536]; new Random().nextBytes(data); out.write(data);/*from w ww . ja v a 2 s . c o m*/ out.complete(); buffer.append("</test>"); XMLStreamReader reader = StAXUtils.createXMLStreamReader(StAXParserConfiguration.NON_COALESCING, new StringReader(buffer.toString())); if (useDHR) { reader = new XOPDecodingStreamReader(reader, null); } try { reader.next(); // Check precondition assertTrue(reader.isStartElement()); DataHandler dh = XMLStreamReaderUtils.getDataHandlerFromElement(reader); // Check postcondition assertTrue(reader.isEndElement()); assertTrue(Arrays.equals(data, IOUtils.toByteArray(dh.getInputStream()))); } finally { reader.close(); } }
From source file:org.apache.axiom.util.stax.XMLStreamReaderUtilsTest.java
public void testGetElementTextAsStream() throws Exception { XMLStreamReader reader = StAXUtils.createXMLStreamReader(new StringReader("<a>test</a>")); reader.next(); Reader in = XMLStreamReaderUtils.getElementTextAsStream(reader, false); assertEquals("test", IOUtils.toString(in)); assertEquals(XMLStreamReader.END_ELEMENT, reader.getEventType()); }