KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > xalan > processor > ProcessorCharacters


1 /*
2  * Copyright 1999-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 /*
17  * $Id: ProcessorCharacters.java,v 1.12 2004/02/11 18:15:51 minchau Exp $
18  */

19 package org.apache.xalan.processor;
20
21 import javax.xml.transform.TransformerException JavaDoc;
22
23 import org.apache.xalan.templates.ElemTemplateElement;
24 import org.apache.xalan.templates.ElemText;
25 import org.apache.xalan.templates.ElemTextLiteral;
26 import org.apache.xml.utils.XMLCharacterRecognizer;
27
28 import org.w3c.dom.Node JavaDoc;
29
30 /**
31  * This class processes character events for a XSLT template element.
32  * @see <a HREF="http://www.w3.org/TR/xslt#dtd">XSLT DTD</a>
33  * @see <a HREF="http://www.w3.org/TR/xslt#section-Creating-the-Result-Tree">section-Creating-the-Result-Tree in XSLT Specification</a>
34  */

35 public class ProcessorCharacters extends XSLTElementProcessor
36 {
37
38   /**
39    * Receive notification of the start of the non-text event. This
40    * is sent to the current processor when any non-text event occurs.
41    *
42    * @param handler non-null reference to current StylesheetHandler that is constructing the Templates.
43    */

44   public void startNonText(StylesheetHandler handler) throws org.xml.sax.SAXException JavaDoc
45   {
46     if (this == handler.getCurrentProcessor())
47     {
48       handler.popProcessor();
49     }
50
51     int nChars = m_accumulator.length();
52
53     if ((nChars > 0)
54             && ((null != m_xslTextElement)
55                 ||!XMLCharacterRecognizer.isWhiteSpace(m_accumulator))
56                 || handler.isSpacePreserve())
57     {
58       ElemTextLiteral elem = new ElemTextLiteral();
59
60       elem.setDOMBackPointer(m_firstBackPointer);
61       elem.setLocaterInfo(handler.getLocator());
62       try
63       {
64         elem.setPrefixes(handler.getNamespaceSupport());
65       }
66       catch(TransformerException JavaDoc te)
67       {
68         throw new org.xml.sax.SAXException JavaDoc(te);
69       }
70
71       boolean doe = (null != m_xslTextElement)
72                     ? m_xslTextElement.getDisableOutputEscaping() : false;
73
74       elem.setDisableOutputEscaping(doe);
75       elem.setPreserveSpace(true);
76
77       char[] chars = new char[nChars];
78
79       m_accumulator.getChars(0, nChars, chars, 0);
80       elem.setChars(chars);
81
82       ElemTemplateElement parent = handler.getElemTemplateElement();
83
84       parent.appendChild(elem);
85     }
86
87     m_accumulator.setLength(0);
88     m_firstBackPointer = null;
89   }
90   
91   protected Node JavaDoc m_firstBackPointer = null;
92
93   /**
94    * Receive notification of character data inside an element.
95    *
96    *
97    * @param handler non-null reference to current StylesheetHandler that is constructing the Templates.
98    * @param ch The characters.
99    * @param start The start position in the character array.
100    * @param length The number of characters to use from the
101    * character array.
102    * @throws org.xml.sax.SAXException Any SAX exception, possibly
103    * wrapping another exception.
104    * @see org.xml.sax.ContentHandler#characters
105    */

106   public void characters(
107           StylesheetHandler handler, char ch[], int start, int length)
108             throws org.xml.sax.SAXException JavaDoc
109   {
110
111     m_accumulator.append(ch, start, length);
112     
113     if(null == m_firstBackPointer)
114       m_firstBackPointer = handler.getOriginatingNode();
115
116     // Catch all events until a non-character event.
117
if (this != handler.getCurrentProcessor())
118       handler.pushProcessor(this);
119   }
120
121   /**
122    * Receive notification of the end of an element.
123    *
124    * @param handler The calling StylesheetHandler/TemplatesBuilder.
125    * @param uri The Namespace URI, or the empty string if the
126    * element has no Namespace URI or if Namespace
127    * processing is not being performed.
128    * @param localName The local name (without prefix), or the
129    * empty string if Namespace processing is not being
130    * performed.
131    * @param rawName The raw XML 1.0 name (with prefix), or the
132    * empty string if raw names are not available.
133    * @param atts The attributes attached to the element. If
134    * there are no attributes, it shall be an empty
135    * Attributes object.
136    * @see org.apache.xalan.processor.StylesheetHandler#startElement
137    * @see org.apache.xalan.processor.StylesheetHandler#endElement
138    * @see org.xml.sax.ContentHandler#startElement
139    * @see org.xml.sax.ContentHandler#endElement
140    * @see org.xml.sax.Attributes
141    */

142   public void endElement(
143           StylesheetHandler handler, String JavaDoc uri, String JavaDoc localName, String JavaDoc rawName)
144             throws org.xml.sax.SAXException JavaDoc
145   {
146
147     // Since this has been installed as the current processor, we
148
// may get and end element event, in which case, we pop and clear
149
// and then call the real element processor.
150
startNonText(handler);
151     handler.getCurrentProcessor().endElement(handler, uri, localName,
152                                              rawName);
153     handler.popProcessor();
154   }
155
156   /**
157    * Accumulate characters, until a non-whitespace event has
158    * occured.
159    */

160   private StringBuffer JavaDoc m_accumulator = new StringBuffer JavaDoc();
161
162   /**
163    * The xsl:text processor will call this to set a
164    * preserve space state.
165    */

166   private ElemText m_xslTextElement;
167
168   /**
169    * Set the current setXslTextElement. The xsl:text
170    * processor will call this to set a preserve space state.
171    *
172    * @param xslTextElement The current xslTextElement that
173    * is preserving state, or null.
174    */

175   void setXslTextElement(ElemText xslTextElement)
176   {
177     m_xslTextElement = xslTextElement;
178   }
179 }
180
Popular Tags