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: ProcessorTemplateElem.java 468640 2006-10-28 06:53:53Z minchau $
20 */
21package org.apache.xalan.processor;
22
23import javax.xml.transform.TransformerException;
24
25import org.apache.xalan.res.XSLTErrorResources;
26import org.apache.xalan.templates.ElemTemplateElement;
27
28import org.xml.sax.Attributes;
29
30/**
31 * This class processes parse events for an XSLT template element.
32 * @see <a href="http://www.w3.org/TR/xslt#dtd">XSLT DTD</a>
33 * @see <a href="http://www.w3.org/TR/xslt#section-Creating-the-Result-Tree">section-Creating-the-Result-Tree in XSLT Specification</a>
34 */
35public class ProcessorTemplateElem extends XSLTElementProcessor
36{
37    static final long serialVersionUID = 8344994001943407235L;
38
39  /**
40   * Receive notification of the start of an element.
41   *
42   * @param handler non-null reference to current StylesheetHandler that is constructing the Templates.
43   * @param uri The Namespace URI, or an empty string.
44   * @param localName The local name (without prefix), or empty string if not namespace processing.
45   * @param rawName The qualified name (with prefix).
46   * @param attributes The specified or defaulted attributes.
47   */
48  public void startElement(
49          StylesheetHandler handler, String uri, String localName, String rawName, Attributes attributes)
50            throws org.xml.sax.SAXException
51  {
52
53    super.startElement(handler, uri, localName, rawName, attributes);
54    try
55    {
56      // ElemTemplateElement parent = handler.getElemTemplateElement();
57      XSLTElementDef def = getElemDef();
58      Class classObject = def.getClassObject();
59      ElemTemplateElement elem = null;
60
61      try
62      {
63        elem = (ElemTemplateElement) classObject.newInstance();
64
65        elem.setDOMBackPointer(handler.getOriginatingNode());
66        elem.setLocaterInfo(handler.getLocator());
67        elem.setPrefixes(handler.getNamespaceSupport());
68      }
69      catch (InstantiationException ie)
70      {
71        handler.error(XSLTErrorResources.ER_FAILED_CREATING_ELEMTMPL, null, ie);//"Failed creating ElemTemplateElement instance!", ie);
72      }
73      catch (IllegalAccessException iae)
74      {
75        handler.error(XSLTErrorResources.ER_FAILED_CREATING_ELEMTMPL, null, iae);//"Failed creating ElemTemplateElement instance!", iae);
76      }
77
78      setPropertiesFromAttributes(handler, rawName, attributes, elem);
79      appendAndPush(handler, elem);
80    }
81    catch(TransformerException te)
82    {
83      throw new org.xml.sax.SAXException(te);
84    }
85  }
86
87  /**
88   * Append the current template element to the current
89   * template element, and then push it onto the current template
90   * element stack.
91   *
92   * @param handler non-null reference to current StylesheetHandler that is constructing the Templates.
93   * @param elem non-null reference to a the current template element.
94   *
95   * @throws org.xml.sax.SAXException Any SAX exception, possibly
96   *            wrapping another exception.
97   */
98  protected void appendAndPush(
99          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}
127