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: ElemAttributeSet.java 468643 2006-10-28 06:56:03Z minchau $
020     */
021    package org.apache.xalan.templates;
022    
023    import javax.xml.transform.TransformerException;
024    
025    import org.apache.xalan.res.XSLMessages;
026    import org.apache.xalan.res.XSLTErrorResources;
027    import org.apache.xalan.transformer.TransformerImpl;
028    import org.apache.xml.utils.QName;
029    
030    /**
031     * Implement xsl:attribute-set.
032     * <pre>
033     * &amp;!ELEMENT xsl:attribute-set (xsl:attribute)*>
034     * &amp;!ATTLIST xsl:attribute-set
035     *   name %qname; #REQUIRED
036     *   use-attribute-sets %qnames; #IMPLIED
037     * &amp;
038     * </pre>
039     * @see <a href="http://www.w3.org/TR/xslt#attribute-sets">attribute-sets in XSLT Specification</a>
040     * @xsl.usage advanced
041     */
042    public class ElemAttributeSet extends ElemUse
043    {
044        static final long serialVersionUID = -426740318278164496L;
045    
046      /**
047       * The name attribute specifies the name of the attribute set.
048       * @serial
049       */
050      public QName m_qname = null;
051    
052      /**
053       * Set the "name" attribute.
054       * The name attribute specifies the name of the attribute set.
055       *
056       * @param name Name attribute to set
057       */
058      public void setName(QName name)
059      {
060        m_qname = name;
061      }
062    
063      /**
064       * Get the "name" attribute.
065       * The name attribute specifies the name of the attribute set.
066       *
067       * @return The name attribute of the attribute set
068       */
069      public QName getName()
070      {
071        return m_qname;
072      }
073    
074      /**
075       * Get an int constant identifying the type of element.
076       * @see org.apache.xalan.templates.Constants
077       *
078       * @return Token ID of the element 
079       */
080      public int getXSLToken()
081      {
082        return Constants.ELEMNAME_DEFINEATTRIBUTESET;
083      }
084    
085      /**
086       * Return the node name.
087       *
088       * @return The name of this element
089       */
090      public String getNodeName()
091      {
092        return Constants.ELEMNAME_ATTRIBUTESET_STRING;
093      }
094    
095      /**
096       * Apply a set of attributes to the element.
097       *
098       * @param transformer non-null reference to the the current transform-time state.
099       *
100       * @throws TransformerException
101       */
102      public void execute(
103              TransformerImpl transformer)
104                throws TransformerException
105      {
106    
107        if (transformer.getDebug())
108              transformer.getTraceManager().fireTraceEvent(this);
109    
110        if (transformer.isRecursiveAttrSet(this))
111        {
112          throw new TransformerException(
113            XSLMessages.createMessage(
114              XSLTErrorResources.ER_XSLATTRSET_USED_ITSELF,
115              new Object[]{ m_qname.getLocalPart() }));  //"xsl:attribute-set '"+m_qname.m_localpart+
116        }
117    
118        transformer.pushElemAttributeSet(this);
119        super.execute(transformer);
120    
121        ElemAttribute attr = (ElemAttribute) getFirstChildElem();
122    
123        while (null != attr)
124        {
125          attr.execute(transformer);
126    
127          attr = (ElemAttribute) attr.getNextSiblingElem();
128        }
129    
130        transformer.popElemAttributeSet();
131       
132        if (transformer.getDebug())
133              transformer.getTraceManager().fireTraceEndEvent(this);
134    
135      }
136    
137      /**
138       * Add a child to the child list.
139       * <!ELEMENT xsl:attribute-set (xsl:attribute)*>
140       * <!ATTLIST xsl:attribute-set
141       *   name %qname; #REQUIRED
142       *   use-attribute-sets %qnames; #IMPLIED
143       * >
144       *
145       * @param newChild Child to be added to this node's list of children
146       *
147       * @return The child that was just added to the list of children
148       *
149       * @throws DOMException
150       */
151      public ElemTemplateElement appendChildElem(ElemTemplateElement newChild)
152      {
153    
154        int type = ((ElemTemplateElement) newChild).getXSLToken();
155    
156        switch (type)
157        {
158        case Constants.ELEMNAME_ATTRIBUTE :
159          break;
160        default :
161          error(XSLTErrorResources.ER_CANNOT_ADD,
162                new Object[]{ newChild.getNodeName(),
163                              this.getNodeName() });  //"Can not add " +((ElemTemplateElement)newChild).m_elemName +
164    
165        //" to " + this.m_elemName);
166        }
167    
168        return super.appendChild(newChild);
169      }
170    
171      /**
172       * This function is called during recomposition to
173       * control how this element is composed.
174       * @param root The root stylesheet for this transformation.
175       */
176      public void recompose(StylesheetRoot root)
177      {
178        root.recomposeAttributeSets(this);
179      }
180    
181    }