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