Home | History | Annotate | Download | only in processor
      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: ProcessorStylesheetElement.java 468640 2006-10-28 06:53:53Z minchau $
     20  */
     21 package org.apache.xalan.processor;
     22 
     23 import javax.xml.transform.TransformerConfigurationException;
     24 import javax.xml.transform.TransformerException;
     25 
     26 import org.apache.xalan.templates.Stylesheet;
     27 import org.apache.xalan.templates.StylesheetComposed;
     28 import org.apache.xalan.templates.StylesheetRoot;
     29 
     30 import org.xml.sax.Attributes;
     31 
     32 /**
     33  * TransformerFactory for xsl:stylesheet or xsl:transform markup.
     34  * @see <a href="http://www.w3.org/TR/xslt#dtd">XSLT DTD</a>
     35  * @see <a href="http://www.w3.org/TR/xslt#stylesheet-element">stylesheet-element in XSLT Specification</a>
     36  *
     37  * @xsl.usage internal
     38  */
     39 public class ProcessorStylesheetElement extends XSLTElementProcessor
     40 {
     41     static final long serialVersionUID = -877798927447840792L;
     42 
     43   /**
     44    * Receive notification of the start of an strip-space element.
     45    *
     46    * @param handler The calling StylesheetHandler/TemplatesBuilder.
     47    * @param uri The Namespace URI, or the empty string if the
     48    *        element has no Namespace URI or if Namespace
     49    *        processing is not being performed.
     50    * @param localName The local name (without prefix), or the
     51    *        empty string if Namespace processing is not being
     52    *        performed.
     53    * @param rawName The raw XML 1.0 name (with prefix), or the
     54    *        empty string if raw names are not available.
     55    * @param attributes The attributes attached to the element.  If
     56    *        there are no attributes, it shall be an empty
     57    *        Attributes object.
     58    */
     59   public void startElement(
     60           StylesheetHandler handler, String uri, String localName, String rawName, Attributes attributes)
     61             throws org.xml.sax.SAXException
     62   {
     63 
     64 		super.startElement(handler, uri, localName, rawName, attributes);
     65     try
     66     {
     67       int stylesheetType = handler.getStylesheetType();
     68       Stylesheet stylesheet;
     69 
     70       if (stylesheetType == StylesheetHandler.STYPE_ROOT)
     71       {
     72         try
     73         {
     74           stylesheet = getStylesheetRoot(handler);
     75         }
     76         catch(TransformerConfigurationException tfe)
     77         {
     78           throw new TransformerException(tfe);
     79         }
     80       }
     81       else
     82       {
     83         Stylesheet parent = handler.getStylesheet();
     84 
     85         if (stylesheetType == StylesheetHandler.STYPE_IMPORT)
     86         {
     87           StylesheetComposed sc = new StylesheetComposed(parent);
     88 
     89           parent.setImport(sc);
     90 
     91           stylesheet = sc;
     92         }
     93         else
     94         {
     95           stylesheet = new Stylesheet(parent);
     96 
     97           parent.setInclude(stylesheet);
     98         }
     99       }
    100 
    101       stylesheet.setDOMBackPointer(handler.getOriginatingNode());
    102       stylesheet.setLocaterInfo(handler.getLocator());
    103 
    104       stylesheet.setPrefixes(handler.getNamespaceSupport());
    105       handler.pushStylesheet(stylesheet);
    106       setPropertiesFromAttributes(handler, rawName, attributes,
    107                                   handler.getStylesheet());
    108       handler.pushElemTemplateElement(handler.getStylesheet());
    109     }
    110     catch(TransformerException te)
    111     {
    112       throw new org.xml.sax.SAXException(te);
    113     }
    114   }
    115 
    116   /**
    117    * This method can be over-ridden by a class that extends this one.
    118    * @param handler The calling StylesheetHandler/TemplatesBuilder.
    119    */
    120   protected Stylesheet getStylesheetRoot(StylesheetHandler handler) throws TransformerConfigurationException
    121   {
    122     StylesheetRoot stylesheet;
    123     stylesheet = new StylesheetRoot(handler.getSchema(), handler.getStylesheetProcessor().getErrorListener());
    124 
    125     if (handler.getStylesheetProcessor().isSecureProcessing())
    126       stylesheet.setSecureProcessing(true);
    127 
    128     return stylesheet;
    129   }
    130 
    131 /**
    132    * Receive notification of the end of an element.
    133    *
    134    * @param handler non-null reference to current StylesheetHandler that is constructing the Templates.
    135    * @param uri The Namespace URI, or an empty string.
    136    * @param localName The local name (without prefix), or empty string if not namespace processing.
    137    * @param rawName The qualified name (with prefix).
    138    */
    139   public void endElement(
    140           StylesheetHandler handler, String uri, String localName, String rawName)
    141             throws org.xml.sax.SAXException
    142   {
    143 		super.endElement(handler, uri, localName, rawName);
    144     handler.popElemTemplateElement();
    145     handler.popStylesheet();
    146   }
    147 }
    148