KickJava   Java API By Example, From Geeks To Geeks.

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


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: ElemAttribute.java,v 1.27 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.serializer.NamespaceMappings;
26 import org.apache.xml.serializer.SerializationHandler;
27 import org.apache.xml.utils.QName;
28 import org.apache.xml.utils.XMLChar;
29
30 import org.xml.sax.SAXException JavaDoc;
31
32 /**
33  * Implement xsl:attribute.
34  * <pre>
35  * &amp;!ELEMENT xsl:attribute %char-template;>
36  * &amp;!ATTLIST xsl:attribute
37  * name %avt; #REQUIRED
38  * namespace %avt; #IMPLIED
39  * %space-att;
40  * &amp;
41  * </pre>
42  * @see <a HREF="http://www.w3.org/TR/xslt#creating-attributes">creating-attributes in XSLT Specification</a>
43  * @xsl.usage advanced
44  */

45 public class ElemAttribute extends ElemElement
46 {
47
48   /**
49    * Get an int constant identifying the type of element.
50    * @see org.apache.xalan.templates.Constants
51    *
52    * @return The token ID for this element
53    */

54   public int getXSLToken()
55   {
56     return Constants.ELEMNAME_ATTRIBUTE;
57   }
58
59   /**
60    * Return the node name.
61    *
62    * @return The element name
63    */

64   public String JavaDoc getNodeName()
65   {
66     return Constants.ELEMNAME_ATTRIBUTE_STRING;
67   }
68
69   /**
70    * Create an attribute in the result tree.
71    * @see <a HREF="http://www.w3.org/TR/xslt#creating-attributes">creating-attributes in XSLT Specification</a>
72    *
73    * @param transformer non-null reference to the the current transform-time state.
74    *
75    * @throws TransformerException
76    */

77   public void execute(
78           TransformerImpl transformer)
79             throws TransformerException JavaDoc
80   {
81     SerializationHandler rhandler = transformer.getSerializationHandler();
82
83     // If they are trying to add an attribute when there isn't an
84
// element pending, it is an error.
85
// I don't think we need this check here because it is checked in
86
// ResultTreeHandler.addAttribute. (is)
87
// if (!rhandler.isElementPending())
88
// {
89
// // Make sure the trace event is sent.
90
// if (TransformerImpl.S_DEBUG)
91
// transformer.getTraceManager().fireTraceEvent(this);
92
//
93
// XPathContext xctxt = transformer.getXPathContext();
94
// int sourceNode = xctxt.getCurrentNode();
95
// String attrName = m_name_avt.evaluate(xctxt, sourceNode, this);
96
// transformer.getMsgMgr().warn(this,
97
// XSLTErrorResources.WG_ILLEGAL_ATTRIBUTE_POSITION,
98
// new Object[]{ attrName });
99
//
100
// if (TransformerImpl.S_DEBUG)
101
// transformer.getTraceManager().fireTraceEndEvent(this);
102
// return;
103
//
104
// // warn(templateChild, sourceNode, "Trying to add attribute after element child has been added, ignoring...");
105
// }
106

107     super.execute(transformer);
108     
109   }
110   
111   /**
112    * Resolve the namespace into a prefix. At this level, if no prefix exists,
113    * then return a manufactured prefix.
114    *
115    * @param rhandler The current result tree handler.
116    * @param prefix The probable prefix if already known.
117    * @param nodeNamespace The namespace, which should not be null.
118    *
119    * @return The prefix to be used.
120    */

121   protected String JavaDoc resolvePrefix(SerializationHandler rhandler,
122                                  String JavaDoc prefix, String JavaDoc nodeNamespace)
123     throws TransformerException JavaDoc
124   {
125
126     if (null != prefix && (prefix.length() == 0 || prefix.equals("xmlns")))
127     {
128       // Since we can't use default namespace, in this case we try and
129
// see if a prefix has already been defined or this namespace.
130
prefix = rhandler.getPrefix(nodeNamespace);
131
132       // System.out.println("nsPrefix: "+nsPrefix);
133
if (null == prefix || prefix.length() == 0 || prefix.equals("xmlns"))
134       {
135         if(nodeNamespace.length() > 0)
136         {
137             NamespaceMappings prefixMapping = rhandler.getNamespaceMappings();
138             prefix = prefixMapping.generateNextPrefix();
139         }
140         else
141           prefix = "";
142       }
143     }
144     return prefix;
145   }
146   
147   /**
148    * Validate that the node name is good.
149    *
150    * @param nodeName Name of the node being constructed, which may be null.
151    *
152    * @return true if the node name is valid, false otherwise.
153    */

154    protected boolean validateNodeName(String JavaDoc nodeName)
155    {
156       if(null == nodeName)
157         return false;
158       if(nodeName.equals("xmlns"))
159         return false;
160       return XMLChar.isValidQName(nodeName);
161    }
162   
163   /**
164    * Construct a node in the result tree. This method is overloaded by
165    * xsl:attribute. At this class level, this method creates an element.
166    *
167    * @param nodeName The name of the node, which may be null.
168    * @param prefix The prefix for the namespace, which may be null.
169    * @param nodeNamespace The namespace of the node, which may be null.
170    * @param transformer non-null reference to the the current transform-time state.
171    * @param sourceNode non-null reference to the <a HREF="http://www.w3.org/TR/xslt#dt-current-node">current source node</a>.
172    * @param mode reference, which may be null, to the <a HREF="http://www.w3.org/TR/xslt#modes">current mode</a>.
173    *
174    * @throws TransformerException
175    */

176   void constructNode(
177           String JavaDoc nodeName, String JavaDoc prefix, String JavaDoc nodeNamespace, TransformerImpl transformer)
178             throws TransformerException JavaDoc
179   {
180
181     if(null != nodeName && nodeName.length() > 0)
182     {
183       SerializationHandler rhandler = transformer.getSerializationHandler();
184       if(prefix != null && prefix.length() > 0)
185       {
186         try
187         {
188           rhandler.startPrefixMapping(prefix, nodeNamespace, false);
189         }
190         catch(SAXException JavaDoc se)
191         {
192           throw new TransformerException JavaDoc(se);
193         }
194       }
195       String JavaDoc val = transformer.transformToString(this);
196       String JavaDoc localName = QName.getLocalPart(nodeName);
197       try
198       {
199         if(prefix != null && prefix.length() > 0){
200             rhandler.addAttribute(nodeNamespace, localName, nodeName, "CDATA", val);
201         }else{
202             rhandler.addAttribute("", localName, nodeName, "CDATA", val);
203         }
204       }
205       catch (SAXException JavaDoc e)
206       {
207       }
208     }
209   }
210
211
212   /**
213    * Add a child to the child list.
214    * <!ELEMENT xsl:attribute %char-template;>
215    * <!ATTLIST xsl:attribute
216    * name %avt; #REQUIRED
217    * namespace %avt; #IMPLIED
218    * %space-att;
219    * >
220    *
221    * @param newChild Child to append to the list of this node's children
222    *
223    * @return The node we just appended to the children list
224    *
225    * @throws DOMException
226    */

227   public ElemTemplateElement appendChild(ElemTemplateElement newChild)
228   {
229
230     int type = ((ElemTemplateElement) newChild).getXSLToken();
231
232     switch (type)
233     {
234
235     // char-instructions
236
case Constants.ELEMNAME_TEXTLITERALRESULT :
237     case Constants.ELEMNAME_APPLY_TEMPLATES :
238     case Constants.ELEMNAME_APPLY_IMPORTS :
239     case Constants.ELEMNAME_CALLTEMPLATE :
240     case Constants.ELEMNAME_FOREACH :
241     case Constants.ELEMNAME_VALUEOF :
242     case Constants.ELEMNAME_COPY_OF :
243     case Constants.ELEMNAME_NUMBER :
244     case Constants.ELEMNAME_CHOOSE :
245     case Constants.ELEMNAME_IF :
246     case Constants.ELEMNAME_TEXT :
247     case Constants.ELEMNAME_COPY :
248     case Constants.ELEMNAME_VARIABLE :
249     case Constants.ELEMNAME_MESSAGE :
250
251       // instructions
252
// case Constants.ELEMNAME_PI:
253
// case Constants.ELEMNAME_COMMENT:
254
// case Constants.ELEMNAME_ELEMENT:
255
// case Constants.ELEMNAME_ATTRIBUTE:
256
break;
257     default :
258       error(XSLTErrorResources.ER_CANNOT_ADD,
259             new Object JavaDoc[]{ newChild.getNodeName(),
260                           this.getNodeName() }); //"Can not add " +((ElemTemplateElement)newChild).m_elemName +
261

262     //" to " + this.m_elemName);
263
}
264
265     return super.appendChild(newChild);
266   }
267     /**
268      * @see ElemElement#setName(AVT)
269      */

270     public void setName(AVT v) {
271         if (v.isSimple())
272         {
273             if (v.getSimpleString().equals("xmlns"))
274             {
275                 throw new IllegalArgumentException JavaDoc();
276             }
277         }
278         super.setName(v);
279     }
280
281 }
282
Popular Tags