(XMLBEANS-536) - move generated sources/classes from schemaorg_apache_xmlbeans to org.apache.xmlbeans

git-svn-id: https://svn.apache.org/repos/asf/xmlbeans/branches/xmlbeans-536@1855791 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/src/typeimpl/org/apache/xmlbeans/impl/schema/SchemaTypeLoaderImpl.java b/src/typeimpl/org/apache/xmlbeans/impl/schema/SchemaTypeLoaderImpl.java
index a4e9472..169a7c3 100644
--- a/src/typeimpl/org/apache/xmlbeans/impl/schema/SchemaTypeLoaderImpl.java
+++ b/src/typeimpl/org/apache/xmlbeans/impl/schema/SchemaTypeLoaderImpl.java
@@ -15,18 +15,12 @@
 
 package org.apache.xmlbeans.impl.schema;
 
-import org.apache.xmlbeans.SchemaTypeLoader;
-import org.apache.xmlbeans.SchemaType;
-import org.apache.xmlbeans.SchemaGlobalElement;
-import org.apache.xmlbeans.SchemaGlobalAttribute;
-import org.apache.xmlbeans.SchemaModelGroup;
-import org.apache.xmlbeans.SchemaAttributeGroup;
-import org.apache.xmlbeans.SchemaTypeSystem;
-import org.apache.xmlbeans.SchemaIdentityConstraint;
-import org.apache.xmlbeans.ResourceLoader;
+import org.apache.xmlbeans.*;
 import org.apache.xmlbeans.impl.common.SystemCache;
 import org.apache.xmlbeans.impl.common.QNameHelper;
 import org.apache.xmlbeans.impl.common.XBeanDebug;
+import org.apache.xmlbeans.impl.xb.xsdschema.SchemaDocument;
+
 import javax.xml.namespace.QName;
 
 import java.io.InputStream;
@@ -62,6 +56,12 @@
     public static String METADATA_PACKAGE_LOAD = METADATA_PACKAGE_GEN;
     private static final Object CACHED_NOT_FOUND = new Object();
 
+    private static final String[] basePackage = { "org.apache.xmlbeans", "schemaorg_apache_xmlbeans" };
+    private static final String[] baseSchemas = { "sXMLCONFIG", "sXMLLANG", "sXMLSCHEMA", "sXMLTOOLS" };
+
+
+
+
     private static class SchemaTypeLoaderCache extends SystemCache
     {
         // The following maintains a cache of SchemaTypeLoaders per ClassLoader per Thread.
@@ -168,44 +168,30 @@
      * @since XmlBeans 3.0.3
      */
     public static SchemaTypeLoader build(final SchemaTypeLoader[] searchPath, ResourceLoader resourceLoader, ClassLoader classLoader, String metadataPath) {
-        final SchemaTypeLoader[] sp;
+        // assemble a flattened search path with no duplicates
+        SubLoaderList list = new SubLoaderList();
 
-        if (searchPath == null) {
-            // if the metadata directory is customized, fallback to the xmlbeans typesystems
-            final boolean isDefaultPath = (metadataPath == null || ("schema" + METADATA_PACKAGE_GEN).equals(metadataPath));
-            if (isDefaultPath) {
-                sp = null;
-            } else {
-                String[] baseHolder = {
-                    "schemaorg_apache_xmlbeans.system.sXMLCONFIG.TypeSystemHolder",
-                    "schemaorg_apache_xmlbeans.system.sXMLLANG.TypeSystemHolder",
-                    "schemaorg_apache_xmlbeans.system.sXMLSCHEMA.TypeSystemHolder",
-                    "schemaorg_apache_xmlbeans.system.sXMLTOOLS.TypeSystemHolder"
-                };
+        list.add(searchPath);
 
-                sp = new SchemaTypeLoader[baseHolder.length];
-                for (int i=0; i<baseHolder.length; i++) {
-                    try {
-                        Class cls = Class.forName(baseHolder[i]);
-                        sp[i] = (SchemaTypeLoader)cls.getDeclaredField("typeSystem").get(null);
-                    } catch (Exception e) {
-                        System.out.println("throw runtime: "+e.toString());
-                        throw new RuntimeException(e);
-                    }
+        ClassLoader cl = (classLoader == null) ? SchemaDocument.class.getClassLoader() :  classLoader;
+
+        for (String prefix : basePackage) {
+            for (String holder : baseSchemas) {
+                String clName = prefix + ".system." + holder + ".TypeSystemHolder";
+                if (cl.getResource(clName.replace(".","/")+".class") == null) {
+                    // if the first class isn't found in the package, continue with the next package
+                    break;
+                }
+                try {
+                    Class cls = Class.forName(clName, true, cl);
+                    list.add((SchemaTypeLoader)cls.getDeclaredField("typeSystem").get(null));
+                } catch (Exception e) {
+                    throw new XmlRuntimeException(e);
                 }
             }
-        } else {
-            // assemble a flattened search path with no duplicates
-            SubLoaderList list = new SubLoaderList();
-            list.add(searchPath);
-            sp = list.toArray();
         }
 
-        if (sp != null && sp.length == 1 && resourceLoader == null && classLoader == null) {
-            return sp[0];
-        }
-
-        return new SchemaTypeLoaderImpl(sp, resourceLoader, classLoader, metadataPath);
+        return new SchemaTypeLoaderImpl(list.toArray(), resourceLoader, classLoader, metadataPath);
     }
 
     /**
@@ -262,17 +248,26 @@
      */
     private SchemaTypeLoaderImpl(SchemaTypeLoader[] searchPath, ResourceLoader resourceLoader, ClassLoader classLoader, String metadataPath)
     {
-        if (searchPath == null)
-            _searchPath = EMPTY_SCHEMATYPELOADER_ARRAY;
-        else
-            _searchPath = searchPath;
+        _searchPath = (searchPath == null) ? EMPTY_SCHEMATYPELOADER_ARRAY : searchPath;
         _resourceLoader = resourceLoader;
         _classLoader = classLoader;
-        this._metadataPath = (metadataPath == null) ? "schema" + METADATA_PACKAGE_LOAD : metadataPath;
+
+        if (metadataPath != null) {
+            this._metadataPath = metadataPath;
+        } else {
+            final String path26 = "schema" + METADATA_PACKAGE_LOAD.replace("/","_");
+            this._metadataPath = (isPath30(_classLoader)) ? METADATA_PACKAGE_LOAD : path26;
+        }
 
         initCaches();
     }
 
+    private static boolean isPath30(ClassLoader loader) {
+        final String path30 = METADATA_PACKAGE_LOAD + "/system";
+        final ClassLoader cl = (loader != null) ? loader : SchemaDocument.class.getClassLoader();
+        return cl.getResource(path30) != null;
+    }
+
     /**
      * Initializes the caches.
      */
diff --git a/src/typeimpl/org/apache/xmlbeans/impl/schema/SchemaTypeSystemImpl.java b/src/typeimpl/org/apache/xmlbeans/impl/schema/SchemaTypeSystemImpl.java
index 2c5ea02..5d20853 100644
--- a/src/typeimpl/org/apache/xmlbeans/impl/schema/SchemaTypeSystemImpl.java
+++ b/src/typeimpl/org/apache/xmlbeans/impl/schema/SchemaTypeSystemImpl.java
@@ -74,6 +74,8 @@
 import java.util.Map;
 import java.util.Random;
 import java.util.Set;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
 import java.util.zip.ZipEntry;
 import java.util.zip.ZipFile;
 
@@ -136,11 +138,16 @@
     static final int FLAG_ATTRIBUTE_TYPE  = 0x80000;
 
     /**
+     * regex to identify the type system holder package namespace
+     */
+    private static final Pattern packPat = Pattern.compile("^(.+)(\\.[^.]+){3}$");
+
+    /**
      * This is to support the feature of a separate/private XMLBeans
      * distribution that will not colide with the public org apache
      * xmlbeans one.
      * METADATA_PACKAGE_GEN will be "" for the original and something like
-     * com_mycompany_private_xmlbeans for a private distribution of XMLBeans.
+     * com.mycompany.private.xmlbeans for a private distribution of XMLBeans.
      *
      * There are two properties:
      *   METADATA_PACKAGE_GEN - used for generating metadata
@@ -158,7 +165,7 @@
             SchemaTypeSystem.class.getName().substring(0, SchemaTypeSystem.class.getName().lastIndexOf(".")) :
             stsPackage.getName();
 
-        METADATA_PACKAGE_GEN = stsPackageName.replaceAll("\\.", "_");
+        METADATA_PACKAGE_GEN = stsPackageName.replace('.', '/');
     }
 
     private static String nameToPathString(String nameForSystem)
@@ -171,6 +178,31 @@
         return nameForSystem;
     }
 
+    protected SchemaTypeSystemImpl() {
+        String fullname = getClass().getName();
+        _name = fullname.substring(0, fullname.lastIndexOf('.'));
+        XBeanDebug.trace(XBeanDebug.TRACE_SCHEMA_LOADING, "Loading type system " + _name, 1);
+        _basePackage = nameToPathString(_name);
+        _classloader = getClass().getClassLoader();
+        _linker = this;
+        _resourceLoader = new ClassLoaderResourceLoader(_classloader);
+        try
+        {
+            initFromHeader();
+        }
+        catch (RuntimeException e)
+        {
+            XBeanDebug.logException(e);
+            throw e;
+        }
+        catch (Error e)
+        {
+            XBeanDebug.logException(e);
+            throw e;
+        }
+        XBeanDebug.trace(XBeanDebug.TRACE_SCHEMA_LOADING, "Finished loading type system " + _name, -1);
+    }
+
     public SchemaTypeSystemImpl(Class indexclass)
     {
         String fullname = indexclass.getName();
@@ -235,7 +267,7 @@
             Class c = Class.forName(name + "." + SchemaTypeCodePrinter.INDEX_CLASSNAME, true, loader);
             return (SchemaTypeSystemImpl)c.getField("typeSystem").get(null);
         }
-        catch (Exception e)
+        catch (Throwable e)
         {
             return null;
         }
@@ -3818,8 +3850,9 @@
      *
      * @since XmlBeans 3.0.3
      */
-    protected String getMetadataPath() {
-        return "schema" + METADATA_PACKAGE_GEN;
+    public String getMetadataPath() {
+        Matcher m = packPat.matcher(getClass().getName());
+        m.find();
+        return m.group(1).replace('.','/');
     }
-
 }
diff --git a/src/xmlcomp/org/apache/xmlbeans/impl/tool/Diff.java b/src/xmlcomp/org/apache/xmlbeans/impl/tool/Diff.java
index b8837ea..504eb46 100644
--- a/src/xmlcomp/org/apache/xmlbeans/impl/tool/Diff.java
+++ b/src/xmlcomp/org/apache/xmlbeans/impl/tool/Diff.java
@@ -99,14 +99,14 @@
         {
             ZipEntry ze = (ZipEntry) entries1.nextElement();
             String name = ze.getName();
-            if (name.startsWith("schema" + SchemaTypeSystemImpl.METADATA_PACKAGE_GEN + "/system/s") && name.endsWith(".xsb"))
+            if (name.startsWith(SchemaTypeSystemImpl.METADATA_PACKAGE_GEN + "/system/s") && name.endsWith(".xsb"))
                 list1.add(ze);
         }
         for (; entries2.hasMoreElements(); )
         {
             ZipEntry ze = (ZipEntry) entries2.nextElement();
             String name = ze.getName();
-            if (name.startsWith("schema" + SchemaTypeSystemImpl.METADATA_PACKAGE_GEN + "/system/s") && name.endsWith(".xsb"))
+            if (name.startsWith(SchemaTypeSystemImpl.METADATA_PACKAGE_GEN + "/system/s") && name.endsWith(".xsb"))
                 list2.add(ze);
         }
         ZipEntry[] files1 = (ZipEntry[]) list1.toArray(new ZipEntry[list1.size()]);
@@ -171,8 +171,8 @@
          * Navigate three directories deep to get to the type system.
          * Assume the schema[METADATA_PACKAGE_LOAD]/system/* structure
          */
-        File temp1 = new File(dir1, "schema" + SchemaTypeSystemImpl.METADATA_PACKAGE_GEN + "/system");
-        File temp2 = new File(dir2, "schema" + SchemaTypeSystemImpl.METADATA_PACKAGE_GEN + "/system");
+        File temp1 = new File(dir1, SchemaTypeSystemImpl.METADATA_PACKAGE_GEN + "/system");
+        File temp2 = new File(dir2, SchemaTypeSystemImpl.METADATA_PACKAGE_GEN + "/system");
         if (temp1.exists() && temp2.exists())
         {
             File[] files1 = temp1.listFiles();
diff --git a/src/xmlcomp/org/apache/xmlbeans/impl/tool/SchemaCompiler.java b/src/xmlcomp/org/apache/xmlbeans/impl/tool/SchemaCompiler.java
index 589de3a..a0491ef 100644
--- a/src/xmlcomp/org/apache/xmlbeans/impl/tool/SchemaCompiler.java
+++ b/src/xmlcomp/org/apache/xmlbeans/impl/tool/SchemaCompiler.java
@@ -1057,7 +1057,7 @@
 
         boolean result = true;
 
-        File schemasDir = IOUtil.createDir(classesDir, "schema" + SchemaTypeSystemImpl.METADATA_PACKAGE_GEN + "/src");
+        File schemasDir = IOUtil.createDir(classesDir, SchemaTypeSystemImpl.METADATA_PACKAGE_GEN + "/src");
 
         // build the in-memory type system
         XmlErrorWatcher errorListener = new XmlErrorWatcher(outerErrorListener);
diff --git a/test/src/compile/scomp/checkin/XmlBeansCompCheckinTests.java b/test/src/compile/scomp/checkin/XmlBeansCompCheckinTests.java
index ab49f41..0e41175 100644
--- a/test/src/compile/scomp/checkin/XmlBeansCompCheckinTests.java
+++ b/test/src/compile/scomp/checkin/XmlBeansCompCheckinTests.java
@@ -37,16 +37,16 @@
     final Vector expSrcType = new Vector();

 

     public XmlBeansCompCheckinTests() {

-        expBinType.add("schemaorg_apache_xmlbeans/system/apiCompile/atypedb57type.xsb");

-        expBinType.add("schemaorg_apache_xmlbeans/system/apiCompile/elname429edoctype.xsb");

-        expBinType.add("schemaorg_apache_xmlbeans/system/apiCompile/elnameelement.xsb");

-        expBinType.add("schemaorg_apache_xmlbeans/system/apiCompile/index.xsb");

-        expBinType.add("schemaorg_apache_xmlbeans/element/http_3A_2F_2Fbaz/elName.xsb");

-        expBinType.add("schemaorg_apache_xmlbeans/type/http_3A_2F_2Fbaz/aType.xsb");

-        expBinType.add("schemaorg_apache_xmlbeans/namespace/http_3A_2F_2Fbaz/xmlns.xsb");

-        expBinType.add("schemaorg_apache_xmlbeans/javaname/baz/ElNameDocument.xsb");

-        expBinType.add("schemaorg_apache_xmlbeans/javaname/baz/AType.xsb");

-        expBinType.add("schemaorg_apache_xmlbeans/system/apiCompile/TypeSystemHolder.class");

+        expBinType.add("org/apache/xmlbeans/system/apiCompile/atypedb57type.xsb");

+        expBinType.add("org/apache/xmlbeans/system/apiCompile/elname429edoctype.xsb");

+        expBinType.add("org/apache/xmlbeans/system/apiCompile/elnameelement.xsb");

+        expBinType.add("org/apache/xmlbeans/system/apiCompile/index.xsb");

+        expBinType.add("org/apache/xmlbeans/element/http_3A_2F_2Fbaz/elName.xsb");

+        expBinType.add("org/apache/xmlbeans/type/http_3A_2F_2Fbaz/aType.xsb");

+        expBinType.add("org/apache/xmlbeans/namespace/http_3A_2F_2Fbaz/xmlns.xsb");

+        expBinType.add("org/apache/xmlbeans/javaname/baz/ElNameDocument.xsb");

+        expBinType.add("org/apache/xmlbeans/javaname/baz/AType.xsb");

+        expBinType.add("org/apache/xmlbeans/system/apiCompile/TypeSystemHolder.class");

 

         expSrcType.add("baz.AType");

         expSrcType.add("baz.impl.ATypeImpl");

diff --git a/test/src/compile/scomp/som/common/SomTestBase.java b/test/src/compile/scomp/som/common/SomTestBase.java
index 5658d18..5c3c9b2 100644
--- a/test/src/compile/scomp/som/common/SomTestBase.java
+++ b/test/src/compile/scomp/som/common/SomTestBase.java
@@ -303,7 +303,7 @@
 
     public boolean checkPSOMSave(SchemaTypeSystem tgtSTS)
     {
-        String outDirName = tgtSTS.getName().split("schemaorg_apache_xmlbeans.system.")[1];
+        String outDirName = tgtSTS.getName().split("org.apache.xmlbeans.system.")[1];
         String outDirNameWithPath = somOutputRootDir + P + runid + P + outDirName;
 
         // call the save
diff --git a/test/src/misc/detailed/JiraRegressionSchemaCompilerTest.java b/test/src/misc/detailed/JiraRegressionSchemaCompilerTest.java
index 62e15d5..1ccfef2 100644
--- a/test/src/misc/detailed/JiraRegressionSchemaCompilerTest.java
+++ b/test/src/misc/detailed/JiraRegressionSchemaCompilerTest.java
@@ -39,6 +39,7 @@
         params.setErrorListener(errors);

         params.setSrcDir(new File(schemaCompOutputDirPath + outputDirName + P + "src"));

         params.setClassesDir(new File(schemaCompOutputDirPath + outputDirName + P + "classes"));

+        params.setQuiet(true);

         SchemaCompiler.compile(params);

         return errors;

     }

diff --git a/test/src/schemas/defaults.xsd b/test/src/schemas/defaults.xsd
deleted file mode 100644
index 6de0224..0000000
--- a/test/src/schemas/defaults.xsd
+++ /dev/null
@@ -1,32 +0,0 @@
-<xs:schema
-   xmlns:xs="http://www.w3.org/2001/XMLSchema"
-   xmlns:tns="http://openuri.org/def"
-   targetNamespace="http://openuri.org/def"
-   elementFormDefault="qualified">
-<!--
-/*   Copyright 2004 The Apache Software Foundation
- *
- *   Licensed under the Apache License, Version 2.0 (the "License");
- *   you may not use this file except in compliance with the License.
- *   You may obtain a copy of the License at
- *
- *       http://www.apache.org/licenses/LICENSE-2.0
- *
- *   Unless required by applicable law or agreed to in writing, software
- *   distributed under the License is distributed on an "AS IS" BASIS,
- *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- *   See the License for the specific language governing permissions and
- *  limitations under the License.
- */
- -->
-  <xs:element name="defaults">
-    <xs:complexType>
-      <xs:sequence>
-      </xs:sequence>
-      <xs:attribute ref="tns:cool"/>
-    </xs:complexType>
-  </xs:element>
-
-  <xs:attribute name="cool" type="xs:int" default="783"/>
-
-</xs:schema>