1 16 19 package org.apache.xalan.lib; 20 21 import javax.xml.parsers.DocumentBuilder ; 22 import javax.xml.parsers.DocumentBuilderFactory ; 23 import javax.xml.transform.TransformerException ; 24 25 import org.apache.xalan.extensions.ExpressionContext; 26 import org.apache.xalan.res.XSLMessages; 27 import org.apache.xalan.res.XSLTErrorResources; 28 import org.apache.xpath.NodeSet; 29 import org.apache.xpath.NodeSetDTM; 30 import org.apache.xpath.XPath; 31 import org.apache.xpath.XPathContext; 32 import org.apache.xpath.objects.XBoolean; 33 import org.apache.xpath.objects.XNodeSet; 34 import org.apache.xpath.objects.XNumber; 35 import org.apache.xpath.objects.XObject; 36 37 import org.w3c.dom.Document ; 38 import org.w3c.dom.Element ; 39 import org.w3c.dom.Node ; 40 import org.w3c.dom.NodeList ; 41 import org.w3c.dom.Text ; 42 43 import org.xml.sax.SAXNotSupportedException ; 44 45 59 public class ExsltDynamic extends ExsltBase 60 { 61 62 public static final String EXSL_URI = "http://exslt.org/common"; 63 64 97 public static double max(ExpressionContext myContext, NodeList nl, String expr) 98 throws SAXNotSupportedException 99 { 100 101 XPathContext xctxt = null; 102 if (myContext instanceof XPathContext.XPathExpressionContext) 103 xctxt = ((XPathContext.XPathExpressionContext) myContext).getXPathContext(); 104 else 105 throw new SAXNotSupportedException (XSLMessages.createMessage(XSLTErrorResources.ER_INVALID_CONTEXT_PASSED, new Object []{myContext })); 106 107 if (expr == null || expr.length() == 0) 108 return Double.NaN; 109 110 NodeSetDTM contextNodes = new NodeSetDTM(nl, xctxt); 111 xctxt.pushContextNodeList(contextNodes); 112 113 double maxValue = - Double.MAX_VALUE; 114 for (int i = 0; i < contextNodes.getLength(); i++) 115 { 116 int contextNode = contextNodes.item(i); 117 xctxt.pushCurrentNode(contextNode); 118 119 double result = 0; 120 try 121 { 122 XPath dynamicXPath = new XPath(expr, xctxt.getSAXLocator(), 123 xctxt.getNamespaceContext(), 124 XPath.SELECT); 125 result = dynamicXPath.execute(xctxt, contextNode, xctxt.getNamespaceContext()).num(); 126 } 127 catch (TransformerException e) 128 { 129 xctxt.popCurrentNode(); 130 xctxt.popContextNodeList(); 131 return Double.NaN; 132 } 133 134 xctxt.popCurrentNode(); 135 136 if (result > maxValue) 137 maxValue = result; 138 } 139 140 xctxt.popContextNodeList(); 141 return maxValue; 142 143 } 144 145 178 public static double min(ExpressionContext myContext, NodeList nl, String expr) 179 throws SAXNotSupportedException 180 { 181 182 XPathContext xctxt = null; 183 if (myContext instanceof XPathContext.XPathExpressionContext) 184 xctxt = ((XPathContext.XPathExpressionContext) myContext).getXPathContext(); 185 else 186 throw new SAXNotSupportedException (XSLMessages.createMessage(XSLTErrorResources.ER_INVALID_CONTEXT_PASSED, new Object []{myContext })); 187 188 if (expr == null || expr.length() == 0) 189 return Double.NaN; 190 191 NodeSetDTM contextNodes = new NodeSetDTM(nl, xctxt); 192 xctxt.pushContextNodeList(contextNodes); 193 194 double minValue = Double.MAX_VALUE; 195 for (int i = 0; i < nl.getLength(); i++) 196 { 197 int contextNode = contextNodes.item(i); 198 xctxt.pushCurrentNode(contextNode); 199 200 double result = 0; 201 try 202 { 203 XPath dynamicXPath = new XPath(expr, xctxt.getSAXLocator(), 204 xctxt.getNamespaceContext(), 205 XPath.SELECT); 206 result = dynamicXPath.execute(xctxt, contextNode, xctxt.getNamespaceContext()).num(); 207 } 208 catch (TransformerException e) 209 { 210 xctxt.popCurrentNode(); 211 xctxt.popContextNodeList(); 212 return Double.NaN; 213 } 214 215 xctxt.popCurrentNode(); 216 217 if (result < minValue) 218 minValue = result; 219 } 220 221 xctxt.popContextNodeList(); 222 return minValue; 223 224 } 225 226 259 public static double sum(ExpressionContext myContext, NodeList nl, String expr) 260 throws SAXNotSupportedException 261 { 262 XPathContext xctxt = null; 263 if (myContext instanceof XPathContext.XPathExpressionContext) 264 xctxt = ((XPathContext.XPathExpressionContext) myContext).getXPathContext(); 265 else 266 throw new SAXNotSupportedException (XSLMessages.createMessage(XSLTErrorResources.ER_INVALID_CONTEXT_PASSED, new Object []{myContext })); 267 268 if (expr == null || expr.length() == 0) 269 return Double.NaN; 270 271 NodeSetDTM contextNodes = new NodeSetDTM(nl, xctxt); 272 xctxt.pushContextNodeList(contextNodes); 273 274 double sum = 0; 275 for (int i = 0; i < nl.getLength(); i++) 276 { 277 int contextNode = contextNodes.item(i); 278 xctxt.pushCurrentNode(contextNode); 279 280 double result = 0; 281 try 282 { 283 XPath dynamicXPath = new XPath(expr, xctxt.getSAXLocator(), 284 xctxt.getNamespaceContext(), 285 XPath.SELECT); 286 result = dynamicXPath.execute(xctxt, contextNode, xctxt.getNamespaceContext()).num(); 287 } 288 catch (TransformerException e) 289 { 290 xctxt.popCurrentNode(); 291 xctxt.popContextNodeList(); 292 return Double.NaN; 293 } 294 295 xctxt.popCurrentNode(); 296 297 sum = sum + result; 298 299 } 300 301 xctxt.popContextNodeList(); 302 return sum; 303 } 304 305 361 public static NodeList map(ExpressionContext myContext, NodeList nl, String expr) 362 throws SAXNotSupportedException 363 { 364 XPathContext xctxt = null; 365 Document lDoc = null; 366 367 if (myContext instanceof XPathContext.XPathExpressionContext) 368 xctxt = ((XPathContext.XPathExpressionContext) myContext).getXPathContext(); 369 else 370 throw new SAXNotSupportedException (XSLMessages.createMessage(XSLTErrorResources.ER_INVALID_CONTEXT_PASSED, new Object []{myContext })); 371 372 if (expr == null || expr.length() == 0) 373 return new NodeSet(); 374 375 NodeSetDTM contextNodes = new NodeSetDTM(nl, xctxt); 376 xctxt.pushContextNodeList(contextNodes); 377 378 NodeSet resultSet = new NodeSet(); 379 resultSet.setShouldCacheNodes(true); 380 381 for (int i = 0; i < nl.getLength(); i++) 382 { 383 int contextNode = contextNodes.item(i); 384 xctxt.pushCurrentNode(contextNode); 385 386 XObject object = null; 387 try 388 { 389 XPath dynamicXPath = new XPath(expr, xctxt.getSAXLocator(), 390 xctxt.getNamespaceContext(), 391 XPath.SELECT); 392 object = dynamicXPath.execute(xctxt, contextNode, xctxt.getNamespaceContext()); 393 394 if (object instanceof XNodeSet) 395 { 396 NodeList nodelist = null; 397 nodelist = ((XNodeSet)object).nodelist(); 398 399 for (int k = 0; k < nodelist.getLength(); k++) 400 { 401 Node n = nodelist.item(k); 402 if (!resultSet.contains(n)) 403 resultSet.addNode(n); 404 } 405 } 406 else 407 { 408 if (lDoc == null) 409 { 410 DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); 411 dbf.setNamespaceAware(true); 412 DocumentBuilder db = dbf.newDocumentBuilder(); 413 lDoc = db.newDocument(); 414 } 415 416 Element element = null; 417 if (object instanceof XNumber) 418 element = lDoc.createElementNS(EXSL_URI, "exsl:number"); 419 else if (object instanceof XBoolean) 420 element = lDoc.createElementNS(EXSL_URI, "exsl:boolean"); 421 else 422 element = lDoc.createElementNS(EXSL_URI, "exsl:string"); 423 424 Text textNode = lDoc.createTextNode(object.str()); 425 element.appendChild(textNode); 426 resultSet.addNode(element); 427 } 428 } 429 catch (Exception e) 430 { 431 xctxt.popCurrentNode(); 432 xctxt.popContextNodeList(); 433 return new NodeSet(); 434 } 435 436 xctxt.popCurrentNode(); 437 438 } 439 440 xctxt.popContextNodeList(); 441 return resultSet; 442 } 443 444 460 public static XObject evaluate(ExpressionContext myContext, String xpathExpr) 461 throws SAXNotSupportedException 462 { 463 if (myContext instanceof XPathContext.XPathExpressionContext) 464 { 465 XPathContext xctxt = null; 466 try 467 { 468 xctxt = ((XPathContext.XPathExpressionContext) myContext).getXPathContext(); 469 XPath dynamicXPath = new XPath(xpathExpr, xctxt.getSAXLocator(), 470 xctxt.getNamespaceContext(), 471 XPath.SELECT); 472 473 return dynamicXPath.execute(xctxt, myContext.getContextNode(), 474 xctxt.getNamespaceContext()); 475 } 476 catch (TransformerException e) 477 { 478 return new XNodeSet(xctxt.getDTMManager()); 479 } 480 } 481 else 482 throw new SAXNotSupportedException (XSLMessages.createMessage(XSLTErrorResources.ER_INVALID_CONTEXT_PASSED, new Object []{myContext })); } 484 485 528 public static NodeList closure(ExpressionContext myContext, NodeList nl, String expr) 529 throws SAXNotSupportedException 530 { 531 XPathContext xctxt = null; 532 if (myContext instanceof XPathContext.XPathExpressionContext) 533 xctxt = ((XPathContext.XPathExpressionContext) myContext).getXPathContext(); 534 else 535 throw new SAXNotSupportedException (XSLMessages.createMessage(XSLTErrorResources.ER_INVALID_CONTEXT_PASSED, new Object []{myContext })); 536 537 if (expr == null || expr.length() == 0) 538 return new NodeSet(); 539 540 NodeSet closureSet = new NodeSet(); 541 closureSet.setShouldCacheNodes(true); 542 543 NodeList iterationList = nl; 544 do 545 { 546 547 NodeSet iterationSet = new NodeSet(); 548 549 NodeSetDTM contextNodes = new NodeSetDTM(iterationList, xctxt); 550 xctxt.pushContextNodeList(contextNodes); 551 552 for (int i = 0; i < iterationList.getLength(); i++) 553 { 554 int contextNode = contextNodes.item(i); 555 xctxt.pushCurrentNode(contextNode); 556 557 XObject object = null; 558 try 559 { 560 XPath dynamicXPath = new XPath(expr, xctxt.getSAXLocator(), 561 xctxt.getNamespaceContext(), 562 XPath.SELECT); 563 object = dynamicXPath.execute(xctxt, contextNode, xctxt.getNamespaceContext()); 564 565 if (object instanceof XNodeSet) 566 { 567 NodeList nodelist = null; 568 nodelist = ((XNodeSet)object).nodelist(); 569 570 for (int k = 0; k < nodelist.getLength(); k++) 571 { 572 Node n = nodelist.item(k); 573 if (!iterationSet.contains(n)) 574 iterationSet.addNode(n); 575 } 576 } 577 else 578 { 579 xctxt.popCurrentNode(); 580 xctxt.popContextNodeList(); 581 return new NodeSet(); 582 } 583 } 584 catch (TransformerException e) 585 { 586 xctxt.popCurrentNode(); 587 xctxt.popContextNodeList(); 588 return new NodeSet(); 589 } 590 591 xctxt.popCurrentNode(); 592 593 } 594 595 xctxt.popContextNodeList(); 596 597 iterationList = iterationSet; 598 599 for (int i = 0; i < iterationList.getLength(); i++) 600 { 601 Node n = iterationList.item(i); 602 if (!closureSet.contains(n)) 603 closureSet.addNode(n); 604 } 605 606 } while(iterationList.getLength() > 0); 607 608 return closureSet; 609 610 } 611 612 } 613 | Popular Tags |