You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@aries.apache.org by dk...@apache.org on 2011/11/17 23:32:58 UTC

svn commit: r1203399 - in /aries/trunk/blueprint: blueprint-core/src/main/java/org/apache/aries/blueprint/namespace/ blueprint-testbundlea/src/main/resources/org/apache/aries/blueprint/testbundlea/ blueprint-testbundleb/src/main/resources/OSGI-INF/blue...

Author: dkulp
Date: Thu Nov 17 22:32:58 2011
New Revision: 1203399

URL: http://svn.apache.org/viewvc?rev=1203399&view=rev
Log:
[ARIES-626] Add a schema.map file that a bundle can provide to map extra
namespaces that may be required into schemas used for validation.

Added:
    aries/trunk/blueprint/blueprint-testbundleb/src/main/resources/OSGI-INF/blueprint/schema.map
    aries/trunk/blueprint/blueprint-testbundleb/src/main/resources/org/
    aries/trunk/blueprint/blueprint-testbundleb/src/main/resources/org/apache/
    aries/trunk/blueprint/blueprint-testbundleb/src/main/resources/org/apache/aries/
    aries/trunk/blueprint/blueprint-testbundleb/src/main/resources/org/apache/aries/blueprint/
    aries/trunk/blueprint/blueprint-testbundleb/src/main/resources/org/apache/aries/blueprint/testbundleb/
    aries/trunk/blueprint/blueprint-testbundleb/src/main/resources/org/apache/aries/blueprint/testbundleb/mystuff.xsd
      - copied, changed from r1203342, aries/trunk/blueprint/blueprint-testbundlea/src/main/resources/org/apache/aries/blueprint/testbundlea/nshandlersiximport.xsd
Modified:
    aries/trunk/blueprint/blueprint-core/src/main/java/org/apache/aries/blueprint/namespace/NamespaceHandlerRegistryImpl.java
    aries/trunk/blueprint/blueprint-testbundlea/src/main/resources/org/apache/aries/blueprint/testbundlea/nshandlersiximport.xsd
    aries/trunk/blueprint/blueprint-testbundleb/src/main/resources/OSGI-INF/blueprint/config.xml

Modified: aries/trunk/blueprint/blueprint-core/src/main/java/org/apache/aries/blueprint/namespace/NamespaceHandlerRegistryImpl.java
URL: http://svn.apache.org/viewvc/aries/trunk/blueprint/blueprint-core/src/main/java/org/apache/aries/blueprint/namespace/NamespaceHandlerRegistryImpl.java?rev=1203399&r1=1203398&r2=1203399&view=diff
==============================================================================
--- aries/trunk/blueprint/blueprint-core/src/main/java/org/apache/aries/blueprint/namespace/NamespaceHandlerRegistryImpl.java (original)
+++ aries/trunk/blueprint/blueprint-core/src/main/java/org/apache/aries/blueprint/namespace/NamespaceHandlerRegistryImpl.java Thu Nov 17 22:32:58 2011
@@ -21,6 +21,7 @@ package org.apache.aries.blueprint.names
 import java.lang.ref.Reference;
 import java.lang.ref.SoftReference;
 import java.net.URI;
+import java.net.URISyntaxException;
 import java.net.URL;
 import java.util.AbstractMap;
 import java.util.AbstractSet;
@@ -33,6 +34,7 @@ import java.util.Iterator;
 import java.util.LinkedList;
 import java.util.List;
 import java.util.Map;
+import java.util.Properties;
 import java.util.Set;
 import java.util.HashSet;
 import java.io.IOException;
@@ -216,24 +218,31 @@ public class NamespaceHandlerRegistryImp
     public void destroy() {
         tracker.close();
     }
-
-    public synchronized Schema getSchema(Map<URI, NamespaceHandler> handlers) throws IOException, SAXException {
+    public synchronized Schema getSchema(Map<URI, NamespaceHandler> handlers)
+        throws IOException, SAXException {
+        return getSchema(handlers, null, new Properties());
+    }
+    private synchronized Schema getSchema(Map<URI, NamespaceHandler> handlers, 
+                                          final Bundle bundle,
+                                          final Properties schemaMap) throws IOException, SAXException {
         Schema schema = null;
         // Find a schema that can handle all the requested namespaces
         // If it contains additional namespaces, it should not be a problem since
         // they won't be used at all
-        for (Map<URI, NamespaceHandler> key : schemas.keySet()) {
-            boolean found = true;
-            for (URI uri : handlers.keySet()) {
-                if (!handlers.get(uri).equals(key.get(uri))) {
-                    found = false;
+        if (schemaMap == null || schemaMap.isEmpty()) {
+            for (Map<URI, NamespaceHandler> key : schemas.keySet()) {
+                boolean found = true;
+                for (URI uri : handlers.keySet()) {
+                    if (!handlers.get(uri).equals(key.get(uri))) {
+                        found = false;
+                        break;
+                    }
+                }
+                if (found) {
+                    schema = schemas.get(key).get();
                     break;
                 }
             }
-            if (found) {
-                schema = schemas.get(key).get();
-                break;
-            }
         }
         if (schema == null) {
             final List<StreamSource> schemaSources = new ArrayList<StreamSource>();
@@ -249,13 +258,43 @@ public class NamespaceHandlerRegistryImp
                         schemaSources.add(new StreamSource(url.openStream(), url.toExternalForm()));
                     }
                 }
+                for (Object ns : schemaMap.values()) {
+                    URL url = bundle.getResource(ns.toString());
+                    if (url == null) {
+                        LOGGER.warn("No URL is defined for schema " + ns + ". This schema will not be validated");
+                    } else {
+                        schemaSources.add(new StreamSource(url.openStream(), url.toExternalForm()));
+                    }
+                }
                 SchemaFactory factory = getSchemaFactory();
                 factory.setResourceResolver(new LSResourceResolver() {
                     public LSInput resolveResource(String type, 
                                                    final String namespaceURI, 
                                                    final String publicId,
                                                    String systemId, String baseURI) {
-                        
+                        String loc = null;
+                        if (namespaceURI != null) {
+                            loc = schemaMap.getProperty(namespaceURI);
+                        }
+                        if (loc == null && publicId != null) {
+                            loc = schemaMap.getProperty(publicId);
+                        }
+                        if (loc == null && systemId != null) {
+                            loc = schemaMap.getProperty(systemId);
+                        }
+                        if (loc != null) {
+                            URL url = bundle.getResource(loc);
+                            if (url != null) {
+                                try {
+                                    StreamSource source 
+                                        = new StreamSource(url.openStream(), url.toExternalForm());
+                                    schemaSources.add(source);
+                                    return new SourceLSInput(source, publicId, url);
+                                } catch (IOException e) {
+                                    throw new RuntimeException(e);
+                                }
+                            }
+                        }
                         URI uri = URI.create((String) namespaceURI);
                         Set<NamespaceHandler> hs = NamespaceHandlerRegistryImpl.this.handlers.get(uri);
                         if (hs == null) {
@@ -268,48 +307,7 @@ public class NamespaceHandlerRegistryImp
                                     final StreamSource source 
                                         = new StreamSource(url.openStream(), url.toExternalForm());
                                     schemaSources.add(source);
-                                    return new LSInput() {
-                                        public Reader getCharacterStream() {
-                                            return null;
-                                        }
-                                        public void setCharacterStream(Reader characterStream) {
-                                        }
-                                        public InputStream getByteStream() {
-                                            return source.getInputStream();
-                                        }
-                                        public void setByteStream(InputStream byteStream) {
-                                        }
-                                        public String getStringData() {
-                                            return null;
-                                        }
-                                        public void setStringData(String stringData) {
-                                        }
-                                        public String getSystemId() {
-                                            return url.toExternalForm();
-                                        }
-                                        public void setSystemId(String systemId) {
-                                        }
-                                        public String getPublicId() {
-                                            return publicId;
-                                        }
-                                        public void setPublicId(String publicId) {
-                                        }
-                                        public String getBaseURI() {
-                                            return null;
-                                        }
-                                        public void setBaseURI(String baseURI) {
-                                        }
-                                        public String getEncoding() {
-                                            return null;
-                                        }
-                                        public void setEncoding(String encoding) {
-                                        }
-                                        public boolean getCertifiedText() {
-                                            return false;
-                                        }
-                                        public void setCertifiedText(boolean certifiedText) {
-                                        }
-                                    };
+                                    return new SourceLSInput(source, publicId, url);
                                 } catch (IOException e) {
                                     throw new RuntimeException(e);
                                 }
@@ -336,7 +334,10 @@ public class NamespaceHandlerRegistryImp
                     }
                 }
                 // Add our new schema
-                schemas.put(handlers, new SoftReference<Schema>(schema));
+                if (schemaMap.isEmpty()) {
+                    //only cache non-custom schemas
+                    schemas.put(handlers, new SoftReference<Schema>(schema));
+                }
             } finally {
                 for (StreamSource s : schemaSources) {
                     try {
@@ -349,6 +350,57 @@ public class NamespaceHandlerRegistryImp
         }
         return schema;
     }
+    
+    private class SourceLSInput implements LSInput {
+        StreamSource source;
+        URL systemId;
+        String publicId;
+        public SourceLSInput(StreamSource src, String pid, URL sys) {
+            source = src;
+            publicId = pid;
+            systemId = sys;
+        }
+        public Reader getCharacterStream() {
+            return null;
+        }
+        public void setCharacterStream(Reader characterStream) {
+        }
+        public InputStream getByteStream() {
+            return source.getInputStream();
+        }
+        public void setByteStream(InputStream byteStream) {
+        }
+        public String getStringData() {
+            return null;
+        }
+        public void setStringData(String stringData) {
+        }
+        public String getSystemId() {
+            return systemId.toExternalForm();
+        }
+        public void setSystemId(String systemId) {
+        }
+        public String getPublicId() {
+            return publicId;
+        }
+        public void setPublicId(String publicId) {
+        }
+        public String getBaseURI() {
+            return null;
+        }
+        public void setBaseURI(String baseURI) {
+        }
+        public String getEncoding() {
+            return null;
+        }
+        public void setEncoding(String encoding) {
+        }
+        public boolean getCertifiedText() {
+            return false;
+        }
+        public void setCertifiedText(boolean certifiedText) {
+        }
+    };
 
     protected synchronized void removeSchemasFor(NamespaceHandler handler) {
         List<Map<URI, NamespaceHandler>> keys = new ArrayList<Map<URI, NamespaceHandler>>();
@@ -375,6 +427,7 @@ public class NamespaceHandlerRegistryImp
         private final Bundle bundle;
         private final Set<URI> namespaces;
         private final Map<URI, NamespaceHandler> handlers;
+        private final Properties schemaMap = new Properties();
         private Schema schema;
 
         public NamespaceHandlerSetImpl(Set<URI> namespaces, Bundle bundle) {
@@ -385,6 +438,35 @@ public class NamespaceHandlerRegistryImp
             for (URI ns : namespaces) {
                 findCompatibleNamespaceHandler(ns);
             }
+            URL url = bundle.getResource("OSGI-INF/blueprint/schema.map");
+            System.out.println("MAP: " + url);
+            if (url != null) {
+                InputStream ins = null;
+                try {
+                    ins = url.openStream();
+                    schemaMap.load(ins);
+                } catch (IOException ex) {
+                    ex.printStackTrace();
+                    //ignore
+                } finally {
+                    if (ins != null) {
+                        try {
+                            ins.close();
+                        } catch (IOException e) {
+                            //ignore
+                        }
+                    }
+                }
+            }
+            System.out.println("MAP: " + schemaMap);
+            for (Object ns : schemaMap.keySet()) {
+                try {
+                    this.namespaces.remove(new URI(ns.toString()));
+                } catch (URISyntaxException e) {
+                    //ignore
+                }
+            }
+            System.out.println("NS: " + namespaces);
         }
 
         public boolean isComplete() {
@@ -404,7 +486,7 @@ public class NamespaceHandlerRegistryImp
                 throw new IllegalStateException("NamespaceHandlerSet is not complete");
             }
             if (schema == null) {
-                schema = NamespaceHandlerRegistryImpl.this.getSchema(handlers);
+                schema = NamespaceHandlerRegistryImpl.this.getSchema(handlers, bundle, schemaMap);
             }
             return schema;
         }

Modified: aries/trunk/blueprint/blueprint-testbundlea/src/main/resources/org/apache/aries/blueprint/testbundlea/nshandlersiximport.xsd
URL: http://svn.apache.org/viewvc/aries/trunk/blueprint/blueprint-testbundlea/src/main/resources/org/apache/aries/blueprint/testbundlea/nshandlersiximport.xsd?rev=1203399&r1=1203398&r2=1203399&view=diff
==============================================================================
--- aries/trunk/blueprint/blueprint-testbundlea/src/main/resources/org/apache/aries/blueprint/testbundlea/nshandlersiximport.xsd (original)
+++ aries/trunk/blueprint/blueprint-testbundlea/src/main/resources/org/apache/aries/blueprint/testbundlea/nshandlersiximport.xsd Thu Nov 17 22:32:58 2011
@@ -25,6 +25,7 @@
    <complexType name="Imported">
         <sequence>
             <element name="stuff" type="xsd:string"/>
+            <any minOccurs="0"/>
         </sequence>
    </complexType>
    <attribute name="attrib" type="xsd:string"/>

Modified: aries/trunk/blueprint/blueprint-testbundleb/src/main/resources/OSGI-INF/blueprint/config.xml
URL: http://svn.apache.org/viewvc/aries/trunk/blueprint/blueprint-testbundleb/src/main/resources/OSGI-INF/blueprint/config.xml?rev=1203399&r1=1203398&r2=1203399&view=diff
==============================================================================
--- aries/trunk/blueprint/blueprint-testbundleb/src/main/resources/OSGI-INF/blueprint/config.xml (original)
+++ aries/trunk/blueprint/blueprint-testbundleb/src/main/resources/OSGI-INF/blueprint/config.xml Thu Nov 17 22:32:58 2011
@@ -86,6 +86,7 @@
   <six:nshandlersix six:id="SIX" sixim:attrib="foo">
       <six:stuff>
           <sixim:stuff>foo</sixim:stuff>
+          <mytest:foo xmlns:mytest="http://ns.handler.mytest">blah</mytest:foo>
       </six:stuff>
   </six:nshandlersix> 
 </blueprint>

Added: aries/trunk/blueprint/blueprint-testbundleb/src/main/resources/OSGI-INF/blueprint/schema.map
URL: http://svn.apache.org/viewvc/aries/trunk/blueprint/blueprint-testbundleb/src/main/resources/OSGI-INF/blueprint/schema.map?rev=1203399&view=auto
==============================================================================
--- aries/trunk/blueprint/blueprint-testbundleb/src/main/resources/OSGI-INF/blueprint/schema.map (added)
+++ aries/trunk/blueprint/blueprint-testbundleb/src/main/resources/OSGI-INF/blueprint/schema.map Thu Nov 17 22:32:58 2011
@@ -0,0 +1 @@
+http\://ns.handler.mytest=/org/apache/aries/blueprint/testbundleb/mystuff.xsd
\ No newline at end of file

Copied: aries/trunk/blueprint/blueprint-testbundleb/src/main/resources/org/apache/aries/blueprint/testbundleb/mystuff.xsd (from r1203342, aries/trunk/blueprint/blueprint-testbundlea/src/main/resources/org/apache/aries/blueprint/testbundlea/nshandlersiximport.xsd)
URL: http://svn.apache.org/viewvc/aries/trunk/blueprint/blueprint-testbundleb/src/main/resources/org/apache/aries/blueprint/testbundleb/mystuff.xsd?p2=aries/trunk/blueprint/blueprint-testbundleb/src/main/resources/org/apache/aries/blueprint/testbundleb/mystuff.xsd&p1=aries/trunk/blueprint/blueprint-testbundlea/src/main/resources/org/apache/aries/blueprint/testbundlea/nshandlersiximport.xsd&r1=1203342&r2=1203399&rev=1203399&view=diff
==============================================================================
--- aries/trunk/blueprint/blueprint-testbundlea/src/main/resources/org/apache/aries/blueprint/testbundlea/nshandlersiximport.xsd (original)
+++ aries/trunk/blueprint/blueprint-testbundleb/src/main/resources/org/apache/aries/blueprint/testbundleb/mystuff.xsd Thu Nov 17 22:32:58 2011
@@ -16,16 +16,11 @@
     limitations under the License.
 -->
 <schema xmlns="http://www.w3.org/2001/XMLSchema" 
-        targetNamespace="http://ns.handler.six.import" 
-        xmlns:tns="http://ns.handler.six.import"
+        targetNamespace="http://ns.handler.mytest" 
+        xmlns:tns="http://ns.handler.mytest"
         xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
         elementFormDefault="qualified" 
         attributeFormDefault="qualified" >
 
-   <complexType name="Imported">
-        <sequence>
-            <element name="stuff" type="xsd:string"/>
-        </sequence>
-   </complexType>
-   <attribute name="attrib" type="xsd:string"/>
+    <element name="foo" type="xsd:string"/>
 </schema>
\ No newline at end of file