KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > xalan > templates > ElemValueOf


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: ElemValueOf.java,v 1.24 2004/02/16 20:32:33 minchau Exp $
18  */

19 package org.apache.xalan.templates;
20
21 import javax.xml.transform.TransformerException JavaDoc;
22
23 import org.apache.xalan.res.XSLTErrorResources;
24 import org.apache.xalan.transformer.TransformerImpl;
25 import org.apache.xml.dtm.DTM;
26 import org.apache.xml.serializer.SerializationHandler;
27 import org.apache.xpath.Expression;
28 import org.apache.xpath.XPath;
29 import org.apache.xpath.XPathContext;
30 import org.apache.xpath.objects.XObject;
31 import org.xml.sax.SAXException JavaDoc;
32
33 /**
34  * Implement xsl:value-of.
35  * <pre>
36  * <!ELEMENT xsl:value-of EMPTY>
37  * <!ATTLIST xsl:value-of
38  * select %expr; #REQUIRED
39  * disable-output-escaping (yes|no) "no"
40  * >
41  * </pre>
42  * @see <a HREF="http://www.w3.org/TR/xslt#value-of">value-of in XSLT Specification</a>
43  * @xsl.usage advanced
44  */

45 public class ElemValueOf extends ElemTemplateElement
46 {
47
48   /**
49    * The select expression to be executed.
50    * @serial
51    */

52   private XPath m_selectExpression = null;
53
54   /**
55    * True if the pattern is a simple ".".
56    * @serial
57    */

58   private boolean m_isDot = false;
59
60   /**
61    * Set the "select" attribute.
62    * The required select attribute is an expression; this expression
63    * is evaluated and the resulting object is converted to a
64    * string as if by a call to the string function.
65    *
66    * @param v The value to set for the "select" attribute.
67    */

68   public void setSelect(XPath v)
69   {
70
71     if (null != v)
72     {
73       String JavaDoc s = v.getPatternString();
74
75       m_isDot = (null != s) && s.equals(".");
76     }
77
78     m_selectExpression = v;
79   }
80
81   /**
82    * Get the "select" attribute.
83    * The required select attribute is an expression; this expression
84    * is evaluated and the resulting object is converted to a
85    * string as if by a call to the string function.
86    *
87    * @return The value of the "select" attribute.
88    */

89   public XPath getSelect()
90   {
91     return m_selectExpression;
92   }
93
94   /**
95    * Tells if this element should disable escaping.
96    * @serial
97    */

98   private boolean m_disableOutputEscaping = false;
99
100   /**
101    * Set the "disable-output-escaping" attribute.
102    * Normally, the xml output method escapes & and < (and
103    * possibly other characters) when outputting text nodes.
104    * This ensures that the output is well-formed XML. However,
105    * it is sometimes convenient to be able to produce output
106    * that is almost, but not quite well-formed XML; for
107    * example, the output may include ill-formed sections
108    * which are intended to be transformed into well-formed
109    * XML by a subsequent non-XML aware process. For this reason,
110    * XSLT provides a mechanism for disabling output escaping.
111    * An xsl:value-of or xsl:text element may have a
112    * disable-output-escaping attribute; the allowed values
113    * are yes or no; the default is no; if the value is yes,
114    * then a text node generated by instantiating the xsl:value-of
115    * or xsl:text element should be output without any escaping.
116    * @see <a HREF="http://www.w3.org/TR/xslt#disable-output-escaping">disable-output-escaping in XSLT Specification</a>
117    *
118    * @param v The value to set for the "disable-output-escaping" attribute.
119    */

120   public void setDisableOutputEscaping(boolean v)
121   {
122     m_disableOutputEscaping = v;
123   }
124
125   /**
126    * Get the "disable-output-escaping" attribute.
127    * Normally, the xml output method escapes & and < (and
128    * possibly other characters) when outputting text nodes.
129    * This ensures that the output is well-formed XML. However,
130    * it is sometimes convenient to be able to produce output
131    * that is almost, but not quite well-formed XML; for
132    * example, the output may include ill-formed sections
133    * which are intended to be transformed into well-formed
134    * XML by a subsequent non-XML aware process. For this reason,
135    * XSLT provides a mechanism for disabling output escaping.
136    * An xsl:value-of or xsl:text element may have a
137    * disable-output-escaping attribute; the allowed values
138    * are yes or no; the default is no; if the value is yes,
139    * then a text node generated by instantiating the xsl:value-of
140    * or xsl:text element should be output without any escaping.
141    * @see <a HREF="http://www.w3.org/TR/xslt#disable-output-escaping">disable-output-escaping in XSLT Specification</a>
142    *
143    * @return The value of the "disable-output-escaping" attribute.
144    */

145   public boolean getDisableOutputEscaping()
146   {
147     return m_disableOutputEscaping;
148   }
149
150   /**
151    * Get an integer representation of the element type.
152    *
153    * @return An integer representation of the element, defined in the
154    * Constants class.
155    * @see org.apache.xalan.templates.Constants
156    */

157   public int getXSLToken()
158   {
159     return Constants.ELEMNAME_VALUEOF;
160   }
161
162   /**
163    * This function is called after everything else has been
164    * recomposed, and allows the template to set remaining
165    * values that may be based on some other property that
166    * depends on recomposition.
167    *
168    * NEEDSDOC @param sroot
169    *
170    * @throws TransformerException
171    */

172   public void compose(StylesheetRoot sroot) throws TransformerException JavaDoc
173   {
174
175     super.compose(sroot);
176
177     java.util.Vector JavaDoc vnames = sroot.getComposeState().getVariableNames();
178
179     if (null != m_selectExpression)
180       m_selectExpression.fixupVariables(
181         vnames, sroot.getComposeState().getGlobalsSize());
182   }
183
184   /**
185    * Return the node name.
186    *
187    * @return The node name
188    */

189   public String JavaDoc getNodeName()
190   {
191     return Constants.ELEMNAME_VALUEOF_STRING;
192   }
193
194   /**
195    * Execute the string expression and copy the text to the
196    * result tree.
197    * The required select attribute is an expression; this expression
198    * is evaluated and the resulting object is converted to a string
199    * as if by a call to the string function. The string specifies
200    * the string-value of the created text node. If the string is
201    * empty, no text node will be created. The created text node will
202    * be merged with any adjacent text nodes.
203    * @see <a HREF="http://www.w3.org/TR/xslt#value-of">value-of in XSLT Specification</a>
204    *
205    * @param transformer non-null reference to the the current transform-time state.
206    * @param sourceNode non-null reference to the <a HREF="http://www.w3.org/TR/xslt#dt-current-node">current source node</a>.
207    * @param mode reference, which may be null, to the <a HREF="http://www.w3.org/TR/xslt#modes">current mode</a>.
208    *
209    * @throws TransformerException
210    */

211   public void execute(TransformerImpl transformer) throws TransformerException JavaDoc
212   {
213
214     XPathContext xctxt = transformer.getXPathContext();
215     SerializationHandler rth = transformer.getResultTreeHandler();
216
217     if (TransformerImpl.S_DEBUG)
218       transformer.getTraceManager().fireTraceEvent(this);
219
220     try
221     {
222       // Optimize for "."
223
if (false && m_isDot && !TransformerImpl.S_DEBUG)
224       {
225         int child = xctxt.getCurrentNode();
226         DTM dtm = xctxt.getDTM(child);
227
228         xctxt.pushCurrentNode(child);
229
230         if (m_disableOutputEscaping)
231           rth.processingInstruction(
232             javax.xml.transform.Result.PI_DISABLE_OUTPUT_ESCAPING, "");
233
234         try
235         {
236           dtm.dispatchCharactersEvents(child, rth, false);
237         }
238         finally
239         {
240           if (m_disableOutputEscaping)
241             rth.processingInstruction(
242               javax.xml.transform.Result.PI_ENABLE_OUTPUT_ESCAPING, "");
243
244           xctxt.popCurrentNode();
245         }
246       }
247       else
248       {
249         xctxt.pushNamespaceContext(this);
250
251         int current = xctxt.getCurrentNode();
252
253         xctxt.pushCurrentNodeAndExpression(current, current);
254
255         if (m_disableOutputEscaping)
256           rth.processingInstruction(
257             javax.xml.transform.Result.PI_DISABLE_OUTPUT_ESCAPING, "");
258
259         try
260         {
261           Expression expr = m_selectExpression.getExpression();
262
263           if (TransformerImpl.S_DEBUG)
264           {
265             XObject obj = expr.execute(xctxt);
266
267             transformer.getTraceManager().fireSelectedEvent(current, this,
268                     "select", m_selectExpression, obj);
269             obj.dispatchCharactersEvents(rth);
270           }
271           else
272           {
273             expr.executeCharsToContentHandler(xctxt, rth);
274           }
275         }
276         finally
277         {
278           if (m_disableOutputEscaping)
279             rth.processingInstruction(
280               javax.xml.transform.Result.PI_ENABLE_OUTPUT_ESCAPING, "");
281
282           xctxt.popNamespaceContext();
283           xctxt.popCurrentNodeAndExpression();
284         }
285       }
286     }
287     catch (SAXException JavaDoc se)
288     {
289       throw new TransformerException JavaDoc(se);
290     }
291     catch (RuntimeException JavaDoc re) {
292         TransformerException JavaDoc te = new TransformerException JavaDoc(re);
293         te.setLocator(this);
294         throw te;
295     }
296     finally
297     {
298       if (TransformerImpl.S_DEBUG)
299         transformer.getTraceManager().fireTraceEndEvent(this);
300     }
301   }
302
303   /**
304    * Add a child to the child list.
305    *
306    * @param newChild Child to add to children list
307    *
308    * @return Child just added to children list
309    *
310    * @throws DOMException
311    */

312   public ElemTemplateElement appendChild(ElemTemplateElement newChild)
313   {
314
315     error(XSLTErrorResources.ER_CANNOT_ADD,
316           new Object JavaDoc[]{ newChild.getNodeName(),
317                         this.getNodeName() }); //"Can not add " +((ElemTemplateElement)newChild).m_elemName +
318

319     //" to " + this.m_elemName);
320
return null;
321   }
322   
323   /**
324    * Call the children visitors.
325    * @param visitor The visitor whose appropriate method will be called.
326    */

327   protected void callChildVisitors(XSLTVisitor visitor, boolean callAttrs)
328   {
329     if(callAttrs)
330         m_selectExpression.getExpression().callVisitors(m_selectExpression, visitor);
331     super.callChildVisitors(visitor, callAttrs);
332   }
333
334 }
335
Popular Tags