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: ProcessorTemplateElem.java 468640 2006-10-28 06:53:53Z minchau $
020     */
021    package org.apache.xalan.processor;
022    
023    import javax.xml.transform.TransformerException;
024    
025    import org.apache.xalan.res.XSLTErrorResources;
026    import org.apache.xalan.templates.ElemTemplateElement;
027    
028    import org.xml.sax.Attributes;
029    
030    /**
031     * This class processes parse events for an XSLT template element.
032     * @see <a href="http://www.w3.org/TR/xslt#dtd">XSLT DTD</a>
033     * @see <a href="http://www.w3.org/TR/xslt#section-Creating-the-Result-Tree">section-Creating-the-Result-Tree in XSLT Specification</a>
034     */
035    public class ProcessorTemplateElem extends XSLTElementProcessor
036    {
037        static final long serialVersionUID = 8344994001943407235L;
038    
039      /**
040       * Receive notification of the start of an element.
041       *
042       * @param handler non-null reference to current StylesheetHandler that is constructing the Templates.
043       * @param uri The Namespace URI, or an empty string.
044       * @param localName The local name (without prefix), or empty string if not namespace processing.
045       * @param rawName The qualified name (with prefix).
046       * @param attributes The specified or defaulted attributes.
047       */
048      public void startElement(
049              StylesheetHandler handler, String uri, String localName, String rawName, Attributes attributes)
050                throws org.xml.sax.SAXException
051      {
052    
053        super.startElement(handler, uri, localName, rawName, attributes);
054        try
055        {
056          // ElemTemplateElement parent = handler.getElemTemplateElement();
057          XSLTElementDef def = getElemDef();
058          Class classObject = def.getClassObject();
059          ElemTemplateElement elem = null;
060    
061          try
062          {
063            elem = (ElemTemplateElement) classObject.newInstance();
064    
065            elem.setDOMBackPointer(handler.getOriginatingNode());
066            elem.setLocaterInfo(handler.getLocator());
067            elem.setPrefixes(handler.getNamespaceSupport());
068          }
069          catch (InstantiationException ie)
070          {
071            handler.error(XSLTErrorResources.ER_FAILED_CREATING_ELEMTMPL, null, ie);//"Failed creating ElemTemplateElement instance!", ie);
072          }
073          catch (IllegalAccessException iae)
074          {
075            handler.error(XSLTErrorResources.ER_FAILED_CREATING_ELEMTMPL, null, iae);//"Failed creating ElemTemplateElement instance!", iae);
076          }
077    
078          setPropertiesFromAttributes(handler, rawName, attributes, elem);
079          appendAndPush(handler, elem);
080        }
081        catch(TransformerException te)
082        {
083          throw new org.xml.sax.SAXException(te);
084        }
085      }
086    
087      /**
088       * Append the current template element to the current
089       * template element, and then push it onto the current template
090       * element stack.
091       *
092       * @param handler non-null reference to current StylesheetHandler that is constructing the Templates.
093       * @param elem non-null reference to a the current template element.
094       *
095       * @throws org.xml.sax.SAXException Any SAX exception, possibly
096       *            wrapping another exception.
097       */
098      protected void appendAndPush(
099              StylesheetHandler handler, ElemTemplateElement elem)
100                throws org.xml.sax.SAXException
101      {
102    
103        ElemTemplateElement parent = handler.getElemTemplateElement();
104        if(null != parent)  // defensive, for better multiple error reporting. -sb
105        {
106          parent.appendChild(elem);
107          handler.pushElemTemplateElement(elem);
108        }
109      }
110    
111      /**
112       * Receive notification of the end of an element.
113       *
114       * @param handler non-null reference to current StylesheetHandler that is constructing the Templates.
115       * @param uri The Namespace URI, or an empty string.
116       * @param localName The local name (without prefix), or empty string if not namespace processing.
117       * @param rawName The qualified name (with prefix).
118       */
119      public void endElement(
120              StylesheetHandler handler, String uri, String localName, String rawName)
121                throws org.xml.sax.SAXException
122      {
123        super.endElement(handler, uri, localName, rawName);
124        handler.popElemTemplateElement().setEndLocaterInfo(handler.getLocator());
125      }
126    }