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: ProcessorNamespaceAlias.java 468640 2006-10-28 06:53:53Z minchau $
     20  */
     21 package org.apache.xalan.processor;
     22 
     23 import org.apache.xalan.res.XSLTErrorResources;
     24 import org.apache.xalan.templates.NamespaceAlias;
     25 import org.xml.sax.Attributes;
     26 
     27 /**
     28  * TransformerFactory for xsl:namespace-alias markup.
     29  * A stylesheet can use the xsl:namespace-alias element to
     30  * declare that one namespace URI is an alias for another namespace URI.
     31  * <pre>
     32  * <!ELEMENT xsl:namespace-alias EMPTY>
     33  * <!ATTLIST xsl:namespace-alias
     34  *   stylesheet-prefix CDATA #REQUIRED
     35  *   result-prefix CDATA #REQUIRED
     36  * >
     37  * </pre>
     38  * @see <a href="http://www.w3.org/TR/xslt#dtd">XSLT DTD</a>
     39  * @see <a href="http://www.w3.org/TR/xslt#literal-result-element">literal-result-element in XSLT Specification</a>
     40  */
     41 class ProcessorNamespaceAlias extends XSLTElementProcessor
     42 {
     43     static final long serialVersionUID = -6309867839007018964L;
     44 
     45   /**
     46    * Receive notification of the start of an xsl:namespace-alias element.
     47    *
     48    * @param handler The calling StylesheetHandler/TemplatesBuilder.
     49    * @param uri The Namespace URI, or the empty string if the
     50    *        element has no Namespace URI or if Namespace
     51    *        processing is not being performed.
     52    * @param localName The local name (without prefix), or the
     53    *        empty string if Namespace processing is not being
     54    *        performed.
     55    * @param rawName The raw XML 1.0 name (with prefix), or the
     56    *        empty string if raw names are not available.
     57    * @param attributes The attributes attached to the element.  If
     58    *        there are no attributes, it shall be an empty
     59    *        Attributes object.
     60    */
     61   public void startElement(
     62           StylesheetHandler handler, String uri, String localName, String rawName, Attributes attributes)
     63             throws org.xml.sax.SAXException
     64   {
     65     final String resultNS;
     66     NamespaceAlias na = new NamespaceAlias(handler.nextUid());
     67 
     68     setPropertiesFromAttributes(handler, rawName, attributes, na);
     69     String prefix = na.getStylesheetPrefix();
     70     if(prefix.equals("#default"))
     71     {
     72       prefix = "";
     73       na.setStylesheetPrefix(prefix);
     74     }
     75     String stylesheetNS = handler.getNamespaceForPrefix(prefix);
     76     na.setStylesheetNamespace(stylesheetNS);
     77     prefix = na.getResultPrefix();
     78     if(prefix.equals("#default"))
     79     {
     80       prefix = "";
     81       na.setResultPrefix(prefix);
     82       resultNS = handler.getNamespaceForPrefix(prefix);
     83       if(null == resultNS)
     84         handler.error(XSLTErrorResources.ER_INVALID_NAMESPACE_URI_VALUE_FOR_RESULT_PREFIX_FOR_DEFAULT, null, null);
     85     }
     86     else
     87     {
     88         resultNS = handler.getNamespaceForPrefix(prefix);
     89         if(null == resultNS)
     90          handler.error(XSLTErrorResources.ER_INVALID_NAMESPACE_URI_VALUE_FOR_RESULT_PREFIX, new Object[] {prefix}, null);
     91     }
     92 
     93     na.setResultNamespace(resultNS);
     94     handler.getStylesheet().setNamespaceAlias(na);
     95     handler.getStylesheet().appendChild(na);
     96   }
     97 }
     98