xmlbeans-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From p...@apache.org
Subject cvs commit: xml-xmlbeans/v2/test/src/drt/drtcases BindingTests.java
Date Thu, 18 Dec 2003 01:58:59 GMT
pcal        2003/12/17 17:58:59

  Modified:    v2/src/binding/org/apache/xmlbeans/impl/binding/compile
                        BindingCompiler.java BindingCompilerTask.java
                        Java2Schema.java Java2SchemaTask.java
                        Schema2Java.java Schema2JavaTask.java
                        SchemaSourceSet.java
               v2/src/binding/org/apache/xmlbeans/impl/binding/tylar
                        DefaultTylarLoader.java ExplodedTylar.java
                        ExplodedTylarImpl.java TylarLoader.java
               v2/test/src/drt/drtcases BindingTests.java
  Added:       v2/src/binding/org/apache/xmlbeans/impl/binding/tylar
                        TylarImpl.java
  Removed:     v2/src/binding/org/apache/xmlbeans/impl/binding/tylar
                        JarredTylar.java
  Log:
  - more refactoring of buildtime
  - fix bug in TylarLoader
  
  Revision  Changes    Path
  1.4       +139 -2    xml-xmlbeans/v2/src/binding/org/apache/xmlbeans/impl/binding/compile/BindingCompiler.java
  
  Index: BindingCompiler.java
  ===================================================================
  RCS file: /home/cvs/xml-xmlbeans/v2/src/binding/org/apache/xmlbeans/impl/binding/compile/BindingCompiler.java,v
  retrieving revision 1.3
  retrieving revision 1.4
  diff -u -r1.3 -r1.4
  --- BindingCompiler.java	16 Dec 2003 09:55:05 -0000	1.3
  +++ BindingCompiler.java	18 Dec 2003 01:58:58 -0000	1.4
  @@ -57,10 +57,22 @@
   
   import org.apache.xmlbeans.impl.binding.tylar.*;
   import org.apache.xmlbeans.impl.binding.joust.CompilingJavaOutputStream;
  +import org.apache.xmlbeans.impl.binding.bts.BindingLoader;
  +import org.apache.xmlbeans.impl.binding.bts.PathBindingLoader;
  +import org.apache.xmlbeans.impl.binding.bts.BuiltinBindingLoader;
   import org.apache.xmlbeans.impl.jam.JElement;
  +import org.apache.xmlbeans.XmlObject;
  +import org.apache.xmlbeans.XmlBeans;
  +import org.apache.xmlbeans.SchemaTypeLoader;
  +import org.apache.xmlbeans.XmlException;
  +import org.apache.xmlbeans.SchemaTypeSystem;
  +import org.apache.xmlbeans.XmlOptions;
  +import org.w3.x2001.xmlSchema.SchemaDocument;
   import java.io.File;
   import java.io.IOException;
   import java.util.logging.Level;
  +import java.util.ArrayList;
  +import java.util.Collection;
   
   /**
    * Abstract base class for classes which produce a tylar based on
  @@ -74,6 +86,9 @@
    * a BindingCompiler is stateful; in addition to the compilation logic, it
    * also includes a set of input artifacts (or at least pointers to them).
    *
  + * Also note that BindingCompilers are not threadsafe.  It's not clear why
  + * multiple threads would want to access one, anyway.  Just don't do it.
  + *
    * @author Patrick Calahan <pcal@bea.com>
    */
   public abstract class BindingCompiler {
  @@ -91,6 +106,10 @@
     private boolean mIgnoreErrors = false;
     private boolean mVerbose = false;
     private boolean mDoCompile = true;
  +  private Tylar[] mBaseLibraries = null;
  +  private BindingLoader mBaseBindingLoader = null;
  +  private SchemaTypeLoader mBaseSchemaTypeLoader = null;
  +  private boolean mIsCompilationStarted = false;
   
     // this is the joust we use to build up the tylar that is passed to
     // the subclass' bind() methods in all cases.  However, BindingCompiler
  @@ -114,7 +133,7 @@
      * to be thrown; the extending class should call logError() with any
      * unexpected exceptions -  BindingCompiler takes care of the rest.
      */
  -  protected abstract void bind(TylarWriter writer);
  +  public abstract void bind(TylarWriter writer);
   
     // ========================================================================
     // Public methods
  @@ -174,11 +193,29 @@
     }
   
     /**
  +   * Sets the list type libraries that should be checked for resolving
  +   * bindings before creating bindings in the new tylar being compiled.
  +   * Bindings provided in these tylars will preempt the compilation of
  +   * equivalent bindings in the tylar generated by this compiler, so it is
  +   * the user's responsibility to ensure that these tylars are made available
  +   * at runtime.
  +   *
  +   * This is an optional setting; if the libraries are not provided, only the
  +   * default (builtin) loader will be used.
  +   */
  +  public void setBaseLibraries(Tylar[] list) {
  +    assertCompilationStarted(false);
  +    if (list == null) throw new IllegalArgumentException("null list");
  +    mBaseLibraries = list;
  +  }
  +
  +  /**
      * Sets the BindingLogger which will receive log messages from work
      * done by this BindingCompiler.
      */
     public void setLogger(BindingLogger logger) {
       if (logger == null) throw new IllegalArgumentException("null logger");
  +    assertCompilationStarted(false);
       mLogger = logger;
     }
   
  @@ -188,7 +225,8 @@
      * This is false bydefault, and generally should be made true only
      * for debugging.
      */
  -  public void setIgnoreSeverErrors(boolean really) {
  +  public void setIgnoreSevereErrors(boolean really) {
  +    assertCompilationStarted(false);
       mIgnoreErrors = true;
     }
   
  @@ -199,6 +237,7 @@
      * have no effect.
      */
     public void setCompileJava(boolean b) {
  +    assertCompilationStarted(false);
       mDoCompile = b;
     }
   
  @@ -209,6 +248,7 @@
      * setting this may have no effect.
      */
     public void setJavac(String javacPath) {
  +    assertCompilationStarted(false);
       mJoust.setJavac(javacPath);
     }
   
  @@ -219,6 +259,7 @@
      * code at all, so setting this may have no effect.
      */
     public void setJavacClasspath(File[] classpath) {
  +    assertCompilationStarted(false);
       mJoust.setJavacClasspath(classpath);
     }
   
  @@ -229,6 +270,7 @@
      * generate any source code at all, so setting this may have no effect.
      */
     public void setKeepGeneratedJava(boolean b) {
  +    assertCompilationStarted(false);
       mJoust.setKeepGenerated(b);
     }
   
  @@ -236,9 +278,104 @@
      * Enables verbose output to our BindingLogger.
      */
     public void setVerbose(boolean b) {
  +    assertCompilationStarted(false);
       mJoust.setVerbose(b);
       mVerbose = b;
     }
  +
  +  // ========================================================================
  +  // Protected methods
  +
  +  /**
  +   * Subclasses should call this method to retrieve the BindingLoader
  +   * to use as a basis for the binding process.  Normally, this will
  +   * simply be the builtin loader.  However, if the user has
  +   * setExistingBindings, the returned loader will also include those
  +   * bindings as well.  Note that this method must not be called until
  +   * binding has actually begun.
  +   *
  +   * @throws IllegalStateException if this method is called before
  +   * the abstract bind() method is called.
  +   */
  +  protected BindingLoader getBaseBindingLoader() {
  +    assertCompilationStarted(true);
  +    if (mBaseBindingLoader == null) {
  +      if (mBaseLibraries == null) {
  +        mBaseBindingLoader = BuiltinBindingLoader.getInstance();
  +      } else {
  +        //new up a loader on the bindings provided in the base libraries
  +        //they gave us
  +        BindingLoader[] loaders = new BindingLoader[mBaseLibraries.length+1];
  +        for(int i=0; i<mBaseLibraries.length; i++) {
  +          loaders[i] = mBaseLibraries[i].getBindingFile();
  +        }
  +        loaders[loaders.length-1] = BuiltinBindingLoader.getInstance();
  +        mBaseBindingLoader = PathBindingLoader.forPath(loaders);
  +      }
  +    }
  +    return mBaseBindingLoader;
  +  }
  +
  +
  +  /**
  +   * Returns a SchemaTypeLoader to be used as a basis for the binding process.
  +   * Normally, this will simply be the builtin loader.  However, if the user
  +   * has setBaseLibraries, the returned loader will also include the schema
  +   * types in those libraries.  Note that this method must not be called until
  +   * binding has actually begun.
  +   *
  +   * @throws IllegalStateException if this method is called before
  +   * the abstract bind() method is called.
  +   */
  +  protected SchemaTypeLoader getBaseSchemaTypeLoader() throws XmlException
  +  {
  +    assertCompilationStarted(true);
  +    if (mBaseSchemaTypeLoader == null) {
  +      if (mBaseLibraries == null) {
  +        mBaseSchemaTypeLoader = XmlBeans.getBuiltinTypeSystem();
  +      } else {
  +        //create a schema type loader based on all of the schemas in all
  +        //of the base tylars
  +        Collection schemas = new ArrayList();
  +        for(int i=0; i<mBaseLibraries.length; i++) {
  +          SchemaDocument[] xsds = mBaseLibraries[i].getSchemas();
  +          for(int j=0; j<xsds.length; j++) schemas.add(xsds[j].getSchema());
  +        }
  +        XmlObject[] xxds = new XmlObject[schemas.size()];
  +        schemas.toArray(xxds);
  +        mBaseSchemaTypeLoader = XmlBeans.
  +                compileXsd(xxds,XmlBeans.getBuiltinTypeSystem(),null);
  +      }
  +
  +    }
  +    return mBaseSchemaTypeLoader;
  +  }
  +
  +  /**
  +   * Asserts that binding compilation has or has not yet begun.  Some
  +   * operations can only occur when the compiler is being initialized, and
  +   * some can only occur after initialization is complete (i.e. binding
  +   * after bind() has been called).
  +   *
  +   * @throws IllegalStateException if the assertion fails.
  +   */
  +  protected void assertCompilationStarted(boolean isStarted) {
  +    if (mIsCompilationStarted != isStarted) {
  +      throw new IllegalStateException
  +              ("This method cannot be invoked "+
  +               (mIsCompilationStarted ? "after" : "before")+
  +               "binding compilation has begun");
  +    }
  +  }
  +
  +  /**
  +   * Should be the first thing called in every implmentation of
  +   * bind(TylarWriter).  Just tells us that the compiler's state has changed.
  +   */
  +  protected void notifyCompilationStarted() {
  +    mIsCompilationStarted = true;
  +  }
  +
   
     // ========================================================================
     // Protected logging methods
  
  
  
  1.3       +45 -1     xml-xmlbeans/v2/src/binding/org/apache/xmlbeans/impl/binding/compile/BindingCompilerTask.java
  
  Index: BindingCompilerTask.java
  ===================================================================
  RCS file: /home/cvs/xml-xmlbeans/v2/src/binding/org/apache/xmlbeans/impl/binding/compile/BindingCompilerTask.java,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- BindingCompilerTask.java	13 Dec 2003 07:56:28 -0000	1.2
  +++ BindingCompilerTask.java	18 Dec 2003 01:58:58 -0000	1.3
  @@ -58,7 +58,15 @@
   import org.apache.tools.ant.BuildException;
   import org.apache.tools.ant.taskdefs.MatchingTask;
   import org.apache.xmlbeans.impl.binding.tylar.Tylar;
  +import org.apache.xmlbeans.SchemaTypeSystem;
  +import org.apache.xmlbeans.XmlException;
  +import org.apache.xmlbeans.XmlObject;
  +import org.apache.xmlbeans.XmlBeans;
  +import org.apache.xmlbeans.XmlOptions;
  +import org.apache.xmlbeans.SchemaTypeLoader;
  +import org.w3.x2001.xmlSchema.SchemaDocument;
   import java.io.File;
  +import java.io.IOException;
   import java.util.logging.Level;
   
   /**
  @@ -71,6 +79,14 @@
   public abstract class BindingCompilerTask extends MatchingTask {
   
     // ========================================================================
  +  // Constants
  +
  +  //used by createSchemaTypeSystem
  +  private static final SchemaTypeLoader SCHEMA_LOADER =
  +          XmlBeans.typeLoaderForClassLoader
  +          (SchemaDocument.class.getClassLoader());
  +
  +  // ========================================================================
     // Variables
   
     private File mDestDir = null;
  @@ -126,7 +142,7 @@
       Tylar tylar = null;
       try {
         BindingCompiler bc = createCompiler();
  -      bc.setIgnoreSeverErrors(mIgnoreErrors);
  +      bc.setIgnoreSevereErrors(mIgnoreErrors);
         bc.setLogger(createLogger());
         bc.setVerbose(mVerbose);
         if (mDestDir != null) {
  @@ -149,7 +165,35 @@
     }
   
     // ========================================================================
  +  // Protected methods
  +
  +  /**
  +   * Utility method for creating a SchemaTypeSystem from a set of
  +   * xsd files.
  +   */
  +  public static SchemaTypeSystem createSchemaTypeSystem(File[] xsdFiles)
  +          throws IOException, XmlException
  +  {
  +    XmlObject[] xsds = new XmlObject[xsdFiles.length];
  +    for (int i = 0; i < xsdFiles.length; i++) {
  +        xsds[i] = parseSchemaFile(xsdFiles[i]);
  +    }
  +    return XmlBeans.compileXsd(xsds, XmlBeans.getBuiltinTypeSystem(), null);
  +  }
  +
  +
  +  // ========================================================================
     // Private methods
  +
  +  private static SchemaDocument parseSchemaFile(File file)
  +          throws IOException, XmlException
  +  {
  +      XmlOptions options = new XmlOptions();
  +      options.setLoadLineNumbers();
  +      options.setLoadMessageDigest();
  +      return (SchemaDocument)SCHEMA_LOADER.parse
  +              (file, SchemaDocument.type, options);
  +  }
   
     private  BindingLogger createLogger() {
       //FIXME this should be an AntBindingLogger
  
  
  
  1.23      +10 -20    xml-xmlbeans/v2/src/binding/org/apache/xmlbeans/impl/binding/compile/Java2Schema.java
  
  Index: Java2Schema.java
  ===================================================================
  RCS file: /home/cvs/xml-xmlbeans/v2/src/binding/org/apache/xmlbeans/impl/binding/compile/Java2Schema.java,v
  retrieving revision 1.22
  retrieving revision 1.23
  diff -u -r1.22 -r1.23
  --- Java2Schema.java	16 Dec 2003 09:55:05 -0000	1.22
  +++ Java2Schema.java	18 Dec 2003 01:58:58 -0000	1.23
  @@ -104,30 +104,22 @@
     // =========================================================================
     // Variables
   
  -  private BindingFile mBindingFile;
  -  private BindingLoader mLoader;
  -  private SchemaDocument mSchemaDocument;
  +  private BindingFile mBindingFile;  // the file we're creating
  +  private BindingLoader mLoader; // the full loader: bindingFile + baseLoader
  +  private SchemaDocument mSchemaDocument; // schema doc we're generating
     private SchemaDocument.Schema mSchema;
  -  private JClass[] mClasses;
  +  private JClass[] mClasses; // the input classes
   
     // =========================================================================
     // Constructors
   
     public Java2Schema(JClass[] classesToBind) {
  +    if (classesToBind == null) {
  +      throw new IllegalArgumentException("null classes");
  +    }
       mClasses = classesToBind;
     }
   
  -  /**
  -   * Initializes a Java2Schema instance to perform binding on the given
  -   * inputs, but does not actually do any binding work.
  -   *
  -   * @deprecated Trying to remove JavaSourceSet
  -   */
  -  public Java2Schema(JavaSourceSet jtsi) {
  -    if (jtsi == null) throw new IllegalArgumentException("null jtsi");
  -    mClasses = jtsi.getJClasses();
  -  }
  -
     // ========================================================================
     // BindingCompiler implementation
   
  @@ -135,11 +127,11 @@
      * Does the binding work on the inputs passed to the constructor and writes
      * out the tylar.
      */
  -  protected void bind(TylarWriter writer) {
  +  public void bind(TylarWriter writer) {
  +    super.notifyCompilationStarted();
       mBindingFile = new BindingFile();
       mLoader = PathBindingLoader.forPath
  -            (new BindingLoader[] {mBindingFile,
  -                                  BuiltinBindingLoader.getInstance()});
  +            (new BindingLoader[] {mBindingFile, super.getBaseBindingLoader()});
       mSchemaDocument = SchemaDocument.Factory.newInstance();
       mSchema = mSchemaDocument.addNewSchema();
       if (mClasses.length > 0) {
  @@ -158,10 +150,8 @@
       }
     }
   
  -
     // ========================================================================
     // Private methods
  -
   
     /**
      * Returns a bts BindingType for the given JClass.  If such a type
  
  
  
  1.13      +1 -7      xml-xmlbeans/v2/src/binding/org/apache/xmlbeans/impl/binding/compile/Java2SchemaTask.java
  
  Index: Java2SchemaTask.java
  ===================================================================
  RCS file: /home/cvs/xml-xmlbeans/v2/src/binding/org/apache/xmlbeans/impl/binding/compile/Java2SchemaTask.java,v
  retrieving revision 1.12
  retrieving revision 1.13
  diff -u -r1.12 -r1.13
  --- Java2SchemaTask.java	12 Dec 2003 01:12:56 -0000	1.12
  +++ Java2SchemaTask.java	18 Dec 2003 01:58:58 -0000	1.13
  @@ -56,7 +56,6 @@
   package org.apache.xmlbeans.impl.binding.compile;
   
   import org.apache.tools.ant.BuildException;
  -import org.apache.tools.ant.taskdefs.MatchingTask;
   import org.apache.tools.ant.types.Path;
   import org.apache.tools.ant.types.Reference;
   import org.apache.xmlbeans.impl.jam.JClass;
  @@ -164,11 +163,6 @@
         ioe.printStackTrace();
         throw new BuildException(ioe);
       }
  -    JavaSourceSet input = new JavaSourceSet() {
  -      public JClass[] getJClasses() { return classes; }
  -      public TylarLoader getTylarLoader() { return null; }
  -      public void compileJavaToBinaries(File classesDir) {}
  -    };
  -    return new Java2Schema(input);
  +    return new Java2Schema(classes);
     }
   }
  
  
  
  1.6       +34 -38    xml-xmlbeans/v2/src/binding/org/apache/xmlbeans/impl/binding/compile/Schema2Java.java
  
  Index: Schema2Java.java
  ===================================================================
  RCS file: /home/cvs/xml-xmlbeans/v2/src/binding/org/apache/xmlbeans/impl/binding/compile/Schema2Java.java,v
  retrieving revision 1.5
  retrieving revision 1.6
  diff -u -r1.5 -r1.6
  --- Schema2Java.java	16 Dec 2003 09:55:05 -0000	1.5
  +++ Schema2Java.java	18 Dec 2003 01:58:58 -0000	1.6
  @@ -91,26 +91,34 @@
     // Variables
   
     private Set usedNames = new HashSet();
  -  private SchemaTypeSystem sts;
  +  private SchemaTypeSystem sts = null;
     private Map scratchFromXmlName = new LinkedHashMap();
     private Map scratchFromSchemaType = new HashMap(); // for convenience
     private Map scratchFromJavaNameString = new HashMap(); // for printing
  -  private BindingLoader path;
  +  private BindingLoader mLoader;
     private int structureCount;
     private BindingFile bindingFile = new BindingFile();
  -  private SchemaSourceSet sourceSet;
     private JavaOutputStream mJoust = null;
   
     // ========================================================================
     // Constructors
   
  +  /**
  +   * Consturcts a Schema2Java to bind the types in the given type system.
  +   */
  +  public Schema2Java(SchemaTypeSystem s) {
  +    setSchemaTypeSystem(s);
  +  }
   
     /**
  -   * Consturcts a Schema2Java to work on the given inputs and using the given
  -   * logger.
  +   * If you use this, you absolutely have to call setInput later.  This is
  +   * here just as a convenience for Schema2JavaTask.
      */
  -  public Schema2Java(SchemaSourceSet input) {
  -    setInput(input);
  +  /*package*/ Schema2Java() {}
  +
  +  /*package*/ void setSchemaTypeSystem(SchemaTypeSystem s) {
  +    if (s == null) throw new IllegalArgumentException("null sts");
  +    sts = s;
     }
   
     // ========================================================================
  @@ -120,7 +128,8 @@
      * Computes the binding.
      */
     public void bind(TylarWriter writer) {
  -    if (sourceSet == null) throw new IllegalStateException("input never set");
  +    if (sts == null) throw new IllegalStateException("SchemaTypeSystem not set");
  +    super.notifyCompilationStarted();
       mJoust = writer.getJavaOutputStream();
       if (mJoust == null) throw new IllegalStateException("joust is null");
       bind();
  @@ -131,32 +140,19 @@
       }
       //FIXME also write the input schemas
       try {
  -      writeJavaFiles(writer.getJavaOutputStream());
  +      writeJavaFiles();
       } catch(IOException ioe) {
         if (!logError(ioe)) return;
       }
     }
   
     // ========================================================================
  -  // Package methods
  -
  -  /**
  -   * If you use this, you absolutely have to call setInput later.  This is
  -   * here just as a convenience for Schema2JavaTask.
  -   */
  -  /*package*/ Schema2Java() {}
  -
  -  /*package*/ void setInput(SchemaSourceSet input) {
  -    if (input == null) throw new IllegalArgumentException("null input");
  -    this.sourceSet = input;
  -    this.sts = input.getSchemaTypeSystem();
  -    this.path = input.getTylarLoader().getBindingLoader();
  -  }
  -
  -  // ========================================================================
     // Private methods
   
     private void bind() {
  +
  +    mLoader = super.getBaseBindingLoader();
  +
       // Every type or global element or global attribute is dropped into
       // one of a number of categories.  (See Scratch constants.)
       //
  @@ -355,11 +351,11 @@
       XmlTypeName xName = btName.getXmlName();
       if (xName.isSchemaType()) {
         return (bindingFile.lookupTypeFor(jName) == null &&
  -              path.lookupTypeFor(jName) == null);
  +              mLoader.lookupTypeFor(jName) == null);
       }
       if (xName.getComponentType() == XmlTypeName.ELEMENT) {
         return (bindingFile.lookupElementFor(jName) == null &&
  -              path.lookupElementFor(jName) == null);
  +              mLoader.lookupElementFor(jName) == null);
       }
       return false;
     }
  @@ -481,7 +477,7 @@
   
     /**
      * Returns a collection of QNameProperties for a given schema type that
  -   * may either be on the path or the current scratch area.
  +   * may either be on the mLoader or the current scratch area.
      */
     private Collection extractProperties(SchemaType sType) {
       // case 1: it's in the current area
  @@ -491,8 +487,8 @@
         return scratch.getQNameProperties();
       }
   
  -    // case 2: it's in the path
  -    BindingType bType = path.getBindingType(path.lookupPojoFor(XmlTypeName.forSchemaType(sType)));
  +    // case 2: it's in the mLoader
  +    BindingType bType = mLoader.getBindingType(mLoader.lookupPojoFor(XmlTypeName.forSchemaType(sType)));
       if (!(bType instanceof ByNameBean)) {
         return null;
       }
  @@ -627,9 +623,9 @@
           return;
         }
   
  -      // or if not within this type system, find the base type on the path
  +      // or if not within this type system, find the base type on the mLoader
         XmlTypeName treatAs = XmlTypeName.forSchemaType(baseType);
  -      BindingType basedOnBinding = path.getBindingType(path.lookupPojoFor(treatAs));
  +      BindingType basedOnBinding = mLoader.getBindingType(mLoader.lookupPojoFor(treatAs));
         if (basedOnBinding != null) {
           scratch.setJavaName(basedOnBinding.getName().getJavaName());
           scratch.setAsIf(treatAs);
  @@ -641,11 +637,11 @@
       }
   
       // builtin at least should give us xs:anyType
  -    throw new IllegalStateException("Builtin binding type loader is not on path.");
  +    throw new IllegalStateException("Builtin binding type loader is not on mLoader.");
     }
   
     /**
  -   * Looks on both the path and in the current scratch area for
  +   * Looks on both the mLoader and in the current scratch area for
      * the binding type corresponding to the given schema type.  Must
      * be called after all the binding types have been created.
      */
  @@ -653,7 +649,7 @@
       Scratch scratch = scratchForSchemaType(sType);
       if (scratch != null)
         return scratch.getBindingType();
  -    return path.getBindingType(path.lookupPojoFor(XmlTypeName.forSchemaType(sType)));
  +    return mLoader.getBindingType(mLoader.lookupPojoFor(XmlTypeName.forSchemaType(sType)));
     }
   
     /**
  @@ -880,11 +876,11 @@
     // class someday.  Somebody conceivably might want to plug in here (at their
     // own risk, of course).   pcal 12/12/03
   
  -  private void writeJavaFiles(JavaOutputStream joust) throws IOException {
  +  private void writeJavaFiles() throws IOException {
       Collection classnames = getToplevelClasses();
       for (Iterator i = classnames.iterator(); i.hasNext();) {
         String className = (String) i.next();
  -      printSourceCode(className, joust);
  +      printSourceCode(className);
       }
     }
   
  @@ -907,7 +903,7 @@
     /**
      * Prints the Java source code for the given generated java class name.
      */
  -  private void printSourceCode(String topLevelClassName, JavaOutputStream out) {
  +  private void printSourceCode(String topLevelClassName) {
       Scratch scratch = scratchForJavaNameString(topLevelClassName);
       if (scratch == null) {
         logError("Could not find scratch for " + topLevelClassName); //?
  
  
  
  1.5       +15 -30    xml-xmlbeans/v2/src/binding/org/apache/xmlbeans/impl/binding/compile/Schema2JavaTask.java
  
  Index: Schema2JavaTask.java
  ===================================================================
  RCS file: /home/cvs/xml-xmlbeans/v2/src/binding/org/apache/xmlbeans/impl/binding/compile/Schema2JavaTask.java,v
  retrieving revision 1.4
  retrieving revision 1.5
  diff -u -r1.4 -r1.5
  --- Schema2JavaTask.java	13 Dec 2003 07:56:28 -0000	1.4
  +++ Schema2JavaTask.java	18 Dec 2003 01:58:58 -0000	1.5
  @@ -61,6 +61,7 @@
   import org.apache.tools.ant.types.Path;
   import org.apache.tools.ant.types.Reference;
   import org.apache.xmlbeans.XmlException;
  +import org.apache.xmlbeans.SchemaTypeSystem;
   
   import java.io.File;
   import java.io.IOException;
  @@ -123,8 +124,6 @@
       mCompiler.setJavacClasspath(classpath);
     }
   
  -
  -
     /**
      * Set the source directories to find the source XSD files.
      */
  @@ -172,14 +171,13 @@
     // BindingCompilerTask implementation
   
     /**
  -   * Execute the task.
  +   * Based on the parameters set for this Task object, create an instance of
  +   * the Schema2Java compiler to be executed.
      */
     protected BindingCompiler createCompiler() throws BuildException {
  -    //get the files
       checkParameters();
  -    // scan source directories and dest directory to build up
  +    // scan source directories and dest directory for schemas to use
       startScan();
  -
       String[] list = mXsdPath.list();
       for (int i = 0; i < list.length; i++) {
         File srcDir = getProject().resolveFile(list[i]);
  @@ -188,39 +186,26 @@
                                    + srcDir.getPath()
                                    + "\" does not exist!", getLocation());
         }
  -
         DirectoryScanner ds = this.getDirectoryScanner(srcDir);
         String[] files = ds.getIncludedFiles();
  -
         scanDir(srcDir, files);
       }
  +    //build up a schema type system from the input schemas and give it to
  +    //the compiler
       File[] xsdFiles = (File[]) mXsdFiles.toArray(new File[mXsdFiles.size()]);
  -
  -    TylarLoader tylarLoader = null;
  -
  -    /* commenting this out because it's not working correctly
  -       and i'm not sure it's the right thing to do anyway
  -      if (mClasspath != null) {
  -        File[] classpath = namesToFiles(mClasspath.list());
  -        tylarLoader = SimpleTylarLoader.forClassPath(classpath);
  -      }
  -      */
  -
  -    //build up the inputs
  -    SchemaSourceSet input = null;
  +    SchemaTypeSystem sts;
       try {
  -      input = SimpleSourceSet.forXsdFiles(xsdFiles, tylarLoader);
  -    } catch (IOException e) {
  -      log(e.getMessage());
  -      throw new BuildException(e);
  -    } catch (XmlException e) {
  -      log(e.getMessage());
  -      throw new BuildException(e);
  +      sts = createSchemaTypeSystem(xsdFiles);
  +    } catch(IOException ioe) {
  +      throw new BuildException(ioe);
  +    } catch(XmlException xe) {
  +      throw new BuildException(xe);
       }
  -    //return the compiler
  -    mCompiler.setInput(input);
  +    mCompiler.setSchemaTypeSystem(sts);
       return mCompiler;
     }
  +
  +
   
     protected void startScan() {
       mXsdFiles = new ArrayList();
  
  
  
  1.2       +4 -1      xml-xmlbeans/v2/src/binding/org/apache/xmlbeans/impl/binding/compile/SchemaSourceSet.java
  
  Index: SchemaSourceSet.java
  ===================================================================
  RCS file: /home/cvs/xml-xmlbeans/v2/src/binding/org/apache/xmlbeans/impl/binding/compile/SchemaSourceSet.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- SchemaSourceSet.java	4 Dec 2003 21:14:55 -0000	1.1
  +++ SchemaSourceSet.java	18 Dec 2003 01:58:58 -0000	1.2
  @@ -60,7 +60,10 @@
   
   import java.io.File;
   
  -public interface SchemaSourceSet // WARNING: this class will be renamed to "SchemaSourceSet"
  +/**
  + * @deprecated This class is being phased out.
  + */
  +public interface SchemaSourceSet
   {
       /**
        * Returns a typesystem that contains all the schema types to be
  
  
  
  1.2       +159 -3    xml-xmlbeans/v2/src/binding/org/apache/xmlbeans/impl/binding/tylar/DefaultTylarLoader.java
  
  Index: DefaultTylarLoader.java
  ===================================================================
  RCS file: /home/cvs/xml-xmlbeans/v2/src/binding/org/apache/xmlbeans/impl/binding/tylar/DefaultTylarLoader.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- DefaultTylarLoader.java	17 Dec 2003 02:09:19 -0000	1.1
  +++ DefaultTylarLoader.java	18 Dec 2003 01:58:58 -0000	1.2
  @@ -56,10 +56,19 @@
   package org.apache.xmlbeans.impl.binding.tylar;
   
   import java.io.File;
  +import java.io.FileInputStream;
   import java.io.FileNotFoundException;
   import java.io.IOException;
  +import java.io.InputStream;
   import java.net.URI;
  +import java.util.ArrayList;
  +import java.util.Collection;
  +import java.util.jar.JarEntry;
  +import java.util.jar.JarInputStream;
  +import org.apache.xml.xmlbeans.bindingConfig.BindingConfigDocument;
   import org.apache.xmlbeans.XmlException;
  +import org.apache.xmlbeans.impl.binding.bts.BindingFile;
  +import org.w3.x2001.xmlSchema.SchemaDocument;
   
   /**
    * Default implementation of TylarLoader.  Currently, only directory and jar
  @@ -74,6 +83,19 @@
   
     private static final String FILE_SCHEME = "file";
   
  +  private static final char[] OTHER_SEPCHARS = {'\\'};
  +  private static final char SEPCHAR = '/';
  +
  +  private static final boolean VERBOSE = false;
  +
  +  private static final String BINDING_FILE_JARENTRY =
  +          normalizeEntryName(TylarConstants.BINDING_FILE);
  +
  +  private static final String SCHEMA_DIR_JARENTRY =
  +          normalizeEntryName(TylarConstants.SCHEMA_DIR);
  +
  +  private static final String SCHEMA_EXT = ".xsd";
  +
     // ========================================================================
     // Singleton
   
  @@ -113,16 +135,24 @@
      * @throws IOException if an i/o error occurs while processing
      * @throws XmlException if an error occurs parsing the contents of the tylar.
      */
  -  public Tylar load(URI uri) throws IOException, XmlException {
  +  public Tylar load(URI uri) throws IOException, XmlException
  +  {
       if (uri == null) throw new IllegalArgumentException("null uri");
       String scheme = uri.getScheme();
       if (scheme.equals(FILE_SCHEME)) {
  -      File file = new File(uri);
  +      File file;
  +      try {
  +        file = new File(uri);
  +      } catch(Exception e) {
  +        //sometimes File can't deal for some reason, so as a last ditch
  +        //we assume it's a jar and read the stream directly
  +        return loadFromJar(new JarInputStream(uri.toURL().openStream()),uri);
  +      }
         if (!file.exists()) throw new FileNotFoundException(uri.toString());
         if (file.isDirectory()) {
           return ExplodedTylarImpl.load(file);
         } else {
  -        return JarredTylar.load(file);
  +        return loadFromJar(file);
         }
       } else {
         throw new IOException("Sorry, the '"+scheme+
  @@ -130,4 +160,130 @@
                               "("+uri+")");
       }
     }
  +
  +  // ========================================================================
  +  // Static utility methods
  +
  +  /**
  +   * Loads a Tylar directly from the given jar file.  This method parses all
  +   * of the tylar's binding artifacts; if it doesn't throw an exception,
  +   * you can be sure that the tylars binding files and schemas are valid.
  +   *
  +   * @param jarFile file containing the tylar
  +   * @return Handle to the tylar
  +   * @throws IOException
  +   */
  +  public static Tylar loadFromJar(File jarFile)
  +          throws IOException, XmlException
  +  {
  +    if (jarFile == null) throw new IllegalArgumentException("null file");
  +    return loadFromJar(new FileInputStream(jarFile),jarFile.toURI());
  +  }
  +
  +  /**
  +   * Loads a Tylar directly from the stream. given jar file.  This method
  +   * parses all of the tylar's binding artifacts; if it doesn't throw an
  +   * exception, you can be sure that the tylars binding files and schemas are
  +   * valid.
  +   *
  +   * @param in input stream on the jar file
  +   * @param source uri from which the tylar was retrieved.  This is used
  +   * for informational purposes only, but it is required.
  +   * @return Handle to the tylar
  +   * @throws IOException
  +   */
  +  public static Tylar loadFromJar(InputStream in, URI source)
  +          throws IOException, XmlException
  +  {
  +    if (in == null) throw new IllegalArgumentException("null in");
  +    if (source == null) throw new IllegalArgumentException("null uri");
  +    HackJarInputStream hin = new HackJarInputStream(in);
  +    JarEntry entry;
  +    BindingFile bf = null;
  +    Collection schemas = null;
  +    while ((entry = hin.getNextJarEntry()) != null) {
  +      if (entry.isDirectory()) continue;
  +      String name = normalizeEntryName(entry.getName());
  +      if (name.equals(BINDING_FILE_JARENTRY)) {
  +        if (VERBOSE) System.out.println("parsing binding file "+name);
  +        bf = BindingFile.forDoc(BindingConfigDocument.Factory.parse(hin));
  +      } else if (name.startsWith(SCHEMA_DIR_JARENTRY) &&
  +              name.endsWith(SCHEMA_EXT)) {
  +        if (schemas == null) schemas = new ArrayList();
  +        if (VERBOSE) System.out.println("parsing schema "+name);
  +        schemas.add(SchemaDocument.Factory.parse(hin));
  +      } else {
  +        if (VERBOSE) {
  +          System.out.println("ignoring unknown jar entry: "+name);
  +          System.out.println("  looking for "+BINDING_FILE_JARENTRY+" or "+
  +                             SCHEMA_DIR_JARENTRY);
  +        }
  +      }
  +      hin.closeEntry();
  +    }
  +    if (bf == null) {
  +      throw new IOException
  +              ("resource at '"+source+
  +               "' is not a tylar: it does not contain a binding file");
  +    }
  +    hin.reallyClose();
  +    return new TylarImpl(source,bf,schemas);
  +  }
  +  // ========================================================================
  +  // Private methods
  +
  +  /**
  +   * Canonicalizes the given zip entry path so that we can look for what
  +   * we want without having to worry about different slashes or
  +   * leading slashes or anything else that can go wrong.
  +   */
  +  private static final String normalizeEntryName(String name) {
  +    name = name.toLowerCase().trim();
  +    for(int i=0; i<OTHER_SEPCHARS.length; i++) {
  +      name = name.replace(OTHER_SEPCHARS[i],SEPCHAR);
  +    }
  +    if (name.charAt(0) == SEPCHAR) name = name.substring(1);
  +    return name;
  +  }
  +
  +  /**
  +   * Grab the contents of the current entry and stuffs them into a string -
  +   * sometimes useful for debugging.
  +   */
  +  /*
  +  private static String getEntryContents(JarInputStream in) throws IOException {
  +  StringWriter writer = new StringWriter();
  +  byte[] buffer = new byte[2056];
  +  int count = 0;
  +  while ((count = in.read(buffer, 0, buffer.length)) != -1) {
  +  writer.write(new String(buffer, 0, count));
  +  }
  +  if (VERBOSE) {
  +  System.out.println("=== ENTRY CONTENTS ===");
  +  System.out.println(writer.toString());
  +  System.out.println("=== ENTRY CONTENTS ===");
  +  }
  +  return writer.toString();
  +  }
  +  */
  +
  +  /**
  +   * This is another hack around what I believe is an xbeans bug - it
  +   * closes the stream on us.  When we're reading out of a jar, we want
  +   * to parse a whole bunch of files from the same stream - this class
  +   * just intercepts the close call.
  +   */
  +  private static class HackJarInputStream extends JarInputStream {
  +
  +    HackJarInputStream(InputStream in) throws IOException {
  +      super(in);
  +    }
  +
  +    public void close() {}
  +
  +    public void reallyClose() throws IOException {
  +      super.close();
  +    }
  +  }
  +
   }
  
  
  
  1.3       +1 -1      xml-xmlbeans/v2/src/binding/org/apache/xmlbeans/impl/binding/tylar/ExplodedTylar.java
  
  Index: ExplodedTylar.java
  ===================================================================
  RCS file: /home/cvs/xml-xmlbeans/v2/src/binding/org/apache/xmlbeans/impl/binding/tylar/ExplodedTylar.java,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- ExplodedTylar.java	11 Dec 2003 01:49:03 -0000	1.2
  +++ ExplodedTylar.java	18 Dec 2003 01:58:58 -0000	1.3
  @@ -99,7 +99,7 @@
   
     /**
      * Jars up the exploded tylar directory into the given file and returns
  -   * a handle to the JarredTylar.  The main advantage of using this method
  +   * a handle to the TylarImpl.  The main advantage of using this method
      * as opposed to jarring it yourself is that you will save you the cost of
      * reparsing the binding file and the schemas in the event that you want to
      * immediately hand the tylar to the runtime.
  
  
  
  1.4       +12 -5     xml-xmlbeans/v2/src/binding/org/apache/xmlbeans/impl/binding/tylar/ExplodedTylarImpl.java
  
  Index: ExplodedTylarImpl.java
  ===================================================================
  RCS file: /home/cvs/xml-xmlbeans/v2/src/binding/org/apache/xmlbeans/impl/binding/tylar/ExplodedTylarImpl.java,v
  retrieving revision 1.3
  retrieving revision 1.4
  diff -u -r1.3 -r1.4
  --- ExplodedTylarImpl.java	13 Dec 2003 07:56:29 -0000	1.3
  +++ ExplodedTylarImpl.java	18 Dec 2003 01:58:58 -0000	1.4
  @@ -55,18 +55,25 @@
   */
   package org.apache.xmlbeans.impl.binding.tylar;
   
  -import java.io.*;
  +import java.io.File;
  +import java.io.FileOutputStream;
  +import java.io.FileReader;
  +import java.io.FileWriter;
  +import java.io.IOException;
  +import java.net.MalformedURLException;
   import java.net.URI;
  -import java.net.URLClassLoader;
   import java.net.URL;
  -import java.net.MalformedURLException;
  +import java.net.URLClassLoader;
   import java.util.ArrayList;
   import java.util.Collection;
   import org.apache.xml.xmlbeans.bindingConfig.BindingConfigDocument;
   import org.apache.xmlbeans.XmlException;
   import org.apache.xmlbeans.XmlOptions;
   import org.apache.xmlbeans.impl.binding.bts.BindingFile;
  -import org.apache.xmlbeans.impl.binding.joust.*;
  +import org.apache.xmlbeans.impl.binding.joust.FileWriterFactory;
  +import org.apache.xmlbeans.impl.binding.joust.JavaOutputStream;
  +import org.apache.xmlbeans.impl.binding.joust.SourceJavaOutputStream;
  +import org.apache.xmlbeans.impl.binding.joust.ValidatingJavaOutputStream;
   import org.apache.xmlbeans.impl.common.JarHelper;
   import org.w3.x2001.xmlSchema.SchemaDocument;
   
  @@ -231,7 +238,7 @@
     public Tylar toJar(File jarfile) throws IOException {
       JarHelper j = new JarHelper();
       j.jarDir(mRootDir,jarfile);
  -    return new JarredTylar(jarfile,mBindingFile,mSchemaDocuments);
  +    return new TylarImpl(jarfile.toURI(),mBindingFile,mSchemaDocuments);
     }
   
     public File getSourceDir() {
  
  
  
  1.2       +1 -1      xml-xmlbeans/v2/src/binding/org/apache/xmlbeans/impl/binding/tylar/TylarLoader.java
  
  Index: TylarLoader.java
  ===================================================================
  RCS file: /home/cvs/xml-xmlbeans/v2/src/binding/org/apache/xmlbeans/impl/binding/tylar/TylarLoader.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- TylarLoader.java	17 Dec 2003 02:09:19 -0000	1.1
  +++ TylarLoader.java	18 Dec 2003 01:58:58 -0000	1.2
  @@ -55,8 +55,8 @@
   */
   package org.apache.xmlbeans.impl.binding.tylar;
   
  -import java.net.URI;
   import java.io.IOException;
  +import java.net.URI;
   import org.apache.xmlbeans.XmlException;
   
   /**
  
  
  
  1.1                  xml-xmlbeans/v2/src/binding/org/apache/xmlbeans/impl/binding/tylar/TylarImpl.java
  
  Index: TylarImpl.java
  ===================================================================
  /*
  * The Apache Software License, Version 1.1
  *
  *
  * Copyright (c) 2003 The Apache Software Foundation.  All rights
  * reserved.
  *
  * Redistribution and use in source and binary forms, with or without
  * modification, are permitted provided that the following conditions
  * are met:
  *
  * 1. Redistributions of source code must retain the above copyright
  *    notice, this list of conditions and the following disclaimer.
  *
  * 2. Redistributions in binary form must reproduce the above copyright
  *    notice, this list of conditions and the following disclaimer in
  *    the documentation and/or other materials provided with the
  *    distribution.
  *
  * 3. The end-user documentation included with the redistribution,
  *    if any, must include the following acknowledgment:
  *       "This product includes software developed by the
  *        Apache Software Foundation (http://www.apache.org/)."
  *    Alternately, this acknowledgment may appear in the software itself,
  *    if and wherever such third-party acknowledgments normally appear.
  *
  * 4. The names "Apache" and "Apache Software Foundation" must
  *    not be used to endorse or promote products derived from this
  *    software without prior written permission. For written
  *    permission, please contact apache@apache.org.
  *
  * 5. Products derived from this software may not be called "Apache
  *    XMLBeans", nor may "Apache" appear in their name, without prior
  *    written permission of the Apache Software Foundation.
  *
  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  * SUCH DAMAGE.
  * ====================================================================
  *
  * This software consists of voluntary contributions made by many
  * individuals on behalf of the Apache Software Foundation and was
  * originally based on software copyright (c) 2000-2003 BEA Systems
  * Inc., <http://www.bea.com/>. For more information on the Apache Software
  * Foundation, please see <http://www.apache.org/>.
  */
  package org.apache.xmlbeans.impl.binding.tylar;
  
  import java.net.MalformedURLException;
  import java.net.URI;
  import java.net.URL;
  import java.net.URLClassLoader;
  import java.util.Collection;
  import org.apache.xmlbeans.impl.binding.bts.BindingFile;
  import org.w3.x2001.xmlSchema.SchemaDocument;
  
  /**
   * Simple implementation of Tylar.
   *
   * @author Patrick Calahan <pcal@bea.com>
   */
  public class TylarImpl implements Tylar {
  
    // ========================================================================
    // Variables
  
    private URI mSourceURI;
    private BindingFile mBindingFile = null;
    private Collection mSchemas = null;
  
    // ========================================================================
    // Constructors
  
    /*package*/ TylarImpl(URI sourceUri,
                          BindingFile bf,
                          Collection schemas)
    {
      mSourceURI = sourceUri;
      mBindingFile = bf;
      mSchemas = schemas;
    }
  
    // ========================================================================
    // Tylar implementation
  
    public BindingFile getBindingFile() {
      return mBindingFile;
    }
  
    public SchemaDocument[] getSchemas() {
      if (mSchemas == null) return new SchemaDocument[0];
      SchemaDocument[] out = new SchemaDocument[mSchemas.size()];
      mSchemas.toArray(out);
      return out;
    }
  
    public URI getLocation() {
      return mSourceURI;
    }
  
    public ClassLoader createClassLoader(ClassLoader parent) {
      try {
        return new URLClassLoader(new URL[] {mSourceURI.toURL()},parent);
      } catch(MalformedURLException mue){
        throw new RuntimeException(mue); //FIXME this is bad
      }
    }
  
  }
  
  
  1.15      +4 -3      xml-xmlbeans/v2/test/src/drt/drtcases/BindingTests.java
  
  Index: BindingTests.java
  ===================================================================
  RCS file: /home/cvs/xml-xmlbeans/v2/test/src/drt/drtcases/BindingTests.java,v
  retrieving revision 1.14
  retrieving revision 1.15
  diff -u -r1.14 -r1.15
  --- BindingTests.java	16 Dec 2003 09:55:06 -0000	1.14
  +++ BindingTests.java	18 Dec 2003 01:58:59 -0000	1.15
  @@ -13,6 +13,7 @@
   import org.apache.xmlbeans.impl.binding.compile.Schema2Java;
   import org.apache.xmlbeans.impl.binding.compile.SchemaSourceSet;
   import org.apache.xmlbeans.impl.binding.compile.SimpleSourceSet;
  +import org.apache.xmlbeans.impl.binding.compile.BindingCompilerTask;
   import org.apache.xmlbeans.impl.binding.joust.SourceJavaOutputStream;
   import org.apache.xmlbeans.impl.binding.joust.WriterFactory;
   import org.apache.xmlbeans.impl.binding.joust.JavaOutputStream;
  @@ -35,9 +36,9 @@
       {
           // bind
           File typesonlyfile = TestEnv.xbeanCase("schema/typesonly/typesonly.xsd");
  -        SchemaSourceSet input = SimpleSourceSet.forXsdFile(typesonlyfile, null);
  -        Schema2Java compiler = new Schema2Java(input);
  -
  +        File[] xsds = new File[] {typesonlyfile};
  +        Schema2Java compiler = new Schema2Java(
  +                BindingCompilerTask.createSchemaTypeSystem(xsds));
           final JavaOutputStream joust = createJoust();
           BindingFile bindingFile = null;
           TylarWriter twriter = new TylarWriter() {
  
  
  

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


Mime
View raw message