History | Log In     View a printable version of the current page.  
Issue Details (XML | Word | Printable)

Key: CASTOR-1986
Type: Bug Bug
Status: Open Open
Priority: Critical Critical
Assignee: Werner Guttmann
Reporter: Godmar Back
Votes: 0
Watchers: 0
Operations

If you were logged in you would be able to see more operations.
castor

castor fails to include import statement for externally defined types

Created: 23/May/07 07:18 PM   Updated: 28/Jul/07 05:57 AM
Component/s: XML code generator
Affects Version/s: 1.1
Fix Version/s: None

Time Tracking:
Not Specified

File Attachments: 1. Text File patch.c1986.20070528.txt (1 kb)

Issue Links:
Related
 


 Description  « Hide
Consider http://worldcatlibraries.org/registry/resolver/ResolverRecords.xsd

The generated code accesses ResolverRegistryEntry in descriptros.RecordsDescriptor.java, but fails to import it with a Java import statement. Consequently, compilation fails - even if the ResolverRegisterEntry type is present.



 All   Comments   Work Log   Change History      Sort Order: Ascending order - Click to sort in descending order
Werner Guttmann - 24/May/07 04:36 AM
Are you using namespace to package mappings in any sense ?

Godmar Back - 24/May/07 07:31 AM
I don't know what you mean.

The problem is that your code doesn't include a necessary import statement. To reproduce, do

wget http://worldcatlibraries.org/registry/resolver/ResolverRecords.xsd
wget http://worldcatlibraries.org/registry/resolver/Resolver.xsd

(The second one is included via
<xs:import namespace="http://worldcatlibraries.org/registry/resolver"
schemaLocation="http://worldcatlibraries.org/registry/resolver/Resolver.xsd"/>
)

Then do
java org.exolab.castor.builder.SourceGenerator -i ResolverRecords.xsd -f -package ppp
java org.exolab.castor.builder.SourceGenerator -i Resolver.xsd -f -package ppp

and try compiling. It fails because you don't import ResolverRegistryEntry in ppp.descriptors.RecordsDescriptor.java


Werner Guttmann - 25/May/07 04:39 AM
Adding the -generateImportedSchemas flag to your first call of the source generator should do the trick, as this way Castor will be able to resolve imports as a result of imports/includes itself.

Godmar Back - 25/May/07 07:55 AM
Thanks, I try that. Even if that works, it is still a bug in Castor that needs to be fixed.
  • Godmar

Godmar Back - 25/May/07 12:25 PM
Still doesn't work because imports are missing:

For instance:
org/libx/xml/oclcregistry/descriptors/ResolverDescriptor.java:330: cannot find symbol
symbol : class OpenURLVersions
location: class org.libx.xml.oclcregistry.descriptors.ResolverDescriptor
desc = new org.exolab.castor.xml.util.XMLFieldDescriptorImpl(OpenURLVersions.class, "_openURLVersionsList", "OpenURLVersions", org.exolab.castor.xml.NodeType.Element);
^


Werner Guttmann - 27/May/07 05:45 AM
I'd be interested to know why you think this is a bug that needs to be fixed (ignoring your second reply for the time being). If a class A refers to class B (that is generated as a result of another XML schema), and that class is not generated at all or elsewhere (e.g. in a different package), I would not consider this to be a bug.

Werner Guttmann - 27/May/07 05:46 AM
Having said that, I will have a look at your scenario.

Werner Guttmann - 27/May/07 06:31 AM
Adding the following property to a custom builder property file

org.exolab.castor.builder.nspackages=\
http://worldcatlibraries.org/registry/resolver=xml.c1986.generated

resolves all compilation problems for me. Please note that I am running this against SVN trunk. Would you be willing to give this a try, using e.g. one of the 1.1.2 snapshot releases, e.g. the latest available.


Werner Guttmann - 27/May/07 06:32 AM
Please note that I agree that it should not be necessary to add a namespace to package mapping at all. But with the high number of (regression) issues being reported right now, I'd rather give you a work-around and have a look at this in more detail later on.

Godmar Back - 27/May/07 09:17 AM

You're misdescribing the scenario. Could you just wget the two files, run the source generator, then compile it yourself while specifying a package name? The scenario is that

"If a class A refers to class B (that is generated as a result of another XML schema), and that class is in fact generated with the same package prefix 'p.q.r', and if Castor fails to "import p.q.r.A;" when it generates the code for "p.q.r.descriptors.B", but still produces code that accesses 'A', rather than p.q.r.A, I would consider this a bug."

Castor refers to a class ('ResolverRegistryEntry') in its code in a class in the descriptor.* subhierarchy, AND it does not use the fully qualified class name when doing so, nor does it import this class - this is incorrect, no matter how you look at it.

I note that as I work-around, I'm running the source generator for both .xsd's, then use a simple sed script to place the missing import statement. The code compiles and runs.

  • Godmar

Werner Guttmann - 27/May/07 11:36 AM
I think there's no need to get upset here, Godmar. For what it's worth, I am spending an hour of my weekend to help you with your issue, and apparently I am not being paid for this work.

> Could you just wget the two files, run the source
> generator, then compile it yourself while specifying a package name?

Which is exactly what I am doing, the only difference being that on the source generator, I am setting setGenerateImportedSchemas() to true, and I am using the custom property as shown above. With these two 'changes', I get code that compiles without any warnings/errors.

In other words, using the setGenerateImportedSchemas() setter or not obviously makes a difference, and given the fact that your main XML schema imports/includes the other one, I'd suggest that you change your code generation strategy. That's basically all I am saying.


Godmar Back - 27/May/07 12:00 PM
which version are you using?

With Castor 1.1, and with setGenerateImportedSchemas given, I'm getting a new set of errors, posted above. (See the post "Still doesn't work because imports are missing:..."

That said, it is incorrect for Castor to output code that it knows will not compile - because it doesn't follow the Java language specification - even if user error were to blame; Castor should output an error message instead.

Your efforts are appreciated.


Werner Guttmann - 27/May/07 12:05 PM
I am running this against SVN trunk (which is almost identical to the latest 1.1.2 snapshot release). And even with 1.1.1, a few problems related to incorrect/missing import statements have been fixed. Would you be willing to try it with the latest 1.1.2 snapshot release ? In other words, it works for me, and there's no compilation errors.

Godmar Back - 27/May/07 12:08 PM
ps: I don't know if this makes a difference, but I have boundproperties enabled in my castor build properties file. And keep in mind the issue occurs because a class defined in the main package directory needs to be explicitly import in classes defined in the descriptors/* subdirectory.

Godmar Back - 27/May/07 12:10 PM
Are you saying the problems have been fixed between 1.1 and 1.1.1? I'm using 1.1

I'm hesitant to willy-nilly upgrade Castor, since different versions often change APIs and require debugging & fixes because of that, but if you know that this bug has been fixed there I'd do it.


Werner Guttmann - 27/May/07 12:56 PM
Godmar, I guess a minor release always tends to be a bug fix release only, where (mostly) regression issues are being addressed. And in the case of 1.1.1, we are mainly talking about issues that are a result of folks switching (for the first time ever) away from 0.9.x releases (which they have not been willing until the arrival of 1.1 - psychology at work). Let me reassure you that 1.1.1 is a bug fix release only. In addition, the release notes available at http://www.castor.org/release-notes.html clearly describe the scope of a release. In other words, we try to provide enough information (and a lot of time to create this information set) for you to come up with an educated decision whether to upgrade or not.

Having said that, yes, there have been some issues with this area, and yes, most of them have been fixed already (as it works on SVN trunk). I know for sure that CASTOR-1934 is one remaining issue which has not been committed yet, as I'd like to imprive the test scenarios for this particular one, but a patch has already been made available.

And finally, can you please attach your custom builder property file to this issue, as clearly this is part of your working environment, which I'd like to reproduce. The more information I have, the better .., for obvious reasons. Having said that, I don't believe that enabling bound properties will actually make a difference.

If I were you, I'd download 1.1.1, and test drive this scenario, If it fixes your problem, fine for you. I'd then subsequently read the release notes for 1.1.1 to see whether there's any potential changes that affect you (in a negative way). If 1.1.1 is not sufficient for your needs, I can offer you a snapshot release that is immutable and should cover your needs until we make 1.1.2 available publicly.,

PS And no, we do not change public APIs as we like, unless we are talking about a major release. And even then we use deprecation (sic!). Sorry, but there's loads of commercial grade users of Castor, and we simply cannot allow ourselves to be unprofessional.


Godmar Back - 27/May/07 08:43 PM
I read the changelog before I filed this bug report, and I didn't see this issue addressed. The issue persists in 1.1.1. To reproduce the issue, please run the following shell script:

mkdir castor
cd castor
wget http://dist.codehaus.org/castor/1.1.1/castor-1.1.1.jar
wget http://dist.codehaus.org/castor/1.1.1/castor-1.1.1-codegen.jar
wget http://worldcatlibraries.org/registry/resolver/Resolver.xsd
wget http://worldcatlibraries.org/registry/resolver/ResolverRecords.xsd
java -Djava.ext.dirs=/usr/share/java -cp castor-1.1.1.jar:castor-1.1.1-codegen.jar:. org.exolab.castor.builder.SourceGenerator -i ResolverRecords.xsd -f -package ppp
java -Djava.ext.dirs=/usr/share/java -cp castor-1.1.1.jar:castor-1.1.1-codegen.jar:. org.exolab.castor.builder.SourceGenerator -i Resolver.xsd -f -package ppp
find ppp -name '*.java' | xargs javac -cp castor-1.1.1.jar

(Adjust the -Djava.ext.dirs as necessary to include your commons logging facility.)

The error msg is:


ppp/descriptors/RecordsDescriptor.java:74: cannot find symbol
symbol : class ResolverRegistryEntry
location: class ppp.descriptors.RecordsDescriptor
desc = new org.exolab.castor.xml.util.XMLFieldDescriptorImpl(ResolverRegistryEntry.class, "_resolverRegistryEntryList", "resolverRegistryEntry", org.exolab.castor.xml.NodeType.Element);
^
ppp/descriptors/RecordsDescriptor.java:87: cannot find symbol
symbol: class ResolverRegistryEntry
target.addResolverRegistryEntry( (ResolverRegistryEntry) value);
^
ppp/descriptors/RecordsDescriptor.java:101: cannot find symbol
symbol: class ResolverRegistryEntry
return new ResolverRegistryEntry();
^


To reproduce the failure of castor 1.1.1 when generateImportSchemas is given, please execute this shell script:


mkdir castor2
cd castor2
wget http://dist.codehaus.org/castor/1.1.1/castor-1.1.1.jar
wget http://dist.codehaus.org/castor/1.1.1/castor-1.1.1-codegen.jar
wget http://worldcatlibraries.org/registry/resolver/ResolverRecords.xsd
java -Djava.ext.dirs=/usr/share/java -cp castor-1.1.1.jar:castor-1.1.1-codegen.jar:. org.exolab.castor.builder.SourceGenerator -generateImportedSchemas -i ResolverRecords.xsd -f -package ppp
find ppp -name '*.java' | xargs javac -cp castor-1.1.1.jar


You'll again get numerous error msg.

Run this script to implement your suggestion of setting org.exolab.castor.builder.nspackages:


mkdir castor3
cd castor3
cat > castorbuilder.properties << EOF
org.exolab.castor.builder.nspackages=http://worldcatlibraries.org/registry/resolver=xml.c1986.generated
EOF
wget http://dist.codehaus.org/castor/1.1.1/castor-1.1.1.jar
wget http://dist.codehaus.org/castor/1.1.1/castor-1.1.1-codegen.jar
wget http://worldcatlibraries.org/registry/resolver/ResolverRecords.xsd
java -Djava.ext.dirs=/usr/share/java -cp castor-1.1.1.jar:castor-1.1.1-codegen.jar:. org.exolab.castor.builder.SourceGenerator -generateImportedSchemas -i ResolverRecords.xsd -f -package ppp
find ppp -name '*.java' | xargs javac -cp castor-1.1.1.jar

which yields errors such as:
"package xml.c1986.generated does not exist"


Godmar Back - 27/May/07 09:24 PM
ps: if I specify the package 'ppp' in castorbuilder.properties instead of the magic 'xml.c1986.generated', it compiles.

If the nspackages directive in castorbuilder.properties is indeed necessary, castor should refuse to generate the .java file unless such as declaration is given, lest it knowingly generates code that does not conform to the JLS.


Godmar Back - 27/May/07 09:37 PM
pps: alternatively, a reasonable default assumption would be that the classes of the externally referred schema use the same package prefix (while still allowing nspackages in castorbuilder.properties to override them.) In either case, silently generating code that is known not to compile is wrong.

Werner Guttmann - 28/May/07 09:23 AM
Godmar, above I have provided you with a work-around that enables you to use Castor without having to wait for a patch to be applied and a new release. And that gives me time to investigate things further .. at my personal pace. If you wanted this to be dealt with in a faster way, please consider taking up professional services. I hope you reckon that this is an open source project that relies on my (unpaid) contributions, contributions from all other committers and the community (potentially including you).

The namespace to package mapping should, indeed, not be required for your XML schemas to work. In other words, yes, there's most likely a bug. And we'll be looking at this. But by providing you with a work-around (using the nspackage directive as shown above), I tried to decrease dependencies for you.


Werner Guttmann - 28/May/07 09:48 AM
Initial patch for review.

Werner Guttmann - 28/May/07 09:49 AM
Hmm, something makes me think that the patch attached here is far cleaner than the one attached with CASTOR-1934.

Werner Guttmann - 01/Jun/07 04:59 AM
Godmar, would you be able to attach a sample XML document that I could use to create a new regression test case for our internal test suite ? Once that has been attached, I am ready to commit the patch as is.

Godmar Back - 01/Jun/07 07:28 AM
Please see the URLs already included above for sample documents.

Werner Guttmann - 01/Jun/07 01:53 PM
Mayve I am blind, but I cannot find a URL for an XML document that happens to be a valid XML document that I could use for unmarshalling and that validates successfully against the given set of schemas.

Godmar Back - 01/Jun/07 02:05 PM
If you need an XML document, rather than the XML schema, download http://worldcatlibraries.org/registry/lookup?IP=128.208.3.88

Werner Guttmann - 01/Jun/07 02:17 PM
Appreciated.