1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one
3  * or more contributor license agreements. See the NOTICE file
4  * distributed with this work for additional information
5  * regarding copyright ownership. The ASF licenses this file
6  * to you under the Apache License, Version 2.0 (the  "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  *     http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */
18 /*
19  * $Id: ElemTextLiteral.java 468643 2006-10-28 06:56:03Z minchau $
20  */
21 package org.apache.xalan.templates;
22 
23 import javax.xml.transform.TransformerException;
24 
25 import org.apache.xalan.transformer.TransformerImpl;
26 import org.apache.xml.serializer.SerializationHandler;
27 import org.xml.sax.SAXException;
28 
29 /**
30  * Implement a text literal.
31  * @see <a href="http://www.w3.org/TR/xslt#section-Creating-Text">section-Creating-Text in XSLT Specification</a>
32  * @xsl.usage advanced
33  */
34 public class ElemTextLiteral extends ElemTemplateElement
35 {
36     static final long serialVersionUID = -7872620006767660088L;
37 
38   /**
39    * Tell if space should be preserved.
40    * @serial
41    */
42   private boolean m_preserveSpace;
43 
44   /**
45    * Set whether or not space should be preserved.
46    *
47    * @param v Boolean flag indicating whether
48    * or not space should be preserved
49    */
setPreserveSpace(boolean v)50   public void setPreserveSpace(boolean v)
51   {
52     m_preserveSpace = v;
53   }
54 
55   /**
56    * Get whether or not space should be preserved.
57    *
58    * @return Boolean flag indicating whether
59    * or not space should be preserved
60    */
getPreserveSpace()61   public boolean getPreserveSpace()
62   {
63     return m_preserveSpace;
64   }
65 
66   /**
67    * The character array.
68    * @serial
69    */
70   private char m_ch[];
71 
72   /**
73    * The character array as a string.
74    * @serial
75    */
76   private String m_str;
77 
78   /**
79    * Set the characters that will be output to the result tree..
80    *
81    * @param v Array of characters that will be output to the result tree
82    */
setChars(char[] v)83   public void setChars(char[] v)
84   {
85     m_ch = v;
86   }
87 
88   /**
89    * Get the characters that will be output to the result tree..
90    *
91    * @return Array of characters that will be output to the result tree
92    */
getChars()93   public char[] getChars()
94   {
95     return m_ch;
96   }
97 
98   /**
99    * Get the value of the node as a string.
100    *
101    * @return null
102    */
getNodeValue()103   public synchronized String getNodeValue()
104   {
105 
106     if(null == m_str)
107     {
108       m_str = new String(m_ch);
109     }
110 
111     return m_str;
112   }
113 
114 
115   /**
116    * Tells if this element should disable escaping.
117    * @serial
118    */
119   private boolean m_disableOutputEscaping = false;
120 
121   /**
122    * Set the "disable-output-escaping" attribute.
123    * Normally, the xml output method escapes & and < (and
124    * possibly other characters) when outputting text nodes.
125    * This ensures that the output is well-formed XML. However,
126    * it is sometimes convenient to be able to produce output
127    * that is almost, but not quite well-formed XML; for
128    * example, the output may include ill-formed sections
129    * which are intended to be transformed into well-formed
130    * XML by a subsequent non-XML aware process. For this reason,
131    * XSLT provides a mechanism for disabling output escaping.
132    * An xsl:value-of or xsl:text element may have a
133    * disable-output-escaping attribute; the allowed values
134    * are yes or no; the default is no; if the value is yes,
135    * then a text node generated by instantiating the xsl:value-of
136    * or xsl:text element should be output without any escaping.
137    * @see <a href="http://www.w3.org/TR/xslt#disable-output-escaping">disable-output-escaping in XSLT Specification</a>
138    *
139    * @param v Boolean value for "disable-output-escaping" attribute.
140    */
setDisableOutputEscaping(boolean v)141   public void setDisableOutputEscaping(boolean v)
142   {
143     m_disableOutputEscaping = v;
144   }
145 
146   /**
147    * Get the "disable-output-escaping" attribute.
148    * Normally, the xml output method escapes & and < (and
149    * possibly other characters) when outputting text nodes.
150    * This ensures that the output is well-formed XML. However,
151    * it is sometimes convenient to be able to produce output
152    * that is almost, but not quite well-formed XML; for
153    * example, the output may include ill-formed sections
154    * which are intended to be transformed into well-formed
155    * XML by a subsequent non-XML aware process. For this reason,
156    * XSLT provides a mechanism for disabling output escaping.
157    * An xsl:value-of or xsl:text element may have a
158    * disable-output-escaping attribute; the allowed values
159    * are yes or no; the default is no; if the value is yes,
160    * then a text node generated by instantiating the xsl:value-of
161    * or xsl:text element should be output without any escaping.
162    * @see <a href="http://www.w3.org/TR/xslt#disable-output-escaping">disable-output-escaping in XSLT Specification</a>
163    *
164    * @return Boolean value of "disable-output-escaping" attribute.
165    */
getDisableOutputEscaping()166   public boolean getDisableOutputEscaping()
167   {
168     return m_disableOutputEscaping;
169   }
170 
171   /**
172    * Get an integer representation of the element type.
173    *
174    * @return An integer representation of the element, defined in the
175    *     Constants class.
176    * @see org.apache.xalan.templates.Constants
177    */
getXSLToken()178   public int getXSLToken()
179   {
180     return Constants.ELEMNAME_TEXTLITERALRESULT;
181   }
182 
183   /**
184    * Return the node name.
185    *
186    * @return The element's name
187    */
getNodeName()188   public String getNodeName()
189   {
190     return "#Text";
191   }
192 
193   /**
194    * Copy the text literal to the result tree.
195    *
196    * @param transformer non-null reference to the the current transform-time state.
197    *
198    * @throws TransformerException
199    */
execute( TransformerImpl transformer)200   public void execute(
201           TransformerImpl transformer)
202             throws TransformerException
203   {
204     try
205     {
206       SerializationHandler rth = transformer.getResultTreeHandler();
207 
208         if (m_disableOutputEscaping)
209       {
210         rth.processingInstruction(javax.xml.transform.Result.PI_DISABLE_OUTPUT_ESCAPING, "");
211       }
212 
213       rth.characters(m_ch, 0, m_ch.length);
214 
215       if (m_disableOutputEscaping)
216       {
217         rth.processingInstruction(javax.xml.transform.Result.PI_ENABLE_OUTPUT_ESCAPING, "");
218       }
219     }
220     catch(SAXException se)
221     {
222       throw new TransformerException(se);
223     }
224   }
225 }
226