Upload
raji-ghawi
View
1.027
Download
3
Embed Size (px)
DESCRIPTION
tutorial about processing XML Schema using Java with XSOM API https://xsom.java.net/
Citation preview
Java and XML Schema
Raji GHAWI
19/01/2009
XSOMXML Schema Object Model
Import required packages
import com.sun.xml.xsom.*;import com.sun.xml.xsom.XSModelGroup.*;import com.sun.xml.xsom.impl.*;import com.sun.xml.xsom.parser.*;
Create the parser
XSOMParser parser = new XSOMParser();
parser.setErrorHandler(new MyErrorHandler());
MyErrorHandler
public class MyErrorHandler implements ErrorHandler{
public void warning(SAXParseException se){ System.err.println("warning : "+se.getMessage()); }
public void error(SAXParseException se){ System.err.println("error : "+se.getMessage()); }
public void fatalError(SAXParseException se){ System.err.println("fatal error : "+se.getMessage()); }}
Parse an XML schema file
try { parser.parse(new File("./example.xsd"));
// ....} catch (IOException ioe) { ioe.printStackTrace();} catch (SAXException se) { se.printStackTrace();}
XSSchemaSet schemaSet = parser.getResult();
Iterator<XSSchema> schemaIter = schemaSet.iterateSchema();while (schemaIter.hasNext()) { XSSchema schema = (XSSchema) schemaIter.next();
if(schema.getTargetNamespace(). equals("http://www.w3.org/2001/XMLSchema")) continue; // .... }
XSSchemaSet
<xs:element name="product"> <xs:complexType> <xs:attribute name="prodid" type="xs:positiveInteger" /> </xs:complexType> </xs:element>
<xs:element name="member" type="personinfo" /> <xs:complexType name="personinfo"> <xs:sequence> <xs:element name="firstname" type="xs:string" /> <xs:element name="lastname" type="xs:string" /> </xs:sequence> </xs:complexType> <xs:element name="car" default="Audi"> <xs:simpleType> <xs:restriction base="xs:string"> <xs:enumeration value="Audi" /> <xs:enumeration value="Golf" /> <xs:enumeration value="BMW" /> </xs:restriction> </xs:simpleType> </xs:element>
<xs:element name="car2" type="carType" fixed="BMW" /> <xs:simpleType name="carType"> <xs:restriction base="xs:string"> <xs:enumeration value="Audi" /> <xs:enumeration value="Golf" /> <xs:enumeration value="BMW" /> </xs:restriction> </xs:simpleType>
<xs:element name="dateborn" type="xs:date" abstract="true" />
anonymous simple type
primitive type
named simple type
anonymous complex type
named complex type
System.out.println("--------- elements ---------");
Iterator<XSElementDecl> elemIter = schema.iterateElementDecls();while (elemIter.hasNext()) { XSElementDecl elem = elemIter.next();
System.out.println(describeElement(elem));}
Elements
public static String describeElement(XSElementDecl elem) { String txt = elem.getName(); XSType type = elem.getType(); txt += (" \t ("+type.toString()+")");
if (elem.isGlobal()) txt += " (global)"; else if (elem.isLocal()) txt += " (local)";
if (elem.isAbstract()) txt += " \t(abstract)";
XmlString defaultValue = elem.getDefaultValue(); if(defaultValue!=null){ txt += " \t(default='"+defaultValue+"')"; }
XmlString fixedValue = elem.getFixedValue(); if(fixedValue!=null){ txt += " \t(fixed='"+fixedValue+"')"; } return txt;} --------- elements ---------
car (anonymous simple type) (global) (default='Audi')member (personinfo complex type) (global)product (anonymous complex type) (global)dateborn (date simple type) (global) (abstract)car2 (carType simple type) (global) (fixed='BMW')
Elements
System.out.println("--------- attributes ---------");
Iterator<XSAttributeDecl> attIter = schema.iterateAttributeDecls();while (attIter.hasNext()) { XSAttributeDecl att = attIter.next();
System.out.println(describeAttribute(att));}
Attributes
public static String describeAttribute(XSAttributeDecl att) { String txt = att.getName()+" \t "+att.getType().getName();
XmlString defaultValue = att.getDefaultValue(); if(defaultValue!=null){ txt += " \t(default='"+defaultValue+"')"; }
XmlString fixedValue = att.getFixedValue(); if(fixedValue!=null){ txt += " \t(fixed='"+fixedValue+"')"; }
// isRequired ?? return txt;}
--------- attributes ---------lang1 stringlang3 string (fixed='EN')lang2 string (default='EN')lang4 string
<xs:attribute name="lang1" type="xs:string" /> <xs:attribute name="lang2" type="xs:string" default="EN" /> <xs:attribute name="lang3" type="xs:string" fixed="EN" /> <xs:attribute name="lang4" type="xs:string" use="required" />
Attributes
System.out.println("--------- types ---------");
Iterator<XSType> typeIter = schema.iterateTypes();while (typeIter.hasNext()) { XSType st = typeIter.next();
System.out.println(describeType(st));}
Types
public static String describeType(XSType type) { String txt = ""; if(type.isAnonymous()) txt += "(anonymous)"; else txt += type.getName();
if (type.isGlobal()) txt += " (global)"; else if (type.isLocal()) txt += " (local)";
if(type.isComplexType()) txt += " (complex)"; else if(type.isSimpleType()) txt += " (simple)";
int deriv = type.getDerivationMethod(); switch(deriv){ case XSType.EXTENSION: txt += " (EXTENSION)"; break; case XSType.RESTRICTION: txt += " (RESTRICTION)"; break; case XSType.SUBSTITUTION: txt += " (SUBSTITUTION)"; break; } return txt;} --------- types ---------
carType (global) (simple) (RESTRICTION)personinfo (global) (complex) (RESTRICTION)
Global Types only !!
Types
public static Vector<XSType> allTypes = new Vector<XSType>();
Iterator<XSType> typeIter = schema.iterateTypes();while (typeIter.hasNext()) { XSType st = typeIter.next(); allTypes.addElement(st);}
// ....
System.out.println("--------- types ---------");for (int i = 0; i < allTypes.size(); i++) { XSType type = allTypes.get(i); System.out.println(describeType(type));}
public static String describeElement(XSElementDecl elem) { String txt = elem.getName(); XSType type = elem.getType();
if(!allTypes.contains(type)){ allTypes.addElement(type); }
// ....}
--------- types ---------carType (global) (simple) (RESTRICTION)personinfo (global) (complex) (RESTRICTION)(anonymous) (local) (simple) (RESTRICTION)(anonymous) (local) (complex) (RESTRICTION)date (global) (simple) (RESTRICTION)
primitive type !!
How to get all types ?
public static String describeElement(XSElementDecl elem) { String txt = elem.getName(); XSType type = elem.getType();
if(!allTypes.contains(type)){ if(type.isSimpleType()){ if(!type.asSimpleType().isPrimitive()){ allTypes.addElement(type); } } else { allTypes.addElement(type); } }
// ....}
--------- types ---------carType (global) (simple) (RESTRICTION)personinfo (global) (complex) (RESTRICTION)(anonymous) (local) (simple) (RESTRICTION)(anonymous) (local) (complex) (RESTRICTION)
How to ignore primitive types ?
// XSType type
if(type.isComplexType()){ XSComplexType complex = type.asComplexType(); //
} else if(type.isSimpleType()){ XSSimpleType simple = type.asSimpleType(); //}
schema.iterateSimpleTypes();
schema.iterateComplexTypes();
Complex and Simple Types
// XSComplexType complex
XSContentType contenetType = complex.getContentType(); if(contenetType instanceof EmptyImpl){ XSContentType empty = contenetType.asEmpty(); //} else if(contenetType instanceof ParticleImpl){ XSTerm term = contenetType.asParticle().getTerm(); //} else if(contenetType instanceof SimpleTypeImpl){ XSSimpleType st = contenetType.asSimpleType(); //}
XSComplexType
// XSTerm term
if(term.isElementDecl()){ XSElementDecl elem = term.asElementDecl(); //} else if(term.isModelGroup()){ XSModelGroup mg = term.asModelGroup(); //} else if(term.isModelGroupDecl()){ XSModelGroupDecl mgd = term.asModelGroupDecl(); XSModelGroup mg = mgd.getModelGroup(); //} else if(term.isWildcard()){ XSWildcard wildcard = term.asWildcard(); //}
XSTerm
// XSModelGroup modelGroup
Compositor comp = modelGroup.getCompositor();if(comp.equals(Compositor.SEQUENCE)){ // } else if(comp.equals(Compositor.ALL)){ // } else if(comp.equals(Compositor.CHOICE)){ //}
XSParticle[] particles = modelGroup.getChildren();for (int i = 0; i < particles.length; i++) { XSTerm term = particles[i].getTerm(); // ....}
XSModelGroup
// XSSimpleType simpe
if(simple.isList()){ XSListSimpleType lst = simple.asList(); //} else if(simple.isUnion()){ XSUnionSimpleType ust = simple.asUnion(); //} else if(simple.isRestriction()){ XSRestrictionSimpleType rst = simple.asRestriction(); //}
XSSimpleType
// XSRestrictionSimpleType restriction
XSType baseType = restriction.getBaseType();// ....
Iterator<?> facets = restriction.getDeclaredFacets().iterator();while (facets.hasNext()) { XSFacet facet = (XSFacet) facets.next(); String name = facet.getName(); XmlString value = facet.getValue(); if(name.equalsIgnoreCase("enumeration")){ // } else if(name.equalsIgnoreCase("minInclusive")){ // } else if(name.equalsIgnoreCase("minExclusive")){ // } else if(name.equalsIgnoreCase("maxInclusive")){ // } else if(name.equalsIgnoreCase("maxExclusive")){ // } else if(name.equalsIgnoreCase("whiteSpace")){ // }}
XSRestrictionSimpleType
References
https://xsom.dev.java.net/ (Kohsuke Kawaguchi, Sun Microsystems )
http://www.stylusstudio.com/w3c/schema0/_index.htm