001    /*
002     * Licensed to the Apache Software Foundation (ASF) under one
003     * or more contributor license agreements. See the NOTICE file
004     * distributed with this work for additional information
005     * regarding copyright ownership. The ASF licenses this file
006     * to you under the Apache License, Version 2.0 (the  "License");
007     * you may not use this file except in compliance with the License.
008     * You may obtain a copy of the License at
009     *
010     *     http://www.apache.org/licenses/LICENSE-2.0
011     *
012     * Unless required by applicable law or agreed to in writing, software
013     * distributed under the License is distributed on an "AS IS" BASIS,
014     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015     * See the License for the specific language governing permissions and
016     * limitations under the License.
017     */
018    /*
019     * $Id: ProcessorKey.java 469688 2006-10-31 22:39:43Z minchau $
020     */
021    package org.apache.xalan.processor;
022    
023    import java.util.ArrayList;
024    import java.util.List;
025    
026    import org.apache.xalan.res.XSLMessages;
027    import org.apache.xalan.res.XSLTErrorResources;
028    import org.apache.xalan.templates.KeyDeclaration;
029    import org.xml.sax.Attributes;
030    
031    /**
032     * TransformerFactory for xsl:key markup.
033     * <pre>
034     * <!ELEMENT xsl:key EMPTY>
035     * <!ATTLIST xsl:key
036     *   name %qname; #REQUIRED
037     *   match %pattern; #REQUIRED
038     *   use %expr; #REQUIRED
039     * >
040     * </pre>
041     * @see <a href="http://www.w3.org/TR/xslt#dtd">XSLT DTD</a>
042     * @see <a href="http://www.w3.org/TR/xslt#key">key in XSLT Specification</a>
043     */
044    class ProcessorKey extends XSLTElementProcessor
045    {
046        static final long serialVersionUID = 4285205417566822979L;
047    
048      /**
049       * Receive notification of the start of an xsl:key element.
050       *
051       * @param handler The calling StylesheetHandler/TemplatesBuilder.
052       * @param uri The Namespace URI, or the empty string if the
053       *        element has no Namespace URI or if Namespace
054       *        processing is not being performed.
055       * @param localName The local name (without prefix), or the
056       *        empty string if Namespace processing is not being
057       *        performed.
058       * @param rawName The raw XML 1.0 name (with prefix), or the
059       *        empty string if raw names are not available.
060       * @param attributes The attributes attached to the element.  If
061       *        there are no attributes, it shall be an empty
062       *        Attributes object.
063       */
064      public void startElement(
065              StylesheetHandler handler, String uri, String localName, String rawName, Attributes attributes)
066                throws org.xml.sax.SAXException
067      {
068    
069        KeyDeclaration kd = new KeyDeclaration(handler.getStylesheet(), handler.nextUid());
070    
071        kd.setDOMBackPointer(handler.getOriginatingNode());
072        kd.setLocaterInfo(handler.getLocator());
073        setPropertiesFromAttributes(handler, rawName, attributes, kd);
074        handler.getStylesheet().setKey(kd);
075      }
076    
077      /**
078       * Set the properties of an object from the given attribute list.
079       * @param handler The stylesheet's Content handler, needed for
080       *                error reporting.
081       * @param rawName The raw name of the owner element, needed for
082       *                error reporting.
083       * @param attributes The list of attributes.
084       * @param target The target element where the properties will be set.
085       */
086      void setPropertiesFromAttributes(
087              StylesheetHandler handler, String rawName, Attributes attributes, 
088              org.apache.xalan.templates.ElemTemplateElement target)
089                throws org.xml.sax.SAXException
090      {
091    
092        XSLTElementDef def = getElemDef();
093    
094        // Keep track of which XSLTAttributeDefs have been processed, so 
095        // I can see which default values need to be set.
096        List processedDefs = new ArrayList();
097        int nAttrs = attributes.getLength();
098    
099        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    }