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: ProcessorStylesheetElement.java 468640 2006-10-28 06:53:53Z minchau $
020     */
021    package org.apache.xalan.processor;
022    
023    import javax.xml.transform.TransformerConfigurationException;
024    import javax.xml.transform.TransformerException;
025    
026    import org.apache.xalan.templates.Stylesheet;
027    import org.apache.xalan.templates.StylesheetComposed;
028    import org.apache.xalan.templates.StylesheetRoot;
029    
030    import org.xml.sax.Attributes;
031    
032    /**
033     * TransformerFactory for xsl:stylesheet or xsl:transform markup.
034     * @see <a href="http://www.w3.org/TR/xslt#dtd">XSLT DTD</a>
035     * @see <a href="http://www.w3.org/TR/xslt#stylesheet-element">stylesheet-element in XSLT Specification</a>
036     * 
037     * @xsl.usage internal
038     */
039    public class ProcessorStylesheetElement extends XSLTElementProcessor
040    {
041        static final long serialVersionUID = -877798927447840792L;
042    
043      /**
044       * Receive notification of the start of an strip-space element.
045       *
046       * @param handler The calling StylesheetHandler/TemplatesBuilder.
047       * @param uri The Namespace URI, or the empty string if the
048       *        element has no Namespace URI or if Namespace
049       *        processing is not being performed.
050       * @param localName The local name (without prefix), or the
051       *        empty string if Namespace processing is not being
052       *        performed.
053       * @param rawName The raw XML 1.0 name (with prefix), or the
054       *        empty string if raw names are not available.
055       * @param attributes The attributes attached to the element.  If
056       *        there are no attributes, it shall be an empty
057       *        Attributes object.
058       */
059      public void startElement(
060              StylesheetHandler handler, String uri, String localName, String rawName, Attributes attributes)
061                throws org.xml.sax.SAXException
062      {
063    
064                    super.startElement(handler, uri, localName, rawName, attributes);
065        try
066        {
067          int stylesheetType = handler.getStylesheetType();
068          Stylesheet stylesheet;
069    
070          if (stylesheetType == StylesheetHandler.STYPE_ROOT)
071          {
072            try
073            {
074              stylesheet = getStylesheetRoot(handler);
075            }
076            catch(TransformerConfigurationException tfe)
077            {
078              throw new TransformerException(tfe);
079            }
080          }
081          else
082          {
083            Stylesheet parent = handler.getStylesheet();
084    
085            if (stylesheetType == StylesheetHandler.STYPE_IMPORT)
086            {
087              StylesheetComposed sc = new StylesheetComposed(parent);
088    
089              parent.setImport(sc);
090    
091              stylesheet = sc;
092            }
093            else
094            {
095              stylesheet = new Stylesheet(parent);
096    
097              parent.setInclude(stylesheet);
098            }
099          }
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    }