Search in sources :

Example 86 with OMAttribute

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

the class OMAttributeHelperTest method testImportOMAttribute.

public void testImportOMAttribute() {
    // Convert from OM to DOOM.
    OMFactory omf = OMAbstractFactory.getOMFactory();
    OMNamespace ns1 = omf.createOMNamespace("http://nsurl", "prefix");
    OMAttribute attr1 = omf.createOMAttribute("attr1", ns1, "attr1value");
    OMFactory doomf = OMAbstractFactory.getMetaFactory(OMAbstractFactory.FEATURE_DOM).getOMFactory();
    OMElement ome1 = doomf.createOMElement("element", ns1.getNamespaceURI(), ns1.getPrefix());
    AttributeHelper.importOMAttribute(attr1, ome1);
    assertNotSame(attr1, ome1.getAttribute(attr1.getQName()));
    assertEquals(attr1.getAttributeValue(), ome1.getAttribute(attr1.getQName()).getAttributeValue());
    // Convert from DOOM to OM.
    OMNamespace ns2 = doomf.createOMNamespace("http://nsurl", "prefix");
    OMAttribute attr2 = doomf.createOMAttribute("attr2", ns2, "attr2value");
    OMElement ome2 = omf.createOMElement("element", ns2.getNamespaceURI(), ns2.getPrefix());
    AttributeHelper.importOMAttribute(attr2, ome2);
    assertNotSame(attr2, ome2.getAttribute(attr2.getQName()));
    assertEquals(attr2.getAttributeValue(), ome2.getAttribute(attr2.getQName()).getAttributeValue());
    // OM only.
    OMNamespace ns3 = omf.createOMNamespace("http://nsurl", "prefix");
    OMAttribute attr3 = omf.createOMAttribute("attr3", ns3, "attr3value");
    OMElement ome3 = omf.createOMElement("element", ns3.getNamespaceURI(), ns3.getPrefix());
    AttributeHelper.importOMAttribute(attr3, ome3);
    assertSame(attr3, ome3.getAttribute(attr3.getQName()));
    // DOOM only.
    OMNamespace ns4 = doomf.createOMNamespace("http://nsurl", "prefix");
    OMAttribute attr4 = doomf.createOMAttribute("attr4", ns4, "attr4value");
    OMElement ome4 = doomf.createOMElement("element", ns4.getNamespaceURI(), ns4.getPrefix());
    AttributeHelper.importOMAttribute(attr4, ome4);
    assertSame(attr4, ome4.getAttribute(attr4.getQName()));
}
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 87 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 88 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 89 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 90 with OMAttribute

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

the class TestAddAttributeWithoutNamespace method runTest.

@Override
protected void runTest() throws Throwable {
    OMFactory factory = metaFactory.getOMFactory();
    OMElement element = factory.createOMElement("test", "urn:test", "");
    // Retrieve the namespace declaration generated by createOMElement
    OMNamespace ns = element.getAllDeclaredNamespaces().next();
    OMAttribute attr = addAttributeStrategy.addAttribute(element, "test", noNamespaceStrategy.createOMNamespace(factory), "test");
    assertNull(attr.getNamespace());
    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)

Aggregations

OMAttribute (org.apache.axiom.om.OMAttribute)187 OMElement (org.apache.axiom.om.OMElement)116 QName (javax.xml.namespace.QName)82 Iterator (java.util.Iterator)41 OMFactory (org.apache.axiom.om.OMFactory)31 OMNamespace (org.apache.axiom.om.OMNamespace)28 SynapseException (org.apache.synapse.SynapseException)28 JaxenException (org.jaxen.JaxenException)28 OMNode (org.apache.axiom.om.OMNode)13 Value (org.apache.synapse.mediators.Value)12 SynapseXPath (org.apache.synapse.util.xpath.SynapseXPath)11 ArrayList (java.util.ArrayList)8 Endpoint (org.apache.synapse.endpoints.Endpoint)7 IOException (java.io.IOException)6 List (java.util.List)6 OMText (org.apache.axiom.om.OMText)6 SOAPEnvelope (org.apache.axiom.soap.SOAPEnvelope)6 StringReader (java.io.StringReader)5 SOAPHeaderBlock (org.apache.axiom.soap.SOAPHeaderBlock)5 EndpointDefinition (org.apache.synapse.endpoints.EndpointDefinition)5