FELIX-2719 Implemented name space support, added a test for it.
git-svn-id: https://svn.apache.org/repos/asf/felix/trunk@1042637 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/metatype/src/main/java/org/apache/felix/metatype/MetaDataReader.java b/metatype/src/main/java/org/apache/felix/metatype/MetaDataReader.java
index 152fa50..c15ea22 100644
--- a/metatype/src/main/java/org/apache/felix/metatype/MetaDataReader.java
+++ b/metatype/src/main/java/org/apache/felix/metatype/MetaDataReader.java
@@ -70,8 +70,8 @@
* @return A {@link MetaData} providing access to the
* raw contents of the XML document.
*
- * @throws IOException If an I/O error occurrs accessing the stream.
- * @throws XmlPullParserException If an error occurrs parsing the XML
+ * @throws IOException If an I/O error occurs accessing the stream.
+ * @throws XmlPullParserException If an error occurs parsing the XML
* document.
*/
public MetaData parse( URL url ) throws IOException, XmlPullParserException
@@ -82,6 +82,7 @@
ins = url.openStream();
this.parser.setProperty( "http://xmlpull.org/v1/doc/properties.html#location", url.toString() );
this.parser.setFeature(KXmlParser.FEATURE_PROCESS_NAMESPACES, true);
+ this.parser.setFeature(KXmlParser.FEATURE_REPORT_NAMESPACE_ATTRIBUTES, false);
return this.parse( ins );
}
finally
@@ -100,7 +101,32 @@
}
}
-
+ /**
+ * Checks if this document has a meta type name space.
+ *
+ * @return <code>true</code> if this document has a meta type name space
+ * @throws XmlPullParserException when there the meta type name space is not valid
+ */
+ private boolean hasMetaTypeNameSpace() throws XmlPullParserException {
+
+ int attrs = this.parser.getAttributeCount();
+ for (int i = 0 ; i < attrs; i++) {
+
+ if ( this.parser.getAttributeName(i).equals("xmlns:metatype") ) {
+ if ( !"http://www.osgi.org/xmlns/metatype/v1.0.0".equals(this.parser.getAttributeValue(i)) ) {
+ throw new XmlPullParserException( "invalid namespace: " + this.parser.getAttributeValue(i), this.parser, null );
+ }
+ return true;
+ }
+ }
+ return false;
+ }
+
+ private String getCurrentTagName() {
+
+ System.out.println("ns : '" + this.parser.getNamespace() + "'");
+ return this.parser.getName();
+ }
/**
* Parses the XML document in the given input stream.
* <p>
@@ -129,9 +155,15 @@
{
if ( eventType == XmlPullParser.START_TAG )
{
- if ( "MetaData".equals( this.parser.getName() ) )
+ boolean nameSpaceAware = hasMetaTypeNameSpace();
+
+ if ( nameSpaceAware && "metatype:MetaData".equals( this.parser.getName() ) )
{
- mti = this.readMetaData();
+ mti = this.readMetaData(nameSpaceAware);
+ }
+ else if ( "MetaData".equals( this.parser.getName() ) )
+ {
+ mti = this.readMetaData(nameSpaceAware);
}
else
{
@@ -145,7 +177,7 @@
}
- private MetaData readMetaData() throws IOException, XmlPullParserException
+ private MetaData readMetaData(boolean nameSpaceAware) throws IOException, XmlPullParserException
{
MetaData mti = this.createMetaData();
mti.setLocalePrefix( this.getOptionalAttribute( "localization" ) );
@@ -170,6 +202,10 @@
}
else if ( eventType == XmlPullParser.END_TAG )
{
+ if ( nameSpaceAware && "metatype:MetaData".equals( this.parser.getName() ) )
+ {
+ break;
+ }
if ( "MetaData".equals( this.parser.getName() ) )
{
break;
diff --git a/metatype/src/test/java/org/apache/felix/metatype/MetaDataReaderTest.java b/metatype/src/test/java/org/apache/felix/metatype/MetaDataReaderTest.java
index d47002a..d5b1857 100644
--- a/metatype/src/test/java/org/apache/felix/metatype/MetaDataReaderTest.java
+++ b/metatype/src/test/java/org/apache/felix/metatype/MetaDataReaderTest.java
@@ -66,6 +66,40 @@
assertNull( mti.getObjectClassDefinitions() );
}
+ public void testWithNamespace() throws IOException, XmlPullParserException
+ {
+ String empty = "<metatype:MetaData xmlns:metatype=\"http://www.osgi.org/xmlns/metatype/v1.0.0\" " +
+ "xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" ></metatype:MetaData>";
+ MetaData mti = read( empty );
+
+ assertNotNull( mti );
+ assertNull( mti.getLocalePrefix() );
+ assertNull( mti.getObjectClassDefinitions() );
+ }
+
+ public void testWithInvalidNamespaceUri() throws IOException, XmlPullParserException
+ {
+ String empty = "<metatype:MetaData xmlns:metatype=\"http://www.osgi.org/xmlns/datatype/v1.0.0\" " +
+ "xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" ></metatype:MetaData>";
+
+ Exception exc = null;
+ try {
+ MetaData mti = read( empty );
+ } catch (Exception e) {
+ exc = e;
+ }
+ assertNotNull(exc);
+ }
+
+ public void testWithInvalidNamespaceName() throws IOException, XmlPullParserException
+ {
+ String empty = "<datatype:MetaData xmlns:metatype=\"http://www.osgi.org/xmlns/metatype/v1.0.0\" " +
+ "xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" ></datatype:MetaData>";
+
+ MetaData mti = read( empty );
+
+ assertNull( mti );
+ }
public void testEmptyLocalization() throws IOException, XmlPullParserException
{