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