Search in sources :

Example 1 with OMProcessingInstruction

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

the class DigestGenerator method getDigest.

/**
     * This method is an overloaded method for the digest generation for OMElement
     *
     * @param element
     * @param digestAlgorithm
     * @return Returns a byte array representing the calculated digest value
     */
public byte[] getDigest(OMElement element, String digestAlgorithm) throws OMException {
    byte[] digest = new byte[0];
    try {
        MessageDigest md = MessageDigest.getInstance(digestAlgorithm);
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        DataOutputStream dos = new DataOutputStream(baos);
        dos.writeInt(1);
        dos.write(getExpandedName(element).getBytes("UnicodeBigUnmarked"));
        dos.write((byte) 0);
        dos.write((byte) 0);
        Collection<OMAttribute> attrs = getAttributesWithoutNS(element);
        dos.writeInt(attrs.size());
        for (Iterator<OMAttribute> itr = attrs.iterator(); itr.hasNext(); ) {
            dos.write(getDigest(itr.next(), digestAlgorithm));
        }
        OMNode node = element.getFirstOMChild();
        // adjoining Texts are merged,
        // there is  no 0-length Text, and
        // comment nodes are removed.
        int length = 0;
        for (Iterator<OMNode> itr = element.getChildren(); itr.hasNext(); ) {
            OMNode child = itr.next();
            if (child instanceof OMElement || child instanceof OMText || child instanceof OMProcessingInstruction) {
                length++;
            }
        }
        dos.writeInt(length);
        while (node != null) {
            dos.write(getDigest(node, digestAlgorithm));
            node = node.getNextOMSibling();
        }
        dos.close();
        md.update(baos.toByteArray());
        digest = md.digest();
    } catch (NoSuchAlgorithmException e) {
        throw new OMException(e);
    } catch (IOException e) {
        throw new OMException(e);
    }
    return digest;
}
Also used : DataOutputStream(java.io.DataOutputStream) OMElement(org.apache.axiom.om.OMElement) ByteArrayOutputStream(java.io.ByteArrayOutputStream) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) IOException(java.io.IOException) OMProcessingInstruction(org.apache.axiom.om.OMProcessingInstruction) OMNode(org.apache.axiom.om.OMNode) OMText(org.apache.axiom.om.OMText) MessageDigest(java.security.MessageDigest) OMAttribute(org.apache.axiom.om.OMAttribute) OMException(org.apache.axiom.om.OMException)

Example 2 with OMProcessingInstruction

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

the class TestRemoveChildren method runTest.

@Override
protected void runTest() throws Throwable {
    OMFactory factory = metaFactory.getOMFactory();
    OMDocument document = OMXMLBuilderFactory.createOMBuilder(factory, new StringReader("<?pi data?><root>text</root>")).getDocument();
    if (complete) {
        document.build();
    }
    OMProcessingInstruction firstChild = (OMProcessingInstruction) document.getFirstOMChild();
    OMElement documentElement;
    if (accessDocumentElement) {
        documentElement = document.getOMDocumentElement();
        assertEquals(complete, documentElement.isComplete());
    } else {
        documentElement = null;
    }
    document.removeChildren();
    // Test that the child has been detached correctly.
    assertNull(firstChild.getParent());
    assertNull(firstChild.getPreviousOMSibling());
    assertNull(firstChild.getNextOMSibling());
    if (documentElement != null) {
        // Test that the child has been detached correctly.
        assertNull(documentElement.getParent());
        assertNull(documentElement.getPreviousOMSibling());
        assertNull(documentElement.getNextOMSibling());
        // Test that we can still get the content of the document element.
        assertEquals("text", documentElement.getText());
    }
    // Test that the document is now empty.
    assertNull(document.getFirstOMChild());
    // Check that the document is in a clean state and that we are able to add
    // new children.
    document.addChild(factory.createOMElement("newroot", null));
    assertAbout(xml()).that(xml(OMDocument.class, document)).hasSameContentAs("<newroot/>");
}
Also used : OMFactory(org.apache.axiom.om.OMFactory) OMProcessingInstruction(org.apache.axiom.om.OMProcessingInstruction) StringReader(java.io.StringReader) OMElement(org.apache.axiom.om.OMElement) OMDocument(org.apache.axiom.om.OMDocument)

Example 3 with OMProcessingInstruction

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

the class OMFactoryImpl method importChildNode.

private AxiomChildNode importChildNode(OMNode child) {
    int type = child.getType();
    switch(type) {
        case OMNode.ELEMENT_NODE:
            {
                OMElement element = (OMElement) child;
                AxiomElement importedElement = createNode(AxiomElement.class);
                copyName(element, importedElement);
                for (Iterator<OMAttribute> it = element.getAllAttributes(); it.hasNext(); ) {
                    importedElement.coreAppendAttribute(importAttribute(it.next()));
                }
                for (Iterator<OMNamespace> it = element.getAllDeclaredNamespaces(); it.hasNext(); ) {
                    OMNamespace ns = it.next();
                    AxiomNamespaceDeclaration nsDecl = createNode(AxiomNamespaceDeclaration.class);
                    nsDecl.coreSetDeclaredNamespace(ns.getPrefix(), ns.getNamespaceURI());
                    importedElement.coreAppendAttribute(nsDecl);
                }
                importChildren(element, importedElement);
                return importedElement;
            }
        case OMNode.TEXT_NODE:
        case OMNode.SPACE_NODE:
        case OMNode.CDATA_SECTION_NODE:
            {
                OMText text = (OMText) child;
                Object content;
                if (text.isBinary()) {
                    content = new TextContent(text.getContentID(), text.getDataHandler(), text.isOptimized());
                } else {
                    content = text.getText();
                }
                return createAxiomText(null, content, type);
            }
        case OMNode.PI_NODE:
            {
                OMProcessingInstruction pi = (OMProcessingInstruction) child;
                AxiomProcessingInstruction importedPI = createNode(AxiomProcessingInstruction.class);
                importedPI.setTarget(pi.getTarget());
                importedPI.setValue(pi.getValue());
                return importedPI;
            }
        case OMNode.COMMENT_NODE:
            {
                OMComment comment = (OMComment) child;
                AxiomComment importedComment = createNode(AxiomComment.class);
                importedComment.setValue(comment.getValue());
                return importedComment;
            }
        case OMNode.DTD_NODE:
            {
                OMDocType docType = (OMDocType) child;
                AxiomDocType importedDocType = createNode(AxiomDocType.class);
                importedDocType.coreSetRootName(docType.getRootName());
                importedDocType.coreSetPublicId(docType.getPublicId());
                importedDocType.coreSetSystemId(docType.getSystemId());
                importedDocType.coreSetInternalSubset(docType.getInternalSubset());
                return importedDocType;
            }
        case OMNode.ENTITY_REFERENCE_NODE:
            AxiomEntityReference importedEntityRef = createNode(AxiomEntityReference.class);
            importedEntityRef.coreSetName(((OMEntityReference) child).getName());
            return importedEntityRef;
        default:
            throw new IllegalArgumentException("Unsupported node type");
    }
}
Also used : OMNamespace(org.apache.axiom.om.OMNamespace) AxiomDocType(org.apache.axiom.om.impl.intf.AxiomDocType) AxiomProcessingInstruction(org.apache.axiom.om.impl.intf.AxiomProcessingInstruction) AxiomEntityReference(org.apache.axiom.om.impl.intf.AxiomEntityReference) OMElement(org.apache.axiom.om.OMElement) AxiomNamespaceDeclaration(org.apache.axiom.om.impl.intf.AxiomNamespaceDeclaration) OMProcessingInstruction(org.apache.axiom.om.OMProcessingInstruction) OMDocType(org.apache.axiom.om.OMDocType) OMComment(org.apache.axiom.om.OMComment) AxiomComment(org.apache.axiom.om.impl.intf.AxiomComment) Iterator(java.util.Iterator) OMText(org.apache.axiom.om.OMText) AxiomElement(org.apache.axiom.om.impl.intf.AxiomElement) TextContent(org.apache.axiom.om.impl.intf.TextContent)

Example 4 with OMProcessingInstruction

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

the class TestSerialize method runTest.

@Override
protected void runTest() throws Throwable {
    OMProcessingInstruction pi = metaFactory.getOMFactory().createOMProcessingInstruction(null, "target", "data");
    XMLStreamWriter writer = mock(XMLStreamWriter.class);
    pi.serialize(writer);
    verify(writer).writeProcessingInstruction(pi.getTarget() + " ", pi.getValue());
    verify(writer, atMost(1)).flush();
    verifyNoMoreInteractions(writer);
}
Also used : OMProcessingInstruction(org.apache.axiom.om.OMProcessingInstruction) XMLStreamWriter(javax.xml.stream.XMLStreamWriter)

Example 5 with OMProcessingInstruction

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

the class TestCreateOMProcessingInstructionWithoutParent method runTest.

@Override
protected void runTest() throws Throwable {
    OMProcessingInstruction pi = metaFactory.getOMFactory().createOMProcessingInstruction(null, "mypi", "data");
    assertNull(pi.getParent());
    assertEquals("mypi", pi.getTarget());
    assertEquals("data", pi.getValue());
}
Also used : OMProcessingInstruction(org.apache.axiom.om.OMProcessingInstruction)

Aggregations

OMProcessingInstruction (org.apache.axiom.om.OMProcessingInstruction)9 OMElement (org.apache.axiom.om.OMElement)4 OMNode (org.apache.axiom.om.OMNode)4 OMText (org.apache.axiom.om.OMText)3 Iterator (java.util.Iterator)2 OMComment (org.apache.axiom.om.OMComment)2 OMDocType (org.apache.axiom.om.OMDocType)2 OMFactory (org.apache.axiom.om.OMFactory)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 DataOutputStream (java.io.DataOutputStream)1 IOException (java.io.IOException)1 StringReader (java.io.StringReader)1 MessageDigest (java.security.MessageDigest)1 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)1 XMLStreamWriter (javax.xml.stream.XMLStreamWriter)1 OMAttribute (org.apache.axiom.om.OMAttribute)1 OMContainer (org.apache.axiom.om.OMContainer)1 OMDocument (org.apache.axiom.om.OMDocument)1 OMException (org.apache.axiom.om.OMException)1 OMNamespace (org.apache.axiom.om.OMNamespace)1