You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@xmlbeans.apache.org by da...@apache.org on 2003/09/23 01:53:21 UTC

cvs commit: xml-xmlbeans/v1/src/xsdschema/schema XMLSchema.xsd

davidbau    2003/09/22 16:53:21

  Modified:    v1/external/lib oldxbean.jar
               v1/src/typeimpl/org/apache/xmlbeans/impl/schema
                        StscImporter.java StscState.java
               v1/src/xmlcomp/org/apache/xmlbeans/impl/tool SchemaCopy.java
               v1/src/xmlpublic/org/apache/xmlbeans XmlOptions.java
               v1/src/xsdschema/schema XMLSchema.xsd
  Log:
  This change fixes two things:
  
  (1) It adds an "EntityResolver" option (see org.apache.xmlbeans.XmlOptions)
  that is used by the Schema compiler to resolve URL references.  This allows
  folks to define their own URL schemes, etc.  Very simple to do, yet wildly
  useful for lots of tricks.  We treat namespace URIs (when applicable) as
  publicIDs on the EntityResolver/InputSource interfaces.
  
  (2) It makes a minor update to XMLSchema.xsd to bring it up-to-date with
  the latest on www.w3.org.  I've also posted on xmlschema-dev@ to tell them
  about the problems we've found+fixed.
  
  Code review: eric vasilik
  Bootstrap: passed
  Regression tests: passed
  
  Revision  Changes    Path
  1.2       +2222 -2203xml-xmlbeans/v1/external/lib/oldxbean.jar
  
  	<<Binary file>>
  
  
  1.2       +73 -20    xml-xmlbeans/v1/src/typeimpl/org/apache/xmlbeans/impl/schema/StscImporter.java
  
  Index: StscImporter.java
  ===================================================================
  RCS file: /home/cvs/xml-xmlbeans/v1/src/typeimpl/org/apache/xmlbeans/impl/schema/StscImporter.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- StscImporter.java	15 Sep 2003 18:20:29 -0000	1.1
  +++ StscImporter.java	22 Sep 2003 23:53:20 -0000	1.2
  @@ -76,11 +76,19 @@
   import java.net.URL;
   import java.net.MalformedURLException;
   import java.io.IOException;
  +import java.io.Reader;
  +import java.io.InputStream;
   
   import org.apache.xmlbeans.XmlObject;
   import org.apache.xmlbeans.XmlException;
   import org.apache.xmlbeans.XmlOptions;
  +import org.apache.xmlbeans.SchemaTypeLoader;
   import org.apache.xmlbeans.impl.common.XmlErrorContext;
  +import org.xml.sax.EntityResolver;
  +import org.xml.sax.InputSource;
  +import org.xml.sax.SAXException;
  +
  +import javax.xml.transform.Source;
   
   public class StscImporter
   {
  @@ -383,28 +391,10 @@
                   return null;
               }
   
  -            if (absoluteURL.startsWith(PROJECT_URL_PREFIX))
  -            {
  -                state.error("Could not find specified resource in the local project.", XmlErrorContext.CANNOT_FIND_RESOURCE, referencedBy);
  -                addFailedDownload(absoluteURL);
  -                return null;
  -            }
  -
               // try to download
               download: try
               {
  -                // For parsing XSD and WSDL files, we should use the SchemaDocument
  -                // classloader rather than the thread context classloader.  This is
  -                // because in some situations (such as when being invoked by ant
  -                // underneath the ide) the context classloader is potentially weird
  -                // (because of the design of ant).
  -
  -                XmlOptions options = new XmlOptions();
  -                options.put( XmlOptions.LOAD_LINE_NUMBERS );
  -                options.put( XmlOptions.LOAD_MESSAGE_DIGEST );
  -
  -                URL urlDownload = new URL(absoluteURL);
  -                XmlObject xdoc = state.getS4SLoader().parse(urlDownload, null, options);
  +                XmlObject xdoc = downloadDocument(state.getS4SLoader(), targetNamespace, absoluteURL);
   
                   Schema result = findMatchByDigest(xdoc);
                   String shortname = state.relativize(absoluteURL);
  @@ -439,7 +429,7 @@
               }
               catch (MalformedURLException malformed)
               {
  -                state.error("URL is not well-formed", XmlErrorContext.CANNOT_FIND_RESOURCE, referencedBy);
  +                state.error("URL \"" + absoluteURL + "\" is not well-formed", XmlErrorContext.CANNOT_FIND_RESOURCE, referencedBy);
               }
               catch (IOException connectionProblem)
               {
  @@ -453,6 +443,69 @@
               // record failure so that we don't try to download this URL again
               addFailedDownload(absoluteURL);
               return null;
  +        }
  +        
  +        private XmlObject downloadDocument(SchemaTypeLoader loader, String namespace, String absoluteURL)
  +                throws MalformedURLException, IOException, XmlException
  +        {
  +            StscState state = StscState.get();
  +            
  +            EntityResolver resolver = state.getEntityResolver();
  +            if (resolver != null)
  +            {
  +                InputSource source;
  +                try
  +                {
  +                    source = resolver.resolveEntity(namespace, absoluteURL);
  +                }
  +                catch (SAXException e)
  +                {
  +                    throw new XmlException(e);
  +                }
  +                    
  +                // first preference for InputSource contract: character stream
  +                Reader reader = source.getCharacterStream();
  +                if (reader != null)
  +                {
  +                    XmlOptions options = new XmlOptions();
  +                    options.setLoadLineNumbers();
  +                    options.setDocumentSourceName(absoluteURL);
  +                    return loader.parse(reader, null, options);
  +                }
  +                
  +                // second preference for InputSource contract: 
  +                InputStream bytes = source.getByteStream();
  +                if (bytes != null)
  +                {
  +                    String encoding = source.getEncoding();
  +                    XmlOptions options = new XmlOptions();
  +                    options.setLoadLineNumbers();
  +                    options.setLoadMessageDigest();
  +                    options.setDocumentSourceName(absoluteURL);
  +                    if (encoding != null)
  +                        options.setCharacterEncoding(encoding);
  +                    return loader.parse(bytes, null, options);
  +                }
  +                
  +                // third preference: use the (possibly redirected) url
  +                String urlToLoad = source.getSystemId();
  +                if (urlToLoad == null)
  +                    throw new IOException("EntityResolver unable to resolve " + absoluteURL + " (for namespace " + namespace + ")");
  +                XmlOptions options = new XmlOptions();
  +                options.setLoadLineNumbers();
  +                options.setLoadMessageDigest();
  +                options.setDocumentSourceName(absoluteURL);
  +                URL urlDownload = new URL(urlToLoad);
  +                return loader.parse(urlDownload, null, options);
  +            }
  +            
  +            // no resolver - just use the URL directly, no substitution
  +            XmlOptions options = new XmlOptions();
  +            options.setLoadLineNumbers();
  +            options.setLoadMessageDigest();
  +                
  +            URL urlDownload = new URL(absoluteURL);
  +            return loader.parse(urlDownload, null, options);
           }
   
           private void addSuccessfulDownload(NsLocPair key, Schema schema)
  
  
  
  1.2       +15 -8     xml-xmlbeans/v1/src/typeimpl/org/apache/xmlbeans/impl/schema/StscState.java
  
  Index: StscState.java
  ===================================================================
  RCS file: /home/cvs/xml-xmlbeans/v1/src/typeimpl/org/apache/xmlbeans/impl/schema/StscState.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- StscState.java	15 Sep 2003 18:20:29 -0000	1.1
  +++ StscState.java	22 Sep 2003 23:53:20 -0000	1.2
  @@ -83,6 +83,7 @@
   import javax.xml.namespace.QName;
   
   import org.w3.x2001.xmlSchema.SchemaDocument;
  +import org.xml.sax.EntityResolver;
   
   /**
    * This class represents the state of the SchemaTypeSystemCompiler as it's
  @@ -120,7 +121,8 @@
       private boolean _noUpa;
       private boolean _noPvr;
       private Set _mdefNamespaces     = buildDefaultMdefNamespaces();
  -    
  +    private EntityResolver _entityResolver;
  +
       private static Set buildDefaultMdefNamespaces()
       {
           // namespaces which are known to appear in WSDLs redundantly
  @@ -269,13 +271,6 @@
       }
   
       /**
  -     * Whether to allow downloads or not.
  -     */
  -    public void setDoingDownloads(boolean doingDownloads)
  -    {
  -    }
  -
  -    /**
        * True if the given URI is a local file
        */
       public boolean shouldDownloadURI(String uriString)
  @@ -314,8 +309,20 @@
                   !"true".equals(System.getProperty("xmlbean.particlerestriction", "true"));
           _doingDownloads = options.hasOption(XmlOptions.COMPILE_DOWNLOAD_URLS) ? true :
                   "true".equals(System.getProperty("xmlbean.downloadurls", "false"));
  +        _entityResolver = (EntityResolver)options.get(XmlOptions.ENTITY_RESOLVER);
  +        if (_entityResolver != null)
  +            _doingDownloads = true;
  +        
           if (options.hasOption(XmlOptions.COMPILE_MDEF_NAMESPACES))
               _mdefNamespaces.addAll((Collection)options.get(XmlOptions.COMPILE_MDEF_NAMESPACES));
  +    }
  +    
  +    /**
  +     * May return null if there is no custom entity resolver.
  +     */ 
  +    public EntityResolver getEntityResolver()
  +    {
  +        return _entityResolver;
       }
       
       /**
  
  
  
  1.2       +2 -1      xml-xmlbeans/v1/src/xmlcomp/org/apache/xmlbeans/impl/tool/SchemaCopy.java
  
  Index: SchemaCopy.java
  ===================================================================
  RCS file: /home/cvs/xml-xmlbeans/v1/src/xmlcomp/org/apache/xmlbeans/impl/tool/SchemaCopy.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- SchemaCopy.java	15 Sep 2003 18:20:33 -0000	1.1
  +++ SchemaCopy.java	22 Sep 2003 23:53:20 -0000	1.2
  @@ -211,7 +211,7 @@
       
       private static final XmlOptions loadOptions = new XmlOptions().
               setLoadSubstituteNamespaces(Collections.singletonMap(
  -                    "http://www.apache.org/internal/xmlbeans/wsdlsubst", "http://schemas.xmlsoap.org/wsdl/"
  +                    "http://schemas.xmlsoap.org/wsdl/", "http://www.apache.org/internal/xmlbeans/wsdlsubst" 
               ));
   
       private static Map findRelativeInOne(URI source, URI target)
  @@ -224,6 +224,7 @@
               xcur.toFirstChild();
   
               Map result = new LinkedHashMap();
  +
               if (xobj instanceof SchemaDocument)
                   putMappingsFromSchema(result, source, target, ((SchemaDocument)xobj).getSchema());
               else if (xobj instanceof DefinitionsDocument)
  
  
  
  1.2       +20 -0     xml-xmlbeans/v1/src/xmlpublic/org/apache/xmlbeans/XmlOptions.java
  
  Index: XmlOptions.java
  ===================================================================
  RCS file: /home/cvs/xml-xmlbeans/v1/src/xmlpublic/org/apache/xmlbeans/XmlOptions.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- XmlOptions.java	15 Sep 2003 18:20:35 -0000	1.1
  +++ XmlOptions.java	22 Sep 2003 23:53:21 -0000	1.2
  @@ -56,6 +56,8 @@
   
   package org.apache.xmlbeans;
   
  +import org.xml.sax.EntityResolver;
  +
   import java.util.HashMap;
   import java.util.Map;
   import java.util.Collection;
  @@ -563,6 +565,21 @@
           return set( VALIDATE_ON_SET );
       }
   
  +    /**
  +     * If this option is set when compiling a schema, then the given
  +     * EntityResolver will be consulted in order to resolve any
  +     * URIs while downloading imported schemas.
  +     *
  +     * EntityResolvers are currently only used by compileXsd; they
  +     * are not consulted by other functions, for example, parse.
  +     * This will likely change in the future.
  +     * 
  +     * @see XmlBeans#compileXsd
  +     */
  +    public XmlOptions setEntityResolver(EntityResolver resolver) {
  +        return set( ENTITY_RESOLVER, resolver );
  +    }
  +
   
       //
       // Complete set of XmlOption's
  @@ -641,6 +658,9 @@
       public static final String COMPILE_MDEF_NAMESPACES         =  "COMPILE_MDEF_NAMESPACES";
       /** @exclude */
       public static final String VALIDATE_ON_SET                 =  "VALIDATE_ON_SET";
  +    /** @exclude */
  +    public static final String ENTITY_RESOLVER                 =  "ENTITY_RESOLVER";
  +    
   
       private static final XmlOptions EMPTY_OPTIONS;
       static {
  
  
  
  1.2       +5 -5      xml-xmlbeans/v1/src/xsdschema/schema/XMLSchema.xsd
  
  Index: XMLSchema.xsd
  ===================================================================
  RCS file: /home/cvs/xml-xmlbeans/v1/src/xsdschema/schema/XMLSchema.xsd,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- XMLSchema.xsd	15 Sep 2003 18:20:39 -0000	1.1
  +++ XMLSchema.xsd	22 Sep 2003 23:53:21 -0000	1.2
  @@ -73,7 +73,7 @@
    <xs:annotation>
     <xs:documentation>
       Part 1 version: Id: XMLSchema.xsd,v 1.53 2003/02/24 17:40:07 ht Exp 
  -    Part 2 version: Id: datatypes.xsd,v 1.58 2003/02/26 14:01:29 ht Exp 
  +    Part 2 version: Id: datatypes.xsd,v 1.60 2003/04/05 11:02:30 ht Exp 
     </xs:documentation>
    </xs:annotation>
   
  @@ -192,7 +192,7 @@
      <xs:documentation>
      A utility type, not for public use</xs:documentation>
      <xs:documentation>
  -   #all or (possibly empty) subset of {extension, restriction}</xs:documentation>
  +   #all or (possibly empty) subset of {extension, restriction, list, union}</xs:documentation>
     </xs:annotation>
     <xs:union>
      <xs:simpleType>    
  @@ -1140,7 +1140,7 @@
                              child:: is also allowed
            </xs:documentation>
           </xs:annotation>
  -        <!-- TSH commented out since this incorrectly does not allow white space -->
  +        <!-- TSH modified to allow white space -->
           <xs:pattern
               value="(\.\s*//\s*)?(((child\s*::\s*)?((\i\c*:)?(\i\c*|\*)))|\.)(\s*/\s*(((child\s*::\s*)?((\i\c*:)?(\i\c*|\*)))|\.))*(\s*\|\s*(\.\s*//\s*)?(((child\s*::\s*)?((\i\c*:)?(\i\c*|\*)))|\.)(\s*/\s*(((child\s*::\s*)?((\i\c*:)?(\i\c*|\*)))|\.))*)*">
           </xs:pattern>
  @@ -1175,7 +1175,7 @@
             Path    ::=    ('.//')? ( Step '/' )* ( Step | '@' NameTest ) 
            </xs:documentation>
           </xs:annotation>
  -        <!-- TSH commented out since this incorrectly does not allow white space -->
  +        <!-- TSH modified to allow white space -->
           <xs:pattern value="(\.\s*//\s*)?((((child\s*::\s*)?((\i\c*:)?(\i\c*|\*)))|\.)\s*/\s*)*((((child\s*::\s*)?((\i\c*:)?(\i\c*|\*)))|\.)|((attribute\s*::\s*|@\s*)((\i\c*:)?(\i\c*|\*))))(\s*\|\s*(\s*\.//\s*)?((((child\s*::\s*)?((\i\c*:)?(\i\c*|\*)))|\.)/)*((((child\s*::\s*)?((\i\c*:)?(\i\c*|\*)))|\.)|((attribute\s*::\s*|@\s*)((\i\c*:)?(\i\c*|\*)))))*">
           </xs:pattern>
          </xs:restriction>
  @@ -2034,7 +2034,7 @@
       </xs:annotation>
       <xs:restriction base="xs:decimal">
         <xs:fractionDigits value="0" fixed="true" id="integer.fractionDigits"/>
  -      <xs:pattern value='[-+]?[0-9]+'/>
  +      <xs:pattern value="[\-+]?[0-9]+"/>
       </xs:restriction>
     </xs:simpleType>
   
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: xmlbeans-cvs-unsubscribe@xml.apache.org
For additional commands, e-mail: xmlbeans-cvs-help@xml.apache.org