I
Ian Pilcher
Howdy all!
I'm working with XML schemas for the first time, so please be gentle!
I am working on a graphics program (in Java if it matters), and I have
decided to use XML files to store the "objects" that make up a drawing.
A subset of the class hierarchy might look like:
+-------------------------+
| |
| skitch.objects.Object |
| |
+------------+------------+
|
+---------------+---------------+
| |
+------------+------------+ +------------+------------+
| | | |
| skitch.objects.Line | | skitch.objects.Square |
| | | |
+-------------------------+ +-------------------------+
I could create a schema that understands lines, squares, and all the
other shapes, but I want the program to be easily extensible and to use
a more dynamic "plug-in" mechanism.
My current solution uses two schemas for each object, a "generic"
schema (object.xsd):
<?xml version="1.0" encoding="US-ASCII"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:sk="xmlns://Skitch"
targetNamespace="xmlns://Skitch"
elementFormDefault="qualified">
<xs:include schemaLocation="basicTypes.xsd"/>
<xs:element name="object">
<xs:complexType>
<xs:sequence>
<xs:element name="objectData"/>
</xs:sequence>
<xs:attribute name="oid" type="skidType" use="required"/>
<xs:attribute name="class" type="xs:string" use="required"/>
<xs:attribute name="schema" type="xs:string" use="required"/>
</xs:complexType>
</xs:element>
</xs:schema>
Once a file has been validated with this schema, the program can
discover the appropriate Java class to instantiate for this object (the
"class" attribute) and the class-specific schema (the "schema"
attribute).
Here's an example of a "line" object:
<?xml version="1.0" encoding="US-ASCII"?>
<skbject xmlns:sk="xmlns://Skitch" oid="00000000"
class="skitch.objects.Line" schema="lineObject.xsd">
<skbjectData>
<sk:start x="0.0" y="0.0"/>
<sk:end x="100" y="100"/>
<sk:color red="0" green="0" blue="0" alpha="255"/>
<sk:stroke width="1.0" cap="CAP_BUTT" join="JOIN_MITER"
miterLimit="10.0" dashPhase="0.0">
<sk:dash>
<sk:segment length="1.0"/>
<sk:segment length="1.0"/>
</sk:dash>
</sk:stroke>
</skbjectData>
</skbject>
And here's its class-specific schema:
<?xml version="1.0" encoding="US-ASCII"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:sk="xmlns://Skitch"
targetNamespace="xmlns://Skitch"
elementFormDefault="qualified">
<xs:include schemaLocation="basicTypes.xsd"/>
<xs:element name="object">
<xs:complexType>
<xs:sequence>
<xs:element name="objectData" type="sk:lineObjectType"/>
</xs:sequence>
<xs:attribute name="oid" type="skidType" use="required"/>
<xs:attribute name="class" type="xs:string" use="required"/>
<xs:attribute name="schema" type="xs:string" use="required"/>
</xs:complexType>
</xs:element>
</xs:schema>
(basicTypes.xsd can be found at
http://home.comcast.net/~i.pilcher/xml/schemas/basicTypes.xsd.)
Things appears to be working so far, but validating every object twice
just seem inelegant.
Is there a better way?
Thanks!
I'm working with XML schemas for the first time, so please be gentle!
I am working on a graphics program (in Java if it matters), and I have
decided to use XML files to store the "objects" that make up a drawing.
A subset of the class hierarchy might look like:
+-------------------------+
| |
| skitch.objects.Object |
| |
+------------+------------+
|
+---------------+---------------+
| |
+------------+------------+ +------------+------------+
| | | |
| skitch.objects.Line | | skitch.objects.Square |
| | | |
+-------------------------+ +-------------------------+
I could create a schema that understands lines, squares, and all the
other shapes, but I want the program to be easily extensible and to use
a more dynamic "plug-in" mechanism.
My current solution uses two schemas for each object, a "generic"
schema (object.xsd):
<?xml version="1.0" encoding="US-ASCII"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:sk="xmlns://Skitch"
targetNamespace="xmlns://Skitch"
elementFormDefault="qualified">
<xs:include schemaLocation="basicTypes.xsd"/>
<xs:element name="object">
<xs:complexType>
<xs:sequence>
<xs:element name="objectData"/>
</xs:sequence>
<xs:attribute name="oid" type="skidType" use="required"/>
<xs:attribute name="class" type="xs:string" use="required"/>
<xs:attribute name="schema" type="xs:string" use="required"/>
</xs:complexType>
</xs:element>
</xs:schema>
Once a file has been validated with this schema, the program can
discover the appropriate Java class to instantiate for this object (the
"class" attribute) and the class-specific schema (the "schema"
attribute).
Here's an example of a "line" object:
<?xml version="1.0" encoding="US-ASCII"?>
<skbject xmlns:sk="xmlns://Skitch" oid="00000000"
class="skitch.objects.Line" schema="lineObject.xsd">
<skbjectData>
<sk:start x="0.0" y="0.0"/>
<sk:end x="100" y="100"/>
<sk:color red="0" green="0" blue="0" alpha="255"/>
<sk:stroke width="1.0" cap="CAP_BUTT" join="JOIN_MITER"
miterLimit="10.0" dashPhase="0.0">
<sk:dash>
<sk:segment length="1.0"/>
<sk:segment length="1.0"/>
</sk:dash>
</sk:stroke>
</skbjectData>
</skbject>
And here's its class-specific schema:
<?xml version="1.0" encoding="US-ASCII"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:sk="xmlns://Skitch"
targetNamespace="xmlns://Skitch"
elementFormDefault="qualified">
<xs:include schemaLocation="basicTypes.xsd"/>
<xs:element name="object">
<xs:complexType>
<xs:sequence>
<xs:element name="objectData" type="sk:lineObjectType"/>
</xs:sequence>
<xs:attribute name="oid" type="skidType" use="required"/>
<xs:attribute name="class" type="xs:string" use="required"/>
<xs:attribute name="schema" type="xs:string" use="required"/>
</xs:complexType>
</xs:element>
</xs:schema>
(basicTypes.xsd can be found at
http://home.comcast.net/~i.pilcher/xml/schemas/basicTypes.xsd.)
Things appears to be working so far, but validating every object twice
just seem inelegant.
Is there a better way?
Thanks!