use of org.w3c.dom.traversal.NodeIterator in project freemarker by apache.
the class XalanXPathSupport method executeQuery.
/* " + ERRMSG_RECOMMEND_JAXEN;*/
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.");
} 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 webtools.sourceediting by eclipse.
the class AbstractStyleSheetAdapter method styleChanged.
/**
* @param srcModel com.ibm.sed.css.model.interfaces.ICSSModel
* @param removed com.ibm.sed.css.model.interfaces.ICSSSelector[]
* @param added com.ibm.sed.css.model.interfaces.ICSSSelector[]
* @param media java.lang.String
*/
public void styleChanged(ICSSModel srcModel, ICSSSelector[] removed, ICSSSelector[] added, String media) {
Element element = getElement();
if (element == null)
// might released
return;
Document doc = element.getOwnerDocument();
if (doc == null)
// error
return;
// to notify GEF tree
if (doc instanceof INodeNotifier) {
Collection adapters = ((INodeNotifier) doc).getAdapters();
if (adapters == null)
return;
Iterator it = adapters.iterator();
if (it == null)
return;
while (it.hasNext()) {
INodeAdapter adapter = (INodeAdapter) it.next();
if (adapter instanceof ICSSStyleListener) {
((ICSSStyleListener) adapter).styleChanged(srcModel, removed, added, media);
}
}
}
if (styleChangedNodes == null) {
styleChangedNodes = new HashSet();
}
try {
int removedSelNum = removed != null ? removed.length : 0;
int addedSelNum = added != null ? added.length : 0;
NodeIterator iter = ((DocumentTraversal) doc).createNodeIterator(doc, NodeFilter.SHOW_ELEMENT, null, true);
Node node;
while ((node = iter.nextNode()) != null) {
if (node.getNodeType() == Node.ELEMENT_NODE) {
Element elm = (Element) node;
boolean match = false;
int i;
for (i = 0; i < removedSelNum && !match; i++) {
match = removed[i].match(elm, null);
}
for (i = 0; i < addedSelNum && !match; i++) {
match = added[i].match(elm, null);
}
if (match) {
if (!styleChangedNodes.contains(elm))
styleChangedNodes.add(elm);
// notifyStyleChanged(elm);
}
}
}
} catch (ClassCastException ex) {
// Document doesn't implement DocumentTraversal...
}
}
use of org.w3c.dom.traversal.NodeIterator in project webtools.sourceediting by eclipse.
the class DocumentImpl method getElementsByTagNameNS.
/**
*/
public NodeList getElementsByTagNameNS(String uri, String tagName) {
if (tagName == null)
return new NodeListImpl();
NodeIterator it = createNodeIterator(this, NodeFilter.SHOW_ALL, null, false);
if (it == null)
return new NodeListImpl();
NodeListImpl elements = new NodeListImpl();
if (uri != null && uri.length() == 1 && uri.charAt(0) == '*') {
// do not care
uri = null;
}
if (tagName.length() == 1 && tagName.charAt(0) == '*') {
// do not care
tagName = null;
}
for (Node node = it.nextNode(); node != null; node = it.nextNode()) {
if (node.getNodeType() != ELEMENT_NODE)
continue;
ElementImpl element = (ElementImpl) node;
if (tagName != null) {
String localName = element.getLocalName();
if (localName == null || !localName.equals(tagName))
continue;
}
if (uri != null) {
String nsURI = element.getNamespaceURI();
if (nsURI == null || !nsURI.equals(uri))
continue;
}
elements.appendNode(element);
}
return elements;
}
use of org.w3c.dom.traversal.NodeIterator in project webtools.sourceediting by eclipse.
the class ElementImpl method getElementsByTagNameNS.
/**
*/
public NodeList getElementsByTagNameNS(String uri, String tagName) {
if (tagName == null)
return new NodeListImpl();
DocumentImpl document = (DocumentImpl) getOwnerDocument();
if (document == null)
return new NodeListImpl();
NodeIterator it = document.createNodeIterator(this, NodeFilter.SHOW_ALL, null, false);
if (it == null)
return new NodeListImpl();
NodeListImpl elements = new NodeListImpl();
if (uri != null && uri.length() == 1 && uri.charAt(0) == '*') {
// do not care
uri = null;
}
if (tagName.length() == 1 && tagName.charAt(0) == '*') {
// do not care
tagName = null;
}
// skip the first node since it is the root from createNodeIterator
it.nextNode();
for (Node node = it.nextNode(); node != null; node = it.nextNode()) {
if (node.getNodeType() != ELEMENT_NODE)
continue;
ElementImpl element = (ElementImpl) node;
if (tagName != null) {
String localName = element.getLocalName();
if (localName == null || !localName.equals(tagName))
continue;
}
if (uri != null) {
String nsURI = element.getNamespaceURI();
if (nsURI == null || !nsURI.equals(uri))
continue;
}
elements.appendNode(element);
}
return elements;
}
use of org.w3c.dom.traversal.NodeIterator in project webtools.sourceediting by eclipse.
the class ElementImpl method getElementsByTagName.
/**
* getElementsByTagName method
*
* @return org.w3c.dom.NodeList
* @param tagName
* java.lang.String
*/
public NodeList getElementsByTagName(String tagName) {
if (tagName == null)
return new NodeListImpl();
DocumentImpl document = (DocumentImpl) getOwnerDocument();
if (document == null)
return new NodeListImpl();
NodeIterator it = document.createNodeIterator(this, NodeFilter.SHOW_ALL, null, false);
if (it == null)
return new NodeListImpl();
NodeListImpl elements = new NodeListImpl();
if (tagName.length() == 1 && tagName.charAt(0) == '*') {
// do not care
tagName = null;
}
// skip the first node since it is the root from createNodeIterator
it.nextNode();
for (Node node = it.nextNode(); node != null; node = it.nextNode()) {
if (node.getNodeType() != ELEMENT_NODE)
continue;
if (tagName != null) {
ElementImpl element = (ElementImpl) node;
if (!element.matchTagName(tagName))
continue;
}
elements.appendNode(node);
}
return elements;
}
Aggregations