use of org.w3c.dom.traversal.NodeIterator in project freemarker by apache.
the class SunInternalXalanXPathSupport method executeQuery.
public synchronized TemplateModel executeQuery(Object context, String xpathQuery) throws TemplateModelException {
if (!(context instanceof Node)) {
if (context != null) {
if (isNodeList(context)) {
int cnt = ((List) context).size();
if (cnt != 0) {
throw new TemplateModelException("Cannot perform an XPath query against a node set of " + cnt + " nodes. Expecting a single node." + ERRMSG_RECOMMEND_JAXEN);
} else {
throw new TemplateModelException(ERRMSG_EMPTY_NODE_SET);
}
} else {
throw new TemplateModelException("Cannot perform an XPath query against a " + context.getClass().getName() + ". Expecting a single org.w3c.dom.Node.");
}
} else {
throw new TemplateModelException(ERRMSG_EMPTY_NODE_SET);
}
}
Node node = (Node) context;
try {
XPath xpath = new XPath(xpathQuery, null, customPrefixResolver, XPath.SELECT, null);
int ctxtNode = xpathContext.getDTMHandleFromNode(node);
XObject xresult = xpath.execute(xpathContext, ctxtNode, customPrefixResolver);
if (xresult instanceof XNodeSet) {
NodeListModel result = new NodeListModel(node);
result.xpathSupport = this;
NodeIterator nodeIterator = xresult.nodeset();
Node n;
do {
n = nodeIterator.nextNode();
if (n != null) {
result.add(n);
}
} while (n != null);
return result.size() == 1 ? result.get(0) : result;
}
if (xresult instanceof XBoolean) {
return ((XBoolean) xresult).bool() ? TemplateBooleanModel.TRUE : TemplateBooleanModel.FALSE;
}
if (xresult instanceof XNull) {
return null;
}
if (xresult instanceof XString) {
return new SimpleScalar(xresult.toString());
}
if (xresult instanceof XNumber) {
return new SimpleNumber(Double.valueOf(((XNumber) xresult).num()));
}
throw new TemplateModelException("Cannot deal with type: " + xresult.getClass().getName());
} catch (TransformerException te) {
throw new TemplateModelException(te);
}
}
use of org.w3c.dom.traversal.NodeIterator in project cloudstack by apache.
the class HypervisorHostHelper method removeOVFNetwork.
/**
* removes the NetworkSection element from the {ovfString} if it is an ovf xml file
* @param ovfString input string
* @return like the input string but if xml elements by name {NetworkSection} removed
*/
public static String removeOVFNetwork(final String ovfString) {
if (ovfString == null || ovfString.isEmpty()) {
return ovfString;
}
try {
final DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
final Document doc = factory.newDocumentBuilder().parse(new ByteArrayInputStream(ovfString.getBytes()));
final DocumentTraversal traversal = (DocumentTraversal) doc;
final NodeIterator iterator = traversal.createNodeIterator(doc.getDocumentElement(), NodeFilter.SHOW_ELEMENT, null, true);
for (Node n = iterator.nextNode(); n != null; n = iterator.nextNode()) {
final Element e = (Element) n;
if ("NetworkSection".equals(e.getTagName())) {
if (e.getParentNode() != null) {
e.getParentNode().removeChild(e);
}
} else if ("rasd:Connection".equals(e.getTagName())) {
if (e.getParentNode() != null && e.getParentNode().getParentNode() != null) {
e.getParentNode().getParentNode().removeChild(e.getParentNode());
}
}
}
final DOMSource domSource = new DOMSource(doc);
final StringWriter writer = new StringWriter();
final StreamResult result = new StreamResult(writer);
final TransformerFactory tf = TransformerFactory.newInstance();
final Transformer transformer = tf.newTransformer();
transformer.transform(domSource, result);
return writer.toString();
} catch (SAXException | IOException | ParserConfigurationException | TransformerException e) {
s_logger.warn("Unexpected exception caught while removing network elements from OVF:", e);
}
return ovfString;
}
Aggregations