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: ProcessorNamespaceAlias.java 468640 2006-10-28 06:53:53Z minchau $
020     */
021    package org.apache.xalan.processor;
022    
023    import org.apache.xalan.res.XSLTErrorResources;
024    import org.apache.xalan.templates.NamespaceAlias;
025    import org.xml.sax.Attributes;
026    
027    /**
028     * TransformerFactory for xsl:namespace-alias markup.
029     * A stylesheet can use the xsl:namespace-alias element to
030     * declare that one namespace URI is an alias for another namespace URI.
031     * <pre>
032     * <!ELEMENT xsl:namespace-alias EMPTY>
033     * <!ATTLIST xsl:namespace-alias
034     *   stylesheet-prefix CDATA #REQUIRED
035     *   result-prefix CDATA #REQUIRED
036     * >
037     * </pre>
038     * @see <a href="http://www.w3.org/TR/xslt#dtd">XSLT DTD</a>
039     * @see <a href="http://www.w3.org/TR/xslt#literal-result-element">literal-result-element in XSLT Specification</a>
040     */
041    class ProcessorNamespaceAlias extends XSLTElementProcessor
042    {
043        static final long serialVersionUID = -6309867839007018964L;
044    
045      /**
046       * Receive notification of the start of an xsl:namespace-alias element.
047       *
048       * @param handler The calling StylesheetHandler/TemplatesBuilder.
049       * @param uri The Namespace URI, or the empty string if the
050       *        element has no Namespace URI or if Namespace
051       *        processing is not being performed.
052       * @param localName The local name (without prefix), or the
053       *        empty string if Namespace processing is not being
054       *        performed.
055       * @param rawName The raw XML 1.0 name (with prefix), or the
056       *        empty string if raw names are not available.
057       * @param attributes The attributes attached to the element.  If
058       *        there are no attributes, it shall be an empty
059       *        Attributes object.
060       */
061      public void startElement(
062              StylesheetHandler handler, String uri, String localName, String rawName, Attributes attributes)
063                throws org.xml.sax.SAXException
064      {
065        final String resultNS;
066        NamespaceAlias na = new NamespaceAlias(handler.nextUid());
067    
068        setPropertiesFromAttributes(handler, rawName, attributes, na);
069        String prefix = na.getStylesheetPrefix();
070        if(prefix.equals("#default"))
071        {
072          prefix = "";
073          na.setStylesheetPrefix(prefix);
074        }
075        String stylesheetNS = handler.getNamespaceForPrefix(prefix);
076        na.setStylesheetNamespace(stylesheetNS);
077        prefix = na.getResultPrefix();
078        if(prefix.equals("#default"))
079        {
080          prefix = "";
081          na.setResultPrefix(prefix);
082          resultNS = handler.getNamespaceForPrefix(prefix);
083          if(null == resultNS)
084            handler.error(XSLTErrorResources.ER_INVALID_NAMESPACE_URI_VALUE_FOR_RESULT_PREFIX_FOR_DEFAULT, null, null);
085        }
086        else
087        {
088            resultNS = handler.getNamespaceForPrefix(prefix);
089            if(null == resultNS)
090             handler.error(XSLTErrorResources.ER_INVALID_NAMESPACE_URI_VALUE_FOR_RESULT_PREFIX, new Object[] {prefix}, null);
091        }
092       
093        na.setResultNamespace(resultNS);
094        handler.getStylesheet().setNamespaceAlias(na);
095        handler.getStylesheet().appendChild(na);
096      }
097    }