* @return
* @throws XMLEncryptionException
*
*/
DocumentFragment deserialize(String source, Node ctx) throws XMLEncryptionException {
DocumentFragment result;
final String tagname = "fragment";
// Create the context to parse the document against
StringBuffer sb;
sb = new StringBuffer();
sb.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?><"+tagname);
// Run through each node up to the document node and find any
// xmlns: nodes
Node wk = ctx;
while (wk != null) {
NamedNodeMap atts = wk.getAttributes();
int length;
if (atts != null)
length = atts.getLength();
else
length = 0;
for (int i = 0 ; i < length ; ++i) {
Node att = atts.item(i);
if (att.getNodeName().startsWith("xmlns:") ||
att.getNodeName().equals("xmlns")) {
// Check to see if this node has already been found
Node p = ctx;
boolean found = false;
while (p != wk) {
NamedNodeMap tstAtts = p.getAttributes();
if (tstAtts != null &&
tstAtts.getNamedItem(att.getNodeName()) != null) {
found = true;
break;
}
p = p.getParentNode();
}
if (found == false) {
// This is an attribute node
sb.append(" " + att.getNodeName() + "=\"" +
att.getNodeValue() + "\"");
}
}
}
wk = wk.getParentNode();
}
sb.append(">" + source + "</" + tagname + ">");
String fragment = sb.toString();
try {
DocumentBuilderFactory dbf =
DocumentBuilderFactory.newInstance();
dbf.setNamespaceAware(true);
dbf.setAttribute("http://xml.org/sax/features/namespaces", Boolean.TRUE);
DocumentBuilder db = dbf.newDocumentBuilder();
Document d = db.parse(
new InputSource(new StringReader(fragment)));
Element fragElt = (Element) _contextDocument.importNode(
d.getDocumentElement(), true);
result = _contextDocument.createDocumentFragment();
Node child = fragElt.getFirstChild();
while (child != null) {
fragElt.removeChild(child);
result.appendChild(child);
child = fragElt.getFirstChild();
}
// String outp = serialize(d);
} catch (SAXException se) {