castor
  1. castor
  2. CASTOR-2279

ClassCastException in method org.exolab.castor.xml.schema.writer.SchemaWriter.processSimpleType

    Details

    • Type: Bug Bug
    • Status: Closed Closed
    • Priority: Major Major
    • Resolution: Fixed
    • Affects Version/s: 1.1, 1.1.1, 1.1.2, 1.1.2.1
    • Fix Version/s: 1.3 rc1
    • Component/s: XML
    • Labels:
      None
    • Environment:
      Castor v1.0
      Xerces J v2.6.2
      JDK v1.5.0_06
    • Number of attachments :
      4

      Description

      In class "org.exolab.castor.xml.schema.writer.SchemaWriter", method "processSimpleType", when looking for the base type of a simple type the only call performed is the one prefixed with a star character:

      private void processSimpleType (SimpleType simpleType, String schemaPrefix) throws SAXException {
      [...]
      //-- handle restriction

      • SimpleType base = (SimpleType)simpleType.getBaseType();
        boolean isRestriction = false;
        if (base != null)
        Unknown macro: { if (simpleType instanceof ListType) { isRestriction = (base instanceof ListType); } else isRestriction = true; }

        [...]
        }

      However, if that reference happens to be null for any reason, then we enter a series of conditions into which the simple element is trusted to be either a union or a list (see lines prefixed with star below):

      private void processSimpleType (SimpleType simpleType, String schemaPrefix) throws SAXException {
      [...]
      else if (simpleType instanceof Union)

      { * processUnion((Union)simpleType, schemaPrefix); }

      //-- handle List
      else

      { String ELEM_LIST = schemaPrefix + SchemaNames.LIST; _atts.clear(); * SimpleType itemType = ((ListType)simpleType).getItemType(); [...] }

      This leads to a potentially invalid cast operation if the simple type isn't one of these though, and unfortunately I got the case, with a simple type being an instance of "org.exolab.castor.xml.schema.DeferredSimpleType", and having a base type defined into another (imported) schema.
      This instance had a null reference to its base type, and caused a class cast exception for being casted to a ListType in the code mentionned above.

      The fix I made in order to unblock the issue locally is to enable a walk throught the list of imported schemas in order to look for the missing base type reference.
      To find the base type instance, I'm using the base type name owned by the simple type that is currently processed.

      To gain ability to find the base type name, I augment the class "org.exolab.castor.xml.schema.XMLType" with a getter on it:

      public String getBaseTypeName()

      { return baseType != null ? baseType.getName() : null; }

      Then in class "org.exolab.castor.xml.schema.DeferredSimpleType", which happens to be the one for which the class cast error occurred for me, I override this getter to return the instance field "baseTypeName" :

      public String getBaseTypeName()

      { return baseTypeName; }

      Now we can go back to method "processSimpleType" of class "org.exolab.castor.xml.schema.writer.SchemaWriter" and complement it with an additional attempt of resolving the reference to the base type:

      private void processSimpleType (SimpleType simpleType, String schemaPrefix) throws SAXException {
      [...]
      //-- handle restriction
      SimpleType base = (SimpleType)simpleType.getBaseType();

      // ADDED (BEGIN)
      String baseTypeName = simpleType.getBaseTypeName();
      if(base == null && baseTypeName != null)

      { Schema schema = simpleType.getSchema(); * base = getTypeFromSchema(schema, baseTypeName); }

      // ADDED (END)

      boolean isRestriction = false;
      [...]
      }

      The method "getTypeFromSchema" mentiomned on the line prefixed with a star is added to this class as well:

      private SimpleType getTypeFromSchema(Schema schema, String typeName) {
      SimpleType base = schema.getSimpleType(typeName);
      if(base == null) {
      Enumeration imports = schema.getImportedSchema();
      while (imports.hasMoreElements() && base == null)

      { Schema sch = (Schema) imports.nextElement(); base = getTypeFromSchema(sch, typeName); }

      }
      return base;
      }

      This allows me to fix the issue, although I'm not sure this is the fix that you guys would apply, since I'm not even sure that this is normal that I got an instance of "org.exolab.castor.xml.schema.DeferredSimpleType" with a null reference to a base type.

      Still, if this can be of any help, then all these debugging hours will not be lost.

      Thanks & regards,
      Alex.

      1. patch.c2279.20080205.txt
        4 kB
        Werner Guttmann

        Activity

        Hide
        Alexandre Delarge added a comment -

        Attached file bug2279 contains the following hierarchy:

        • Instructions for problem reproduction in file "\README.txt"
        • JUnit test case for problem reproduction in "\TestClassCastOnDeferredSimpleType.java"
        • XML Schemas for problem reproduction "\xsd"
        • Suggested patch on 3 classes (diff command under Windows/Cygwin was used):
          • \patch\src\main\java\org\exolab\castor\xml\schema\DeferredSimpleType.java.diff
          • \patch\src\main\java\org\exolab\castor\xml\schema\XMLType.java.diff
          • \patch\src\main\java\org\exolab\castor\xml\schema\writer\SchemaWriter.java.diff
        Show
        Alexandre Delarge added a comment - Attached file bug2279 contains the following hierarchy: Instructions for problem reproduction in file "\README.txt" JUnit test case for problem reproduction in "\TestClassCastOnDeferredSimpleType.java" XML Schemas for problem reproduction "\xsd" Suggested patch on 3 classes (diff command under Windows/Cygwin was used): \patch\src\main\java\org\exolab\castor\xml\schema\DeferredSimpleType.java.diff \patch\src\main\java\org\exolab\castor\xml\schema\XMLType.java.diff \patch\src\main\java\org\exolab\castor\xml\schema\writer\SchemaWriter.java.diff
        Hide
        Werner Guttmann added a comment -

        Thanks, Alexandre, for looking into this in so much detail. Rest assured that your dedication and the time you have put in won't be lost in anyway. Having said that, are you in a position to supply us with a unified patch that you created using 'svn diff' ?

        Show
        Werner Guttmann added a comment - Thanks, Alexandre, for looking into this in so much detail. Rest assured that your dedication and the time you have put in won't be lost in anyway. Having said that, are you in a position to supply us with a unified patch that you created using 'svn diff' ?
        Hide
        Alexandre Delarge added a comment -

        Hi Werner,

        I haven't looked at the SVN diff steps yet, is there a great difference between what I could issue using that procedure and the DIFF files in the ZIP archive I attached ?

        Those were created using the Cygwin port of the diff command under Windows, and against Castor v1.1.2.1.

        If you confirm the need for the SVN version of the patch, let me know and I'll take the time to do it.

        Regards,
        Alex.

        Show
        Alexandre Delarge added a comment - Hi Werner, I haven't looked at the SVN diff steps yet, is there a great difference between what I could issue using that procedure and the DIFF files in the ZIP archive I attached ? Those were created using the Cygwin port of the diff command under Windows, and against Castor v1.1.2.1. If you confirm the need for the SVN version of the patch, let me know and I'll take the time to do it. Regards, Alex.
        Hide
        Werner Guttmann added a comment -

        I have had some problems in the past integrating 'plain' diffs, but let's see how it goes. I'll ping you again if I am running into problems. What dif you use as a base for the diffs. A SVN ckeckout or a source distro ?

        Show
        Werner Guttmann added a comment - I have had some problems in the past integrating 'plain' diffs, but let's see how it goes. I'll ping you again if I am running into problems. What dif you use as a base for the diffs. A SVN ckeckout or a source distro ?
        Hide
        Alexandre Delarge added a comment -

        Hi Werner,

        I used the distribution of the sources, version 1.1.2.1.

        Regards,
        Alex.

        Show
        Alexandre Delarge added a comment - Hi Werner, I used the distribution of the sources, version 1.1.2.1. Regards, Alex.
        Hide
        Werner Guttmann added a comment -

        No good ... as these sources are more than 8 months old. To be honest, to ease my life, I'd appreciate if you checked out SVN trunk and attached at least diffs built against this code base. 8 months is along time, and as you can see yourself at http://jira.codehaus.org/browse/CASTOR (roadmap), a lot of work has been done and a lot of code committed.

        Show
        Werner Guttmann added a comment - No good ... as these sources are more than 8 months old. To be honest, to ease my life, I'd appreciate if you checked out SVN trunk and attached at least diffs built against this code base. 8 months is along time, and as you can see yourself at http://jira.codehaus.org/browse/CASTOR (roadmap), a lot of work has been done and a lot of code committed.
        Hide
        Werner Guttmann added a comment -

        Btw, if you need help re: SVN checkout, feel free to ping me on the IRC channel or send us emails on the dev list.

        Show
        Werner Guttmann added a comment - Btw, if you need help re: SVN checkout, feel free to ping me on the IRC channel or send us emails on the dev list.
        Hide
        Alexandre Delarge added a comment -

        Hi Werner,

        I'll grab the Cygwin port of subversion and will try to release the svn version of these diff files within the day.

        Rgds,
        Alex.

        Show
        Alexandre Delarge added a comment - Hi Werner, I'll grab the Cygwin port of subversion and will try to release the svn version of these diff files within the day. Rgds, Alex.
        Hide
        Werner Guttmann added a comment -

        Thanks in advance.

        Show
        Werner Guttmann added a comment - Thanks in advance.
        Hide
        Alexandre Delarge added a comment -

        The provided archive "bug2279-svn.zip" contains the same tentative patch as previously sent, except it was made against the available "TRUNK" from subversion as of 30th Jan 2008.

        Patch concerns 3 classes, and was generated using the Windows/Cygwin port of the "svn diff" command:

        • \patch\src\main\java\org\exolab\castor\xml\schema\DeferredSimpleType.java.diff
        • \patch\src\main\java\org\exolab\castor\xml\schema\XMLType.java.diff
        • \patch\src\main\java\org\exolab\castor\xml\schema\writer\SchemaWriter.java.diff
        Show
        Alexandre Delarge added a comment - The provided archive "bug2279-svn.zip" contains the same tentative patch as previously sent, except it was made against the available "TRUNK" from subversion as of 30th Jan 2008. Patch concerns 3 classes, and was generated using the Windows/Cygwin port of the "svn diff" command: \patch\src\main\java\org\exolab\castor\xml\schema\DeferredSimpleType.java.diff \patch\src\main\java\org\exolab\castor\xml\schema\XMLType.java.diff \patch\src\main\java\org\exolab\castor\xml\schema\writer\SchemaWriter.java.diff
        Hide
        Alexandre Delarge added a comment -

        Hi Werner,

        Let me know if the provided version suits you.

        Rgds,
        Alex.

        Show
        Alexandre Delarge added a comment - Hi Werner, Let me know if the provided version suits you. Rgds, Alex.
        Hide
        Werner Guttmann added a comment -

        Alexandre, can I please ask you to re-create your patch again. Due to internal discussion, we have decided to move all XML schema related classes to a new (Maven) module. As such, you'll find the source code for the XML schema classes in the new 'schema' sub-directory under the project root. Sorry for any inconvenience caused, but this restructuring was really necessary, as it greatly eases our life during deployment.

        Show
        Werner Guttmann added a comment - Alexandre, can I please ask you to re-create your patch again. Due to internal discussion, we have decided to move all XML schema related classes to a new (Maven) module. As such, you'll find the source code for the XML schema classes in the new 'schema' sub-directory under the project root. Sorry for any inconvenience caused, but this restructuring was really necessary, as it greatly eases our life during deployment.
        Hide
        Werner Guttmann added a comment -

        In addition, can I ask you why you are enlisting Xerces 2.6.2 as dependency in the environment section. As you are on Java 5.0, Xerces comes shipped with the JDK/JRE by default. Just being curious here .....

        Show
        Werner Guttmann added a comment - In addition, can I ask you why you are enlisting Xerces 2.6.2 as dependency in the environment section. As you are on Java 5.0, Xerces comes shipped with the JDK/JRE by default. Just being curious here .....
        Hide
        Alexandre Delarge added a comment -

        Hi Werner,

        I'll make one last patch as you ask, later this day I hope.

        For Xerces this is due to the tool I debugged and that relied onto a specific suite of components:

        • Castor v1.0
        • Xerces J v2.6.2
        • JDK v1.5.0_06
        • SAAJ v1.2
        • Axis v1.4
        • etc.

        For some reason this tool uses Xerces-J to parse the result of a schema serialization made with castor.
        I imagine this is to provide Xerces with a schema that contains all definitions for a given schema + the definitions ones defined into the imported schemas.

        Cheers,
        Alex.

        Show
        Alexandre Delarge added a comment - Hi Werner, I'll make one last patch as you ask, later this day I hope. For Xerces this is due to the tool I debugged and that relied onto a specific suite of components: Castor v1.0 Xerces J v2.6.2 JDK v1.5.0_06 SAAJ v1.2 Axis v1.4 etc. For some reason this tool uses Xerces-J to parse the result of a schema serialization made with castor. I imagine this is to provide Xerces with a schema that contains all definitions for a given schema + the definitions ones defined into the imported schemas. Cheers, Alex.
        Hide
        Alexandre Delarge added a comment -

        As requested, provided archive "bug2279-svn-2nd.zip" contains a tentative patch made against "TRUNK" subversion as of 31th Jan 2008.

        Patch concerns 3 classes, and was generated using the Windows/Cygwin port of the "svn diff" command:

        • \bug2279-svn\patch\castor\castor\trunk\schema\src\main\java\org\exolab\castor\xml\schema\DeferredSimpleType.java.diff
        • \bug2279-svn\patch\castor\castor\trunk\schema\src\main\java\org\exolab\castor\xml\schema\XMLType.java.diff
        • \bug2279-svn\patch\castor\castor\trunk\schema\src\main\java\org\exolab\castor\xml\schema\writer\SchemaWriter.java.diff
        Show
        Alexandre Delarge added a comment - As requested, provided archive "bug2279-svn-2nd.zip" contains a tentative patch made against "TRUNK" subversion as of 31th Jan 2008. Patch concerns 3 classes, and was generated using the Windows/Cygwin port of the "svn diff" command: \bug2279-svn\patch\castor\castor\trunk\schema\src\main\java\org\exolab\castor\xml\schema\DeferredSimpleType.java.diff \bug2279-svn\patch\castor\castor\trunk\schema\src\main\java\org\exolab\castor\xml\schema\XMLType.java.diff \bug2279-svn\patch\castor\castor\trunk\schema\src\main\java\org\exolab\castor\xml\schema\writer\SchemaWriter.java.diff
        Hide
        Alexandre Delarge added a comment -

        Werner,

        Please find attached the adjusted patch you requested.

        Rgds,
        Alex.

        Show
        Alexandre Delarge added a comment - Werner, Please find attached the adjusted patch you requested. Rgds, Alex.
        Hide
        Werner Guttmann added a comment -

        Unified patch against ./schema. In addition, all tests run successfully.

        Show
        Werner Guttmann added a comment - Unified patch against ./schema. In addition, all tests run successfully.

          People

          • Assignee:
            Werner Guttmann
            Reporter:
            Alexandre Delarge
          • Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

            Dates

            • Created:
              Updated:
              Resolved:

              Time Tracking

              Estimated:
              Original Estimate - Not Specified
              Not Specified
              Remaining:
              Remaining Estimate - 0 minutes
              0m
              Logged:
              Time Spent - 20 minutes
              20m