From c31be72c8a9610a23e35d1fa84909f4c3cbb221e Mon Sep 17 00:00:00 2001 From: Jeremy Long Date: Tue, 4 Jun 2013 22:54:09 -0400 Subject: [PATCH] added filter to add the correct Maven namespace to the POM if it is missing Former-commit-id: 79efc8a8a876831739874914a97ba2d764dd6a7a --- .../analyzer/pom/MavenNamespaceFilter.java | 79 +++++++++++++++++++ 1 file changed, 79 insertions(+) create mode 100644 src/main/java/org/owasp/dependencycheck/analyzer/pom/MavenNamespaceFilter.java diff --git a/src/main/java/org/owasp/dependencycheck/analyzer/pom/MavenNamespaceFilter.java b/src/main/java/org/owasp/dependencycheck/analyzer/pom/MavenNamespaceFilter.java new file mode 100644 index 000000000..3a5c3754b --- /dev/null +++ b/src/main/java/org/owasp/dependencycheck/analyzer/pom/MavenNamespaceFilter.java @@ -0,0 +1,79 @@ +/* + * To change this template, choose Tools | Templates + * and open the template in the editor. + */ +package org.owasp.dependencycheck.analyzer.pom; + +import org.xml.sax.Attributes; +import org.xml.sax.SAXException; +import org.xml.sax.helpers.XMLFilterImpl; + +/** + * This filter is used when parsing POM documents. Some POM documents + * do not specify the xmlns="http://maven.apache.org/POM/4.0.0". This + * filter ensures that the correct namespace is added so that both + * types of POMs can be read. + * @author Jeremy Long (jeremy.long@gmail.com) + */ +public class MavenNamespaceFilter extends XMLFilterImpl { + + /** + * The namespace to add for Maven POMs. + */ + private static final String NAMESPACE = "http://maven.apache.org/POM/4.0.0"; + /** + * A flag indicating whether or not the namespace (prefix) has been added. + */ + private boolean namespaceAdded = false; + + /** + * Called at the start of the document parsing. + * @throws SAXException thrown if there is a SAXException + */ + @Override + public void startDocument() throws SAXException { + super.startDocument(); + startPrefixMapping("", NAMESPACE); + } + + /** + * Called when an element is started. + * @param uri the uri + * @param localName the localName + * @param qName the qualified name + * @param atts the attributes + * @throws SAXException thrown if there is a SAXException + */ + @Override + public void startElement(String uri, String localName, String qName, Attributes atts) throws SAXException { + super.startElement(NAMESPACE, localName, qName, atts); + } + + /** + * Indicatees the start of the document. + * @param uri the uri + * @param localName the localName + * @param qName the qualified name + * @throws SAXException thrown if there is a SAXException + */ + @Override + public void endElement(String uri, String localName, String qName) + throws SAXException { + super.endElement(NAMESPACE, localName, qName); + } + + /** + * Called when prefix mapping + * @param prefix the prefix + * @param url the url + * @throws SAXException thrown if there is a SAXException + */ + @Override + public void startPrefixMapping(String prefix, String url) throws SAXException { + if (!this.namespaceAdded) { + namespaceAdded = true; + super.startPrefixMapping("", NAMESPACE); + } + } + +}