|
From: peter murray-r. <pm...@ca...> - 2006-02-12 15:30:32
|
At 12:32 12/02/2006, Egon Willighagen wrote:
This is exciting!
>Dear Peter,
>
>I know you autogenerate bindings for C, Fortran and Java automatically from
>the XML Schema...
I did in Jumbo4.6, though the code was rather convoluted and was
determined by composing syntax rather than creating
language-independent semantic objects and transforming them to a
language. So Jumbo5.0 (and 5.1) now generates results closer to
language-independent. However it is still a hybrid.
Also Jumbo is geared towards my subset of XSD schema, rather than a
generic schema parser (which would be horrendous).
>can you explain what would be needed to make bindings for
>the Qt DOM model [1,2]? The model is quite similar to, for example, the XOM
>DOM model, and things like Document, Element, Node, etc are available in the
>Qt DOM model too...
Look in package org.xmlcml.schemagen;
Most of the classes are concerned with reading the schema and
creating language-independent objects. Of course these objects are
oriented towards Java. This gives some of the concepts
AbstractClassGenerator
ArgGenerator
ClassWriter
ConstructorGenerator
ExceptionGenerator
ExpressionComponent
ExpressionGenerator
FieldGenerator
ImportGenerator
LineGenerator
MethodCallGenerator
MethodGenerator
OperatorGenerator
PackageGenerator
VariableGenerator
If you can map a Java method to a C++ method it should not be too
difficult to adjust this.
Then there are 4 writers:
TypeWriter - CML types
AttributeWiter - attributes
AbstractCML write the classes from elements
NodeFactoryWriter - creates a NodeFactory.
A typical example is given. Note that some objects are well defined
but others have much more java syntax. You would be very welcome to
generalise this! The first bit has little Java...
//class
// change to write to a single package
setPackage("org.xmlcml.cml.element");
setAbstract(true);
setExtends("CMLElement");
String simpleDoc = eg.getSummary();
// class documentation from element doc in schema
setSimpleDoc(simpleDoc);
String fullDoc = eg.getDescription();
setFullDoc("\n"+fullDoc+"\n\nNON-MOFIFIABLE class
autogenerated from schema\nDO NOT EDIT; ADD FUNCTIONALITY TO SUBCLASS");
//imports
makeImports();
if (eg.attributeVector.size() > 0) {
makeAttributeGroupNameTable();
}
//write attribute declarations (fields)
for (CMLAttribute attribute : eg.attributeVector) {
makeAttributeDeclaration(attribute);
}
FieldGenerator field =
new FieldGenerator(this, "String", false,
"TAG", "local name", S_QUOT+name+S_QUOT);
field.setStatic("static");
field.setFinal("final");
field.setAccess("public");
this.addField(field);
// and content pseudoattribute declaration (field)
if (eg.contentAttribute != null) {
field = new FieldGenerator(this,
"CMLAttribute", false, CMLXSD_XMLCONTENT, "content pseudoattribute", "");
field.setAccess("protected");
this.addField(field);
}
makeConstructors();
//write attribute accessor methods
for (int i = 0; i < eg.attributeVector.size(); i++) {
makeAccessorForAttribute((CMLAttribute)
eg.attributeVector.get(i));
}
//write child element declarations
for (Iterator it = eg.elementSet.iterator(); it.hasNext();) {
addAccessorsForChild((String) it.next());
}
//write content declarations
if (eg.contentType != null) {
comment("CONTENT of type :"+eg.contentTypeName);
makeAccessorsForText();
}
but this has more Java syntax...
this.addMethod(mg);
// get content
String getMethod = eg.contentAttribute.getJavaGetMethod();
mg = new MethodGenerator(this, "getXMLContent", "get
content");
mg.setAccess("public");
mg.setReturnType(type);
mg.addLine("String content = this.getValue()"+EL());
mg.addLine("if ("+attName+" == null) {");
mg.addLine(" "+attName+" = new
"+attributeClass+"(\""+CMLXSD_XMLCONTENT+"\")"+EL());
mg.addLine("
"+attName+".setSchemaType(CMLTypeList.getType("+S_QUOT+eg.contentType.getName()+S_QUOT+"))"+EL());
mg.addLine("}");
mg.addLine(""+CMLXSD_XMLCONTENT+".setCMLValue(content)"+EL());
mg.addLine("return
(("+attributeClass+")"+CMLXSD_XMLCONTENT+")."+getMethod+"()"+EL());
this.addMethod(mg);
}
Hope this makes sense. It would be great to get schemagen on a more
robust basis
P.
Peter Murray-Rust
Unilever Centre for Molecular Sciences Informatics
University of Cambridge,
Lensfield Road, Cambridge CB2 1EW, UK
+44-1223-763069
|