Java and XML Schema
XSOM
XML Schema Object Model

Raji GHAWI
19/01/2009
Import required packages

import
import
import
import

com.sun.xml.xsom.*;
com.sun.xml.xsom.XSModelGroup.*;
com.sun.xml.xs...
Create the parser

XSOMParser parser = new XSOMParser();
parser.setErrorHandler(new MyErrorHandler());
MyErrorHandler

public class MyErrorHandler implements ErrorHandler{
public void warning(SAXParseException se){
System.err...
Parse an XML schema file

try {
parser.parse(new File("./example.xsd"));
// ....
} catch (IOException ioe) {
ioe.printStac...
XSSchemaSet

XSSchemaSet schemaSet = parser.getResult();
Iterator<XSSchema> schemaIter = schemaSet.iterateSchema();
while ...
<xs:element name="product">
<xs:complexType>
<xs:attribute name="prodid" type="xs:positiveInteger" />
</xs:complexType>
</...
Elements

System.out.println("--------- elements ---------");
Iterator<XSElementDecl> elemIter = schema.iterateElementDecl...
Elements
public static String describeElement(XSElementDecl elem) {
String txt = elem.getName();
XSType type = elem.getTyp...
Attributes

System.out.println("--------- attributes ---------");
Iterator<XSAttributeDecl> attIter = schema.iterateAttrib...
Attributes
public static String describeAttribute(XSAttributeDecl att) {
String txt = att.getName()+" t "+att.getType().ge...
Types

System.out.println("--------- types ---------");
Iterator<XSType> typeIter = schema.iterateTypes();
while (typeIter...
Types
public static String describeType(XSType type) {
String txt = "";
if(type.isAnonymous()) txt += "(anonymous)";
else ...
How to get all types ?
public static Vector<XSType> allTypes = new Vector<XSType>();
Iterator<XSType> typeIter = schema.it...
How to ignore primitive types ?

public static String describeElement(XSElementDecl elem) {
String txt = elem.getName();
X...
Complex and Simple Types

// XSType type
if(type.isComplexType()){
XSComplexType complex = type.asComplexType();
//
} else...
XSComplexType

// XSComplexType complex
XSContentType contenetType = complex.getContentType();
if(contenetType instanceof ...
XSTerm

//

XSTerm term

if(term.isElementDecl()){
XSElementDecl elem = term.asElementDecl();
//
} else if(term.isModelGro...
XSModelGroup

// XSModelGroup modelGroup
Compositor comp = modelGroup.getCompositor();
if(comp.equals(Compositor.SEQUENCE)...
XSSimpleType

// XSSimpleType simpe
if(simple.isList()){
XSListSimpleType lst = simple.asList();
//
} else if(simple.isUni...
XSRestrictionSimpleType
// XSRestrictionSimpleType restriction
XSType baseType = restriction.getBaseType();
// ....

Itera...
References


https://xsom.dev.java.net/
(Kohsuke Kawaguchi, Sun Microsystems )



http://www.stylusstudio.com/w3c/schema...
Upcoming SlideShare
Loading in …5
×

Java and XML Schema

2,048 views

Published on

tutorial about processing XML Schema using Java with XSOM API
https://xsom.java.net/

Published in: Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
2,048
On SlideShare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
58
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Java and XML Schema

  1. 1. Java and XML Schema XSOM XML Schema Object Model Raji GHAWI 19/01/2009
  2. 2. Import required packages import import import import com.sun.xml.xsom.*; com.sun.xml.xsom.XSModelGroup.*; com.sun.xml.xsom.impl.*; com.sun.xml.xsom.parser.*;
  3. 3. Create the parser XSOMParser parser = new XSOMParser(); parser.setErrorHandler(new MyErrorHandler());
  4. 4. 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()); } }
  5. 5. Parse an XML schema file try { parser.parse(new File("./example.xsd")); // .... } catch (IOException ioe) { ioe.printStackTrace(); } catch (SAXException se) { se.printStackTrace(); }
  6. 6. XSSchemaSet 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; // .... }
  7. 7. <xs:element name="product"> <xs:complexType> <xs:attribute name="prodid" type="xs:positiveInteger" /> </xs:complexType> </xs:element> anonymous complex type <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> named complex type <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> anonymous simple type <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> named simple type <xs:element name="dateborn" type="xs:date" abstract="true" /> primitive type
  8. 8. Elements System.out.println("--------- elements ---------"); Iterator<XSElementDecl> elemIter = schema.iterateElementDecls(); while (elemIter.hasNext()) { XSElementDecl elem = elemIter.next(); System.out.println(describeElement(elem)); }
  9. 9. 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')
  10. 10. Attributes System.out.println("--------- attributes ---------"); Iterator<XSAttributeDecl> attIter = schema.iterateAttributeDecls(); while (attIter.hasNext()) { XSAttributeDecl att = attIter.next(); System.out.println(describeAttribute(att)); }
  11. 11. 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; } <xs:attribute <xs:attribute <xs:attribute <xs:attribute name="lang1" name="lang2" name="lang3" name="lang4" type="xs:string" type="xs:string" type="xs:string" type="xs:string" /> default="EN" /> fixed="EN" /> use="required" /> --------- attributes --------lang1 string lang3 string (fixed='EN') lang2 string (default='EN') lang4 string
  12. 12. Types System.out.println("--------- types ---------"); Iterator<XSType> typeIter = schema.iterateTypes(); while (typeIter.hasNext()) { XSType st = typeIter.next(); System.out.println(describeType(st)); }
  13. 13. 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 !!
  14. 14. How to get all 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); } // .... } primitive type !! --------- types --------carType (global) (simple) (RESTRICTION) personinfo (global) (complex) (RESTRICTION) (anonymous) (local) (simple) (RESTRICTION) (anonymous) (local) (complex) (RESTRICTION) date (global) (simple) (RESTRICTION)
  15. 15. How to ignore primitive 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)
  16. 16. Complex and Simple Types // XSType type if(type.isComplexType()){ XSComplexType complex = type.asComplexType(); // } else if(type.isSimpleType()){ XSSimpleType simple = type.asSimpleType(); // } schema.iterateSimpleTypes(); schema.iterateComplexTypes();
  17. 17. XSComplexType // 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(); // }
  18. 18. XSTerm // 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(); // }
  19. 19. XSModelGroup // 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(); // .... }
  20. 20. XSSimpleType // 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(); // }
  21. 21. XSRestrictionSimpleType // 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")){ // } }
  22. 22. References  https://xsom.dev.java.net/ (Kohsuke Kawaguchi, Sun Microsystems )  http://www.stylusstudio.com/w3c/schema0/_index.htm

×