1 16 19 package org.apache.xalan.transformer; 20 21 import java.io.IOException ; 22 import java.io.StringWriter ; 23 import java.util.Enumeration ; 24 import java.util.Properties ; 25 import java.util.Stack ; 26 import java.util.StringTokenizer ; 27 import java.util.Vector ; 28 29 import javax.xml.parsers.DocumentBuilder ; 30 import javax.xml.parsers.DocumentBuilderFactory ; 31 import javax.xml.parsers.ParserConfigurationException ; 32 import javax.xml.transform.ErrorListener ; 33 import javax.xml.transform.OutputKeys ; 34 import javax.xml.transform.Result ; 35 import javax.xml.transform.Source ; 36 import javax.xml.transform.SourceLocator ; 37 import javax.xml.transform.Transformer ; 38 import javax.xml.transform.TransformerException ; 39 import javax.xml.transform.URIResolver ; 40 import javax.xml.transform.dom.DOMResult ; 41 import javax.xml.transform.dom.DOMSource ; 42 import javax.xml.transform.sax.SAXResult ; 43 import javax.xml.transform.sax.SAXSource ; 44 import javax.xml.transform.stream.StreamResult ; 45 import javax.xml.transform.stream.StreamSource ; 46 47 import org.apache.xalan.extensions.ExtensionsTable; 48 import org.apache.xalan.res.XSLMessages; 49 import org.apache.xalan.res.XSLTErrorResources; 50 import org.apache.xml.serializer.Method; 51 import org.apache.xml.serializer.Serializer; 52 import org.apache.xml.serializer.SerializerFactory; 53 import org.apache.xalan.templates.AVT; 54 import org.apache.xalan.templates.Constants; 55 import org.apache.xalan.templates.ElemAttributeSet; 56 import org.apache.xalan.templates.ElemForEach; 57 import org.apache.xalan.templates.ElemSort; 58 import org.apache.xalan.templates.ElemTemplate; 59 import org.apache.xalan.templates.ElemTemplateElement; 60 import org.apache.xalan.templates.ElemTextLiteral; 61 import org.apache.xalan.templates.ElemVariable; 62 import org.apache.xalan.templates.OutputProperties; 63 import org.apache.xalan.templates.Stylesheet; 64 import org.apache.xalan.templates.StylesheetComposed; 65 import org.apache.xalan.templates.StylesheetRoot; 66 import org.apache.xalan.templates.XUnresolvedVariable; 67 import org.apache.xalan.trace.GenerateEvent; 68 import org.apache.xalan.trace.TraceManager; 69 import org.apache.xml.dtm.DTM; 70 import org.apache.xml.dtm.DTMIterator; 71 import org.apache.xml.dtm.DTMManager; 72 import org.apache.xml.dtm.DTMWSFilter; 73 import org.apache.xml.serializer.ToHTMLSAXHandler; 74 import org.apache.xml.serializer.ToSAXHandler; 75 import org.apache.xml.serializer.ToTextSAXHandler; 76 import org.apache.xml.serializer.ToTextStream; 77 import org.apache.xml.serializer.ToXMLSAXHandler; 78 import org.apache.xml.serializer.SerializationHandler; 79 import org.apache.xml.utils.BoolStack; 80 import org.apache.xml.utils.DOMBuilder; 81 import org.apache.xml.utils.NodeVector; 82 import org.apache.xml.utils.ObjectPool; 83 import org.apache.xml.utils.ObjectStack; 84 import org.apache.xml.utils.QName; 85 import org.apache.xml.utils.SAXSourceLocator; 86 import org.apache.xml.utils.ThreadControllerWrapper; 87 import org.apache.xpath.Arg; 88 import org.apache.xpath.ExtensionsProvider; 89 import org.apache.xpath.VariableStack; 90 import org.apache.xpath.XPathContext; 91 import org.apache.xpath.functions.FuncExtFunction; 92 import org.apache.xpath.objects.XObject; 93 import org.xml.sax.Attributes ; 94 import org.xml.sax.ContentHandler ; 95 import org.xml.sax.SAXException ; 96 import org.xml.sax.SAXNotRecognizedException ; 97 import org.xml.sax.SAXNotSupportedException ; 98 import org.xml.sax.ext.DeclHandler ; 99 import org.xml.sax.ext.LexicalHandler ; 100 101 107 public class TransformerImpl extends Transformer 108 implements Runnable , DTMWSFilter, ExtensionsProvider, org.apache.xml.serializer.SerializerTrace 109 { 110 111 114 115 private Boolean m_reentryGuard = new Boolean (true); 116 117 120 private java.io.FileOutputStream m_outputStream = null; 121 122 126 private boolean m_parserEventsOnMain = true; 127 128 129 private Thread m_transformThread; 130 131 132 private String m_urlOfSource = null; 133 134 135 private Result m_outputTarget = null; 136 137 140 private OutputProperties m_outputFormat; 141 142 143 146 ContentHandler m_inputContentHandler; 147 148 151 private ContentHandler m_outputContentHandler = null; 152 153 160 161 DocumentBuilder m_docBuilder = null; 162 163 167 private ObjectPool m_textResultHandlerObjectPool = 168 new ObjectPool(ToTextStream.class); 169 170 175 private ObjectPool m_stringWriterObjectPool = 176 new ObjectPool(StringWriter .class); 177 178 182 private OutputProperties m_textformat = new OutputProperties(Method.TEXT); 183 184 196 203 ObjectStack m_currentTemplateElements 204 = new ObjectStack(XPathContext.RECURSIONLIMIT); 205 206 207 209 216 Stack m_currentMatchTemplates = new Stack (); 217 218 226 NodeVector m_currentMatchedNodes = new NodeVector(); 227 228 231 private StylesheetRoot m_stylesheetRoot = null; 232 233 237 private boolean m_quietConflictWarnings = true; 238 239 244 private XPathContext m_xcontext; 245 246 250 private StackGuard m_stackGuard; 251 252 255 private SerializationHandler m_serializationHandler; 256 257 258 private KeyManager m_keyManager = new KeyManager(); 259 260 264 Stack m_attrSetStack = null; 265 266 270 CountersTable m_countersTable = null; 271 272 275 BoolStack m_currentTemplateRuleIsNull = new BoolStack(); 276 277 282 ObjectStack m_currentFuncResult = new ObjectStack(); 283 284 288 private MsgMgr m_msgMgr; 289 290 294 public static boolean S_DEBUG = false; 295 296 299 private ErrorListener m_errorHandler = 300 new org.apache.xml.utils.DefaultErrorHandler(); 301 302 305 private TraceManager m_traceManager = new TraceManager(this); 306 307 312 private Exception m_exceptionThrown = null; 313 314 320 private Source m_xmlSource; 321 322 327 private int m_doc; 328 329 333 private boolean m_isTransformDone = false; 334 335 336 private boolean m_hasBeenReset = false; 337 338 339 private boolean m_shouldReset = true; 340 341 347 public void setShouldReset(boolean shouldReset) 348 { 349 m_shouldReset = shouldReset; 350 } 351 352 355 private Stack m_modes = new Stack (); 356 357 361 366 public TransformerImpl(StylesheetRoot stylesheet) 367 { 369 setStylesheet(stylesheet); 370 setXPathContext(new XPathContext(this)); 371 getXPathContext().setNamespaceContext(stylesheet); 372 m_stackGuard = new StackGuard(this); 373 } 374 375 377 380 private ExtensionsTable m_extensionsTable = null; 381 382 387 public ExtensionsTable getExtensionsTable() 388 { 389 return m_extensionsTable; 390 } 391 392 399 void setExtensionsTable(StylesheetRoot sroot) 400 throws javax.xml.transform.TransformerException 401 { 402 try 403 { 404 if (sroot.getExtensions() != null) 405 m_extensionsTable = new ExtensionsTable(sroot); 406 } 407 catch (javax.xml.transform.TransformerException te) 408 {te.printStackTrace();} 409 } 410 411 413 public boolean functionAvailable(String ns, String funcName) 414 throws javax.xml.transform.TransformerException 415 { 416 return getExtensionsTable().functionAvailable(ns, funcName); 417 } 418 419 public boolean elementAvailable(String ns, String elemName) 420 throws javax.xml.transform.TransformerException 421 { 422 return getExtensionsTable().elementAvailable(ns, elemName); 423 } 424 425 public Object extFunction(String ns, String funcName, 426 Vector argVec, Object methodKey) 427 throws javax.xml.transform.TransformerException 428 { return getExtensionsTable().extFunction(ns, funcName, 430 argVec, methodKey, 431 getXPathContext().getExpressionContext()); 432 } 433 434 public Object extFunction(FuncExtFunction extFunction, Vector argVec) 435 throws javax.xml.transform.TransformerException 436 { 437 return getExtensionsTable().extFunction(extFunction, argVec, 438 getXPathContext().getExpressionContext()); 439 } 440 441 443 447 public void reset() 448 { 449 450 if (!m_hasBeenReset && m_shouldReset) 451 { 452 m_hasBeenReset = true; 453 454 if (this.m_outputStream != null) 455 { 456 try 457 { 458 m_outputStream.close(); 459 } 460 catch (java.io.IOException ioe){} 461 } 462 463 m_outputStream = null; 464 465 m_countersTable = null; 468 469 m_xcontext.reset(); 470 471 m_xcontext.getVarStack().reset(); 472 resetUserParameters(); 473 474 475 m_currentTemplateElements.removeAllElements(); 476 m_currentMatchTemplates.removeAllElements(); 477 m_currentMatchedNodes.removeAllElements(); 478 479 m_serializationHandler = null; 480 m_outputTarget = null; 481 m_keyManager = new KeyManager(); 482 m_attrSetStack = null; 483 m_countersTable = null; 484 m_currentTemplateRuleIsNull = new BoolStack(); 485 m_xmlSource = null; 486 m_doc = DTM.NULL; 487 m_isTransformDone = false; 488 m_transformThread = null; 489 490 m_xcontext.getSourceTreeManager().reset(); 493 } 494 495 } 497 498 507 public boolean getProperty(String property) 508 { 509 return false; 510 } 511 512 520 public void setProperty(String property, Object value) 521 { 522 } 523 524 526 534 public boolean isParserEventsOnMain() 535 { 536 return m_parserEventsOnMain; 537 } 538 539 545 public Thread getTransformThread() 546 { 547 return m_transformThread; 548 } 549 550 556 public void setTransformThread(Thread t) 557 { 558 m_transformThread = t; 559 } 560 561 562 private boolean m_hasTransformThreadErrorCatcher = false; 563 564 570 public boolean hasTransformThreadErrorCatcher() 571 { 572 return m_hasTransformThreadErrorCatcher; 573 } 574 575 581 public void transform(Source source) throws TransformerException 582 { 583 transform(source, true); 584 } 585 586 593 public void transform(Source source, boolean shouldRelease) throws TransformerException 594 { 595 596 try 597 { 598 599 if(getXPathContext().getNamespaceContext() == null){ 604 getXPathContext().setNamespaceContext(getStylesheet()); 605 } 606 String base = source.getSystemId(); 607 608 if(null == base) 610 { 611 base = m_stylesheetRoot.getBaseIdentifier(); 612 } 613 614 if(null == base) 616 { 617 String currentDir = ""; 618 try { 619 currentDir = System.getProperty("user.dir"); 620 } 621 catch (SecurityException se) {} 623 if (currentDir.startsWith(java.io.File.separator)) 624 base = "file://" + currentDir; 625 else 626 base = "file:///" + currentDir; 627 628 base = base + java.io.File.separatorChar 629 + source.getClass().getName(); 630 } 631 setBaseURLOfSource(base); 632 DTMManager mgr = m_xcontext.getDTMManager(); 633 641 if ((source instanceof StreamSource && source.getSystemId()==null && 642 ((StreamSource )source).getInputStream()==null && 643 ((StreamSource )source).getReader()==null)|| 644 (source instanceof SAXSource && 645 ((SAXSource )source).getInputSource()==null && 646 ((SAXSource )source).getXMLReader()==null )|| 647 (source instanceof DOMSource && ((DOMSource )source).getNode()==null)){ 648 try { 649 DocumentBuilderFactory builderF = 650 DocumentBuilderFactory.newInstance(); 651 DocumentBuilder builder = builderF.newDocumentBuilder(); 652 String systemID = source.getSystemId(); 653 source = new DOMSource (builder.newDocument()); 654 655 if (systemID != null) { 657 source.setSystemId(systemID); 658 } 659 } catch (ParserConfigurationException e) { 660 fatalError(e); 661 } 662 } 663 DTM dtm = mgr.getDTM(source, false, this, true, true); 664 dtm.setDocumentBaseURI(base); 665 666 boolean hardDelete = true; 668 try 669 { 670 this.transformNode(dtm.getDocument()); 674 } 675 finally 676 { 677 if (shouldRelease) 678 mgr.release(dtm, hardDelete); 679 } 680 681 Exception e = getExceptionThrown(); 687 688 if (null != e) 689 { 690 if (e instanceof javax.xml.transform.TransformerException ) 691 { 692 throw (javax.xml.transform.TransformerException ) e; 693 } 694 else if (e instanceof org.apache.xml.utils.WrappedRuntimeException) 695 { 696 fatalError( 697 ((org.apache.xml.utils.WrappedRuntimeException) e).getException()); 698 } 699 else 700 { 701 throw new javax.xml.transform.TransformerException (e); 702 } 703 } 704 else if (null != m_serializationHandler) 705 { 706 m_serializationHandler.endDocument(); 707 } 708 } 709 catch (org.apache.xml.utils.WrappedRuntimeException wre) 710 { 711 Throwable throwable = wre.getException(); 712 713 while (throwable 714 instanceof org.apache.xml.utils.WrappedRuntimeException) 715 { 716 throwable = 717 ((org.apache.xml.utils.WrappedRuntimeException) throwable).getException(); 718 } 719 720 fatalError(throwable); 721 } 722 723 catch (org.xml.sax.SAXParseException spe) 725 { 726 fatalError(spe); 727 } 728 catch (org.xml.sax.SAXException se) 729 { 730 m_errorHandler.fatalError(new TransformerException (se)); 731 } 732 finally 733 { 734 m_hasTransformThreadErrorCatcher = false; 735 736 reset(); 738 } 739 } 740 741 private void fatalError(Throwable throwable) throws TransformerException 742 { 743 if (throwable instanceof org.xml.sax.SAXParseException ) 744 m_errorHandler.fatalError(new TransformerException (throwable.getMessage(),new SAXSourceLocator((org.xml.sax.SAXParseException )throwable))); 745 else 746 m_errorHandler.fatalError(new TransformerException (throwable)); 747 748 } 749 750 755 public String getBaseURLOfSource() 756 { 757 return m_urlOfSource; 758 } 759 760 767 public void setBaseURLOfSource(String base) 768 { 769 m_urlOfSource = base; 770 } 771 772 777 public Result getOutputTarget() 778 { 779 return m_outputTarget; 780 } 781 782 790 public void setOutputTarget(Result outputTarget) 791 { 792 m_outputTarget = outputTarget; 793 } 794 795 813 public String getOutputProperty(String qnameString) 814 throws IllegalArgumentException 815 { 816 817 String value = null; 818 OutputProperties props = getOutputFormat(); 819 820 value = props.getProperty(qnameString); 821 822 if (null == value) 823 { 824 if (!OutputProperties.isLegalPropertyKey(qnameString)) 825 throw new IllegalArgumentException (XSLMessages.createMessage(XSLTErrorResources.ER_OUTPUT_PROPERTY_NOT_RECOGNIZED, new Object []{qnameString})); } 828 829 return value; 830 } 831 832 846 public String getOutputPropertyNoDefault(String qnameString) 847 throws IllegalArgumentException 848 { 849 850 String value = null; 851 OutputProperties props = getOutputFormat(); 852 853 value = (String ) props.getProperties().get(qnameString); 854 855 if (null == value) 856 { 857 if (!OutputProperties.isLegalPropertyKey(qnameString)) 858 throw new IllegalArgumentException (XSLMessages.createMessage(XSLTErrorResources.ER_OUTPUT_PROPERTY_NOT_RECOGNIZED, new Object []{qnameString})); } 861 862 return value; 863 } 864 865 876 public void setOutputProperty(String name, String value) 877 throws IllegalArgumentException 878 { 879 880 synchronized (m_reentryGuard) 881 { 882 883 if (null == m_outputFormat) 886 { 887 m_outputFormat = 888 (OutputProperties) getStylesheet().getOutputComposed().clone(); 889 } 890 891 if (!OutputProperties.isLegalPropertyKey(name)) 892 throw new IllegalArgumentException (XSLMessages.createMessage(XSLTErrorResources.ER_OUTPUT_PROPERTY_NOT_RECOGNIZED, new Object []{name})); 895 m_outputFormat.setProperty(name, value); 896 } 897 } 898 899 917 public void setOutputProperties(Properties oformat) 918 throws IllegalArgumentException 919 { 920 921 synchronized (m_reentryGuard) 922 { 923 if (null != oformat) 924 { 925 926 String method = (String ) oformat.get(OutputKeys.METHOD); 928 929 if (null != method) 930 m_outputFormat = new OutputProperties(method); 931 else if(m_outputFormat==null) 932 m_outputFormat = new OutputProperties(); 933 934 m_outputFormat.copyFrom(oformat); 935 m_outputFormat.copyFrom(m_stylesheetRoot.getOutputProperties()); 939 } 940 else { 941 m_outputFormat = null; 944 } 945 } 946 } 947 948 961 public Properties getOutputProperties() 962 { 963 return (Properties ) getOutputFormat().getProperties().clone(); 964 } 965 966 978 public SerializationHandler createSerializationHandler(Result outputTarget) 979 throws TransformerException 980 { 981 SerializationHandler xoh = 982 createSerializationHandler(outputTarget, getOutputFormat()); 983 return xoh; 984 } 985 986 999 public SerializationHandler createSerializationHandler( 1000 Result outputTarget, OutputProperties format) 1001 throws TransformerException 1002 { 1003 1004 SerializationHandler xoh; 1005 1006 org.w3c.dom.Node outputNode = null; 1009 1010 if (outputTarget instanceof DOMResult ) 1011 { 1012 outputNode = ((DOMResult ) outputTarget).getNode(); 1013 1014 org.w3c.dom.Document doc; 1015 short type; 1016 1017 if (null != outputNode) 1018 { 1019 type = outputNode.getNodeType(); 1020 doc = (org.w3c.dom.Node.DOCUMENT_NODE == type) 1021 ? (org.w3c.dom.Document ) outputNode 1022 : outputNode.getOwnerDocument(); 1023 } 1024 else 1025 { 1026 doc = org.apache.xml.utils.DOMHelper.createDocument(); 1027 outputNode = doc; 1028 type = outputNode.getNodeType(); 1029 1030 ((DOMResult ) outputTarget).setNode(outputNode); 1031 } 1032 1033 ContentHandler handler = 1034 (org.w3c.dom.Node.DOCUMENT_FRAGMENT_NODE == type) 1035 ? new DOMBuilder(doc, (org.w3c.dom.DocumentFragment ) outputNode) 1036 : new DOMBuilder(doc, outputNode); 1037 String encoding = format.getProperty(OutputKeys.ENCODING); 1038 xoh = new ToXMLSAXHandler(handler, (LexicalHandler )handler, encoding); 1039 } 1040 else if (outputTarget instanceof SAXResult ) 1041 { 1042 ContentHandler handler = ((SAXResult ) outputTarget).getHandler(); 1043 1044 if (null == handler) 1045 throw new IllegalArgumentException ( 1046 "handler can not be null for a SAXResult"); 1047 1048 LexicalHandler lexHandler; 1049 if (handler instanceof LexicalHandler ) 1050 lexHandler = (LexicalHandler ) handler; 1051 else 1052 lexHandler = null; 1053 1054 String encoding = format.getProperty(OutputKeys.ENCODING); 1055 String method = format.getProperty(OutputKeys.METHOD); 1056 if (org.apache.xml.serializer.Method.HTML.equals(method)) 1057 { 1058 xoh = new ToHTMLSAXHandler(handler, lexHandler, encoding); 1059 } 1060 else if (org.apache.xml.serializer.Method.TEXT.equals(method)) 1061 { 1062 xoh = new ToTextSAXHandler(handler, lexHandler, encoding); 1063 } 1064 else 1065 { 1066 ToXMLSAXHandler toXMLSAXHandler = new ToXMLSAXHandler(handler, lexHandler, encoding); 1067 toXMLSAXHandler.setShouldOutputNSAttr(false); 1068 xoh = toXMLSAXHandler; 1069 1070 } 1071 1072 String publicID = format.getProperty(OutputKeys.DOCTYPE_PUBLIC); 1073 String systemID = format.getProperty(OutputKeys.DOCTYPE_SYSTEM); 1074 if (systemID != null) 1075 xoh.setDoctypeSystem(systemID); 1076 if (publicID != null) 1077 xoh.setDoctypePublic(publicID); 1078 1079 if (handler instanceof TransformerClient) { 1080 XalanTransformState state = new XalanTransformState(); 1081 ((TransformerClient)handler).setTransformState(state); 1082 ((ToSAXHandler)xoh).setTransformState(state); 1083 } 1084 1085 1086 } 1087 1088 else if (outputTarget instanceof StreamResult ) 1091 { 1092 StreamResult sresult = (StreamResult ) outputTarget; 1093 String method = format.getProperty(OutputKeys.METHOD); 1094 1095 try 1096 { 1097 SerializationHandler serializer = 1098 (SerializationHandler) SerializerFactory.getSerializer(format.getProperties()); 1099 1100 if (null != sresult.getWriter()) 1101 serializer.setWriter(sresult.getWriter()); 1102 else if (null != sresult.getOutputStream()) 1103 serializer.setOutputStream(sresult.getOutputStream()); 1104 else if (null != sresult.getSystemId()) 1105 { 1106 String fileURL = sresult.getSystemId(); 1107 1108 if (fileURL.startsWith("file:///")) 1109 { 1110 if (fileURL.substring(8).indexOf(":") >0) 1111 fileURL = fileURL.substring(8); 1112 else 1113 fileURL = fileURL.substring(7); 1114 } 1115 1116 m_outputStream = new java.io.FileOutputStream (fileURL); 1117 1118 serializer.setOutputStream(m_outputStream); 1119 1120 xoh = serializer; 1121 } 1122 else 1123 throw new TransformerException (XSLMessages.createMessage(XSLTErrorResources.ER_NO_OUTPUT_SPECIFIED, null)); 1125 1127 1129 xoh = serializer; 1130 } 1131 catch (IOException ioe) 1136 { 1137 throw new TransformerException (ioe); 1138 } 1139 } 1140 else 1141 { 1142 throw new TransformerException (XSLMessages.createMessage(XSLTErrorResources.ER_CANNOT_TRANSFORM_TO_RESULT_TYPE, new Object []{outputTarget.getClass().getName()})); } 1146 1147 xoh.setTransformer(this); 1150 1151 SourceLocator srcLocator = getStylesheet(); 1152 xoh.setSourceLocator(srcLocator); 1153 1154 1155 return xoh; 1156 1157 1158 } 1159 1160 1167 public void transform(Source xmlSource, Result outputTarget) 1168 throws TransformerException 1169 { 1170 transform(xmlSource, outputTarget, true); 1171 } 1172 1173 1181 public void transform(Source xmlSource, Result outputTarget, boolean shouldRelease) 1182 throws TransformerException 1183 { 1184 1185 synchronized (m_reentryGuard) 1186 { 1187 SerializationHandler xoh = createSerializationHandler(outputTarget); 1188 this.setSerializationHandler(xoh); 1189 1190 m_outputTarget = outputTarget; 1191 1192 transform(xmlSource, shouldRelease); 1193 } 1194 } 1195 1196 1206 public void transformNode(int node, Result outputTarget) 1207 throws TransformerException 1208 { 1209 1210 1211 SerializationHandler xoh = createSerializationHandler(outputTarget); 1212 this.setSerializationHandler(xoh); 1213 1214 m_outputTarget = outputTarget; 1215 1216 transformNode(node); 1217 } 1218 1219 1229 public void transformNode(int node) throws TransformerException 1230 { 1231 setExtensionsTable(getStylesheet()); 1233 synchronized (m_serializationHandler) 1235 { 1236 m_hasBeenReset = false; 1237 1238 XPathContext xctxt = getXPathContext(); 1239 DTM dtm = xctxt.getDTM(node); 1240 1241 try 1242 { 1243 pushGlobalVars(node); 1244 1245 StylesheetRoot stylesheet = this.getStylesheet(); 1249 int n = stylesheet.getGlobalImportCount(); 1250 1251 for (int i = 0; i < n; i++) 1252 { 1253 StylesheetComposed imported = stylesheet.getGlobalImport(i); 1254 int includedCount = imported.getIncludeCountComposed(); 1255 1256 for (int j = -1; j < includedCount; j++) 1257 { 1258 Stylesheet included = imported.getIncludeComposed(j); 1259 1260 included.runtimeInit(this); 1261 1262 for (ElemTemplateElement child = included.getFirstChildElem(); 1263 child != null; child = child.getNextSiblingElem()) 1264 { 1265 child.runtimeInit(this); 1266 } 1267 } 1268 } 1269 DTMIterator dtmIter = new org.apache.xpath.axes.SelfIteratorNoPredicate(); 1272 dtmIter.setRoot(node, xctxt); 1273 xctxt.pushContextNodeList(dtmIter); 1274 try 1275 { 1276 this.applyTemplateToNode(null, null, node); 1277 } 1278 finally 1279 { 1280 xctxt.popContextNodeList(); 1281 } 1282 1284 if (null != m_serializationHandler) 1286 { 1287 m_serializationHandler.endDocument(); 1288 } 1289 } 1290 catch (Exception se) 1291 { 1292 1293 1299 while(se instanceof org.apache.xml.utils.WrappedRuntimeException) 1301 { 1302 Exception e = ((org.apache.xml.utils.WrappedRuntimeException)se).getException(); 1303 if(null != e) 1304 se = e; 1305 } 1306 1307 if (null != m_serializationHandler) 1308 { 1309 try 1310 { 1311 if(se instanceof org.xml.sax.SAXParseException ) 1312 m_serializationHandler.fatalError((org.xml.sax.SAXParseException )se); 1313 else if(se instanceof TransformerException ) 1314 { 1315 TransformerException te = ((TransformerException )se); 1316 SAXSourceLocator sl = new SAXSourceLocator( te.getLocator() ); 1317 m_serializationHandler.fatalError(new org.xml.sax.SAXParseException (te.getMessage(), sl, te)); 1318 } 1319 else 1320 { 1321 m_serializationHandler.fatalError(new org.xml.sax.SAXParseException (se.getMessage(), new SAXSourceLocator(), se)); 1322 } 1323 } 1324 catch (Exception e){} 1325 } 1326 1327 if(se instanceof TransformerException ) 1328 { 1329 m_errorHandler.fatalError((TransformerException )se); 1330 } 1331 else if(se instanceof org.xml.sax.SAXParseException ) 1332 { 1333 m_errorHandler.fatalError(new TransformerException (se.getMessage(), 1334 new SAXSourceLocator((org.xml.sax.SAXParseException )se), 1335 se)); 1336 } 1337 else 1338 { 1339 m_errorHandler.fatalError(new TransformerException (se)); 1340 } 1341 1342 } 1343 finally 1344 { 1345 this.reset(); 1346 } 1347 } 1348 } 1349 1350 1357 public ContentHandler getInputContentHandler() 1358 { 1359 return getInputContentHandler(false); 1360 } 1361 1362 1372 public ContentHandler getInputContentHandler(boolean doDocFrag) 1373 { 1374 1375 if (null == m_inputContentHandler) 1376 { 1377 1378 m_inputContentHandler = new TransformerHandlerImpl(this, doDocFrag, 1381 m_urlOfSource); 1382 } 1383 1384 return m_inputContentHandler; 1385 } 1386 1387 1393 public DeclHandler getInputDeclHandler() 1394 { 1395 1396 if (m_inputContentHandler instanceof DeclHandler ) 1397 return (DeclHandler ) m_inputContentHandler; 1398 else 1399 return null; 1400 } 1401 1402 1408 public LexicalHandler getInputLexicalHandler() 1409 { 1410 1411 if (m_inputContentHandler instanceof LexicalHandler ) 1412 return (LexicalHandler ) m_inputContentHandler; 1413 else 1414 return null; 1415 } 1416 1417 1425 public void setOutputFormat(OutputProperties oformat) 1426 { 1427 m_outputFormat = oformat; 1428 } 1429 1430 1436 public OutputProperties getOutputFormat() 1437 { 1438 1439 OutputProperties format = (null == m_outputFormat) 1442 ? getStylesheet().getOutputComposed() 1443 : m_outputFormat; 1444 1445 return format; 1446 } 1447 1448 1458 public void setParameter(String name, String namespace, Object value) 1459 { 1460 1461 VariableStack varstack = getXPathContext().getVarStack(); 1462 QName qname = new QName(namespace, name); 1463 XObject xobject = XObject.create(value, getXPathContext()); 1464 1465 StylesheetRoot sroot = m_stylesheetRoot; 1466 Vector vars = sroot.getVariablesAndParamsComposed(); 1467 int i = vars.size(); 1468 while (--i >= 0) 1469 { 1470 ElemVariable variable = (ElemVariable)vars.elementAt(i); 1471 if(variable.getXSLToken() == Constants.ELEMNAME_PARAMVARIABLE && 1472 variable.getName().equals(qname)) 1473 { 1474 varstack.setGlobalVariable(i, xobject); 1475 } 1476 } 1477 } 1478 1479 1480 Vector m_userParams; 1481 1482 1492 public void setParameter(String name, Object value) 1493 { 1494 1495 if (value == null) { 1496 throw new IllegalArgumentException (XSLMessages.createMessage(XSLTErrorResources.ER_INVALID_SET_PARAM_VALUE, new Object []{name})); 1497 } 1498 1499 StringTokenizer tokenizer = new StringTokenizer (name, "{}", false); 1500 1501 try 1502 { 1503 1504 String s1 = tokenizer.nextToken(); 1507 String s2 = tokenizer.hasMoreTokens() ? tokenizer.nextToken() : null; 1508 1509 if (null == m_userParams) 1510 m_userParams = new Vector (); 1511 1512 if (null == s2) 1513 { 1514 replaceOrPushUserParam(new QName(s1), XObject.create(value, getXPathContext())); 1515 setParameter(s1, null, value); 1516 } 1517 else 1518 { 1519 replaceOrPushUserParam(new QName(s1, s2), XObject.create(value, getXPathContext())); 1520 setParameter(s2, s1, value); 1521 } 1522 } 1523 catch (java.util.NoSuchElementException nsee) 1524 { 1525 1526 } 1528 } 1529 1530 1537 private void replaceOrPushUserParam(QName qname, XObject xval) 1538 { 1539 1540 int n = m_userParams.size(); 1541 1542 for (int i = n - 1; i >= 0; i--) 1543 { 1544 Arg arg = (Arg) m_userParams.elementAt(i); 1545 1546 if (arg.getQName().equals(qname)) 1547 { 1548 m_userParams.setElementAt(new Arg(qname, xval, true), i); 1549 1550 return; 1551 } 1552 } 1553 1554 m_userParams.addElement(new Arg(qname, xval, true)); 1555 } 1556 1557 1568 public Object getParameter(String name) 1569 { 1570 1571 try 1572 { 1573 1574 QName qname = QName.getQNameFromString(name); 1578 1579 if (null == m_userParams) 1580 return null; 1581 1582 int n = m_userParams.size(); 1583 1584 for (int i = n - 1; i >= 0; i--) 1585 { 1586 Arg arg = (Arg) m_userParams.elementAt(i); 1587 1588 if (arg.getQName().equals(qname)) 1589 { 1590 return arg.getVal().object(); 1591 } 1592 } 1593 1594 return null; 1595 } 1596 catch (java.util.NoSuchElementException nsee) 1597 { 1598 1599 return null; 1601 } 1602 } 1603 1604 1610 private void resetUserParameters() 1611 { 1612 1613 try 1614 { 1615 1616 if (null == m_userParams) 1617 return; 1618 1619 int n = m_userParams.size(); 1620 for (int i = n - 1; i >= 0; i--) 1621 { 1622 Arg arg = (Arg) m_userParams.elementAt(i); 1623 QName name = arg.getQName(); 1624 String s1 = name.getNamespace(); 1627 String s2 = name.getLocalPart(); 1628 1629 setParameter(s2, s1, arg.getVal().object()); 1630 1631 } 1632 1633 } 1634 catch (java.util.NoSuchElementException nsee) 1635 { 1636 1638 } 1639 } 1640 1641 1655 public void setParameters(Properties params) 1656 { 1657 1658 clearParameters(); 1659 1660 Enumeration names = params.propertyNames(); 1661 1662 while (names.hasMoreElements()) 1663 { 1664 String name = params.getProperty((String ) names.nextElement()); 1665 StringTokenizer tokenizer = new StringTokenizer (name, "{}", false); 1666 1667 try 1668 { 1669 1670 String s1 = tokenizer.nextToken(); 1673 String s2 = tokenizer.hasMoreTokens() ? tokenizer.nextToken() : null; 1674 1675 if (null == s2) 1676 setParameter(s1, null, params.getProperty(name)); 1677 else 1678 setParameter(s2, s1, params.getProperty(name)); 1679 } 1680 catch (java.util.NoSuchElementException nsee) 1681 { 1682 1683 } 1685 } 1686 } 1687 1688 1691 public void clearParameters() 1692 { 1693 1694 synchronized (m_reentryGuard) 1695 { 1696 VariableStack varstack = new VariableStack(); 1697 1698 m_xcontext.setVarStack(varstack); 1699 1700 m_userParams = null; 1701 } 1702 } 1703 1704 1705 1724 protected void pushGlobalVars(int contextNode) throws TransformerException 1725 { 1726 1727 XPathContext xctxt = m_xcontext; 1728 VariableStack vs = xctxt.getVarStack(); 1729 StylesheetRoot sr = getStylesheet(); 1730 Vector vars = sr.getVariablesAndParamsComposed(); 1731 1732 int i = vars.size(); 1733 vs.link(i); 1734 1735 while (--i >= 0) 1736 { 1737 ElemVariable v = (ElemVariable) vars.elementAt(i); 1738 1739 XObject xobj = new XUnresolvedVariable(v, contextNode, this, 1741 vs.getStackFrame(), 0, true); 1742 1743 if(null == vs.elementAt(i)) 1744 vs.setGlobalVariable(i, xobj); 1745 } 1746 1747 } 1748 1749 1755 public void setURIResolver(URIResolver resolver) 1756 { 1757 1758 synchronized (m_reentryGuard) 1759 { 1760 m_xcontext.getSourceTreeManager().setURIResolver(resolver); 1761 } 1762 } 1763 1764 1771 public URIResolver getURIResolver() 1772 { 1773 return m_xcontext.getSourceTreeManager().getURIResolver(); 1774 } 1775 1776 1778 1788 public void setContentHandler(ContentHandler handler) 1789 { 1790 1791 if (handler == null) 1792 { 1793 throw new NullPointerException (XSLMessages.createMessage(XSLTErrorResources.ER_NULL_CONTENT_HANDLER, null)); } 1795 else 1796 { 1797 m_outputContentHandler = handler; 1798 1799 if (null == m_serializationHandler) 1800 { 1801 ToXMLSAXHandler h = new ToXMLSAXHandler(); 1802 h.setContentHandler(handler); 1803 h.setTransformer(this); 1804 1805 m_serializationHandler = h; 1806 } 1807 else 1808 m_serializationHandler.setContentHandler(handler); 1809 } 1810 } 1811 1812 1818 public ContentHandler getContentHandler() 1819 { 1820 return m_outputContentHandler; 1821 } 1822 1823 1833 public int transformToRTF(ElemTemplateElement templateParent) 1834 throws TransformerException 1835 { 1836 DTM dtmFrag = m_xcontext.getRTFDTM(); 1839 return transformToRTF(templateParent,dtmFrag); 1840 } 1841 1842 1856 public int transformToGlobalRTF(ElemTemplateElement templateParent) 1857 throws TransformerException 1858 { 1859 DTM dtmFrag = m_xcontext.getGlobalRTFDTM(); 1862 return transformToRTF(templateParent,dtmFrag); 1863 } 1864 1865 1875 private int transformToRTF(ElemTemplateElement templateParent,DTM dtmFrag) 1876 throws TransformerException 1877 { 1878 1879 XPathContext xctxt = m_xcontext; 1880 1881 ContentHandler rtfHandler = dtmFrag.getContentHandler(); 1882 1883 int resultFragment; 1889 SerializationHandler savedRTreeHandler = this.m_serializationHandler; 1891 1892 1893 ToSAXHandler h = new ToXMLSAXHandler(); 1895 h.setContentHandler(rtfHandler); 1896 h.setTransformer(this); 1897 1898 m_serializationHandler = h; 1900 1901 SerializationHandler rth = m_serializationHandler; 1903 1904 try 1905 { 1906 rth.startDocument(); 1907 1908 rth.flushPending(); 1912 1913 try 1914 { 1915 1916 executeChildTemplates(templateParent, true); 1918 1919 rth.flushPending(); 1921 1922 resultFragment = dtmFrag.getDocument(); 1927 } 1928 finally 1929 { 1930 rth.endDocument(); 1931 } 1932 } 1933 catch (org.xml.sax.SAXException se) 1934 { 1935 throw new TransformerException (se); 1936 } 1937 finally 1938 { 1939 1940 this.m_serializationHandler = savedRTreeHandler; 1942 } 1943 1944 return resultFragment; 1945 } 1946 1947 1954 public ObjectPool getStringWriterPool() 1955 { 1956 return m_stringWriterObjectPool; 1957 } 1958 1959 1974 public String transformToString(ElemTemplateElement elem) 1975 throws TransformerException 1976 { 1977 ElemTemplateElement firstChild = elem.getFirstChildElem(); 1978 if(null == firstChild) 1979 return ""; 1980 if(elem.hasTextLitOnly() && org.apache.xalan.processor.TransformerFactoryImpl.m_optimize) 1981 { 1982 return ((ElemTextLiteral)firstChild).getNodeValue(); 1983 } 1984 1985 SerializationHandler savedRTreeHandler = this.m_serializationHandler; 1987 1988 StringWriter sw = (StringWriter ) m_stringWriterObjectPool.getInstance(); 1991 1992 m_serializationHandler = 1993 (ToTextStream) m_textResultHandlerObjectPool.getInstance(); 1994 1995 if (null == m_serializationHandler) 1996 { 1997 1999 2000 Serializer serializer = org.apache.xml.serializer.SerializerFactory.getSerializer( 2001 m_textformat.getProperties()); 2002 m_serializationHandler = (SerializationHandler) serializer; 2003 } 2004 2005 m_serializationHandler.setTransformer(this); 2006 m_serializationHandler.setWriter(sw); 2007 2008 2009 String result; 2010 2011 try 2012 { 2013 2016 2018 executeChildTemplates(elem, true); 2020 this.m_serializationHandler.endDocument(); 2021 2022 result = sw.toString(); 2023 } 2024 catch (org.xml.sax.SAXException se) 2025 { 2026 throw new TransformerException (se); 2027 } 2028 finally 2029 { 2030 sw.getBuffer().setLength(0); 2031 2032 try 2033 { 2034 sw.close(); 2035 } 2036 catch (Exception ioe){} 2037 2038 m_stringWriterObjectPool.freeInstance(sw); 2039 m_serializationHandler.reset(); 2040 m_textResultHandlerObjectPool.freeInstance(m_serializationHandler); 2041 2042 m_serializationHandler = savedRTreeHandler; 2044 } 2045 2046 return result; 2047 } 2048 2049 2061 public boolean applyTemplateToNode(ElemTemplateElement xslInstruction, ElemTemplate template, int child) 2063 throws TransformerException 2064 { 2065 2066 DTM dtm = m_xcontext.getDTM(child); 2067 short nodeType = dtm.getNodeType(child); 2068 boolean isDefaultTextRule = false; 2069 boolean isApplyImports = false; 2070 2071 if (null == template) 2072 { 2073 int maxImportLevel, endImportLevel=0; 2074 isApplyImports = ((xslInstruction == null) 2075 ? false 2076 : xslInstruction.getXSLToken() 2077 == Constants.ELEMNAME_APPLY_IMPORTS); 2078 2079 if (isApplyImports) 2080 { 2081 maxImportLevel = 2082 xslInstruction.getStylesheetComposed().getImportCountComposed() - 1; 2083 endImportLevel = 2084 xslInstruction.getStylesheetComposed().getEndImportCountComposed(); 2085 } 2086 else 2087 { 2088 maxImportLevel = -1; 2089 } 2090 2091 if (isApplyImports && (maxImportLevel == -1)) 2098 { 2099 template = null; 2100 } 2101 else 2102 { 2103 2104 XPathContext xctxt = m_xcontext; 2107 2108 try 2109 { 2110 xctxt.pushNamespaceContext(xslInstruction); 2111 2112 QName mode = this.getMode(); 2113 2114 if (isApplyImports) 2115 template = m_stylesheetRoot.getTemplateComposed(xctxt, child, mode, 2116 maxImportLevel, endImportLevel, m_quietConflictWarnings, dtm); 2117 else 2118 template = m_stylesheetRoot.getTemplateComposed(xctxt, child, mode, 2119 m_quietConflictWarnings, dtm); 2120 2121 } 2122 finally 2123 { 2124 xctxt.popNamespaceContext(); 2125 } 2126 } 2127 2128 if (null == template) 2131 { 2132 switch (nodeType) 2133 { 2134 case DTM.DOCUMENT_FRAGMENT_NODE : 2135 case DTM.ELEMENT_NODE : 2136 template = m_stylesheetRoot.getDefaultRule(); 2137 break; 2138 case DTM.CDATA_SECTION_NODE : 2139 case DTM.TEXT_NODE : 2140 case DTM.ATTRIBUTE_NODE : 2141 template = m_stylesheetRoot.getDefaultTextRule(); 2142 isDefaultTextRule = true; 2143 break; 2144 case DTM.DOCUMENT_NODE : 2145 template = m_stylesheetRoot.getDefaultRootRule(); 2146 break; 2147 default : 2148 2149 return false; 2151 } 2152 } 2153 } 2154 2155 try 2158 { 2159 pushElemTemplateElement(template); 2160 m_xcontext.pushCurrentNode(child); 2161 pushPairCurrentMatched(template, child); 2162 2163 if (!isApplyImports) { 2165 DTMIterator cnl = new org.apache.xpath.NodeSetDTM(child, m_xcontext.getDTMManager()); 2166 m_xcontext.pushContextNodeList(cnl); 2167 } 2168 2169 if (isDefaultTextRule) 2170 { 2171 switch (nodeType) 2172 { 2173 case DTM.CDATA_SECTION_NODE : 2174 case DTM.TEXT_NODE : 2175 ClonerToResultTree.cloneToResultTree(child, nodeType, 2176 dtm, getResultTreeHandler(), false); 2177 break; 2178 case DTM.ATTRIBUTE_NODE : 2179 dtm.dispatchCharactersEvents(child, getResultTreeHandler(), false); 2180 break; 2181 } 2182 } 2183 else 2184 { 2185 2186 2188 if (TransformerImpl.S_DEBUG) 2189 getTraceManager().fireTraceEvent(template); 2190 m_xcontext.setSAXLocator(template); 2200 m_xcontext.getVarStack().link(template.m_frameSize); 2202 executeChildTemplates(template, true); 2203 2204 if (TransformerImpl.S_DEBUG) 2205 getTraceManager().fireTraceEndEvent(template); 2206 } 2207 } 2208 catch (org.xml.sax.SAXException se) 2209 { 2210 throw new TransformerException (se); 2211 } 2212 finally 2213 { 2214 if (!isDefaultTextRule) 2215 m_xcontext.getVarStack().unlink(); 2216 m_xcontext.popCurrentNode(); 2217 if (!isApplyImports) { 2218 m_xcontext.popContextNodeList(); 2219 popCurrentMatched(); 2220 } 2221 popElemTemplateElement(); 2222 } 2223 2224 return true; 2225 } 2226 2227 2228 2243 public void executeChildTemplates( 2244 ElemTemplateElement elem, org.w3c.dom.Node context, QName mode, ContentHandler handler) 2245 throws TransformerException 2246 { 2247 2248 XPathContext xctxt = m_xcontext; 2249 2250 try 2251 { 2252 if(null != mode) 2253 pushMode(mode); 2254 xctxt.pushCurrentNode(xctxt.getDTMHandleFromNode(context)); 2255 executeChildTemplates(elem, handler); 2256 } 2257 finally 2258 { 2259 xctxt.popCurrentNode(); 2260 2261 if (null != mode) 2264 popMode(); 2265 } 2266 } 2267 2268 2282 public void executeChildTemplates( 2283 ElemTemplateElement elem, boolean shouldAddAttrs) 2284 throws TransformerException 2285 { 2286 2287 ElemTemplateElement t = elem.getFirstChildElem(); 2289 2290 if (null == t) 2291 return; 2292 2293 if(elem.hasTextLitOnly() && org.apache.xalan.processor.TransformerFactoryImpl.m_optimize) 2294 { 2295 char[] chars = ((ElemTextLiteral)t).getChars(); 2296 try 2297 { 2298 this.pushElemTemplateElement(t); 2300 m_serializationHandler.characters(chars, 0, chars.length); 2301 } 2302 catch(SAXException se) 2303 { 2304 throw new TransformerException (se); 2305 } 2306 finally 2307 { 2308 this.popElemTemplateElement(); 2309 } 2310 return; 2311 } 2312 2313 2319 XPathContext xctxt = m_xcontext; 2320 xctxt.pushSAXLocatorNull(); 2321 int currentTemplateElementsTop = m_currentTemplateElements.size(); 2322 m_currentTemplateElements.push(null); 2323 2324 try 2325 { 2326 for (; t != null; t = t.getNextSiblingElem()) 2329 { 2330 if (!shouldAddAttrs 2331 && t.getXSLToken() == Constants.ELEMNAME_ATTRIBUTE) 2332 continue; 2333 2334 xctxt.setSAXLocator(t); 2335 m_currentTemplateElements.setElementAt(t,currentTemplateElementsTop); 2336 t.execute(this); 2337 } 2338 } 2339 catch(RuntimeException re) 2340 { 2341 TransformerException te = new TransformerException (re); 2342 te.setLocator(t); 2343 throw te; 2344 } 2345 finally 2346 { 2347 m_currentTemplateElements.pop(); 2348 xctxt.popSAXLocator(); 2349 } 2350 2351 } 2355 2366 public void executeChildTemplates( 2367 ElemTemplateElement elem, ContentHandler handler) 2368 throws TransformerException 2369 { 2370 2371 SerializationHandler xoh = this.getSerializationHandler(); 2372 2373 SerializationHandler savedHandler = xoh; 2377 2378 try 2379 { 2380 xoh.flushPending(); 2381 2382 LexicalHandler lex = null; 2384 if (handler instanceof LexicalHandler ) { 2385 lex = (LexicalHandler ) handler; 2386 } 2387 m_serializationHandler = new ToXMLSAXHandler(handler, lex, savedHandler.getEncoding()); 2388 m_serializationHandler.setTransformer(this); 2389 executeChildTemplates(elem, true); 2390 } 2391 catch (TransformerException e) 2392 { 2393 throw e; 2394 } 2395 catch (SAXException se) { 2396 throw new TransformerException (se); 2397 } 2398 finally 2399 { 2400 m_serializationHandler = savedHandler; 2401 } 2402 } 2403 2404 2417 public Vector processSortKeys(ElemForEach foreach, int sourceNodeContext) 2418 throws TransformerException 2419 { 2420 2421 Vector keys = null; 2422 XPathContext xctxt = m_xcontext; 2423 int nElems = foreach.getSortElemCount(); 2424 2425 if (nElems > 0) 2426 keys = new Vector (); 2427 2428 for (int i = 0; i < nElems; i++) 2430 { 2431 ElemSort sort = foreach.getSortElem(i); 2432 2433 if (TransformerImpl.S_DEBUG) 2434 getTraceManager().fireTraceEvent(sort); 2435 2436 String langString = 2437 (null != sort.getLang()) 2438 ? sort.getLang().evaluate(xctxt, sourceNodeContext, foreach) : null; 2439 String dataTypeString = sort.getDataType().evaluate(xctxt, 2440 sourceNodeContext, foreach); 2441 2442 if (dataTypeString.indexOf(":") >= 0) 2443 System.out.println( 2444 "TODO: Need to write the hooks for QNAME sort data type"); 2445 else if (!(dataTypeString.equalsIgnoreCase(Constants.ATTRVAL_DATATYPE_TEXT)) 2446 &&!(dataTypeString.equalsIgnoreCase( 2447 Constants.ATTRVAL_DATATYPE_NUMBER))) 2448 foreach.error(XSLTErrorResources.ER_ILLEGAL_ATTRIBUTE_VALUE, 2449 new Object []{ Constants.ATTRNAME_DATATYPE, 2450 dataTypeString }); 2451 2452 boolean treatAsNumbers = 2453 ((null != dataTypeString) && dataTypeString.equals( 2454 Constants.ATTRVAL_DATATYPE_NUMBER)) ? true : false; 2455 String orderString = sort.getOrder().evaluate(xctxt, sourceNodeContext, 2456 foreach); 2457 2458 if (!(orderString.equalsIgnoreCase(Constants.ATTRVAL_ORDER_ASCENDING)) 2459 &&!(orderString.equalsIgnoreCase( 2460 Constants.ATTRVAL_ORDER_DESCENDING))) 2461 foreach.error(XSLTErrorResources.ER_ILLEGAL_ATTRIBUTE_VALUE, 2462 new Object []{ Constants.ATTRNAME_ORDER, 2463 orderString }); 2464 2465 boolean descending = 2466 ((null != orderString) && orderString.equals( 2467 Constants.ATTRVAL_ORDER_DESCENDING)) ? true : false; 2468 AVT caseOrder = sort.getCaseOrder(); 2469 boolean caseOrderUpper; 2470 2471 if (null != caseOrder) 2472 { 2473 String caseOrderString = caseOrder.evaluate(xctxt, sourceNodeContext, 2474 foreach); 2475 2476 if (!(caseOrderString.equalsIgnoreCase(Constants.ATTRVAL_CASEORDER_UPPER)) 2477 &&!(caseOrderString.equalsIgnoreCase( 2478 Constants.ATTRVAL_CASEORDER_LOWER))) 2479 foreach.error(XSLTErrorResources.ER_ILLEGAL_ATTRIBUTE_VALUE, 2480 new Object []{ Constants.ATTRNAME_CASEORDER, 2481 caseOrderString }); 2482 2483 caseOrderUpper = 2484 ((null != caseOrderString) && caseOrderString.equals( 2485 Constants.ATTRVAL_CASEORDER_UPPER)) ? true : false; 2486 } 2487 else 2488 { 2489 caseOrderUpper = false; 2490 } 2491 2492 keys.addElement(new NodeSortKey(this, sort.getSelect(), treatAsNumbers, 2493 descending, langString, caseOrderUpper, 2494 foreach)); 2495 if (TransformerImpl.S_DEBUG) 2496 getTraceManager().fireTraceEndEvent(sort); 2497 } 2498 2499 return keys; 2500 } 2501 2502 2506 2512 public Vector getElementCallstack() 2513 { 2514 Vector elems = new Vector (); 2515 int nStackSize = m_currentTemplateElements.size(); 2516 for(int i = 0; i < nStackSize; i++) 2517 { 2518 ElemTemplateElement elem = (ElemTemplateElement) m_currentTemplateElements.elementAt(i); 2519 if(null != elem) 2520 { 2521 elems.addElement(elem); 2522 } 2523 } 2524 return elems; 2525 } 2526 2527 2533 public int getCurrentTemplateElementsCount() 2534 { 2535 return m_currentTemplateElements.size(); 2536 } 2537 2538 2539 2545 public ObjectStack getCurrentTemplateElements() 2546 { 2547 return m_currentTemplateElements; 2548 } 2549 2550 2556 public void pushElemTemplateElement(ElemTemplateElement elem) 2557 { 2558 m_currentTemplateElements.push(elem); 2559 } 2560 2561 2564 public void popElemTemplateElement() 2565 { 2566 m_currentTemplateElements.pop(); 2567 } 2568 2569 2576 public void setCurrentElement(ElemTemplateElement e) 2577 { 2578 m_currentTemplateElements.setTop(e); 2579 } 2580 2581 2588 public ElemTemplateElement getCurrentElement() 2589 { 2590 return (m_currentTemplateElements.size() > 0) ? 2591 (ElemTemplateElement) m_currentTemplateElements.peek() : null; 2592 } 2593 2594 2600 public int getCurrentNode() 2601 { 2602 return m_xcontext.getCurrentNode(); 2603 } 2604 2605 2612 public Vector getTemplateCallstack() 2613 { 2614 Vector elems = new Vector (); 2615 int nStackSize = m_currentTemplateElements.size(); 2616 for(int i = 0; i < nStackSize; i++) 2617 { 2618 ElemTemplateElement elem = (ElemTemplateElement) m_currentTemplateElements.elementAt(i); 2619 if(null != elem && (elem.getXSLToken() != Constants.ELEMNAME_TEMPLATE)) 2620 { 2621 elems.addElement(elem); 2622 } 2623 } 2624 return elems; 2625 } 2626 2627 2628 2639 public ElemTemplate getCurrentTemplate() 2640 { 2641 2642 ElemTemplateElement elem = getCurrentElement(); 2643 2644 while ((null != elem) 2645 && (elem.getXSLToken() != Constants.ELEMNAME_TEMPLATE)) 2646 { 2647 elem = elem.getParentElem(); 2648 } 2649 2650 return (ElemTemplate) elem; 2651 } 2652 2653 2661 public void pushPairCurrentMatched(ElemTemplateElement template, int child) 2662 { 2663 m_currentMatchTemplates.push(template); 2664 m_currentMatchedNodes.push(child); 2665 } 2666 2667 2670 public void popCurrentMatched() 2671 { 2672 m_currentMatchTemplates.pop(); 2673 m_currentMatchedNodes.pop(); 2674 } 2675 2676 2685 public ElemTemplate getMatchedTemplate() 2686 { 2687 return (ElemTemplate) m_currentMatchTemplates.peek(); 2688 } 2689 2690 2697 public int getMatchedNode() 2698 { 2699 return m_currentMatchedNodes.peepTail(); 2700 } 2701 2702 2707 public DTMIterator getContextNodeList() 2708 { 2709 2710 try 2711 { 2712 DTMIterator cnl = m_xcontext.getContextNodeList(); 2713 2714 return (cnl == null) ? null : (DTMIterator) cnl.cloneWithReset(); 2715 } 2716 catch (CloneNotSupportedException cnse) 2717 { 2718 2719 return null; 2721 } 2722 } 2723 2724 2729 public Transformer getTransformer() 2730 { 2731 return this; 2732 } 2733 2734 2738 2748 public void setStylesheet(StylesheetRoot stylesheetRoot) 2749 { 2750 m_stylesheetRoot = stylesheetRoot; 2751 } 2752 2753 2759 public final StylesheetRoot getStylesheet() 2760 { 2761 return m_stylesheetRoot; 2762 } 2763 2764 2772 public boolean getQuietConflictWarnings() 2773 { 2774 return m_quietConflictWarnings; 2775 } 2776 2777 2786 public void setQuietConflictWarnings(boolean b) 2787 { 2788 m_quietConflictWarnings = b; 2789 } 2790 2791 2798 public void setXPathContext(XPathContext xcontext) 2799 { 2800 m_xcontext = xcontext; 2801 } 2802 2803 2808 public final XPathContext getXPathContext() 2809 { 2810 return m_xcontext; 2811 } 2812 2813 2820 public StackGuard getStackGuard() 2821 { 2822 return m_stackGuard; 2823 } 2824 2825 2837 public int getRecursionLimit() 2838 { 2839 return m_stackGuard.getRecursionLimit(); 2840 } 2841 2842 2855 public void setRecursionLimit(int limit) 2856 { 2857 m_stackGuard.setRecursionLimit(limit); 2858 } 2859 2860 2866 public SerializationHandler getResultTreeHandler() 2867 { 2868 return m_serializationHandler; 2869 } 2870 2871 2877 public SerializationHandler getSerializationHandler() 2878 { 2879 return m_serializationHandler; 2880 } 2881 2882 2888 public KeyManager getKeyManager() 2889 { 2890 return m_keyManager; 2891 } 2892 2893 2900 public boolean isRecursiveAttrSet(ElemAttributeSet attrSet) 2901 { 2902 2903 if (null == m_attrSetStack) 2904 { 2905 m_attrSetStack = new Stack (); 2906 } 2907 2908 if (!m_attrSetStack.empty()) 2909 { 2910 int loc = m_attrSetStack.search(attrSet); 2911 2912 if (loc > -1) 2913 { 2914 return true; 2915 } 2916 } 2917 2918 return false; 2919 } 2920 2921 2927 public void pushElemAttributeSet(ElemAttributeSet attrSet) 2928 { 2929 m_attrSetStack.push(attrSet); 2930 } 2931 2932 2935 public void popElemAttributeSet() 2936 { 2937 m_attrSetStack.pop(); 2938 } 2939 2940 2945 public CountersTable getCountersTable() 2946 { 2947 2948 if (null == m_countersTable) 2949 m_countersTable = new CountersTable(); 2950 2951 return m_countersTable; 2952 } 2953 2954 2960 public boolean currentTemplateRuleIsNull() 2961 { 2962 return ((!m_currentTemplateRuleIsNull.isEmpty()) 2963 && (m_currentTemplateRuleIsNull.peek() == true)); 2964 } 2965 2966 2973 public void pushCurrentTemplateRuleIsNull(boolean b) 2974 { 2975 m_currentTemplateRuleIsNull.push(b); 2976 } 2977 2978 2982 public void popCurrentTemplateRuleIsNull() 2983 { 2984 m_currentTemplateRuleIsNull.pop(); 2985 } 2986 2987 2995 public void pushCurrentFuncResult(Object val) { 2996 m_currentFuncResult.push(val); 2997 } 2998 2999 3004 public Object popCurrentFuncResult() { 3005 return m_currentFuncResult.pop(); 3006 } 3007 3008 3015 public boolean currentFuncResultSeen() { 3016 return !m_currentFuncResult.empty() 3017 && m_currentFuncResult.peek() != null; 3018 } 3019 3020 3025 public MsgMgr getMsgMgr() 3026 { 3027 3028 if (null == m_msgMgr) 3029 m_msgMgr = new MsgMgr(this); 3030 3031 return m_msgMgr; 3032 } 3033 3034 3040 public void setErrorListener(ErrorListener listener) 3041 throws IllegalArgumentException 3042 { 3043 3044 synchronized (m_reentryGuard) 3045 { 3046 if (listener == null) 3047 throw new IllegalArgumentException (XSLMessages.createMessage(XSLTErrorResources.ER_NULL_ERROR_HANDLER, null)); 3049 m_errorHandler = listener; 3050 } 3051 } 3052 3053 3058 public ErrorListener getErrorListener() 3059 { 3060 return m_errorHandler; 3061 } 3062 3063 3070 public TraceManager getTraceManager() 3071 { 3072 return m_traceManager; 3073 } 3074 3075 3107 public boolean getFeature(String name) 3108 throws SAXNotRecognizedException , SAXNotSupportedException 3109 { 3110 3111 if ("http://xml.org/trax/features/sax/input".equals(name)) 3112 return true; 3113 else if ("http://xml.org/trax/features/dom/input".equals(name)) 3114 return true; 3115 3116 throw new SAXNotRecognizedException (name); 3117 } 3118 3119 3121 3127 public QName getMode() 3128 { 3129 return m_modes.isEmpty() ? null : (QName) m_modes.peek(); 3130 } 3131 3132 3134 3140 public void pushMode(QName mode) 3141 { 3142 m_modes.push(mode); 3143 } 3144 3145 3147 3151 public void popMode() 3152 { 3153 m_modes.pop(); 3154 } 3155 3156 3162 public void runTransformThread(int priority) 3163 { 3164 3165 Thread t = ThreadControllerWrapper.runThread(this, priority); 3167 this.setTransformThread(t); 3168 } 3169 3170 3175 public void runTransformThread() 3176 { 3177 ThreadControllerWrapper.runThread(this, -1); 3178 } 3179 3180 3186 public static void runTransformThread(Runnable runnable) 3187 { 3188 ThreadControllerWrapper.runThread(runnable, -1); 3189 } 3190 3191 3197 public void waitTransformThread() throws SAXException 3198 { 3199 3200 Thread transformThread = this.getTransformThread(); 3206 3207 if (null != transformThread) 3208 { 3209 try 3210 { 3211 ThreadControllerWrapper.waitThread(transformThread, this); 3212 3213 if (!this.hasTransformThreadErrorCatcher()) 3214 { 3215 Exception e = this.getExceptionThrown(); 3216 3217 if (null != e) 3218 { 3219 e.printStackTrace(); 3220 throw new org.xml.sax.SAXException (e); 3221 } 3222 } 3223 3224 this.setTransformThread(null); 3225 } 3226 catch (InterruptedException ie){} 3227 } 3228 } 3229 3230 3237 public Exception getExceptionThrown() 3238 { 3239 return m_exceptionThrown; 3240 } 3241 3242 3249 public void setExceptionThrown(Exception e) 3250 { 3251 m_exceptionThrown = e; 3252 } 3253 3254 3260 public void setSourceTreeDocForThread(int doc) 3261 { 3262 m_doc = doc; 3263 } 3264 3265 3272 public void setXMLSource(Source source) 3273 { 3274 m_xmlSource = source; 3275 } 3276 3277 3283 public boolean isTransformDone() 3284 { 3285 3286 synchronized (this) 3287 { 3288 return m_isTransformDone; 3289 } 3290 } 3291 3292 3298 public void setIsTransformDone(boolean done) 3299 { 3300 3301 synchronized (this) 3302 { 3303 m_isTransformDone = done; 3304 } 3305 } 3306 3307 3313 void postExceptionFromThread(Exception e) 3314 { 3315 3316 3331 m_isTransformDone = true; 3337 m_exceptionThrown = e; 3338 ; 3340 synchronized (this) 3341 { 3342 3343 notifyAll(); 3348 3349 } 3357 } 3358 3359 3362 public void run() 3363 { 3364 3365 m_hasBeenReset = false; 3366 3367 try 3368 { 3369 3370 try 3373 { 3374 m_isTransformDone = false; 3375 3376 3383 transformNode(m_doc); 3384 3385 } 3386 catch (Exception e) 3387 { 3388 3390 if (null != m_transformThread) 3392 postExceptionFromThread(e); else 3394 throw new RuntimeException (e.getMessage()); 3395 } 3396 finally 3397 { 3398 m_isTransformDone = true; 3399 3400 if (m_inputContentHandler instanceof TransformerHandlerImpl) 3401 { 3402 ((TransformerHandlerImpl) m_inputContentHandler).clearCoRoutine(); 3403 } 3404 3405 } 3410 } 3411 catch (Exception e) 3412 { 3413 3414 if (null != m_transformThread) 3416 postExceptionFromThread(e); 3417 else 3418 throw new RuntimeException (e.getMessage()); } 3420 } 3421 3422 3424 3431 public TransformSnapshot getSnapshot() 3432 { 3433 return new TransformSnapshotImpl(this); 3434 } 3435 3436 3446 public void executeFromSnapshot(TransformSnapshot ts) 3447 throws TransformerException 3448 { 3449 3450 ElemTemplateElement template = getMatchedTemplate(); 3451 int child = getMatchedNode(); 3452 3453 pushElemTemplateElement(template); m_xcontext.pushCurrentNode(child); this.executeChildTemplates(template, true); } 3457 3458 3465 public void resetToStylesheet(TransformSnapshot ts) 3466 { 3467 ((TransformSnapshotImpl) ts).apply(this); 3468 } 3469 3470 3474 public void stopTransformation(){} 3475 3476 3486 public short getShouldStripSpace(int elementHandle, DTM dtm) 3487 { 3488 3489 try 3490 { 3491 org.apache.xalan.templates.WhiteSpaceInfo info = 3492 m_stylesheetRoot.getWhiteSpaceInfo(m_xcontext, elementHandle, dtm); 3493 3494 if (null == info) 3495 { 3496 return DTMWSFilter.INHERIT; 3497 } 3498 else 3499 { 3500 3501 return info.getShouldStripSpace() 3503 ? DTMWSFilter.STRIP : DTMWSFilter.NOTSTRIP; 3504 } 3505 } 3506 catch (TransformerException se) 3507 { 3508 return DTMWSFilter.INHERIT; 3509 } 3510 } 3511 3517 public void init(ToXMLSAXHandler h,Transformer transformer, ContentHandler realHandler) 3518 { 3519 h.setTransformer(transformer); 3520 h.setContentHandler(realHandler); 3521 } 3522 3523 public void setSerializationHandler(SerializationHandler xoh) 3524 { 3525 m_serializationHandler = xoh; 3526 } 3527 3528 3529 3530 3534 public void fireGenerateEvent( 3535 int eventType, 3536 char[] ch, 3537 int start, 3538 int length) { 3539 3540 GenerateEvent ge = new GenerateEvent(this, eventType, ch, start, length); 3541 m_traceManager.fireGenerateEvent(ge); 3542 } 3543 3544 3548 public void fireGenerateEvent( 3549 int eventType, 3550 String name, 3551 Attributes atts) { 3552 3553 GenerateEvent ge = new GenerateEvent(this, eventType, name, atts); 3554 m_traceManager.fireGenerateEvent(ge); 3555 } 3556 3557 3561 public void fireGenerateEvent(int eventType, String name, String data) { 3562 GenerateEvent ge = new GenerateEvent(this, eventType, name,data); 3563 m_traceManager.fireGenerateEvent(ge); 3564 } 3565 3566 3570 public void fireGenerateEvent(int eventType, String data) { 3571 GenerateEvent ge = new GenerateEvent(this, eventType, data); 3572 m_traceManager.fireGenerateEvent(ge); 3573 } 3574 3575 3579 public void fireGenerateEvent(int eventType) { 3580 GenerateEvent ge = new GenerateEvent(this, eventType); 3581 m_traceManager.fireGenerateEvent(ge); 3582 } 3583 3584 3587 public boolean hasTraceListeners() { 3588 return m_traceManager.hasTraceListeners(); 3589 } 3590 3591} 3593 | Popular Tags |