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()));
}
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;
}
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();
}
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();
}
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());
}
Aggregations