SlideShare ist ein Scribd-Unternehmen logo
1 von 22
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.xsom.impl.*;
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

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;
// ....
}
<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
Elements

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')
Attributes

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;
}

<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
Types

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 !!
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)
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)
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();
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();
//
}
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();
//
}
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();
// ....
}
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();
//
}
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")){
//
}
}
References
ïŻ

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

ïŻ

http://www.stylusstudio.com/w3c/schema0/_index.htm

Weitere Àhnliche Inhalte

Ähnlich wie Java and XML Schema

Java 5 Features
Java 5 FeaturesJava 5 Features
Java 5 Featuressholavanalli
 
Java architecture for xml binding
Java architecture for xml bindingJava architecture for xml binding
Java architecture for xml bindingHosein Zare
 
The State of JavaScript (2015)
The State of JavaScript (2015)The State of JavaScript (2015)
The State of JavaScript (2015)Domenic Denicola
 
Scala presentation by Aleksandar Prokopec
Scala presentation by Aleksandar ProkopecScala presentation by Aleksandar Prokopec
Scala presentation by Aleksandar ProkopecLoĂŻc Descotte
 
RMI Java Programming Lab Manual 2019
RMI Java Programming Lab Manual 2019RMI Java Programming Lab Manual 2019
RMI Java Programming Lab Manual 2019Gebreigziabher Ab
 
XML Support: Specifications and Development
XML Support: Specifications and DevelopmentXML Support: Specifications and Development
XML Support: Specifications and DevelopmentPeter Eisentraut
 
Summary of C++17 features
Summary of C++17 featuresSummary of C++17 features
Summary of C++17 featuresBartlomiej Filipek
 
Ast transformations
Ast transformationsAst transformations
Ast transformationsHamletDRC
 
Sax Dom Tutorial
Sax Dom TutorialSax Dom Tutorial
Sax Dom Tutorialvikram singh
 
Design Patterns in .Net
Design Patterns in .NetDesign Patterns in .Net
Design Patterns in .NetDmitri Nesteruk
 
Xml generation and extraction using XMLDB
Xml generation and extraction using XMLDBXml generation and extraction using XMLDB
Xml generation and extraction using XMLDBpallavi kasibhotla
 
Working With XML in IDS Applications
Working With XML in IDS ApplicationsWorking With XML in IDS Applications
Working With XML in IDS ApplicationsKeshav Murthy
 
Swift study: Closure
Swift study: ClosureSwift study: Closure
Swift study: ClosureFutada Takashi
 
Advance Java Programs skeleton
Advance Java Programs skeletonAdvance Java Programs skeleton
Advance Java Programs skeletonIram Ramrajkar
 
Namespaces
NamespacesNamespaces
Namespaceszindadili
 

Ähnlich wie Java and XML Schema (20)

Java 5 Features
Java 5 FeaturesJava 5 Features
Java 5 Features
 
Java architecture for xml binding
Java architecture for xml bindingJava architecture for xml binding
Java architecture for xml binding
 
The State of JavaScript (2015)
The State of JavaScript (2015)The State of JavaScript (2015)
The State of JavaScript (2015)
 
Scala presentation by Aleksandar Prokopec
Scala presentation by Aleksandar ProkopecScala presentation by Aleksandar Prokopec
Scala presentation by Aleksandar Prokopec
 
RMI Java Programming Lab Manual 2019
RMI Java Programming Lab Manual 2019RMI Java Programming Lab Manual 2019
RMI Java Programming Lab Manual 2019
 
XML Support: Specifications and Development
XML Support: Specifications and DevelopmentXML Support: Specifications and Development
XML Support: Specifications and Development
 
Unfiltered Unveiled
Unfiltered UnveiledUnfiltered Unveiled
Unfiltered Unveiled
 
Summary of C++17 features
Summary of C++17 featuresSummary of C++17 features
Summary of C++17 features
 
Ast transformations
Ast transformationsAst transformations
Ast transformations
 
Sax Dom Tutorial
Sax Dom TutorialSax Dom Tutorial
Sax Dom Tutorial
 
Design Patterns in .Net
Design Patterns in .NetDesign Patterns in .Net
Design Patterns in .Net
 
Xml generation and extraction using XMLDB
Xml generation and extraction using XMLDBXml generation and extraction using XMLDB
Xml generation and extraction using XMLDB
 
Wien15 java8
Wien15 java8Wien15 java8
Wien15 java8
 
Java serialization
Java serializationJava serialization
Java serialization
 
What is new in Java 8
What is new in Java 8What is new in Java 8
What is new in Java 8
 
Working With XML in IDS Applications
Working With XML in IDS ApplicationsWorking With XML in IDS Applications
Working With XML in IDS Applications
 
zinno
zinnozinno
zinno
 
Swift study: Closure
Swift study: ClosureSwift study: Closure
Swift study: Closure
 
Advance Java Programs skeleton
Advance Java Programs skeletonAdvance Java Programs skeleton
Advance Java Programs skeleton
 
Namespaces
NamespacesNamespaces
Namespaces
 

Mehr von Raji Ghawi

Database Programming Techniques
Database Programming TechniquesDatabase Programming Techniques
Database Programming TechniquesRaji Ghawi
 
Java and XML
Java and XMLJava and XML
Java and XMLRaji Ghawi
 
Java and SPARQL
Java and SPARQLJava and SPARQL
Java and SPARQLRaji Ghawi
 
Java and OWL
Java and OWLJava and OWL
Java and OWLRaji Ghawi
 
Ontology-based Cooperation of Information Systems
Ontology-based Cooperation of Information SystemsOntology-based Cooperation of Information Systems
Ontology-based Cooperation of Information SystemsRaji Ghawi
 
OWSCIS: Ontology and Web Service based Cooperation of Information Sources
OWSCIS: Ontology and Web Service based Cooperation of Information SourcesOWSCIS: Ontology and Web Service based Cooperation of Information Sources
OWSCIS: Ontology and Web Service based Cooperation of Information SourcesRaji Ghawi
 
Coopération des SystÚmes d'Informations basée sur les Ontologies
Coopération des SystÚmes d'Informations basée sur les OntologiesCoopération des SystÚmes d'Informations basée sur les Ontologies
Coopération des SystÚmes d'Informations basée sur les OntologiesRaji Ghawi
 
Building Ontologies from Multiple Information Sources
Building Ontologies from Multiple Information SourcesBuilding Ontologies from Multiple Information Sources
Building Ontologies from Multiple Information SourcesRaji Ghawi
 
Database-to-Ontology Mapping Generation for Semantic Interoperability
Database-to-Ontology Mapping Generation for Semantic InteroperabilityDatabase-to-Ontology Mapping Generation for Semantic Interoperability
Database-to-Ontology Mapping Generation for Semantic InteroperabilityRaji Ghawi
 

Mehr von Raji Ghawi (12)

Database Programming Techniques
Database Programming TechniquesDatabase Programming Techniques
Database Programming Techniques
 
Java and XML
Java and XMLJava and XML
Java and XML
 
Java and SPARQL
Java and SPARQLJava and SPARQL
Java and SPARQL
 
Java and OWL
Java and OWLJava and OWL
Java and OWL
 
SPARQL
SPARQLSPARQL
SPARQL
 
XQuery
XQueryXQuery
XQuery
 
XPath
XPathXPath
XPath
 
Ontology-based Cooperation of Information Systems
Ontology-based Cooperation of Information SystemsOntology-based Cooperation of Information Systems
Ontology-based Cooperation of Information Systems
 
OWSCIS: Ontology and Web Service based Cooperation of Information Sources
OWSCIS: Ontology and Web Service based Cooperation of Information SourcesOWSCIS: Ontology and Web Service based Cooperation of Information Sources
OWSCIS: Ontology and Web Service based Cooperation of Information Sources
 
Coopération des SystÚmes d'Informations basée sur les Ontologies
Coopération des SystÚmes d'Informations basée sur les OntologiesCoopération des SystÚmes d'Informations basée sur les Ontologies
Coopération des SystÚmes d'Informations basée sur les Ontologies
 
Building Ontologies from Multiple Information Sources
Building Ontologies from Multiple Information SourcesBuilding Ontologies from Multiple Information Sources
Building Ontologies from Multiple Information Sources
 
Database-to-Ontology Mapping Generation for Semantic Interoperability
Database-to-Ontology Mapping Generation for Semantic InteroperabilityDatabase-to-Ontology Mapping Generation for Semantic Interoperability
Database-to-Ontology Mapping Generation for Semantic Interoperability
 

KĂŒrzlich hochgeladen

Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FMESafe Software
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAndrey Devyatkin
 
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdfRising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdfOrbitshub
 
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...Jeffrey Haguewood
 
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...apidays
 
ICT role in 21st century education and its challenges
ICT role in 21st century education and its challengesICT role in 21st century education and its challenges
ICT role in 21st century education and its challengesrafiqahmad00786416
 
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...apidays
 
Elevate Developer Efficiency & build GenAI Application with Amazon Q​
Elevate Developer Efficiency & build GenAI Application with Amazon Q​Elevate Developer Efficiency & build GenAI Application with Amazon Q​
Elevate Developer Efficiency & build GenAI Application with Amazon Q​Bhuvaneswari Subramani
 
Understanding the FAA Part 107 License ..
Understanding the FAA Part 107 License ..Understanding the FAA Part 107 License ..
Understanding the FAA Part 107 License ..Christopher Logan Kennedy
 
Six Myths about Ontologies: The Basics of Formal Ontology
Six Myths about Ontologies: The Basics of Formal OntologySix Myths about Ontologies: The Basics of Formal Ontology
Six Myths about Ontologies: The Basics of Formal Ontologyjohnbeverley2021
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyKhushali Kathiriya
 
Exploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with MilvusExploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with MilvusZilliz
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherRemote DBA Services
 
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...apidays
 
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWEREMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWERMadyBayot
 
Vector Search -An Introduction in Oracle Database 23ai.pptx
Vector Search -An Introduction in Oracle Database 23ai.pptxVector Search -An Introduction in Oracle Database 23ai.pptx
Vector Search -An Introduction in Oracle Database 23ai.pptxRemote DBA Services
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerThousandEyes
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodJuan lago vĂĄzquez
 
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...Orbitshub
 

KĂŒrzlich hochgeladen (20)

Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of Terraform
 
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdfRising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
 
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
 
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
 
ICT role in 21st century education and its challenges
ICT role in 21st century education and its challengesICT role in 21st century education and its challenges
ICT role in 21st century education and its challenges
 
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
 
Elevate Developer Efficiency & build GenAI Application with Amazon Q​
Elevate Developer Efficiency & build GenAI Application with Amazon Q​Elevate Developer Efficiency & build GenAI Application with Amazon Q​
Elevate Developer Efficiency & build GenAI Application with Amazon Q​
 
Understanding the FAA Part 107 License ..
Understanding the FAA Part 107 License ..Understanding the FAA Part 107 License ..
Understanding the FAA Part 107 License ..
 
Six Myths about Ontologies: The Basics of Formal Ontology
Six Myths about Ontologies: The Basics of Formal OntologySix Myths about Ontologies: The Basics of Formal Ontology
Six Myths about Ontologies: The Basics of Formal Ontology
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : Uncertainty
 
Exploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with MilvusExploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with Milvus
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
 
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWEREMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
 
Vector Search -An Introduction in Oracle Database 23ai.pptx
Vector Search -An Introduction in Oracle Database 23ai.pptxVector Search -An Introduction in Oracle Database 23ai.pptx
Vector Search -An Introduction in Oracle Database 23ai.pptx
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
 
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
 

Java and XML Schema

  • 1. Java and XML Schema XSOM XML Schema Object Model Raji GHAWI 19/01/2009
  • 3. Create the parser XSOMParser parser = new XSOMParser(); parser.setErrorHandler(new MyErrorHandler());
  • 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. Parse an XML schema file try { parser.parse(new File("./example.xsd")); // .... } catch (IOException ioe) { ioe.printStackTrace(); } catch (SAXException se) { se.printStackTrace(); }
  • 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. <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. Elements System.out.println("--------- elements ---------"); Iterator<XSElementDecl> elemIter = schema.iterateElementDecls(); while (elemIter.hasNext()) { XSElementDecl elem = elemIter.next(); System.out.println(describeElement(elem)); }
  • 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. Attributes System.out.println("--------- attributes ---------"); Iterator<XSAttributeDecl> attIter = schema.iterateAttributeDecls(); while (attIter.hasNext()) { XSAttributeDecl att = attIter.next(); System.out.println(describeAttribute(att)); }
  • 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. Types System.out.println("--------- types ---------"); Iterator<XSType> typeIter = schema.iterateTypes(); while (typeIter.hasNext()) { XSType st = typeIter.next(); System.out.println(describeType(st)); }
  • 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. 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. 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. 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. 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. 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. 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. 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. 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. References ïŻ https://xsom.dev.java.net/ (Kohsuke Kawaguchi, Sun Microsystems ) ïŻ http://www.stylusstudio.com/w3c/schema0/_index.htm