If I am correct about what you mean, you want to load a schema into a java
object, modify the schema and then write it back to a file. If so, here is
what you do. Use the org.exolab.castor.xml.schema.reader to generate an
org.exolab.castor.xml.schema.Schema object. You can then programmatically
add elements and types to the Schema, and then write it back to a file using
org.exolab.castor.xml.schema.writer. Figuring out how the Schema object
model works is a little tricky because the docs are not very good. I had to
load a Schema object into JBuilder debugger and peek around it to figure it
all out.
Here is some code that loads a schema and then traverses to find its main
element and ComplexType. Note that this code only loads a pretty simply
structured schema. Once you have loaded it you can programmatically add
ElementDecls to what I call the rootGroup. This should get you going but if
you are going to need to do more complex stuff you are going to have to
figure it out.
private void loadSchema(Reader schemaFile) throws
SchemaLoadException {
try{
InputSource source = new InputSource(schemaFile);
Parser parser = Configuration.getParser();
source.setSystemId(systemId); //This needs to be
build from a props
SchemaUnmarshaller schemaUnmarshaller = new
SchemaUnmarshaller();
parser.setDocumentHandler(schemaUnmarshaller);
parser.setErrorHandler(schemaUnmarshaller);
parser.parse(source);
schema = schemaUnmarshaller.getSchema();
}catch(Exception e){
logger.error("Exception in loadSchema.", e);
throw new SchemaLoadException("Error loading
Schema");
}
}
private void traverseSchema() throws SchemaLoadException{
//Scan through the schema object looking for the root
ComplexType and its Group (sequence)
if (schema == null) throw new SchemaLoadException("The
schema was null while attempting to traverseSchema().");
targetNamespace = schema.getTargetNamespace();
if (logger.isDebugEnabled()) logger.debug("Starting
Traverse, namespace = " + targetNamespace);
//Two routes to go here to get the root type (going with 2):
//(1) get the element declartion from the schema, and then
get its type
//(2) just get the type from the element name + "Type";
rootType = schema.getComplexType(type.getURIName() +
"Type");
if (rootType == null) throw new
SchemaLoadException("traverseSchema was unable to find the root ComplexType
'" + type.getURIName() + "Type'.");
if (rootType.getParticleCount() == 1){
Particle p = rootType.getParticle(0);
if (p instanceof Group && ((Group)p).getOrder() ==
Order.seq){
for(Enumeration e = ((Group)p).enumerate();
e.hasMoreElements();) {
Particle prt =
(Particle)e.nextElement();
switch(prt.getStructureType()){
case Structure.ELEMENT:
type.addElement((ElementDecl)prt);
break;
default:
throw new
SchemaLoadException("Unsupported Structure Type in Sequence Group: " +
prt.getStructureType());
}
}
}else{
throw new SchemaLoadException("The root
ComplexType '" + type.getURIName() + "Type', ");
}
}else{
throw new SchemaLoadException("The root ComplexType
'" + type.getURIName() + "Type', ");
}
}
Make note that the writer is experimental and is not supported, there are
some problems with the one in 0.9.3, but the latest version in CVS has some
fixes that make it usable for my purposes, but maybe not yours. Its pretty
simple to use:
SchemaWriter.enable = true;
SchemaWriter sw = new SchemaWriter(new
FileWriter(file));
sw.write(schema);
-----Original Message-----
From: Dan Schmierer [mailto:[EMAIL PROTECTED]]
Sent: Thursday, August 30, 2001 2:35 AM
To: [EMAIL PROTECTED]
Subject: [castor-dev] Using Castor to generate java objects at runtime
from an XSD instance
I can't find any good documentation describing how this would happen, anyone
care to run me through it? Via soap communication, I can receive an xml
schema instance that I'd like to convert into a java object on the fly,
manipulate, then convert back into xsd and send. Any steering in the right
direction would be appreciated.
Cheers
Dan Schmierer
-----------------------------------------------------------
If you wish to unsubscribe from this mailing, send mail to
[EMAIL PROTECTED] with a subject of:
unsubscribe castor-dev
-----------------------------------------------------------
If you wish to unsubscribe from this mailing, send mail to
[EMAIL PROTECTED] with a subject of:
unsubscribe castor-dev