View Javadoc

    1   /*
    2    * Copyright 2002,2004 The Apache Software Foundation.
    3    *
    4    * Licensed under the Apache License, Version 2.0 (the "License");
    5    * you may not use this file except in compliance with the License.
    6    * You may obtain a copy of the License at
    7    *
    8    *      http://www.apache.org/licenses/LICENSE-2.0
    9    *
    10   * Unless required by applicable law or agreed to in writing, software
    11   * distributed under the License is distributed on an "AS IS" BASIS,
    12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13   * See the License for the specific language governing permissions and
    14   * limitations under the License.
    15   */
    16  package org.apache.commons.jelly.parser;
    17
    18  import org.xml.sax.Attributes;
    19  import org.xml.sax.SAXException;
    20  import org.xml.sax.XMLReader;
    21  import org.xml.sax.helpers.XMLFilterImpl;
    22
    23  /***
    24   * XMLFilter that can provide a default namespace when
    25   * one has not been declared by the XML document.  Note:
    26   * this class does not address the namespace of attributes.
    27   *
    28   * @author Morgan Delagrange
    29   */
    30  public class DefaultNamespaceFilter extends XMLFilterImpl {
    31
    32      protected String uriDefault = null;
    33
    34      /***
    35       * Filter for undefined an undefined namespace
    36       *
    37       * @param defaultNamespace
    38       *               uri for the jelly namespace
    39       * @param reader XMLReader to filter
    40       */
    41      public DefaultNamespaceFilter(String defaultNamespace, XMLReader reader) {
    42          super(reader);
    43          this.uriDefault = defaultNamespace;
    44      }
    45
    46      /***
    47       * All incoming empty URIs will be remapped to the default.
    48       *
    49       * @param prefix incoming prefix
    50       * @param uri    URI to check and potentially replace
    51       * @exception SAXException
    52       */
    53      public void startPrefixMapping(java.lang.String prefix,
    54                                     java.lang.String uri)
    55      throws SAXException {
    56
    57          if (uri.equals("")) {
    58              super.startPrefixMapping(prefix,this.uriDefault);
    59          } else {
    60              super.startPrefixMapping(prefix,uri);
    61          }
    62      }
    63
    64      /***
    65       * All incoming empty URIs will be remapped to the default.
    66       *
    67       * @param uri       URI to check and potentially replace
    68       * @param localName
    69       * @param qName
    70       * @param atts
    71       * @exception SAXException
    72       */
    73      public void startElement(java.lang.String uri,
    74                               java.lang.String localName,
    75                               java.lang.String qName,
    76                               Attributes atts)
    77      throws SAXException {
    78
    79          if (uri.equals("")) {
    80              super.startElement(this.uriDefault,localName,qName,atts);
    81          } else {
    82              super.startElement(uri,localName,qName,atts);
    83          }
    84
    85      }
    86
    87      /***
    88       * All incoming empty URIs will be remapped to the default.
    89       *
    90       * @param namespaceURI
    91       *                  URI to check and potentially replace
    92       * @param localName
    93       * @param qName
    94       * @exception SAXException
    95       */
    96      public void endElement(String namespaceURI, String localName, String qName)
    97      throws SAXException {
    98          if (namespaceURI.equals("")) {
    99              super.endElement(this.uriDefault,localName,qName);
    100         } else {
    101             super.endElement(namespaceURI,localName,qName);
    102         }
    103     }
    104 }