Mark,
Yes the code is identical to what I do but I don't execute Ant to do the
generation I load the WSDL Schema and go from there.
Ant is probably simpler :o).
The code builds the types found in a <schema> tag within a WSDL
document.
The following is the code to generate the .xsb etc.
private void buildXMLBeansSchema() {
if (logger.isDebugEnabled()) {
logger.debug("buildXMLBeansSchema() - start");
}
System.out.println("Generating Java types to handle XML Schema
(WSDL).");
SchemaTypeLoader loader = null;
SchemaTypeLoader linkTo = null;
SchemaTypeSystemCompiler.Parameters params = null;
SchemaTypeSystem system = null;
org.apache.xmlbeans.ResourceLoader cpResourceLoader = null;
XmlObject wsdldoc = null;
XmlOptions xmlOptions = new XmlOptions();
ArrayList outerErrorListener = new ArrayList();
xmlOptions.setLoadLineNumbers();
xmlOptions.setLoadUseDefaultResolver();
// Check the commandline to see if the Xmlbeans -nopvr
// option has been specified
xmlOptions.setErrorListener(outerErrorListener);
if ( mCommandLine.hasOption(XMLBEANS_NO_PVR_FLAG) )
xmlOptions.setCompileNoPvrRule();
// Check the commandline to see if the Xmlbeans -noupa
// option has been specified
if ( mCommandLine.hasOption(XMLBEANS_NO_UPA_FLAG) )
xmlOptions.setCompileNoUpaRule();
xmlOptions.setLoadSubstituteNamespaces(Collections.singletonMap(
"http://schemas.xmlsoap.org/wsdl/",
"http://www.apache.org/internal/xmlbeans/wsdlsubst"));
// If we have WSDL data
if (mWSDLData != null) {
try {
// Create an Xmlbeans class loader
loader =
XmlBeans.typeLoaderForClassLoader(SchemaDocument.class
.getClassLoader());
// Parse the WSDL using the Xmlbeans loader
// The return type is the most generic Xmlbeans object
// XmlObject which the XML equiv. to java.lang.Object
wsdldoc = loader.parse(mWSDLData, null, xmlOptions);
// Check and see if the returned root element is a WSDL
document root
if ( ! (wsdldoc instanceof
org.apache.xmlbeans.impl.xb.substwsdl.DefinitionsDocument) ) {
if (logger.isDebugEnabled()) {
logger
.debug("buildXMLBeansSchema() - Not a
valid WSDL schema. Exiting ");
}
System.exit(-4);
}
// SOAP encoded WSDL documents not supported
if (wsdlContainsEncoded(wsdldoc)) {
if (logger.isDebugEnabled()) {
logger
.debug("buildXMLBeansSchema() - The WSDL
description uses SOAP encoding. SOAP encoding is not compatible with
literal XML Schema.");
}
System.exit(-5);
}
// Get the elements contained in the WSDL <definitions>
// section of the document
XmlObject[] types =
((org.apache.xmlbeans.impl.xb.substwsdl.DefinitionsDocument) wsdldoc)
.getDefinitions().getTypesArray();
int count = 0;
XmlObject[] schemas = null;
// Gat an array of the schema elements for the types
found
for (int j = 0; j < types.length; j++) {
schemas = types[j]
.selectPath("declare namespace
xs=\"http://www.w3.org/2001/XMLSchema\" xs:schema");
if (schemas.length == 0) {
if (logger.isDebugEnabled()) {
logger
.debug("buildXMLBeansSchema() - The
WSDL definition did not have any schema documents in namespace
'http://www.w3.org/2001/XMLSchema'");
}
continue;
}
// Create an XmlOptions var. This is used to specify
// properties to Xmlbeans which it uses
// during processing
XmlOptions opts = new XmlOptions();
opts.setValidateTreatLaxAsSkip();
// An ArrayList in which Xmlbeans can store error
// messages it creates during processing. The
// objects are of type XmlError
opts.setErrorListener(outerErrorListener);
// Loop around the schemas found.
for (int k = 0; k < schemas.length; k++) {
// Check the schema really is a schema
if (schemas[k] instanceof SchemaDocument.Schema
// And make sure it is a valid schema
&& schemas[k].validate(opts)) {
count++;
if (logger.isDebugEnabled()) {
logger
.debug("buildXMLBeansSchema() -
Valid schema found.");
}
} else {
if (logger.isDebugEnabled()) {
logger
.debug("buildXMLBeansSchema() -
Invalid schema found.");
}
}
}
}
// Convert the array of schemas to an Xmlbeans
structured array of
// Schemas
SchemaDocument.Schema[] sdocs = new
SchemaDocument.Schema[schemas.length];
sdocs = (SchemaDocument.Schema[]) schemas;
ConfigDocument.Config[] cdocs = null;
linkTo = SchemaTypeLoaderImpl.build(null,
cpResourceLoader,
null);
params = new SchemaTypeSystemCompiler.Parameters();
params.setSchemas(sdocs);
params.setLinkTo(linkTo);
params.setJavaize(true);
params.setOptions(xmlOptions);
// build the in-memory type system
XmlErrorWatcher errorListener = new XmlErrorWatcher(
outerErrorListener);
try {
// Build the necessary Xmlbeans .xsb binary files
mSchemaTypeSystem =
SchemaTypeSystemCompiler.compile(params);
// Check sure we recieved a SchemaTypeSystem back
if (mSchemaTypeSystem != null) {
if (logger.isDebugEnabled()) {
logger
.debug("buildXMLBeansSchema() -
SchemaTypeSystem"
+
mSchemaTypeSystem.toString());
}
// Output directory for bin files
File classesDir = null;
// Check to see if the output directory has
// been specified on the commandline
if ( mCommandLine.hasOption(XMLBEANS_BIN_DIR) )
{
classesDir =
new
File(mCommandLine.getOptionValue(XMLBEANS_BIN_DIR));
} else {
// Set the default output directory
// The binary file output directory
classesDir = new File("./bin");
}
// Source directory
File srcDir = null;
// Check to see if the source directory has
// been specified on the commandline
if ( mCommandLine.hasOption(XMLBEANS_SRC_DIR) )
{
srcDir =
new
File(mCommandLine.getOptionValue(XMLBEANS_SRC_DIR));
} else {
// Set the default output directory
// The binary file output directory
srcDir = new File("./src");
}
if (!classesDir.exists())
classesDir.mkdirs();
if (!srcDir.exists())
srcDir.mkdirs();
FilerImpl filer = new FilerImpl(classesDir,
srcDir,
null, true, false);
mSchemaTypeSystem.save(filer);
if (SchemaTypeSystemCompiler.generateTypes(
mSchemaTypeSystem, filer, new
XmlOptions())) {
if (logger.isDebugEnabled()) {
logger
.debug("buildXMLBeansSchema() -
generateTypes() success");
}
// If a Java source code compiler has been
specified then
// compiled the generated java classes
if ( mSourceCompiler != null ) {
Iterator lSourceFileIter =
filer.getSourceFiles().iterator();
while (lSourceFileIter.hasNext()) {
File lSourceFile =
(File)lSourceFileIter.next();
mSourceCompiler.compileSourceFile(srcDir, lSourceFile);
}
}
mGeneratedClassesMap =
mSourceCompiler.getClasses();
} else {
if (logger.isDebugEnabled()) {
logger
.debug("buildXMLBeansSchema() -
generateTypes() failed. Exiting ...");
}
System.exit(-6);
}
} else {
if (logger.isDebugEnabled()) {
logger
.debug("buildXMLBeansSchema() - An
unknown error occurred whilst parsing the schema. Exiting ...");
}
System.exit(-3);
}
} catch (Exception e) {
logger.error("buildXMLBeansSchema()", e);
}
if (errorListener.size() > 0) {
}
} catch (Exception e) {
logger.error("buildXMLBeansSchema()", e);
}
}
if (logger.isDebugEnabled()) {
logger.debug("buildXMLBeansSchema() - end");
}
}
Any questions give me a shout ...
Regards
Don
-----Original Message-----
From: Mark Hansen [mailto:[EMAIL PROTECTED]
Sent: 08 September 2005 21:46
To: [email protected]
Subject: Re: using SchemaType.getFullJavaName with dynamically compiled
schema
Thanks Don. I've been able to make it work using `scomp' to generate
the Java Type jars. To do this on the fly, I've had to invoke the Ant
task (org.apache.xmlbeans.impl.tool.XMLBean) inside my code. Then I
creating a class loader, and then a SchemaTypeLoader from the class
loader as follows:
ClassLoader cl = new URLCLassLoader(new URL[] {jarfile.toURL()});
SchemaTypeLoader stl = XmlBeans.typeLoaderForClassLoader(classLoader);
Is this roughly what you are doing? Using the Ant task this way is ugly
- are you doing something more elegant?
-- Mark
Don Stewart wrote:
> Mark,
>
> I do just what your asking. I load and parse a schema, generate the
> xsb's then for each SchemaType in the typesystem I get the java
> classname.
> After that I load the generated java files using Janino, using the
> classloader for java source which compiles them on the fly and then do
> some introspection/futher work using the class.
>
> My code is spread across a number of java source file. If you want
> mail me offline and I'll see about extracting the code into a mail for
you.
>
>
> Regards
>
> Don
>
> -----Original Message-----
> From: Mark Hansen [mailto:[EMAIL PROTECTED]
> Sent: 07 September 2005 18:49
> To: [email protected]
> Subject: using SchemaType.getFullJavaName with dynamically compiled
> schema
>
> I would like to do the following:
> (1) Compile a schema on the fly using XmlBeans.compileXsd
> (2) Inspect the resulting SchemaType instances to get information
> about their underlying Java methods so that I can use these methods to
> manipulate the instances.
>
> But, it seems that the compileXsd method (below) does not create the
> "Java Types Generated from User-Derived Schema" (as defined in
> docs/guide/conJavaTypesGeneratedFromUserDerived.html), but only the
> SchemaTypeSystem representations.
>
> public static SchemaTypeSystem compileXsd(XmlObject[] schemas,
> SchemaTypeLoader typepath,
> XmlOptions options)
> throws XmlException
>
> It seems that if I want to generate the Java Types, then I would need
> to use XmlBeans.compileXmlBeans. But, in order to use that method, I
> need to get an instance of BindingConfig and Filer, which seem to be
> empty implementations at this point.
>
> How can I create te Java Types on the fly so that methods such as
> SchemaType.getFullJavaName will work? Right now, these methods always
> return null.
>
> My code and sample output are below.
>
> Code Snippet
> ============
>
> SchemaTypeSystem schemaTypeSystem = XmlBeans.compileXsd
> (new XmlObject[] {schema}, XmlBeans.getBuiltinTypeSystem(), null);
> SchemaTypeLoader schemaTypeLoader = XmlBeans.typeLoaderUnion(
> new SchemaTypeLoader[] { schemaTypeSystem,
> XmlBeans.getBuiltinTypeSystem() });
>
> QName poElementQName = new QName("http://javector.com/samples",
> "PurchaseOrder");
> SchemaType poElementType =
> schemaTypeLoader.findElement(poElementQName).getType();
> String poElementJavaName = poElementType.getFullJavaName();
> System.out.println("PurchaseOrder element (root) has signature: " +
> poElementType.toString());
> System.out.println("PurchaseOrder element (root) has java class: " +
> poElementJavaName);
>
> Output
> ======
> PurchaseOrder element (root) has signature:
> E=PurchaseOrder|[EMAIL PROTECTED]://javector.com/samples
> PurchaseOrder element (root) has java class: null
>
>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [EMAIL PROTECTED]
> For additional commands, e-mail: [EMAIL PROTECTED]
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [EMAIL PROTECTED]
> For additional commands, e-mail: [EMAIL PROTECTED]
>
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]