Search in sources :

Example 46 with OMDocument

use of org.apache.axiom.om.OMDocument in project webservices-axiom by apache.

the class OMDataSourceExtBase method reader2writer.

/**
     * Simple utility that takes an XMLStreamReader and writes it
     * to an XMLStreamWriter
     * @param reader
     * @param writer
     * @throws XMLStreamException
     */
private static void reader2writer(XMLStreamReader reader, XMLStreamWriter writer) throws XMLStreamException {
    OMXMLParserWrapper builder = OMXMLBuilderFactory.createStAXOMBuilder(reader);
    try {
        OMDocument omDocument = builder.getDocument();
        Iterator<OMNode> it = omDocument.getChildren();
        while (it.hasNext()) {
            // TODO: this is extremely inefficient since next() will actually build the node!
            OMNode omNode = it.next();
            // TODO: quick fix required because OMChildrenIterator#next() no longer builds the node
            omNode.getNextOMSibling();
            omNode.serializeAndConsume(writer);
        }
    } finally {
        builder.close();
    }
}
Also used : OMNode(org.apache.axiom.om.OMNode) OMXMLParserWrapper(org.apache.axiom.om.OMXMLParserWrapper) OMDocument(org.apache.axiom.om.OMDocument)

Example 47 with OMDocument

use of org.apache.axiom.om.OMDocument in project webservices-axiom by apache.

the class OMXMLReader method parse.

private void parse() throws SAXException {
    if (root instanceof OMDocument) {
        generateEvents((OMDocument) root);
    } else {
        OMElement element = (OMElement) root;
        contentHandler.startDocument();
        generateParentPrefixMappingEvents(element, true);
        generateEvents(element);
        generateParentPrefixMappingEvents(element, false);
        contentHandler.endDocument();
    }
}
Also used : OMElement(org.apache.axiom.om.OMElement) OMDocument(org.apache.axiom.om.OMDocument)

Example 48 with OMDocument

use of org.apache.axiom.om.OMDocument in project webservices-axiom by apache.

the class SerializeToXMLStreamWriter method serialize.

@Override
public XML serialize(OMContainer container) throws Exception {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    String encoding = null;
    // the output encoding so that it will match the encoding in the XML declaration.
    if (container instanceof OMDocument) {
        encoding = ((OMDocument) container).getXMLEncoding();
    }
    if (encoding == null) {
        encoding = "UTF-8";
    }
    XMLStreamWriter writer = StAX.createXMLStreamWriter(baos, encoding);
    if (cache) {
        container.serialize(writer);
    } else {
        container.serializeAndConsume(writer);
    }
    writer.close();
    return new XMLAsByteArray(baos.toByteArray());
}
Also used : XMLStreamWriter(javax.xml.stream.XMLStreamWriter) ByteArrayOutputStream(java.io.ByteArrayOutputStream) OMDocument(org.apache.axiom.om.OMDocument)

Example 49 with OMDocument

use of org.apache.axiom.om.OMDocument in project webservices-axiom by apache.

the class DigestTestCase method runTest.

@Override
protected final void runTest() throws Throwable {
    OMInformationItem node = createInformationItem();
    DigestGenerator digestGenerator = new DigestGenerator();
    byte[] digest;
    if (node instanceof OMDocument) {
        digest = digestGenerator.getDigest((OMDocument) node, algorithm);
    } else if (node instanceof OMAttribute) {
        digest = digestGenerator.getDigest((OMAttribute) node, algorithm);
    } else {
        digest = digestGenerator.getDigest((OMNode) node, algorithm);
    }
    assertEquals(expectedDigest, DigestUtils.toHexString(digest));
}
Also used : OMInformationItem(org.apache.axiom.om.OMInformationItem) DigestGenerator(org.apache.axiom.om.util.DigestGenerator) OMAttribute(org.apache.axiom.om.OMAttribute) OMDocument(org.apache.axiom.om.OMDocument)

Example 50 with OMDocument

use of org.apache.axiom.om.OMDocument in project webservices-axiom by apache.

the class DialectTest method testDTD.

/**
     * Tests that Axiom is able to read a DOCTYPE declaration. Since accessing the information in
     * the DOCTYPE declaration is not standardized by the StAX specification, this will fail if the
     * StAX dialect is not detected correctly.
     * 
     * @throws Exception
     */
@Test
public void testDTD() throws Exception {
    OMDocument document = OMXMLBuilderFactory.createOMBuilder(new StringReader("<!DOCTYPE root><root/>")).getDocument();
    OMDocType dtd = (OMDocType) document.getFirstOMChild();
    assertEquals("root", dtd.getRootName());
}
Also used : OMDocType(org.apache.axiom.om.OMDocType) StringReader(java.io.StringReader) OMDocument(org.apache.axiom.om.OMDocument) Test(org.junit.Test)

Aggregations

OMDocument (org.apache.axiom.om.OMDocument)51 OMElement (org.apache.axiom.om.OMElement)19 OMFactory (org.apache.axiom.om.OMFactory)18 StringReader (java.io.StringReader)13 OMNode (org.apache.axiom.om.OMNode)9 ByteArrayOutputStream (java.io.ByteArrayOutputStream)5 Document (org.w3c.dom.Document)5 StringWriter (java.io.StringWriter)4 XMLStreamReader (javax.xml.stream.XMLStreamReader)4 InputStream (java.io.InputStream)3 ArrayList (java.util.ArrayList)3 OMCloneOptions (org.apache.axiom.om.OMCloneOptions)3 OMInformationItem (org.apache.axiom.om.OMInformationItem)3 OMNamespace (org.apache.axiom.om.OMNamespace)3 OMXMLParserWrapper (org.apache.axiom.om.OMXMLParserWrapper)3 ByteArrayInputStream (java.io.ByteArrayInputStream)2 Transformer (javax.xml.transform.Transformer)2 DTDReader (org.apache.axiom.ext.stax.DTDReader)2 OMAttribute (org.apache.axiom.om.OMAttribute)2 OMComment (org.apache.axiom.om.OMComment)2