KickJava   Java API By Example, From Geeks To Geeks.

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


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: ProcessorKey.java,v 1.13 2004/02/11 18:15:51 minchau Exp $
18  */

19 package org.apache.xalan.processor;
20
21 import java.util.Vector JavaDoc;
22
23 import org.apache.xalan.res.XSLMessages;
24 import org.apache.xalan.res.XSLTErrorResources;
25 import org.apache.xalan.templates.KeyDeclaration;
26
27 import org.xml.sax.Attributes JavaDoc;
28
29 /**
30  * TransformerFactory for xsl:key markup.
31  * <pre>
32  * <!ELEMENT xsl:key EMPTY>
33  * <!ATTLIST xsl:key
34  * name %qname; #REQUIRED
35  * match %pattern; #REQUIRED
36  * use %expr; #REQUIRED
37  * >
38  * </pre>
39  * @see <a HREF="http://www.w3.org/TR/xslt#dtd">XSLT DTD</a>
40  * @see <a HREF="http://www.w3.org/TR/xslt#key">key in XSLT Specification</a>
41  */

42 class ProcessorKey extends XSLTElementProcessor
43 {
44
45   /**
46    * Receive notification of the start of an xsl:key element.
47    *
48    * @param handler The calling StylesheetHandler/TemplatesBuilder.
49    * @param uri The Namespace URI, or the empty string if the
50    * element has no Namespace URI or if Namespace
51    * processing is not being performed.
52    * @param localName The local name (without prefix), or the
53    * empty string if Namespace processing is not being
54    * performed.
55    * @param rawName The raw XML 1.0 name (with prefix), or the
56    * empty string if raw names are not available.
57    * @param attributes The attributes attached to the element. If
58    * there are no attributes, it shall be an empty
59    * Attributes object.
60    */

61   public void startElement(
62           StylesheetHandler handler, String JavaDoc uri, String JavaDoc localName, String JavaDoc rawName, Attributes JavaDoc attributes)
63             throws org.xml.sax.SAXException JavaDoc
64   {
65
66     KeyDeclaration kd = new KeyDeclaration(handler.getStylesheet(), handler.nextUid());
67
68     kd.setDOMBackPointer(handler.getOriginatingNode());
69     kd.setLocaterInfo(handler.getLocator());
70     setPropertiesFromAttributes(handler, rawName, attributes, kd);
71     handler.getStylesheet().setKey(kd);
72   }
73
74   /**
75    * Set the properties of an object from the given attribute list.
76    * @param handler The stylesheet's Content handler, needed for
77    * error reporting.
78    * @param rawName The raw name of the owner element, needed for
79    * error reporting.
80    * @param attributes The list of attributes.
81    * @param target The target element where the properties will be set.
82    */

83   void setPropertiesFromAttributes(
84           StylesheetHandler handler, String JavaDoc rawName, Attributes JavaDoc attributes,
85           org.apache.xalan.templates.ElemTemplateElement target)
86             throws org.xml.sax.SAXException JavaDoc
87   {
88
89     XSLTElementDef def = getElemDef();
90
91     // Keep track of which XSLTAttributeDefs have been processed, so
92
// I can see which default values need to be set.
93
Vector JavaDoc processedDefs = new Vector JavaDoc();
94     int nAttrs = attributes.getLength();
95
96     for (int i = 0; i < nAttrs; i++)
97     {
98       String JavaDoc attrUri = attributes.getURI(i);
99       String JavaDoc attrLocalName = attributes.getLocalName(i);
100       XSLTAttributeDef attrDef = def.getAttributeDef(attrUri, attrLocalName);
101
102       if (null == attrDef)
103       {
104
105         // Then barf, because this element does not allow this attribute.
106
handler.error(attributes.getQName(i)
107                       + "attribute is not allowed on the " + rawName
108                       + " element!", null);
109       }
110       else
111       {
112         String JavaDoc valueString = attributes.getValue(i);
113
114         if (valueString.indexOf(org.apache.xpath.compiler.Keywords.FUNC_KEY_STRING
115                                 + "(") >= 0)
116           handler.error(
117             XSLMessages.createMessage(
118             XSLTErrorResources.ER_INVALID_KEY_CALL, null), null);
119
120         processedDefs.addElement(attrDef);
121         attrDef.setAttrValue(handler, attrUri, attrLocalName,
122                              attributes.getQName(i), attributes.getValue(i),
123                              target);
124       }
125     }
126
127     XSLTAttributeDef[] attrDefs = def.getAttributes();
128     int nAttrDefs = attrDefs.length;
129
130     for (int i = 0; i < nAttrDefs; i++)
131     {
132       XSLTAttributeDef attrDef = attrDefs[i];
133       String JavaDoc defVal = attrDef.getDefault();
134
135       if (null != defVal)
136       {
137         if (!processedDefs.contains(attrDef))
138         {
139           attrDef.setDefAttrValue(handler, target);
140         }
141       }
142
143       if (attrDef.getRequired())
144       {
145         if (!processedDefs.contains(attrDef))
146           handler.error(
147             XSLMessages.createMessage(
148               XSLTErrorResources.ER_REQUIRES_ATTRIB, new Object JavaDoc[]{ rawName,
149                                                                    attrDef.getName() }), null);
150       }
151     }
152   }
153 }
154
Popular Tags