* @return The node set after evaluation
*/
public static NodeList map(ExpressionContext myContext, NodeList nl, String expr)
throws SAXNotSupportedException
{
XPathContext xctxt = null;
Document lDoc = null;
if (myContext instanceof XPathContext.XPathExpressionContext)
xctxt = ((XPathContext.XPathExpressionContext) myContext).getXPathContext();
else
throw new SAXNotSupportedException(XSLMessages.createMessage(XSLTErrorResources.ER_INVALID_CONTEXT_PASSED, new Object[]{myContext }));
if (expr == null || expr.length() == 0)
return new NodeSet();
NodeSetDTM contextNodes = new NodeSetDTM(nl, xctxt);
xctxt.pushContextNodeList(contextNodes);
NodeSet resultSet = new NodeSet();
resultSet.setShouldCacheNodes(true);
for (int i = 0; i < nl.getLength(); i++)
{
int contextNode = contextNodes.item(i);
xctxt.pushCurrentNode(contextNode);
XObject object = null;
try
{
XPath dynamicXPath = new XPath(expr, xctxt.getSAXLocator(),
xctxt.getNamespaceContext(),
XPath.SELECT);
object = dynamicXPath.execute(xctxt, contextNode, xctxt.getNamespaceContext());
if (object instanceof XNodeSet)
{
NodeList nodelist = null;
nodelist = ((XNodeSet)object).nodelist();
for (int k = 0; k < nodelist.getLength(); k++)
{
Node n = nodelist.item(k);
if (!resultSet.contains(n))
resultSet.addNode(n);
}
}
else
{
if (lDoc == null)
{
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
dbf.setNamespaceAware(true);
DocumentBuilder db = dbf.newDocumentBuilder();
lDoc = db.newDocument();
}
Element element = null;
if (object instanceof XNumber)
element = lDoc.createElementNS(EXSL_URI, "exsl:number");
else if (object instanceof XBoolean)
element = lDoc.createElementNS(EXSL_URI, "exsl:boolean");
else
element = lDoc.createElementNS(EXSL_URI, "exsl:string");
Text textNode = lDoc.createTextNode(object.str());
element.appendChild(textNode);
resultSet.addNode(element);
}
}
catch (Exception e)
{
xctxt.popCurrentNode();
xctxt.popContextNodeList();
return new NodeSet();
}
xctxt.popCurrentNode();
}
xctxt.popContextNodeList();
return resultSet;
}