Best way to model object inheritance?

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="sk:eek:idType" 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"?>

<sk:eek:bject xmlns:sk="xmlns://Skitch" oid="00000000"
class="skitch.objects.Line" schema="lineObject.xsd">

<sk:eek:bjectData>
<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>
</sk:eek:bjectData>

</sk:eek:bject>

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="sk:eek:idType" 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!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top