KickJava   Java API By Example, From Geeks To Geeks.

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


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: ElemPI.java,v 1.19 2004/02/24 03:55:47 zongaro 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.utils.XMLChar;
26 import org.apache.xpath.XPathContext;
27
28 /**
29  * Implement xsl:processing-instruction.
30  * <pre>
31  * <!ELEMENT xsl:processing-instruction %char-template;>
32  * <!ATTLIST xsl:processing-instruction
33  * name %avt; #REQUIRED
34  * %space-att;
35  * >
36  * </pre>
37  * @see <a HREF="http://www.w3.org/TR/xslt#section-Creating-Processing-Instructions">section-Creating-Processing-Instructions in XSLT Specification</a>
38  * @xsl.usage advanced
39  */

40 public class ElemPI extends ElemTemplateElement
41 {
42
43   /**
44    * The xsl:processing-instruction element has a required name
45    * attribute that specifies the name of the processing instruction node.
46    * The value of the name attribute is interpreted as an
47    * attribute value template.
48    * @serial
49    */

50   private AVT m_name_atv = null;
51
52   /**
53    * Set the "name" attribute.
54    * DJD
55    *
56    * @param v Value for the name attribute
57    */

58   public void setName(AVT v)
59   {
60     m_name_atv = v;
61   }
62
63   /**
64    * Get the "name" attribute.
65    * DJD
66    *
67    * @return The value of the "name" attribute
68    */

69   public AVT getName()
70   {
71     return m_name_atv;
72   }
73   
74   /**
75    * This function is called after everything else has been
76    * recomposed, and allows the template to set remaining
77    * values that may be based on some other property that
78    * depends on recomposition.
79    */

80   public void compose(StylesheetRoot sroot) throws TransformerException JavaDoc
81   {
82     super.compose(sroot);
83     java.util.Vector JavaDoc vnames = sroot.getComposeState().getVariableNames();
84     if(null != m_name_atv)
85       m_name_atv.fixupVariables(vnames, sroot.getComposeState().getGlobalsSize());
86   }
87
88
89
90   /**
91    * Get an int constant identifying the type of element.
92    * @see org.apache.xalan.templates.Constants
93    *
94    * @return The token ID for the element
95    */

96   public int getXSLToken()
97   {
98     return Constants.ELEMNAME_PI;
99   }
100
101   /**
102    * Return the node name.
103    *
104    * @return The element's name
105    */

106   public String JavaDoc getNodeName()
107   {
108     return Constants.ELEMNAME_PI_STRING;
109   }
110
111   /**
112    * Create a processing instruction in the result tree.
113    * The content of the xsl:processing-instruction element is a
114    * template for the string-value of the processing instruction node.
115    * @see <a HREF="http://www.w3.org/TR/xslt#section-Creating-Processing-Instructions">section-Creating-Processing-Instructions in XSLT Specification</a>
116    *
117    * @param transformer non-null reference to the the current transform-time state.
118    * @param sourceNode non-null reference to the <a HREF="http://www.w3.org/TR/xslt#dt-current-node">current source node</a>.
119    * @param mode reference, which may be null, to the <a HREF="http://www.w3.org/TR/xslt#modes">current mode</a>.
120    *
121    * @throws TransformerException
122    */

123   public void execute(
124           TransformerImpl transformer)
125             throws TransformerException JavaDoc
126   {
127
128     if (TransformerImpl.S_DEBUG)
129       transformer.getTraceManager().fireTraceEvent(this);
130
131     XPathContext xctxt = transformer.getXPathContext();
132     int sourceNode = xctxt.getCurrentNode();
133     
134     String JavaDoc piName = m_name_atv == null ? null : m_name_atv.evaluate(xctxt, sourceNode, this);
135     
136     // Ignore processing instruction if name is null
137
if (piName == null) return;
138
139     if (piName.equalsIgnoreCase("xml"))
140     {
141         transformer.getMsgMgr().warn(
142         this, XSLTErrorResources.WG_PROCESSINGINSTRUCTION_NAME_CANT_BE_XML,
143               new Object JavaDoc[]{ Constants.ATTRNAME_NAME, piName });
144         return;
145     }
146     
147     // Only check if an avt was used (ie. this wasn't checked at compose time.)
148
// Ignore processing instruction, if invalid
149
else if ((!m_name_atv.isSimple()) && (!XMLChar.isValidNCName(piName)))
150     {
151         transformer.getMsgMgr().warn(
152         this, XSLTErrorResources.WG_PROCESSINGINSTRUCTION_NOTVALID_NCNAME,
153               new Object JavaDoc[]{ Constants.ATTRNAME_NAME, piName });
154         return;
155     }
156
157     // Note the content model is:
158
// <!ENTITY % instructions "
159
// %char-instructions;
160
// | xsl:processing-instruction
161
// | xsl:comment
162
// | xsl:element
163
// | xsl:attribute
164
// ">
165
String JavaDoc data = transformer.transformToString(this);
166
167     try
168     {
169       transformer.getResultTreeHandler().processingInstruction(piName, data);
170     }
171     catch(org.xml.sax.SAXException JavaDoc se)
172     {
173       throw new TransformerException JavaDoc(se);
174     }
175     
176     if (TransformerImpl.S_DEBUG)
177       transformer.getTraceManager().fireTraceEndEvent(this);
178   }
179
180   /**
181    * Add a child to the child list.
182    *
183    * @param newChild Child to add to child list
184    *
185    * @return The child just added to the child list
186    *
187    * @throws DOMException
188    */

189   public ElemTemplateElement appendChild(ElemTemplateElement newChild)
190   {
191
192     int type = ((ElemTemplateElement) newChild).getXSLToken();
193
194     switch (type)
195     {
196
197     // char-instructions
198
case Constants.ELEMNAME_TEXTLITERALRESULT :
199     case Constants.ELEMNAME_APPLY_TEMPLATES :
200     case Constants.ELEMNAME_APPLY_IMPORTS :
201     case Constants.ELEMNAME_CALLTEMPLATE :
202     case Constants.ELEMNAME_FOREACH :
203     case Constants.ELEMNAME_VALUEOF :
204     case Constants.ELEMNAME_COPY_OF :
205     case Constants.ELEMNAME_NUMBER :
206     case Constants.ELEMNAME_CHOOSE :
207     case Constants.ELEMNAME_IF :
208     case Constants.ELEMNAME_TEXT :
209     case Constants.ELEMNAME_COPY :
210     case Constants.ELEMNAME_VARIABLE :
211     case Constants.ELEMNAME_MESSAGE :
212
213       // instructions
214
// case Constants.ELEMNAME_PI:
215
// case Constants.ELEMNAME_COMMENT:
216
// case Constants.ELEMNAME_ELEMENT:
217
// case Constants.ELEMNAME_ATTRIBUTE:
218
break;
219     default :
220       error(XSLTErrorResources.ER_CANNOT_ADD,
221             new Object JavaDoc[]{ newChild.getNodeName(),
222                           this.getNodeName() }); //"Can not add " +((ElemTemplateElement)newChild).m_elemName +
223

224     //" to " + this.m_elemName);
225
}
226
227     return super.appendChild(newChild);
228   }
229 }
230
Popular Tags