RE: Question about generating Java Source Code from XML Schemas

2013-08-21 Thread Paul Gillen
As is typical of me I probably don't understand your question or problem
thoroughly.  Well you said you couldn't use Maven so a non-maven response is
what you're getting.  :) 

Typical XmlBeans usage is to generate the classes in a jar and to then
reference this jar while developing your code.  If you can't use Ant or
Maven for some reason there is a command line utility to do this.  I
personally have a little bat file I use for generating demos:
scomp -out lib/%1.jar xsd/%1.xsd
If however there is some reason you have to "compile on the fly" I refer you
to an excellent discussion of doing this:

http://www.oracle.com/technetwork/articles/entarch/incremental-compilation-x
mlbeans-089127.html
Please reply with your use case for this.

As to your questions:
* Why do you get "URL "./th08_extern.xsd" is not well-formed"?  
Possibly because "./th08_extern.xsd" is not well-formed.
Post it and we'll see.  My demo of this worked correctly (see below).
* I want everything XmlBeans generates in the package
"ch.mypackage"; how can I to this? 
This is controlled by the schema namespace setting:
http://www.w3.org/2001/XMLSchema";>
Note that in typical "Computer Science Weenie my way is
better" fashion the namespace is llq.hlq.  XmlBeans will generate classes in
the more typical hlq.llq package.
* Why is this class not generated?
Well, nothing in your posted source showed that you compiled
the generated java files.  Refer to the URL above for more detail.  Or
previously compile using scomp as above.

Cordially,
Paul Gillen


http://www.w3.org/2001/XMLSchema";>











http://www.w3.org/2001/XMLSchema";>







package org.eltesto;

import java.io.File;

import org.apache.xmlbeans.BindingConfig;
import org.apache.xmlbeans.Filer;
import org.apache.xmlbeans.XmlBeans;
import org.apache.xmlbeans.XmlObject;
import org.apache.xmlbeans.XmlOptions;
import org.apache.xmlbeans.impl.util.FilerImpl;
import org.apache.xmlbeans.impl.xb.xsdschema.SchemaDocument;

public class Main {
public static void main(String[] args) throws Exception {
Main m = new Main();
m.go(args);
}
private void go(String[] args) throws Exception {
File xsd = new File("xsd/Root.xsd");
SchemaDocument xsdo = SchemaDocument.Factory.parse(xsd);


final Filer filer = new FilerImpl(null, new File("src/"),
null, true, true); 
final XmlOptions options = new XmlOptions(); 
options.setCompileDownloadUrls(); 
BindingConfig config = new BindingConfig();
XmlBeans.compileXmlBeans("TypeName", null, new XmlObject[] {
xsdo }, config, null, filer, options);
}
}

-Original Message-
From: Michael Szalay [mailto:michael.sza...@abacus.ch] 
Sent: Wednesday, August 21, 2013 9:39 AM
To: user@xmlbeans.apache.org
Subject: Question about generating Java Source Code from XML Schemas

Hi all

I have some xsd schemas in my source tree and I want to generate java
sources with XML Beans.
I cannot use ant or maven task, I have to do it in plain java.

I use the following java code:

final InputStream schema1Stream =
BeanGenerator.class.getResourceAsStream("xsd/schema1.xsd");
final XmlObject schema1 = XmlObject.Factory.parse(schema1Stream);
final InputStream schema2Stream =
BeanGenerator.class.getResourceAsStream("xsd/schema2.xsd");
final XmlObject schema2 = XmlObject.Factory.parse(schema2Stream);
final XmlObject[] schemas = { schema1, schema2 }; final BindingConfig config
= new BindingConfig() {
   @Override
   public String lookupPackageForNamespace(String uri) {
 return "ch.mypackage";
   }
};

final Filer filer = new FilerImpl(null, new File(".java/src/"), null, true,
true); final XmlOptions options = new XmlOptions();
options.setCompileDownloadUrls(); XmlBeans.compileXmlBeans(null, null,
schemas, config, null, filer, options);


I have questions:

1) The schemas have include statements like this: 

   

How can I make XmlBeans to include this schemas? Its located in the same
folder as the original schema, but XMLBeans does not find it:

URL "./th08_extern.xsd" is not well-formed

how to solve that?

2) I want everything XmlBeans generates in the package "ch.mypackage".
How can I to this? XmlBeans generates a lot of other stuff like xsb files
which are not in that package.

How can I say to XmlBeans to not generate this files?

3) When I use one of the generated classes, there is the following runtime
error:

java.lang.RuntimeException: Cannot load SchemaTypeSystem. Unable to load
class with name
schemaorg_apache_xmlbeans.system.s2EF0617F3F8756BE3

Re: Question about generating Java Source Code from XML Schemas

2013-08-21 Thread Michael Bishop
I think I can answer your first question. From what I recall in
parsing/reading documents, InputStream is a poor choice for XML documents
that include other documents. An InputStream object can't tell you where
its source is. Therefore, there is no way to find relative paths. I would
try the following:

final URL schema1Url = BeanGenerator.class.getResource("xsd/schema1.xsd");
final XmlObject schema1 = XmlObject.Factory.parse(schema1Url);

See if that helps.



On Wed, Aug 21, 2013 at 9:39 AM, Michael Szalay wrote:

> Hi all
>
> I have some xsd schemas in my source tree and I want to generate java
> sources with XML Beans.
> I cannot use ant or maven task, I have to do it in plain java.
>
> I use the following java code:
>
> final InputStream schema1Stream =
> BeanGenerator.class.getResourceAsStream("xsd/schema1.xsd");
> final XmlObject schema1 = XmlObject.Factory.parse(schema1Stream);
> final InputStream schema2Stream =
> BeanGenerator.class.getResourceAsStream("xsd/schema2.xsd");
> final XmlObject schema2 = XmlObject.Factory.parse(schema2Stream);
> final XmlObject[] schemas = { schema1, schema2 };
> final BindingConfig config = new BindingConfig() {
>@Override
>public String lookupPackageForNamespace(String uri) {
>  return "ch.mypackage";
>}
> };
>
> final Filer filer = new FilerImpl(null, new File(".java/src/"), null,
> true, true);
> final XmlOptions options = new XmlOptions();
> options.setCompileDownloadUrls();
> XmlBeans.compileXmlBeans(null, null, schemas, config, null, filer,
> options);
>
>
> I have questions:
>
> 1) The schemas have include statements like this:
>
>
>
> How can I make XmlBeans to include this schemas? Its located in the same
> folder as the original schema, but XMLBeans does not
> find it:
>
> URL "./th08_extern.xsd" is not well-formed
>
> how to solve that?
>
> 2) I want everything XmlBeans generates in the package "ch.mypackage".
> How can I to this? XmlBeans generates a lot of other stuff like xsb
> files which are not in that package.
>
> How can I say to XmlBeans to not generate this files?
>
> 3) When I use one of the generated classes, there is the following
> runtime error:
>
> java.lang.RuntimeException: Cannot load SchemaTypeSystem. Unable to load
> class with name
>
> schemaorg_apache_xmlbeans.system.s2EF0617F3F8756BE32108E3CF22693B4.TypeSystemHolder.
> Make sure the generated binary files are on the classpath.
> at
> org.apache.xmlbeans.XmlBeans.typeSystemForClassLoader(XmlBeans.java:783)
> ... 29 more
> Caused by: java.lang.ClassNotFoundException:
>
> schemaorg_apache_xmlbeans.system.s2EF0617F3F8756BE32108E3CF22693B4.TypeSystemHolder
> at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
> at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
> at java.security.AccessController.doPrivileged(Native Method)
> at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
> at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
> at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
> at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
> at
> org.apache.xmlbeans.XmlBeans.typeSystemForClassLoader(XmlBeans.java:769)
> ... 30 more
>
> What is the problem here? Why is this class not generated?
>
> Thanks for a response
> Michael
>
>
>
> -
> To unsubscribe, e-mail: user-unsubscr...@xmlbeans.apache.org
> For additional commands, e-mail: user-h...@xmlbeans.apache.org
>
>