14c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilson/* 24c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilson * Licensed to the Apache Software Foundation (ASF) under one 34c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilson * or more contributor license agreements. See the NOTICE file 44c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilson * distributed with this work for additional information 54c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilson * regarding copyright ownership. The ASF licenses this file 64c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilson * to you under the Apache License, Version 2.0 (the "License"); 74c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilson * you may not use this file except in compliance with the License. 84c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilson * You may obtain a copy of the License at 94c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilson * 104c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilson * http://www.apache.org/licenses/LICENSE-2.0 114c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilson * 124c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilson * Unless required by applicable law or agreed to in writing, software 134c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilson * distributed under the License is distributed on an "AS IS" BASIS, 144c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilson * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 154c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilson * See the License for the specific language governing permissions and 164c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilson * limitations under the License. 174c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilson */ 184c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilson/* 194c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilson * $Id: ProcessorCharacters.java 468640 2006-10-28 06:53:53Z minchau $ 204c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilson */ 214c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilsonpackage org.apache.xalan.processor; 224c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilson 234c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilsonimport javax.xml.transform.TransformerException; 244c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilson 254c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilsonimport org.apache.xalan.templates.ElemTemplateElement; 264c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilsonimport org.apache.xalan.templates.ElemText; 274c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilsonimport org.apache.xalan.templates.ElemTextLiteral; 284c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilsonimport org.apache.xml.utils.XMLCharacterRecognizer; 294c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilson 304c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilsonimport org.w3c.dom.Node; 314c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilson 324c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilson/** 334c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilson * This class processes character events for a XSLT template element. 344c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilson * @see <a href="http://www.w3.org/TR/xslt#dtd">XSLT DTD</a> 354c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilson * @see <a href="http://www.w3.org/TR/xslt#section-Creating-the-Result-Tree">section-Creating-the-Result-Tree in XSLT Specification</a> 364c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilson */ 374c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilsonpublic class ProcessorCharacters extends XSLTElementProcessor 384c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilson{ 394c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilson static final long serialVersionUID = 8632900007814162650L; 404c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilson 414c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilson /** 424c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilson * Receive notification of the start of the non-text event. This 434c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilson * is sent to the current processor when any non-text event occurs. 444c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilson * 454c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilson * @param handler non-null reference to current StylesheetHandler that is constructing the Templates. 464c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilson */ 474c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilson public void startNonText(StylesheetHandler handler) throws org.xml.sax.SAXException 484c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilson { 494c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilson if (this == handler.getCurrentProcessor()) 504c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilson { 514c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilson handler.popProcessor(); 524c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilson } 534c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilson 544c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilson int nChars = m_accumulator.length(); 554c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilson 564c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilson if ((nChars > 0) 574c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilson && ((null != m_xslTextElement) 584c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilson ||!XMLCharacterRecognizer.isWhiteSpace(m_accumulator)) 594c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilson || handler.isSpacePreserve()) 604c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilson { 614c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilson ElemTextLiteral elem = new ElemTextLiteral(); 624c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilson 634c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilson elem.setDOMBackPointer(m_firstBackPointer); 644c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilson elem.setLocaterInfo(handler.getLocator()); 654c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilson try 664c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilson { 674c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilson elem.setPrefixes(handler.getNamespaceSupport()); 684c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilson } 694c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilson catch(TransformerException te) 704c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilson { 714c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilson throw new org.xml.sax.SAXException(te); 724c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilson } 734c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilson 744c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilson boolean doe = (null != m_xslTextElement) 754c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilson ? m_xslTextElement.getDisableOutputEscaping() : false; 764c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilson 774c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilson elem.setDisableOutputEscaping(doe); 784c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilson elem.setPreserveSpace(true); 794c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilson 804c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilson char[] chars = new char[nChars]; 814c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilson 824c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilson m_accumulator.getChars(0, nChars, chars, 0); 834c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilson elem.setChars(chars); 844c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilson 854c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilson ElemTemplateElement parent = handler.getElemTemplateElement(); 864c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilson 874c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilson parent.appendChild(elem); 884c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilson } 894c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilson 904c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilson m_accumulator.setLength(0); 914c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilson m_firstBackPointer = null; 924c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilson } 934c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilson 944c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilson protected Node m_firstBackPointer = null; 954c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilson 964c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilson /** 974c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilson * Receive notification of character data inside an element. 984c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilson * 994c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilson * 1004c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilson * @param handler non-null reference to current StylesheetHandler that is constructing the Templates. 1014c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilson * @param ch The characters. 1024c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilson * @param start The start position in the character array. 1034c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilson * @param length The number of characters to use from the 1044c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilson * character array. 1054c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilson * @throws org.xml.sax.SAXException Any SAX exception, possibly 1064c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilson * wrapping another exception. 1074c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilson * @see org.xml.sax.ContentHandler#characters 1084c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilson */ 1094c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilson public void characters( 1104c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilson StylesheetHandler handler, char ch[], int start, int length) 1114c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilson throws org.xml.sax.SAXException 1124c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilson { 1134c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilson 1144c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilson m_accumulator.append(ch, start, length); 1154c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilson 1164c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilson if(null == m_firstBackPointer) 1174c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilson m_firstBackPointer = handler.getOriginatingNode(); 1184c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilson 1194c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilson // Catch all events until a non-character event. 1204c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilson if (this != handler.getCurrentProcessor()) 1214c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilson handler.pushProcessor(this); 1224c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilson } 1234c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilson 1244c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilson /** 1254c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilson * Receive notification of the end of an element. 1264c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilson * 1274c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilson * @param handler The calling StylesheetHandler/TemplatesBuilder. 1284c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilson * @param uri The Namespace URI, or the empty string if the 1294c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilson * element has no Namespace URI or if Namespace 1304c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilson * processing is not being performed. 1314c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilson * @param localName The local name (without prefix), or the 1324c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilson * empty string if Namespace processing is not being 1334c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilson * performed. 1344c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilson * @param rawName The raw XML 1.0 name (with prefix), or the 1354c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilson * empty string if raw names are not available. 1364c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilson * @see org.apache.xalan.processor.StylesheetHandler#startElement 1374c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilson * @see org.apache.xalan.processor.StylesheetHandler#endElement 1384c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilson * @see org.xml.sax.ContentHandler#startElement 1394c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilson * @see org.xml.sax.ContentHandler#endElement 1404c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilson * @see org.xml.sax.Attributes 1414c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilson */ 1424c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilson public void endElement( 1434c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilson StylesheetHandler handler, String uri, String localName, String rawName) 1444c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilson throws org.xml.sax.SAXException 1454c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilson { 1464c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilson 1474c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilson // Since this has been installed as the current processor, we 1484c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilson // may get and end element event, in which case, we pop and clear 1494c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilson // and then call the real element processor. 1504c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilson startNonText(handler); 1514c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilson handler.getCurrentProcessor().endElement(handler, uri, localName, 1524c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilson rawName); 1534c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilson handler.popProcessor(); 1544c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilson } 1554c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilson 1564c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilson /** 1574c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilson * Accumulate characters, until a non-whitespace event has 1584c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilson * occured. 1594c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilson */ 1604c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilson private StringBuffer m_accumulator = new StringBuffer(); 1614c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilson 1624c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilson /** 1634c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilson * The xsl:text processor will call this to set a 1644c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilson * preserve space state. 1654c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilson */ 1664c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilson private ElemText m_xslTextElement; 1674c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilson 1684c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilson /** 1694c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilson * Set the current setXslTextElement. The xsl:text 1704c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilson * processor will call this to set a preserve space state. 1714c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilson * 1724c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilson * @param xslTextElement The current xslTextElement that 1734c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilson * is preserving state, or null. 1744c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilson */ 1754c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilson void setXslTextElement(ElemText xslTextElement) 1764c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilson { 1774c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilson m_xslTextElement = xslTextElement; 1784c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilson } 1794c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilson} 180