Search in sources :

Example 41 with OMDocument

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

the class TestGetSAXSourceIdentityTransform method runTest.

@Override
protected void runTest() throws Throwable {
    Transformer transformer = xsltImplementation.newTransformerFactory().newTransformer();
    OMFactory factory = metaFactory.getOMFactory();
    OMElement element = OMXMLBuilderFactory.createOMBuilder(factory, getInput()).getDocumentElement();
    OMDocument outputDocument = factory.createOMDocument();
    transformer.transform(element.getSAXSource(cache), outputDocument.getSAXResult());
    assertAbout(xml()).that(xml(OMDocument.class, outputDocument)).ignoringWhitespaceInPrologAndEpilog().hasSameContentAs(getInput());
    element.close(false);
}
Also used : OMFactory(org.apache.axiom.om.OMFactory) Transformer(javax.xml.transform.Transformer) OMElement(org.apache.axiom.om.OMElement) OMDocument(org.apache.axiom.om.OMDocument)

Example 42 with OMDocument

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

the class TestStandaloneConfiguration method runTest.

@Override
protected void runTest() throws Throwable {
    InputStream is = TestStandaloneConfiguration.class.getResourceAsStream("web_w_dtd2.xml");
    OMXMLParserWrapper builder = OMXMLBuilderFactory.createOMBuilder(metaFactory.getOMFactory(), StAXParserConfiguration.STANDALONE, is);
    OMElement root = builder.getDocumentElement();
    assertTrue(root.getLocalName().equals("web-app"));
    OMDocument document = builder.getDocument();
    Iterator<OMNode> i = document.getChildren();
    OMDocType docType = null;
    while (docType == null && i.hasNext()) {
        OMNode obj = i.next();
        if (obj instanceof OMDocType) {
            docType = (OMDocType) obj;
        }
    }
    assertTrue(docType != null);
    root.close(false);
}
Also used : OMNode(org.apache.axiom.om.OMNode) OMDocType(org.apache.axiom.om.OMDocType) InputStream(java.io.InputStream) OMElement(org.apache.axiom.om.OMElement) OMXMLParserWrapper(org.apache.axiom.om.OMXMLParserWrapper) OMDocument(org.apache.axiom.om.OMDocument)

Example 43 with OMDocument

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

the class TestSetOptimize method runTest.

@Override
protected void runTest() throws Throwable {
    InputStream in = XOP_SPEC_SAMPLE.getInputStream();
    try {
        OMDocument document = OMXMLBuilderFactory.createOMBuilder(metaFactory.getOMFactory(), StAXParserConfiguration.DEFAULT, MultipartBody.builder().setInputStream(in).setContentType(XOP_SPEC_SAMPLE.getContentType()).build()).getDocument();
        for (Iterator<OMSerializable> it = document.getDescendants(false); it.hasNext(); ) {
            OMSerializable node = it.next();
            if (node instanceof OMText) {
                OMText text = (OMText) node;
                if (text.isBinary()) {
                    text.setOptimize(optimize);
                }
            }
        }
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        OMOutputFormat format = new OMOutputFormat();
        format.setDoOptimize(true);
        document.serialize(out, format);
        Multipart mp = new MimeMultipart(new ByteArrayDataSource(out.toByteArray(), format.getContentType()));
        assertThat(mp.getCount()).isEqualTo(optimize ? 3 : 1);
    } finally {
        in.close();
    }
}
Also used : MimeMultipart(javax.mail.internet.MimeMultipart) Multipart(javax.mail.Multipart) MimeMultipart(javax.mail.internet.MimeMultipart) InputStream(java.io.InputStream) OMText(org.apache.axiom.om.OMText) OMSerializable(org.apache.axiom.om.OMSerializable) ByteArrayOutputStream(java.io.ByteArrayOutputStream) OMOutputFormat(org.apache.axiom.om.OMOutputFormat) ByteArrayDataSource(javax.mail.util.ByteArrayDataSource) OMDocument(org.apache.axiom.om.OMDocument)

Example 44 with OMDocument

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

the class DocumentNavigator method getNamespaceAxisIterator.

/**
     * Retrieves an <code>Iterator</code> matching the <code>namespace</code> XPath axis.
     *
     * @param contextNode the original context node
     * @return Returns an Iterator capable of traversing the axis, not null.
     * @throws UnsupportedAxisException if the semantics of the namespace axis are not supported by
     *                                  this object model
     */
@Override
public Iterator<?> getNamespaceAxisIterator(Object contextNode) throws UnsupportedAxisException {
    if (!(contextNode instanceof OMContainer && contextNode instanceof OMElement)) {
        return JaxenConstants.EMPTY_ITERATOR;
    }
    OMContainer omContextNode = (OMContainer) contextNode;
    List<OMNamespaceEx> nsList = new ArrayList<OMNamespaceEx>();
    Set<String> prefixes = new HashSet<String>();
    for (OMContainer context = omContextNode; context != null && !(context instanceof OMDocument); context = ((OMElement) context).getParent()) {
        OMElement element = (OMElement) context;
        List<OMNamespace> declaredNS = new ArrayList<OMNamespace>();
        Iterator<OMNamespace> i = element.getAllDeclaredNamespaces();
        while (i != null && i.hasNext()) {
            declaredNS.add(i.next());
        }
        declaredNS.add(element.getNamespace());
        for (Iterator<OMAttribute> iter = element.getAllAttributes(); iter != null && iter.hasNext(); ) {
            OMAttribute attr = iter.next();
            OMNamespace namespace = attr.getNamespace();
            if (namespace != null) {
                declaredNS.add(namespace);
            }
        }
        for (OMNamespace namespace : declaredNS) {
            if (namespace != null) {
                String prefix = namespace.getPrefix();
                if (prefix != null && !prefixes.contains(prefix)) {
                    prefixes.add(prefix);
                    nsList.add(new OMNamespaceEx(namespace, context));
                }
            }
        }
    }
    nsList.add(new OMNamespaceEx(omContextNode.getOMFactory().createOMNamespace("http://www.w3.org/XML/1998/namespace", "xml"), omContextNode));
    return nsList.iterator();
}
Also used : OMNamespace(org.apache.axiom.om.OMNamespace) ArrayList(java.util.ArrayList) OMElement(org.apache.axiom.om.OMElement) OMDocument(org.apache.axiom.om.OMDocument) OMAttribute(org.apache.axiom.om.OMAttribute) OMContainer(org.apache.axiom.om.OMContainer) HashSet(java.util.HashSet)

Example 45 with OMDocument

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

the class CopyUtils method reader2writer.

/**
     * Simple utility that takes an XMLStreamReader and writes it
     * to an XMLStreamWriter
     * @param reader
     * @param writer
     * @throws XMLStreamException
     */
public 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()) {
            OMNode omNode = it.next();
            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)

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