Search in sources :

Example 11 with OMAttribute

use of org.apache.axiom.om.OMAttribute 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 12 with OMAttribute

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

the class DigestGenerator method getAttributesWithoutNS.

/**
     * Gets the collection of attributes which are none namespace declarations for an OMElement
     *
     * @param element
     * @return Returns the collection of attributes which are none namespace declarations
     */
public Collection<OMAttribute> getAttributesWithoutNS(OMElement element) {
    SortedMap<String, OMAttribute> map = new TreeMap<String, OMAttribute>();
    Iterator<OMAttribute> itr = element.getAllAttributes();
    while (itr.hasNext()) {
        OMAttribute attribute = itr.next();
        if (!(attribute.getLocalName().equals("xmlns") || attribute.getLocalName().startsWith("xmlns:")))
            map.put(getExpandedName(attribute), attribute);
    }
    return map.values();
}
Also used : TreeMap(java.util.TreeMap) OMAttribute(org.apache.axiom.om.OMAttribute)

Example 13 with OMAttribute

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

the class DocumentNavigator method getAttributeQName.

/**
     * Retrieves the QName of the given attribute node.
     *
     * @param object the context attribute node
     * @return Returns the qualified name of the attribute node.
     */
@Override
public String getAttributeQName(Object object) {
    OMAttribute attr = (OMAttribute) object;
    String prefix = attr.getNamespace().getPrefix();
    if (prefix == null || "".equals(prefix)) {
        return attr.getLocalName();
    }
    return prefix + ":" + attr.getLocalName();
}
Also used : OMAttribute(org.apache.axiom.om.OMAttribute)

Example 14 with OMAttribute

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

the class TestAddAttributeGeneratedPrefix method runTest.

@Override
protected void runTest() throws Throwable {
    OMFactory factory = metaFactory.getOMFactory();
    OMNamespace otherNS = factory.createOMNamespace("urn:ns2", "p");
    OMElement parent = factory.createOMElement("parent", otherNS);
    if (defaultNamespaceInScope) {
        parent.declareDefaultNamespace("urn:test");
    }
    OMElement element = factory.createOMElement("test", otherNS, parent);
    OMAttribute attr = element.addAttribute("attr", "value", factory.createOMNamespace("urn:test", null));
    OMNamespace ns = attr.getNamespace();
    assertTrue(ns.getPrefix().length() > 0);
    Iterator<OMNamespace> it = element.getAllDeclaredNamespaces();
    assertTrue(it.hasNext());
    assertEquals(ns, it.next());
    assertFalse(it.hasNext());
}
Also used : OMFactory(org.apache.axiom.om.OMFactory) OMNamespace(org.apache.axiom.om.OMNamespace) OMElement(org.apache.axiom.om.OMElement) OMAttribute(org.apache.axiom.om.OMAttribute)

Example 15 with OMAttribute

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

the class TestAddAttributeReplace method runTest.

@Override
protected void runTest() throws Throwable {
    OMFactory factory = metaFactory.getOMFactory();
    // Use same namespace URI but different prefixes
    OMNamespace ns1 = factory.createOMNamespace("urn:ns", "p1");
    OMNamespace ns2 = factory.createOMNamespace("urn:ns", "p2");
    OMElement element = factory.createOMElement(new QName("test"));
    OMAttribute att1 = strategy.addAttribute(element, "test", ns1, "value1");
    OMAttribute att2 = strategy.addAttribute(element, "test", ns2, "value2");
    Iterator<OMAttribute> it = element.getAllAttributes();
    assertThat(it.hasNext()).isTrue();
    assertThat(it.next()).isSameAs(att2);
    assertThat(it.hasNext()).isFalse();
    assertThat(att1.getOwner()).isNull();
    assertThat(att2.getOwner()).isSameAs(element);
    assertThat(att1).hasValue("value1");
    assertThat(att2).hasValue("value2");
}
Also used : OMFactory(org.apache.axiom.om.OMFactory) OMNamespace(org.apache.axiom.om.OMNamespace) QName(javax.xml.namespace.QName) OMElement(org.apache.axiom.om.OMElement) OMAttribute(org.apache.axiom.om.OMAttribute)

Aggregations

OMAttribute (org.apache.axiom.om.OMAttribute)55 OMFactory (org.apache.axiom.om.OMFactory)31 OMElement (org.apache.axiom.om.OMElement)26 OMNamespace (org.apache.axiom.om.OMNamespace)18 QName (javax.xml.namespace.QName)16 OMNode (org.apache.axiom.om.OMNode)4 OMSourcedElement (org.apache.axiom.om.OMSourcedElement)4 SOAPHeaderBlock (org.apache.axiom.soap.SOAPHeaderBlock)4 Attr (org.w3c.dom.Attr)4 StringReader (java.io.StringReader)3 OMException (org.apache.axiom.om.OMException)3 OMXMLParserWrapper (org.apache.axiom.om.OMXMLParserWrapper)3 SOAPFaultText (org.apache.axiom.soap.SOAPFaultText)3 PullOMDataSource (org.apache.axiom.ts.om.sourcedelement.util.PullOMDataSource)3 Element (org.w3c.dom.Element)3 OMDocument (org.apache.axiom.om.OMDocument)2 OMText (org.apache.axiom.om.OMText)2 SOAPBody (org.apache.axiom.soap.SOAPBody)2 SOAPEnvelope (org.apache.axiom.soap.SOAPEnvelope)2 SOAPFault (org.apache.axiom.soap.SOAPFault)2