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: ProcessorKey.java 469688 2006-10-31 22:39:43Z minchau $
20 */
21package org.apache.xalan.processor;
22
23import java.util.ArrayList;
24import java.util.List;
25
26import org.apache.xalan.res.XSLMessages;
27import org.apache.xalan.res.XSLTErrorResources;
28import org.apache.xalan.templates.KeyDeclaration;
29import org.xml.sax.Attributes;
30
31/**
32 * TransformerFactory for xsl:key markup.
33 * <pre>
34 * <!ELEMENT xsl:key EMPTY>
35 * <!ATTLIST xsl:key
36 *   name %qname; #REQUIRED
37 *   match %pattern; #REQUIRED
38 *   use %expr; #REQUIRED
39 * >
40 * </pre>
41 * @see <a href="http://www.w3.org/TR/xslt#dtd">XSLT DTD</a>
42 * @see <a href="http://www.w3.org/TR/xslt#key">key in XSLT Specification</a>
43 */
44class ProcessorKey extends XSLTElementProcessor
45{
46    static final long serialVersionUID = 4285205417566822979L;
47
48  /**
49   * Receive notification of the start of an xsl:key element.
50   *
51   * @param handler The calling StylesheetHandler/TemplatesBuilder.
52   * @param uri The Namespace URI, or the empty string if the
53   *        element has no Namespace URI or if Namespace
54   *        processing is not being performed.
55   * @param localName The local name (without prefix), or the
56   *        empty string if Namespace processing is not being
57   *        performed.
58   * @param rawName The raw XML 1.0 name (with prefix), or the
59   *        empty string if raw names are not available.
60   * @param attributes The attributes attached to the element.  If
61   *        there are no attributes, it shall be an empty
62   *        Attributes object.
63   */
64  public void startElement(
65          StylesheetHandler handler, String uri, String localName, String rawName, Attributes attributes)
66            throws org.xml.sax.SAXException
67  {
68
69    KeyDeclaration kd = new KeyDeclaration(handler.getStylesheet(), handler.nextUid());
70
71    kd.setDOMBackPointer(handler.getOriginatingNode());
72    kd.setLocaterInfo(handler.getLocator());
73    setPropertiesFromAttributes(handler, rawName, attributes, kd);
74    handler.getStylesheet().setKey(kd);
75  }
76
77  /**
78   * Set the properties of an object from the given attribute list.
79   * @param handler The stylesheet's Content handler, needed for
80   *                error reporting.
81   * @param rawName The raw name of the owner element, needed for
82   *                error reporting.
83   * @param attributes The list of attributes.
84   * @param target The target element where the properties will be set.
85   */
86  void setPropertiesFromAttributes(
87          StylesheetHandler handler, String rawName, Attributes attributes,
88          org.apache.xalan.templates.ElemTemplateElement target)
89            throws org.xml.sax.SAXException
90  {
91
92    XSLTElementDef def = getElemDef();
93
94    // Keep track of which XSLTAttributeDefs have been processed, so
95    // I can see which default values need to be set.
96    List processedDefs = new ArrayList();
97    int nAttrs = attributes.getLength();
98
99    for (int i = 0; i < nAttrs; i++)
100    {
101      String attrUri = attributes.getURI(i);
102      String attrLocalName = attributes.getLocalName(i);
103      XSLTAttributeDef attrDef = def.getAttributeDef(attrUri, attrLocalName);
104
105      if (null == attrDef)
106      {
107
108        // Then barf, because this element does not allow this attribute.
109        handler.error(attributes.getQName(i)
110                      + "attribute is not allowed on the " + rawName
111                      + " element!", null);
112      }
113      else
114      {
115        String valueString = attributes.getValue(i);
116
117        if (valueString.indexOf(org.apache.xpath.compiler.Keywords.FUNC_KEY_STRING
118                                + "(") >= 0)
119          handler.error(
120            XSLMessages.createMessage(
121            XSLTErrorResources.ER_INVALID_KEY_CALL, null), null);
122
123        processedDefs.add(attrDef);
124        attrDef.setAttrValue(handler, attrUri, attrLocalName,
125                             attributes.getQName(i), attributes.getValue(i),
126                             target);
127      }
128    }
129
130    XSLTAttributeDef[] attrDefs = def.getAttributes();
131    int nAttrDefs = attrDefs.length;
132
133    for (int i = 0; i < nAttrDefs; i++)
134    {
135      XSLTAttributeDef attrDef = attrDefs[i];
136      String defVal = attrDef.getDefault();
137
138      if (null != defVal)
139      {
140        if (!processedDefs.contains(attrDef))
141        {
142          attrDef.setDefAttrValue(handler, target);
143        }
144      }
145
146      if (attrDef.getRequired())
147      {
148        if (!processedDefs.contains(attrDef))
149          handler.error(
150            XSLMessages.createMessage(
151              XSLTErrorResources.ER_REQUIRES_ATTRIB, new Object[]{ rawName,
152                                                                   attrDef.getName() }), null);
153      }
154    }
155  }
156}
157