下面列出了javax.xml.xpath.XPathFunction#org.apache.xpath.objects.XNodeSet 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* Given an select expression and a context, evaluate the XPath
* and return the resulting iterator, but do not clone.
*
* @param xctxt The execution context.
* @param contextNode The node that "." expresses.
*
*
* @return A valid DTMIterator.
* @throws TransformerException thrown if the active ProblemListener decides
* the error condition is severe enough to halt processing.
*
* @throws javax.xml.transform.TransformerException
* @xsl.usage experimental
*/
public DTMIterator asIteratorRaw(XPathContext xctxt, int contextNode)
throws javax.xml.transform.TransformerException
{
try
{
xctxt.pushCurrentNodeAndExpression(contextNode, contextNode);
XNodeSet nodeset = (XNodeSet)execute(xctxt);
return nodeset.iterRaw();
}
finally
{
xctxt.popCurrentNodeAndExpression();
}
}
/**
* Execute the function. The function must return
* a valid object.
* @param xctxt The current execution context.
* @return A valid XObject.
*
* @throws javax.xml.transform.TransformerException
*/
public XObject execute(XPathContext xctxt) throws javax.xml.transform.TransformerException
{
SubContextList subContextList = xctxt.getCurrentNodeList();
int currentNode = DTM.NULL;
if (null != subContextList) {
if (subContextList instanceof PredicatedNodeTest) {
LocPathIterator iter = ((PredicatedNodeTest)subContextList)
.getLocPathIterator();
currentNode = iter.getCurrentContextNode();
} else if(subContextList instanceof StepPattern) {
throw new RuntimeException(XSLMessages.createMessage(
XSLTErrorResources.ER_PROCESSOR_ERROR,null));
}
} else {
// not predicate => ContextNode == CurrentNode
currentNode = xctxt.getContextNode();
}
return new XNodeSet(currentNode, xctxt.getDTMManager());
}
/**
* Evaluates an XPath expression from the specified node, returning the resultant nodes.
*
* @param <T> the type class
* @param node the node to start searching from
* @param xpathExpr the XPath expression
* @param resolver the prefix resolver to use for resolving namespace prefixes, or null
* @return the list of objects found
*/
@SuppressWarnings("unchecked")
public static <T> List<T> getByXPath(final DomNode node, final String xpathExpr,
final PrefixResolver resolver) {
if (xpathExpr == null) {
throw new IllegalArgumentException("Null is not a valid XPath expression");
}
PROCESS_XPATH_.set(Boolean.TRUE);
final List<T> list = new ArrayList<>();
try {
final XObject result = evaluateXPath(node, xpathExpr, resolver);
if (result instanceof XNodeSet) {
final NodeList nodelist = ((XNodeSet) result).nodelist();
for (int i = 0; i < nodelist.getLength(); i++) {
list.add((T) nodelist.item(i));
}
}
else if (result instanceof XNumber) {
list.add((T) Double.valueOf(result.num()));
}
else if (result instanceof XBoolean) {
list.add((T) Boolean.valueOf(result.bool()));
}
else if (result instanceof XString) {
list.add((T) result.str());
}
else {
throw new RuntimeException("Unproccessed " + result.getClass().getName());
}
}
catch (final Exception e) {
throw new RuntimeException("Could not retrieve XPath >" + xpathExpr + "< on " + node, e);
}
finally {
PROCESS_XPATH_.set(Boolean.FALSE);
}
return list;
}
/**
* Evaluates an XPath expression from the specified node, returning the resultant nodes.
*
* @param <T> the type class
* @param node the node to start searching from
* @param xpathExpr the XPath expression
* @param resolver the prefix resolver to use for resolving namespace prefixes, or null
* @return the list of objects found
*/
@SuppressWarnings("unchecked")
public static <T> List<T> getByXPath(final DomNode node, final String xpathExpr, final PrefixResolver resolver) {
if (xpathExpr == null) {
throw new NullPointerException("Null is not a valid XPath expression");
}
PROCESS_XPATH_.set(Boolean.TRUE);
final List<T> list = new ArrayList<>();
try {
final XObject result = evaluateXPath(node, xpathExpr, resolver);
if (result instanceof XNodeSet) {
final NodeList nodelist = ((XNodeSet) result).nodelist();
for (int i = 0; i < nodelist.getLength(); i++) {
list.add((T) nodelist.item(i));
}
}
else if (result instanceof XNumber) {
list.add((T) Double.valueOf(result.num()));
}
else if (result instanceof XBoolean) {
list.add((T) Boolean.valueOf(result.bool()));
}
else if (result instanceof XString) {
list.add((T) result.str());
}
else {
throw new RuntimeException("Unproccessed " + result.getClass().getName());
}
}
catch (final Exception e) {
throw new RuntimeException("Could not retrieve XPath >" + xpathExpr + "< on " + node, e);
}
finally {
PROCESS_XPATH_.set(Boolean.FALSE);
}
return list;
}
public XNodeSet resolve(ExpressionContext context, String uri, String sStatic)
throws TransformerException, JDOMException {
LOGGER.debug("including xml " + uri);
Element xml = resolve(uri, sStatic);
Node node = jdom2dom(xml);
try {
return asNodeSet(context, node);
} catch (Exception ex) {
LOGGER.error(ex);
throw ex;
}
}
/**
* Build a keys table.
* @param doc The owner document key.
* @param nscontext The stylesheet's namespace context.
* @param name The key name
* @param keyDeclarations The stylesheet's xsl:key declarations.
*
* @throws javax.xml.transform.TransformerException
*/
public KeyTable(
int doc, PrefixResolver nscontext, QName name, Vector keyDeclarations, XPathContext xctxt)
throws javax.xml.transform.TransformerException
{
m_docKey = doc;
m_keyDeclarations = keyDeclarations;
KeyIterator ki = new KeyIterator(name, keyDeclarations);
m_keyNodes = new XNodeSet(ki);
m_keyNodes.allowDetachToRelease(false);
m_keyNodes.setRoot(doc, xctxt);
}
public XNodeSet resolve(ExpressionContext context, String ref) throws JDOMException, TransformerException {
LOGGER.debug("including component " + ref);
Map<String, Element> cache = chooseCacheLevel(ref, Boolean.FALSE.toString());
Element resolved = cache.get(ref);
return (resolved == null ? null : asNodeSet(context, jdom2dom(resolved)));
}
private XNodeSet asNodeSet(ExpressionContext context, Node node) throws TransformerException, JDOMException {
NodeSet nodeSet = new NodeSet();
nodeSet.addNode(node);
XPathContext xpc = context.getXPathContext();
return new XNodeSetForDOM((NodeList) nodeSet, xpc);
}
/**
* Constructor NodeCompareElem
*
*
* @param node Current node
*
* @throws javax.xml.transform.TransformerException
*/
NodeCompareElem(int node) throws javax.xml.transform.TransformerException
{
m_node = node;
if (!m_keys.isEmpty())
{
NodeSortKey k1 = (NodeSortKey) m_keys.elementAt(0);
XObject r = k1.m_selectPat.execute(m_execContext, node,
k1.m_namespaceContext);
double d;
if (k1.m_treatAsNumbers)
{
d = r.num();
// Can't use NaN for compare. They are never equal. Use zero instead.
m_key1Value = new Double(d);
}
else
{
m_key1Value = k1.m_col.getCollationKey(r.str());
}
if (r.getType() == XObject.CLASS_NODESET)
{
// %REVIEW%
DTMIterator ni = ((XNodeSet)r).iterRaw();
int current = ni.getCurrentNode();
if(DTM.NULL == current)
current = ni.nextNode();
// if (ni instanceof ContextNodeList) // %REVIEW%
// tryNextKey = (DTM.NULL != current);
// else abdicate... should never happen, but... -sb
}
if (m_keys.size() > 1)
{
NodeSortKey k2 = (NodeSortKey) m_keys.elementAt(1);
XObject r2 = k2.m_selectPat.execute(m_execContext, node,
k2.m_namespaceContext);
if (k2.m_treatAsNumbers) {
d = r2.num();
m_key2Value = new Double(d);
} else {
m_key2Value = k2.m_col.getCollationKey(r2.str());
}
}
/* Leave this in case we decide to use an array later
while (kIndex <= m_keys.size() && kIndex < maxkey)
{
NodeSortKey k = (NodeSortKey)m_keys.elementAt(kIndex);
XObject r = k.m_selectPat.execute(m_execContext, node, k.m_namespaceContext);
if(k.m_treatAsNumbers)
m_KeyValue[kIndex] = r.num();
else
m_KeyValue[kIndex] = r.str();
} */
} // end if not empty
}
/**
* Execute the expression. Meant for reuse by other FilterExpr iterators
* that are not derived from this object.
*/
public static XNodeSet executeFilterExpr(int context, XPathContext xctxt,
PrefixResolver prefixResolver,
boolean isTopLevel,
int stackFrame,
Expression expr )
throws org.apache.xml.utils.WrappedRuntimeException
{
PrefixResolver savedResolver = xctxt.getNamespaceContext();
XNodeSet result = null;
try
{
xctxt.pushCurrentNode(context);
xctxt.setNamespaceContext(prefixResolver);
// The setRoot operation can take place with a reset operation,
// and so we may not be in the context of LocPathIterator#nextNode,
// so we have to set up the variable context, execute the expression,
// and then restore the variable context.
if (isTopLevel)
{
// System.out.println("calling m_expr.execute(getXPathContext())");
VariableStack vars = xctxt.getVarStack();
// These three statements need to be combined into one operation.
int savedStart = vars.getStackFrame();
vars.setStackFrame(stackFrame);
result = (org.apache.xpath.objects.XNodeSet) expr.execute(xctxt);
result.setShouldCacheNodes(true);
// These two statements need to be combined into one operation.
vars.setStackFrame(savedStart);
}
else
result = (org.apache.xpath.objects.XNodeSet) expr.execute(xctxt);
}
catch (javax.xml.transform.TransformerException se)
{
// TODO: Fix...
throw new org.apache.xml.utils.WrappedRuntimeException(se);
}
finally
{
xctxt.popCurrentNode();
xctxt.setNamespaceContext(savedResolver);
}
return result;
}
/**
* Execute this iterator, meaning create a clone that can
* store state, and initialize it for fast execution from
* the current runtime state. When this is called, no actual
* query from the current context node is performed.
*
* @param xctxt The XPath execution context.
*
* @return An XNodeSet reference that holds this iterator.
*
* @throws javax.xml.transform.TransformerException
*/
public XObject execute(XPathContext xctxt)
throws javax.xml.transform.TransformerException
{
XNodeSet iter = new XNodeSet((LocPathIterator)m_clones.getInstance());
iter.setRoot(xctxt.getCurrentNode(), xctxt);
return iter;
}
/**
* Given an select expression and a context, evaluate the XPath
* and return the resulting iterator.
*
* @param xctxt The execution context.
* @param contextNode The node that "." expresses.
* @throws TransformerException thrown if the active ProblemListener decides
* the error condition is severe enough to halt processing.
*
* @throws javax.xml.transform.TransformerException
* @xsl.usage experimental
*/
public DTMIterator asIterator(
XPathContext xctxt, int contextNode)
throws javax.xml.transform.TransformerException
{
XNodeSet iter = new XNodeSet((LocPathIterator)m_clones.getInstance());
iter.setRoot(contextNode, xctxt);
return iter;
}