Author: bramk
Date: Fri Apr 13 15:30:10 2012
New Revision: 1325804
URL: http://svn.apache.org/viewvc?rev=1325804&view=rev
Log:
ACE-259 More robust namespace checking / Using sax instead of document parser
Added:
ace/trunk/ace-client-repository-helper-configuration/src/test/java/org/apache/ace/client/
ace/trunk/ace-client-repository-helper-configuration/src/test/java/org/apache/ace/client/repository/
ace/trunk/ace-client-repository-helper-configuration/src/test/java/org/apache/ace/client/repository/helper/
ace/trunk/ace-client-repository-helper-configuration/src/test/java/org/apache/ace/client/repository/helper/configuration/
ace/trunk/ace-client-repository-helper-configuration/src/test/java/org/apache/ace/client/repository/helper/configuration/impl/
ace/trunk/ace-client-repository-helper-configuration/src/test/java/org/apache/ace/client/repository/helper/configuration/impl/ConfigurationHelperImplTest.java
ace/trunk/ace-client-repository-helper-configuration/src/test/resources/
ace/trunk/ace-client-repository-helper-configuration/src/test/resources/invalid13.xml
ace/trunk/ace-client-repository-helper-configuration/src/test/resources/valid10.xml
ace/trunk/ace-client-repository-helper-configuration/src/test/resources/valid11.xml
ace/trunk/ace-client-repository-helper-configuration/src/test/resources/valid12.xml
ace/trunk/ace-client-repository-helper-configuration/src/test/resources/validWithComment.xml
Modified:
ace/trunk/ace-client-repository-helper-configuration/src/main/java/org/apache/ace/client/repository/helper/configuration/impl/ConfigurationHelperImpl.java
Modified:
ace/trunk/ace-client-repository-helper-configuration/src/main/java/org/apache/ace/client/repository/helper/configuration/impl/ConfigurationHelperImpl.java
URL:
http://svn.apache.org/viewvc/ace/trunk/ace-client-repository-helper-configuration/src/main/java/org/apache/ace/client/repository/helper/configuration/impl/ConfigurationHelperImpl.java?rev=1325804&r1=1325803&r2=1325804&view=diff
==============================================================================
---
ace/trunk/ace-client-repository-helper-configuration/src/main/java/org/apache/ace/client/repository/helper/configuration/impl/ConfigurationHelperImpl.java
(original)
+++
ace/trunk/ace-client-repository-helper-configuration/src/main/java/org/apache/ace/client/repository/helper/configuration/impl/ConfigurationHelperImpl.java
Fri Apr 13 15:30:10 2012
@@ -19,22 +19,24 @@
package org.apache.ace.client.repository.helper.configuration.impl;
import java.io.File;
+import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Map;
-import javax.xml.parsers.DocumentBuilderFactory;
+import javax.xml.parsers.SAXParser;
+import javax.xml.parsers.SAXParserFactory;
import org.apache.ace.client.repository.helper.ArtifactPreprocessor;
import org.apache.ace.client.repository.helper.ArtifactRecognizer;
import
org.apache.ace.client.repository.helper.base.VelocityArtifactPreprocessor;
import
org.apache.ace.client.repository.helper.configuration.ConfigurationHelper;
import org.apache.ace.client.repository.object.ArtifactObject;
-import org.w3c.dom.Document;
-import org.w3c.dom.NamedNodeMap;
-import org.w3c.dom.Node;
+import org.xml.sax.Attributes;
+import org.xml.sax.SAXException;
+import org.xml.sax.helpers.DefaultHandler;
public class ConfigurationHelperImpl implements ArtifactRecognizer,
ConfigurationHelper {
@@ -42,6 +44,14 @@ public class ConfigurationHelperImpl imp
private static final String NAMESPACE_1_0 =
"http://www.osgi.org/xmlns/metatype/v1.0.0";
private static final String NAMESPACE_1_1 =
"http://www.osgi.org/xmlns/metatype/v1.1.0";
private static final String NAMESPACE_1_2 =
"http://www.osgi.org/xmlns/metatype/v1.2.0";
+
+ private final SAXParserFactory m_saxParserFactory;
+
+ public ConfigurationHelperImpl() {
+ m_saxParserFactory = SAXParserFactory.newInstance();
+ m_saxParserFactory.setNamespaceAware(false);
+ m_saxParserFactory.setValidating(false);
+ }
public boolean canHandle(String mimetype) {
return MIMETYPE.equals(mimetype);
@@ -64,13 +74,15 @@ public class ConfigurationHelperImpl imp
}
public String recognize(URL artifact) {
+ MetaDataNamespaceCollector handler = new MetaDataNamespaceCollector();
+ InputStream input = null;
try {
- InputStream in = artifact.openStream();
- Document doc =
DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(in);
- Node first = doc.getFirstChild();
- NamedNodeMap attributes = first.getAttributes();
- Node metatype = attributes.getNamedItem("xmlns:metatype");
- String namespace = metatype.getTextContent();
+ input = artifact.openStream();
+ SAXParser parser = m_saxParserFactory.newSAXParser();
+ parser.parse(input, handler);
+ }
+ catch (Exception e) {
+ String namespace = handler.getMetaDataNamespace();
if (namespace != null
&& (namespace.equals(NAMESPACE_1_0)
|| namespace.equals(NAMESPACE_1_1)
@@ -78,10 +90,14 @@ public class ConfigurationHelperImpl imp
return MIMETYPE;
}
}
- catch (Exception e) {
- // Does not matter.
+ finally {
+ if (input != null) {
+ try {
+ input.close();
+ }
+ catch (IOException e) {}
+ }
}
-
return null;
}
@@ -122,5 +138,32 @@ public class ConfigurationHelperImpl imp
public String getExtension(URL artifact) {
return ".xml";
}
-
+
+ static class MetaDataNamespaceCollector extends DefaultHandler {
+
+ private String m_metaDataNameSpace = "";
+
+ public String getMetaDataNamespace() {
+ return m_metaDataNameSpace;
+ }
+
+ @Override
+ public void startElement(String uri, String localName, String qName,
Attributes attributes)
+ throws SAXException {
+ if (qName.equals("MetaData") || qName.endsWith(":MetaData")) {
+ String nsAttributeQName = "xmlns";
+ if (qName.endsWith(":MetaData")) {
+ nsAttributeQName = "xmlns" + ":" + qName.split(":")[0];
+ }
+ for (int i = 0; i < attributes.getLength(); i++) {
+ if (attributes.getQName(i).equals(nsAttributeQName)) {
+ m_metaDataNameSpace = attributes.getValue(i);
+ }
+ }
+ }
+ // first element is expected to have been the MetaData
+ // root so we can now terminate processing.
+ throw new SAXException("Done");
+ }
+ }
}
\ No newline at end of file
Added:
ace/trunk/ace-client-repository-helper-configuration/src/test/java/org/apache/ace/client/repository/helper/configuration/impl/ConfigurationHelperImplTest.java
URL:
http://svn.apache.org/viewvc/ace/trunk/ace-client-repository-helper-configuration/src/test/java/org/apache/ace/client/repository/helper/configuration/impl/ConfigurationHelperImplTest.java?rev=1325804&view=auto
==============================================================================
---
ace/trunk/ace-client-repository-helper-configuration/src/test/java/org/apache/ace/client/repository/helper/configuration/impl/ConfigurationHelperImplTest.java
(added)
+++
ace/trunk/ace-client-repository-helper-configuration/src/test/java/org/apache/ace/client/repository/helper/configuration/impl/ConfigurationHelperImplTest.java
Fri Apr 13 15:30:10 2012
@@ -0,0 +1,62 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.ace.client.repository.helper.configuration.impl;
+
+import org.junit.Assert;
+import org.junit.Test;
+
+public class ConfigurationHelperImplTest {
+
+ // ACE-259 Basic recognizer tests
+
+ @Test
+ public void testNamespace10Recognized() throws Exception {
+ ConfigurationHelperImpl c = new ConfigurationHelperImpl();
+ String mime =
c.recognize(this.getClass().getClassLoader().getResource("valid10.xml"));
+ Assert.assertNotNull(mime);
+ }
+
+ @Test
+ public void testNamespace11Recognized() throws Exception {
+ ConfigurationHelperImpl c = new ConfigurationHelperImpl();
+ String mime =
c.recognize(this.getClass().getClassLoader().getResource("valid11.xml"));
+ Assert.assertNotNull(mime);
+ }
+
+ @Test
+ public void testNamespace12Recognized() throws Exception {
+ ConfigurationHelperImpl c = new ConfigurationHelperImpl();
+ String mime =
c.recognize(this.getClass().getClassLoader().getResource("valid12.xml"));
+ Assert.assertNotNull(mime);
+ }
+
+ @Test
+ public void testNamespace13NotRecognized() throws Exception {
+ ConfigurationHelperImpl c = new ConfigurationHelperImpl();
+ String mime =
c.recognize(this.getClass().getClassLoader().getResource("invalid13.xml"));
+ Assert.assertNull(mime);
+ }
+
+ @Test
+ public void testCanHandleCommentBeforeRoot() throws Exception {
+ ConfigurationHelperImpl c = new ConfigurationHelperImpl();
+ String mime =
c.recognize(this.getClass().getClassLoader().getResource("validWithComment.xml"));
+ Assert.assertNotNull(mime);
+ }
+}
\ No newline at end of file
Added:
ace/trunk/ace-client-repository-helper-configuration/src/test/resources/invalid13.xml
URL:
http://svn.apache.org/viewvc/ace/trunk/ace-client-repository-helper-configuration/src/test/resources/invalid13.xml?rev=1325804&view=auto
==============================================================================
---
ace/trunk/ace-client-repository-helper-configuration/src/test/resources/invalid13.xml
(added)
+++
ace/trunk/ace-client-repository-helper-configuration/src/test/resources/invalid13.xml
Fri Apr 13 15:30:10 2012
@@ -0,0 +1,13 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<MetaData xmlns="http://www.osgi.org/xmlns/metatype/v1.3.0">
+ <OCD name="Apache Felix Http Config" id="org.osgi.service.http">
+ <AD id="org.osgi.service.http.port" type="STRING" cardinality="0" />
+ </OCD>
+ <Designate pid="org.apache.felix.http" bundle="*">
+ <Object ocdref="org.osgi.service.http">
+ <Attribute adref="org.osgi.service.http.port">
+ <Value>8080</Value>
+ </Attribute>
+ </Object>
+ </Designate>
+</MetaData>
Added:
ace/trunk/ace-client-repository-helper-configuration/src/test/resources/valid10.xml
URL:
http://svn.apache.org/viewvc/ace/trunk/ace-client-repository-helper-configuration/src/test/resources/valid10.xml?rev=1325804&view=auto
==============================================================================
---
ace/trunk/ace-client-repository-helper-configuration/src/test/resources/valid10.xml
(added)
+++
ace/trunk/ace-client-repository-helper-configuration/src/test/resources/valid10.xml
Fri Apr 13 15:30:10 2012
@@ -0,0 +1,13 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<MetaData xmlns="http://www.osgi.org/xmlns/metatype/v1.0.0">
+ <OCD name="Apache Felix Http Config" id="org.osgi.service.http">
+ <AD id="org.osgi.service.http.port" type="STRING" cardinality="0" />
+ </OCD>
+ <Designate pid="org.apache.felix.http" bundle="*">
+ <Object ocdref="org.osgi.service.http">
+ <Attribute adref="org.osgi.service.http.port">
+ <Value>8080</Value>
+ </Attribute>
+ </Object>
+ </Designate>
+</MetaData>
Added:
ace/trunk/ace-client-repository-helper-configuration/src/test/resources/valid11.xml
URL:
http://svn.apache.org/viewvc/ace/trunk/ace-client-repository-helper-configuration/src/test/resources/valid11.xml?rev=1325804&view=auto
==============================================================================
---
ace/trunk/ace-client-repository-helper-configuration/src/test/resources/valid11.xml
(added)
+++
ace/trunk/ace-client-repository-helper-configuration/src/test/resources/valid11.xml
Fri Apr 13 15:30:10 2012
@@ -0,0 +1,13 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<foo:MetaData xmlns:foo="http://www.osgi.org/xmlns/metatype/v1.1.0">
+ <OCD name="Apache Felix Http Config" id="org.osgi.service.http">
+ <AD id="org.osgi.service.http.port" type="STRING" cardinality="0" />
+ </OCD>
+ <Designate pid="org.apache.felix.http" bundle="*">
+ <Object ocdref="org.osgi.service.http">
+ <Attribute adref="org.osgi.service.http.port">
+ <Value>8080</Value>
+ </Attribute>
+ </Object>
+ </Designate>
+</foo:MetaData>
Added:
ace/trunk/ace-client-repository-helper-configuration/src/test/resources/valid12.xml
URL:
http://svn.apache.org/viewvc/ace/trunk/ace-client-repository-helper-configuration/src/test/resources/valid12.xml?rev=1325804&view=auto
==============================================================================
---
ace/trunk/ace-client-repository-helper-configuration/src/test/resources/valid12.xml
(added)
+++
ace/trunk/ace-client-repository-helper-configuration/src/test/resources/valid12.xml
Fri Apr 13 15:30:10 2012
@@ -0,0 +1,13 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<metatype:MetaData xmlns:metatype="http://www.osgi.org/xmlns/metatype/v1.2.0">
+ <OCD name="Apache Felix Http Config" id="org.osgi.service.http">
+ <AD id="org.osgi.service.http.port" type="STRING" cardinality="0" />
+ </OCD>
+ <Designate pid="org.apache.felix.http" bundle="*">
+ <Object ocdref="org.osgi.service.http">
+ <Attribute adref="org.osgi.service.http.port">
+ <Value>8080</Value>
+ </Attribute>
+ </Object>
+ </Designate>
+</metatype:MetaData>
Added:
ace/trunk/ace-client-repository-helper-configuration/src/test/resources/validWithComment.xml
URL:
http://svn.apache.org/viewvc/ace/trunk/ace-client-repository-helper-configuration/src/test/resources/validWithComment.xml?rev=1325804&view=auto
==============================================================================
---
ace/trunk/ace-client-repository-helper-configuration/src/test/resources/validWithComment.xml
(added)
+++
ace/trunk/ace-client-repository-helper-configuration/src/test/resources/validWithComment.xml
Fri Apr 13 15:30:10 2012
@@ -0,0 +1,16 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+ This is valid
+-->
+<metatype:MetaData xmlns:metatype="http://www.osgi.org/xmlns/metatype/v1.0.0">
+ <OCD name="Apache Felix Http Config" id="org.osgi.service.http">
+ <AD id="org.osgi.service.http.port" type="STRING" cardinality="0" />
+ </OCD>
+ <Designate pid="org.apache.felix.http" bundle="*">
+ <Object ocdref="org.osgi.service.http">
+ <Attribute adref="org.osgi.service.http.port">
+ <Value>8080</Value>
+ </Attribute>
+ </Object>
+ </Designate>
+</metatype:MetaData>