You are viewing a plain text version of this content. The canonical link for it is here.
Posted to cvs@cocoon.apache.org by pi...@apache.org on 2005/09/08 00:13:39 UTC

svn commit: r279438 - in /cocoon/branches/BRANCH_2_1_X/src/blocks/validation/java/org/apache/cocoon/components/validation/impl: JingContext.java XercesEntityResolver.java XercesGrammarParser.java XercesSchema.java

Author: pier
Date: Wed Sep  7 15:13:34 2005
New Revision: 279438

URL: http://svn.apache.org/viewcvs?rev=279438&view=rev
Log:
Implementation of resolver for Xerces

Modified:
    cocoon/branches/BRANCH_2_1_X/src/blocks/validation/java/org/apache/cocoon/components/validation/impl/JingContext.java
    cocoon/branches/BRANCH_2_1_X/src/blocks/validation/java/org/apache/cocoon/components/validation/impl/XercesEntityResolver.java
    cocoon/branches/BRANCH_2_1_X/src/blocks/validation/java/org/apache/cocoon/components/validation/impl/XercesGrammarParser.java
    cocoon/branches/BRANCH_2_1_X/src/blocks/validation/java/org/apache/cocoon/components/validation/impl/XercesSchema.java

Modified: cocoon/branches/BRANCH_2_1_X/src/blocks/validation/java/org/apache/cocoon/components/validation/impl/JingContext.java
URL: http://svn.apache.org/viewcvs/cocoon/branches/BRANCH_2_1_X/src/blocks/validation/java/org/apache/cocoon/components/validation/impl/JingContext.java?rev=279438&r1=279437&r2=279438&view=diff
==============================================================================
--- cocoon/branches/BRANCH_2_1_X/src/blocks/validation/java/org/apache/cocoon/components/validation/impl/JingContext.java (original)
+++ cocoon/branches/BRANCH_2_1_X/src/blocks/validation/java/org/apache/cocoon/components/validation/impl/JingContext.java Wed Sep  7 15:13:34 2005
@@ -138,7 +138,7 @@
      */
     public InputSource resolveEntity(String publicId, String systemId)
     throws SAXException, IOException {
-        if (this.sourceValidity == null) throw new IllegalStateException();
+        if (this.sourceValidity == null) throw new IOException("Can't resolve now");
 
         /* Try to resolve the public id if we don't have a system id */
         if (systemId == null) {

Modified: cocoon/branches/BRANCH_2_1_X/src/blocks/validation/java/org/apache/cocoon/components/validation/impl/XercesEntityResolver.java
URL: http://svn.apache.org/viewcvs/cocoon/branches/BRANCH_2_1_X/src/blocks/validation/java/org/apache/cocoon/components/validation/impl/XercesEntityResolver.java?rev=279438&r1=279437&r2=279438&view=diff
==============================================================================
--- cocoon/branches/BRANCH_2_1_X/src/blocks/validation/java/org/apache/cocoon/components/validation/impl/XercesEntityResolver.java (original)
+++ cocoon/branches/BRANCH_2_1_X/src/blocks/validation/java/org/apache/cocoon/components/validation/impl/XercesEntityResolver.java Wed Sep  7 15:13:34 2005
@@ -17,68 +17,91 @@
 
 import java.io.IOException;
 
+import org.apache.excalibur.source.Source;
+import org.apache.excalibur.source.SourceResolver;
 import org.apache.excalibur.source.SourceValidity;
+import org.apache.excalibur.source.impl.validity.AggregatedValidity;
+import org.apache.excalibur.xml.EntityResolver;
 import org.apache.xerces.xni.XMLResourceIdentifier;
 import org.apache.xerces.xni.XNIException;
 import org.apache.xerces.xni.parser.XMLEntityResolver;
 import org.apache.xerces.xni.parser.XMLInputSource;
+import org.xml.sax.InputSource;
+import org.xml.sax.SAXException;
 
 /**
- * <p>TODO: ...</p>
+ * <p>An implementation of Xerces' {@link XMLEntityResolver} resolving URIs using
+ * Excalibur's {@link SourceResolver} and {@link EntityResolver}.</p>
  *
  * @author <a href="mailto:pier@betaversion.org">Pier Fumagalli</a>
  */
 public class XercesEntityResolver implements XMLEntityResolver {
     
+    private final AggregatedValidity sourceValidity = new AggregatedValidity();
+    private final SourceResolver sourceResolver;
+    private final EntityResolver entityResolver;
+
+    public XercesEntityResolver() {
+        this(null, null);
+    }
+
+    public XercesEntityResolver(SourceResolver sourceResolver,
+                                EntityResolver entityResolver) {
+        this.entityResolver = entityResolver;
+        this.sourceResolver = sourceResolver;
+    }
+
     public SourceValidity getSourceValidity() {
-        return null;
+        return this.sourceValidity;
     }
 
-    public XMLInputSource resolveEntity(String location)
+    public XMLInputSource resolveUri(String location)
     throws XNIException, IOException {
-        /*
+        if (this.sourceResolver == null) throw new IOException("Can't resolve now");
+
+        /* Use Cocoon's SourceResolver to resolve the system id */
+        Source source = this.sourceResolver.resolveURI(location);
+        location = source.getURI();
         try {
-            URI base = new URI("file://" + System.getProperty("user.dir") + "/");
-            System.err.println("BASE URI: " + base.toASCIIString());
-            URI relative = new URI(location);
-            System.err.println("RELATIVE: " + relative.toASCIIString());
-            URI resolved = base.resolve(relative);
-            System.err.println("RELATIVE: " + resolved.toASCIIString());
-            location = resolved.toASCIIString();
-            XMLInputSource source = new XMLInputSource(null, location, location);
-            source.setByteStream(resolved.toURL().openStream());
-            return source;
-        } catch (URISyntaxException exception) {
-            String message = "Cannot resolve " + location;
-            Throwable throwable = new IOException(message);
-            throw (IOException) throwable.initCause(exception);
+            this.sourceValidity.add(source.getValidity());
+            XMLInputSource input = new XMLInputSource(null, location, location);
+            input.setByteStream(source.getInputStream());
+            return input;
+        } finally {
+            this.sourceResolver.release(source);
         }
-        */
-        throw new IOException("Not implemented"); 
     }
 
     public XMLInputSource resolveEntity(XMLResourceIdentifier identifier)
     throws XNIException, IOException {
-        /*
-        System.err.println("Resolving Identifier: "
-                           + identifier.getLiteralSystemId()
-                           + " from " + identifier.getBaseSystemId()
-                           + " [exp=" + identifier.getExpandedSystemId() + "]");
+        if (this.sourceResolver == null) throw new IOException("Can't resolve now");
+
+        String publicId = identifier.getPublicId();
+        String systemId = identifier.getLiteralSystemId();
+        String baseURI = identifier.getBaseSystemId();
+
+        /* Try to resolve the public id if we don't have a system id */
+        if ((systemId == null) && (this.entityResolver != null)) try {
+            InputSource source = this.entityResolver.resolveEntity(publicId, null);
+            if ((source == null) || (source.getSystemId() == null)) {
+                throw new IOException("Can't resolve \"" + publicId + "\"");
+            } else {
+                systemId = source.getSystemId();
+            }
+        } catch (SAXException exception) {
+            throw new XNIException("Error resolving public id", exception);
+        }
+
+        /* Use Cocoon's SourceResolver to resolve the system id */
+        Source source = this.sourceResolver.resolveURI(systemId, baseURI, null);
+        systemId = source.getURI();
         try {
-            URI base = new URI(identifier.getBaseSystemId());
-            URI relative = new URI(identifier.getLiteralSystemId());
-            URI resolved = base.resolve(relative);
-            XMLInputSource source = new XMLInputSource(identifier.getPublicId(),
-                    resolved.toASCIIString(),
-                    base.toASCIIString());
-            source.setByteStream(resolved.toURL().openStream());
-            return source;
-        } catch (URISyntaxException exception) {
-            String message = "Cannot resolve " + identifier.getLiteralSystemId();
-            Throwable throwable = new IOException(message);
-            throw (IOException) throwable.initCause(exception);
+            this.sourceValidity.add(source.getValidity());
+            XMLInputSource input = new XMLInputSource(publicId, systemId, baseURI);
+            input.setByteStream(source.getInputStream());
+            return input;
+        } finally {
+            this.sourceResolver.release(source);
         }
-        */
-        throw new IOException("Not implemented"); 
     }
 }

Modified: cocoon/branches/BRANCH_2_1_X/src/blocks/validation/java/org/apache/cocoon/components/validation/impl/XercesGrammarParser.java
URL: http://svn.apache.org/viewcvs/cocoon/branches/BRANCH_2_1_X/src/blocks/validation/java/org/apache/cocoon/components/validation/impl/XercesGrammarParser.java?rev=279438&r1=279437&r2=279438&view=diff
==============================================================================
--- cocoon/branches/BRANCH_2_1_X/src/blocks/validation/java/org/apache/cocoon/components/validation/impl/XercesGrammarParser.java (original)
+++ cocoon/branches/BRANCH_2_1_X/src/blocks/validation/java/org/apache/cocoon/components/validation/impl/XercesGrammarParser.java Wed Sep  7 15:13:34 2005
@@ -54,22 +54,23 @@
     throws IOException, SAXException {
         /* Create a Xerces Grammar Pool and Entity Resolver */
         XMLGrammarPool pool = new XMLGrammarPoolImpl();
-        XercesEntityResolver resolver = new XercesEntityResolver();
+        XercesEntityResolver res = new XercesEntityResolver(super.sourceResolver,
+                                                            super.entityResolver);
 
         /* Create a Xerces component manager contextualizing the loader */
-        XercesContext context = new XercesContext(pool, resolver);
+        XercesContext context = new XercesContext(pool, res);
 
         /* Create a new XML Schema Loader and set the pool into it */
         XMLGrammarLoader loader = this.newGrammarLoader();
         ((XMLComponent) loader).reset(context);
 
         /* Load (parse and interpret) the grammar */
-        loader.loadGrammar(resolver.resolveEntity(uri));
-        
+        loader.loadGrammar(res.resolveUri(uri));
+
         /* Return a new Schema instance */
-        SourceValidity validity = resolver.getSourceValidity();
+        SourceValidity validity = res.getSourceValidity();
         Class validator = this.getValidationHandlerClass();
-        return new XercesSchema(pool, validity, resolver, validator);
+        return new XercesSchema(pool, validity, validator);
     }
 
     /**

Modified: cocoon/branches/BRANCH_2_1_X/src/blocks/validation/java/org/apache/cocoon/components/validation/impl/XercesSchema.java
URL: http://svn.apache.org/viewcvs/cocoon/branches/BRANCH_2_1_X/src/blocks/validation/java/org/apache/cocoon/components/validation/impl/XercesSchema.java?rev=279438&r1=279437&r2=279438&view=diff
==============================================================================
--- cocoon/branches/BRANCH_2_1_X/src/blocks/validation/java/org/apache/cocoon/components/validation/impl/XercesSchema.java (original)
+++ cocoon/branches/BRANCH_2_1_X/src/blocks/validation/java/org/apache/cocoon/components/validation/impl/XercesSchema.java Wed Sep  7 15:13:34 2005
@@ -30,7 +30,6 @@
  */
 public class XercesSchema extends AbstractSchema {
     
-    private final XercesEntityResolver entityResolver;
     private final XMLGrammarPool grammarPool;
     private final Class validatorClass;
 
@@ -38,10 +37,9 @@
      * <p>Create a new {@link XercesSchema} instance.</p>
      */
     public XercesSchema(XMLGrammarPool grammarPool, SourceValidity sourceValidity,
-                        XercesEntityResolver entityResolver, Class validatorClass) {
+                        Class validatorClass) {
         super(sourceValidity);
         grammarPool.lockPool();
-        this.entityResolver = entityResolver;
         this.validatorClass = validatorClass;
         this.grammarPool = grammarPool;
     }
@@ -57,7 +55,7 @@
     public ContentHandler newValidator(ErrorHandler errorHandler)
     throws SAXException {
         XercesContext context = new XercesContext(this.grammarPool,
-                                                  this.entityResolver,
+                                                  new XercesEntityResolver(),
                                                   errorHandler);
         try {
             Object instance = this.validatorClass.newInstance();