View Javadoc

    1   /*
    2    * This file is part of dependency-check-core.
    3    *
    4    * Dependency-check-core is free software: you can redistribute it and/or modify it
    5    * under the terms of the GNU General Public License as published by the Free
    6    * Software Foundation, either version 3 of the License, or (at your option) any
    7    * later version.
    8    *
    9    * Dependency-check-core is distributed in the hope that it will be useful, but
    10   * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    11   * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
    12   * details.
    13   *
    14   * You should have received a copy of the GNU General Public License along with
    15   * dependency-check-core. If not, see http://www.gnu.org/licenses/.
    16   *
    17   * Copyright (c) 2013 Jeremy Long. All Rights Reserved.
    18   */
    19  package org.owasp.dependencycheck.jaxb.pom;
    20
    21  import org.xml.sax.Attributes;
    22  import org.xml.sax.SAXException;
    23  import org.xml.sax.helpers.XMLFilterImpl;
    24
    25  /**
    26   * This filter is used when parsing POM documents. Some POM documents do not
    27   * specify the xmlns="http://maven.apache.org/POM/4.0.0". This filter ensures
    28   * that the correct namespace is added so that both types of POMs can be read.
    29   *
    30   * @author Jeremy Long (jeremy.long@owasp.org)
    31   */
    32  public class MavenNamespaceFilter extends XMLFilterImpl {
    33
    34      /**
    35       * The namespace to add for Maven POMs.
    36       */
    37      private static final String NAMESPACE = "http://maven.apache.org/POM/4.0.0";
    38      /**
    39       * A flag indicating whether or not the namespace (prefix) has been added.
    40       */
    41      private boolean namespaceAdded = false;
    42
    43      /**
    44       * Called at the start of the document parsing.
    45       *
    46       * @throws SAXException thrown if there is a SAXException
    47       */
    48      @Override
    49      public void startDocument() throws SAXException {
    50          super.startDocument();
    51          startPrefixMapping("", NAMESPACE);
    52      }
    53
    54      /**
    55       * Called when an element is started.
    56       *
    57       * @param uri the uri
    58       * @param localName the localName
    59       * @param qName the qualified name
    60       * @param atts the attributes
    61       * @throws SAXException thrown if there is a SAXException
    62       */
    63      @Override
    64      public void startElement(String uri, String localName, String qName, Attributes atts) throws SAXException {
    65          super.startElement(NAMESPACE, localName, qName, atts);
    66      }
    67
    68      /**
    69       * Indicatees the start of the document.
    70       *
    71       * @param uri the uri
    72       * @param localName the localName
    73       * @param qName the qualified name
    74       * @throws SAXException thrown if there is a SAXException
    75       */
    76      @Override
    77      public void endElement(String uri, String localName, String qName)
    78              throws SAXException {
    79          super.endElement(NAMESPACE, localName, qName);
    80      }
    81
    82      /**
    83       * Called when prefix mapping is started.
    84       *
    85       * @param prefix the prefix
    86       * @param url the url
    87       * @throws SAXException thrown if there is a SAXException
    88       */
    89      @Override
    90      public void startPrefixMapping(String prefix, String url) throws SAXException {
    91          if (!this.namespaceAdded) {
    92              namespaceAdded = true;
    93              super.startPrefixMapping("", NAMESPACE);
    94          }
    95      }
    96  }