19f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson/* 29f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * Licensed to the Apache Software Foundation (ASF) under one 39f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * or more contributor license agreements. See the NOTICE file 49f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * distributed with this work for additional information 59f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * regarding copyright ownership. The ASF licenses this file 69f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * to you under the Apache License, Version 2.0 (the "License"); 79f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * you may not use this file except in compliance with the License. 89f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * You may obtain a copy of the License at 99f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * 109f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * http://www.apache.org/licenses/LICENSE-2.0 119f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * 129f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * Unless required by applicable law or agreed to in writing, software 139f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * distributed under the License is distributed on an "AS IS" BASIS, 149f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 159f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * See the License for the specific language governing permissions and 169f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * limitations under the License. 179f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson */ 189f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson/* 199f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * $Id: DTMTreeWalker.java 468653 2006-10-28 07:07:05Z minchau $ 209f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson */ 219f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilsonpackage org.apache.xml.dtm.ref; 229f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson 239f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilsonimport org.apache.xml.dtm.DTM; 249f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilsonimport org.apache.xml.utils.NodeConsumer; 259f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilsonimport org.apache.xml.utils.XMLString; 269f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson 279f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilsonimport org.xml.sax.ContentHandler; 289f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilsonimport org.xml.sax.ext.LexicalHandler; 299f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson 309f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson/** 319f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * This class does a pre-order walk of the DTM tree, calling a ContentHandler 329f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * interface as it goes. As such, it's more like the Visitor design pattern 339f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * than like the DOM's TreeWalker. 349f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * 359f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * I think normally this class should not be needed, because 369f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * of DTM#dispatchToEvents. 379f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * @xsl.usage advanced 389f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson */ 399f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilsonpublic class DTMTreeWalker 409f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson{ 419f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson 429f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson /** Local reference to a ContentHandler */ 439f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson private ContentHandler m_contentHandler = null; 449f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson 459f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson /** DomHelper for this TreeWalker */ 469f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson protected DTM m_dtm; 479f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson 489f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson /** 499f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * Set the DTM to be traversed. 509f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * 519f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * @param dtm The Document Table Model to be used. 529f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson */ 539f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson public void setDTM(DTM dtm) 549f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson { 559f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson m_dtm = dtm; 569f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson } 579f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson 589f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson /** 599f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * Get the ContentHandler used for the tree walk. 609f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * 619f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * @return the ContentHandler used for the tree walk 629f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson */ 639f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson public ContentHandler getcontentHandler() 649f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson { 659f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson return m_contentHandler; 669f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson } 679f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson 689f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson /** 699f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * Set the ContentHandler used for the tree walk. 709f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * 719f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * @param ch the ContentHandler to be the result of the tree walk. 729f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson */ 739f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson public void setcontentHandler(ContentHandler ch) 749f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson { 759f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson m_contentHandler = ch; 769f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson } 779f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson 789f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson 799f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson /** 809f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * Constructor. 819f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson */ 829f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson public DTMTreeWalker() 839f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson { 849f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson } 859f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson 869f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson /** 879f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * Constructor. 889f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * @param contentHandler The implemention of the 899f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * contentHandler operation (toXMLString, digest, ...) 909f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson */ 919f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson public DTMTreeWalker(ContentHandler contentHandler, DTM dtm) 929f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson { 939f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson this.m_contentHandler = contentHandler; 949f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson m_dtm = dtm; 959f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson } 969f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson 979f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson /** Perform a non-recursive pre-order/post-order traversal, 989f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * operating as a Visitor. startNode (preorder) and endNode 999f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * (postorder) are invoked for each node as we traverse over them, 1009f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * with the result that the node is written out to m_contentHandler. 1019f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * 1029f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * @param pos Node in the tree at which to start (and end) traversal -- 1039f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * in other words, the root of the subtree to traverse over. 1049f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * 1059f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * @throws TransformerException */ 1069f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson public void traverse(int pos) throws org.xml.sax.SAXException 1079f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson { 1089f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson // %REVIEW% Why isn't this just traverse(pos,pos)? 1099f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson 1109f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson int top = pos; // Remember the root of this subtree 1119f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson 1129f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson while (DTM.NULL != pos) 1139f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson { 1149f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson startNode(pos); 1159f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson int nextNode = m_dtm.getFirstChild(pos); 1169f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson while (DTM.NULL == nextNode) 1179f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson { 1189f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson endNode(pos); 1199f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson 1209f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson if (top == pos) 1219f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson break; 1229f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson 1239f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson nextNode = m_dtm.getNextSibling(pos); 1249f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson 1259f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson if (DTM.NULL == nextNode) 1269f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson { 1279f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson pos = m_dtm.getParent(pos); 1289f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson 1299f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson if ((DTM.NULL == pos) || (top == pos)) 1309f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson { 1319f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson // %REVIEW% This condition isn't tested in traverse(pos,top) 1329f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson // -- bug? 1339f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson if (DTM.NULL != pos) 1349f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson endNode(pos); 1359f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson 1369f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson nextNode = DTM.NULL; 1379f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson 1389f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson break; 1399f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson } 1409f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson } 1419f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson } 1429f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson 1439f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson pos = nextNode; 1449f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson } 1459f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson } 1469f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson 1479f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson /** Perform a non-recursive pre-order/post-order traversal, 1489f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * operating as a Visitor. startNode (preorder) and endNode 1499f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * (postorder) are invoked for each node as we traverse over them, 1509f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * with the result that the node is written out to m_contentHandler. 1519f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * 1529f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * @param pos Node in the tree where to start traversal 1539f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * @param top Node in the tree where to end traversal. 1549f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * If top==DTM.NULL, run through end of document. 1559f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * 1569f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * @throws TransformerException 1579f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson */ 1589f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson public void traverse(int pos, int top) throws org.xml.sax.SAXException 1599f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson { 1609f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson // %OPT% Can we simplify the loop conditionals by adding: 1619f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson // if(top==DTM.NULL) top=0 1629f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson // -- or by simply ignoring this case and relying on the fact that 1639f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson // pos will never equal DTM.NULL until we're ready to exit? 1649f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson 1659f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson while (DTM.NULL != pos) 1669f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson { 1679f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson startNode(pos); 1689f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson int nextNode = m_dtm.getFirstChild(pos); 1699f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson while (DTM.NULL == nextNode) 1709f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson { 1719f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson endNode(pos); 1729f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson 1739f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson if ((DTM.NULL != top) && top == pos) 1749f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson break; 1759f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson 1769f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson nextNode = m_dtm.getNextSibling(pos); 1779f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson 1789f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson if (DTM.NULL == nextNode) 1799f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson { 1809f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson pos = m_dtm.getParent(pos); 1819f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson 1829f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson if ((DTM.NULL == pos) || ((DTM.NULL != top) && (top == pos))) 1839f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson { 1849f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson nextNode = DTM.NULL; 1859f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson 1869f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson break; 1879f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson } 1889f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson } 1899f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson } 1909f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson 1919f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson pos = nextNode; 1929f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson } 1939f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson } 1949f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson 1959f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson /** Flag indicating whether following text to be processed is raw text */ 1969f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson boolean nextIsRaw = false; 1979f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson 1989f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson /** 1999f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * Optimized dispatch of characters. 2009f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson */ 2019f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson private final void dispatachChars(int node) 2029f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson throws org.xml.sax.SAXException 2039f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson { 2049f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson m_dtm.dispatchCharactersEvents(node, m_contentHandler, false); 2059f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson } 2069f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson 2079f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson /** 2089f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * Start processing given node 2099f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * 2109f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * 2119f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * @param node Node to process 2129f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * 2139f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * @throws org.xml.sax.SAXException 2149f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson */ 2159f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson protected void startNode(int node) throws org.xml.sax.SAXException 2169f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson { 2179f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson 2189f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson if (m_contentHandler instanceof NodeConsumer) 2199f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson { 2209f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson // %TBD% 2219f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson// ((NodeConsumer) m_contentHandler).setOriginatingNode(node); 2229f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson } 2239f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson 2249f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson switch (m_dtm.getNodeType(node)) 2259f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson { 2269f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson case DTM.COMMENT_NODE : 2279f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson { 2289f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson XMLString data = m_dtm.getStringValue(node); 2299f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson 2309f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson if (m_contentHandler instanceof LexicalHandler) 2319f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson { 2329f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson LexicalHandler lh = ((LexicalHandler) this.m_contentHandler); 2339f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson data.dispatchAsComment(lh); 2349f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson } 2359f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson } 2369f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson break; 2379f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson case DTM.DOCUMENT_FRAGMENT_NODE : 2389f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson 2399f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson // ??; 2409f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson break; 2419f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson case DTM.DOCUMENT_NODE : 2429f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson this.m_contentHandler.startDocument(); 2439f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson break; 2449f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson case DTM.ELEMENT_NODE : 2459f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson DTM dtm = m_dtm; 2469f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson 2479f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson for (int nsn = dtm.getFirstNamespaceNode(node, true); DTM.NULL != nsn; 2489f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson nsn = dtm.getNextNamespaceNode(node, nsn, true)) 2499f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson { 2509f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson // String prefix = dtm.getPrefix(nsn); 2519f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson String prefix = dtm.getNodeNameX(nsn); 2529f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson 2539f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson this.m_contentHandler.startPrefixMapping(prefix, dtm.getNodeValue(nsn)); 2549f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson 2559f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson } 2569f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson 2579f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson // System.out.println("m_dh.getNamespaceOfNode(node): "+m_dh.getNamespaceOfNode(node)); 2589f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson // System.out.println("m_dh.getLocalNameOfNode(node): "+m_dh.getLocalNameOfNode(node)); 2599f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson String ns = dtm.getNamespaceURI(node); 2609f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson if(null == ns) 2619f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson ns = ""; 2629f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson 2639f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson // %OPT% !! 2649f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson org.xml.sax.helpers.AttributesImpl attrs = 2659f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson new org.xml.sax.helpers.AttributesImpl(); 2669f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson 2679f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson for (int i = dtm.getFirstAttribute(node); 2689f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson i != DTM.NULL; 2699f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson i = dtm.getNextAttribute(i)) 2709f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson { 2719f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson attrs.addAttribute(dtm.getNamespaceURI(i), 2729f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson dtm.getLocalName(i), 2739f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson dtm.getNodeName(i), 2749f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson "CDATA", 2759f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson dtm.getNodeValue(i)); 2769f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson } 2779f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson 2789f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson 2799f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson this.m_contentHandler.startElement(ns, 2809f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson m_dtm.getLocalName(node), 2819f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson m_dtm.getNodeName(node), 2829f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson attrs); 2839f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson break; 2849f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson case DTM.PROCESSING_INSTRUCTION_NODE : 2859f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson { 2869f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson String name = m_dtm.getNodeName(node); 2879f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson 2889f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson // String data = pi.getData(); 2899f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson if (name.equals("xslt-next-is-raw")) 2909f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson { 2919f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson nextIsRaw = true; 2929f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson } 2939f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson else 2949f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson { 2959f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson this.m_contentHandler.processingInstruction(name, 2969f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson m_dtm.getNodeValue(node)); 2979f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson } 2989f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson } 2999f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson break; 3009f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson case DTM.CDATA_SECTION_NODE : 3019f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson { 3029f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson boolean isLexH = (m_contentHandler instanceof LexicalHandler); 3039f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson LexicalHandler lh = isLexH 3049f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson ? ((LexicalHandler) this.m_contentHandler) : null; 3059f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson 3069f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson if (isLexH) 3079f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson { 3089f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson lh.startCDATA(); 3099f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson } 3109f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson 3119f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson dispatachChars(node); 3129f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson 3139f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson { 3149f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson if (isLexH) 3159f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson { 3169f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson lh.endCDATA(); 3179f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson } 3189f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson } 3199f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson } 3209f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson break; 3219f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson case DTM.TEXT_NODE : 3229f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson { 3239f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson if (nextIsRaw) 3249f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson { 3259f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson nextIsRaw = false; 3269f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson 3279f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson m_contentHandler.processingInstruction(javax.xml.transform.Result.PI_DISABLE_OUTPUT_ESCAPING, ""); 3289f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson dispatachChars(node); 3299f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson m_contentHandler.processingInstruction(javax.xml.transform.Result.PI_ENABLE_OUTPUT_ESCAPING, ""); 3309f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson } 3319f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson else 3329f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson { 3339f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson dispatachChars(node); 3349f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson } 3359f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson } 3369f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson break; 3379f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson case DTM.ENTITY_REFERENCE_NODE : 3389f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson { 3399f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson if (m_contentHandler instanceof LexicalHandler) 3409f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson { 3419f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson ((LexicalHandler) this.m_contentHandler).startEntity( 3429f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson m_dtm.getNodeName(node)); 3439f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson } 3449f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson else 3459f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson { 3469f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson 3479f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson // warning("Can not output entity to a pure SAX ContentHandler"); 3489f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson } 3499f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson } 3509f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson break; 3519f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson default : 3529f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson } 3539f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson } 3549f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson 3559f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson /** 3569f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * End processing of given node 3579f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * 3589f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * 3599f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * @param node Node we just finished processing 3609f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * 3619f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * @throws org.xml.sax.SAXException 3629f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson */ 3639f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson protected void endNode(int node) throws org.xml.sax.SAXException 3649f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson { 3659f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson 3669f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson switch (m_dtm.getNodeType(node)) 3679f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson { 3689f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson case DTM.DOCUMENT_NODE : 3699f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson this.m_contentHandler.endDocument(); 3709f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson break; 3719f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson case DTM.ELEMENT_NODE : 3729f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson String ns = m_dtm.getNamespaceURI(node); 3739f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson if(null == ns) 3749f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson ns = ""; 3759f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson this.m_contentHandler.endElement(ns, 3769f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson m_dtm.getLocalName(node), 3779f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson m_dtm.getNodeName(node)); 3789f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson 3799f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson for (int nsn = m_dtm.getFirstNamespaceNode(node, true); DTM.NULL != nsn; 3809f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson nsn = m_dtm.getNextNamespaceNode(node, nsn, true)) 3819f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson { 3829f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson // String prefix = m_dtm.getPrefix(nsn); 3839f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson String prefix = m_dtm.getNodeNameX(nsn); 3849f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson 3859f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson this.m_contentHandler.endPrefixMapping(prefix); 3869f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson } 3879f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson break; 3889f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson case DTM.CDATA_SECTION_NODE : 3899f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson break; 3909f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson case DTM.ENTITY_REFERENCE_NODE : 3919f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson { 3929f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson if (m_contentHandler instanceof LexicalHandler) 3939f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson { 3949f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson LexicalHandler lh = ((LexicalHandler) this.m_contentHandler); 3959f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson 3969f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson lh.endEntity(m_dtm.getNodeName(node)); 3979f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson } 3989f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson } 3999f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson break; 4009f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson default : 4019f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson } 4029f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson } 4039f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson} //TreeWalker 4049f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson 405