Bind Pages; Xml Pages; [ Cjos Audience]
Summary
The gchii.library.xmlbind2c package illustrates how to create an XML model using the Bind Pattern.
Application
This package is suitable for a wide variety of code-generation applications.
- Generate XVCL source code. The resulting XVCL code is able to generate Java source code. The resulting Java code is able to (fill in the blank).
- Generate Java source code. The resulting Java code is able to fully read a specific design of an SQL database.
- Generate Java source code. The resulting Java code is able to fully read a specific doctype of an XML document.
Credits
Some terminology is borrowed from the work of Christian Heller, Cybernetics Oriented Programming - An Investigation in the Applicability of Inter-Discipline Concepts to Software System Development. See also Cybol Pages.
The XML model is then passed to an XVCL Processor. This enables an x-framework to generate corresponding source code. See also Xvcl Pages.
Template
----- template.xml -----
<?xml version="1.0"?>
<model>
<part>
<property>
<constraint/>
</property>
</part>
</model>
Result:
<set-multi var="model_0.list" value="part_1"/>
<set-multi var="model_0.part_1.list" value="property_1"/>
<set-multi var="model_0.part_1.property_1.list" value="constraint_1"/>
Rules:
- By default, model.id is "model_0". Use the id attribute to override.
- By default, part.id is "part_n" where n starts with 1. Use the id attribute to override.
- By default, property.id is "property_n" where n starts with 1. Use the id attribute to override.
- By default, constraint.id is "constraint_n" where n starts with 1. Use the id attribute to override.
Example
----- example.xml -----
<?xml version="1.0"?>
<model package="org.jos.demo">
<part entity="xvcl-generator" classname="Generator">
<property id="directory" attribute="javadir">
<constraint id="directory"/>
</property>
<property id="javapackage" attribute="package">
<constraint id="package"/>
</property>
</part>
</model>
Result:
<set-multi var="model_0.list" value="generator"/>
<set var="model_0.directory" value="org/jos/demo"/>
<set var="model_0.javapackage" value="org.jos.demo"/>
<set-multi var="model_0.generator.list" value="directory,javapackage"/>
<set var="model_0.generator.name" value="Generator"/>
Rules:
- Typically, a model represents a Java package.
- When possible, model_0.javapackage is converted from a Java package to a directory name and stored in model_0.directory.
- Typically, part.name is used as the Java class name.
- By default, part.xmlname comes from part.id.
- By default, property.name comes from property.id.
- For constraint.id, an x-frame provides a map of build-in constraints to a constraint classname. A custom constraint is also a part.
- When designing multiple types of parts in a single XML document (not yet recommended), an x-frame provides a map of part.type to a base class or interface for each part.
Model
The model is the highest level part. It represents an XML document. It has zero or more parts.
Part
The part is next. It has zero or more properties.
Property
The property is next. It has zero or more constraints.
Constraint
The constraint is the smallest part.
Construction at runtime
A Java model is constructed from a document object model (DOM). In turn, a DOM is constructed from an XML document. The pure DOM is used as an intermediate model.
Version 2a
This version provides an Apache Ant task to read an XML document and invoke the XVCLAntTask.
Project Summary
The xmlbind-eclipse project is committed to CjOS Project CVS at sourceforge.net.
Project: xmlbind-eclipse
CVS Module: projects/xmlbind-eclipse
IDE: Eclipse 3.1
Source Code Compliance: 1.4
Generated By : XVCL Processor 2.07 (1g)
SPC Location : projects/xmlbind-eclipse/module/source
Target: taskdef
<target name="taskdef">
<taskdef
name="model"
classname="gchii.library.xmlbind2a.ModelAntTask"
classpathref="cp1"/>
</target>
Target: model
ModelAntTask passes some properties to XVCLAntTask.
<target name="model">
<model
spc="spc-2a.xvcl"
projectPath="."
beautifyOutput="true"
failonerror="true"
verbose="true"
prefix="xmlbind"
xml="xmlbind-2a.xml"/>
</target>
- prefix (optional): Adds a constant prefix to all variables passed to the XVCL Processor.
- xml: Name of XML document. The expected document root is "model".
Future
Should ModelAntTask extend XVCLAntTask? This would ensure that set and setmulti is supported.
Version 2c
When they are similar, two XML documents can be merged.
Part a = ModelBuilder.getPart( "a.xml" );
Part b = ModelBuilder.getPart( "b.xml" );
Part c = a.plus( b );
Example 1
Merging properties (attributes).
c = a + b
----- a.xml -----
<model a-name="a-value"/>
----- b.xml -----
<model b-name="b-value"/>
----- c.xml -----
<model a-name="a-value" b-name="b-value"/>
Example 2
Merging tags.
c = a + b
----- a.xml -----
<model a-name="model a-name">
<a-tag name="a-name"/>
<c-tag name="c-tag a-value" value="a.value"/>
<z-tag name="z-tag"/>
</model>
----- b.xml -----
<model b-name="model b-name">
<b-tag name="b-name"/>
<c-tag name="c-tag b-value" value="b.value"/>
<z-tag name="z-tag"/>
</model>
----- c.xml -----
<model a-name="model a-name" b-name="model b-name">
<a-tag name="a-name"/>
<c-tag name="c-tag a-value" value="a.value"/>
<z-tag name="z-tag"/>
<b-tag name="b-name"/>
</model>
- a-tag is added because it is in a, not b.
- c-tag is added from a because it is in both a and b, but a has precedence.
- z-tag is added because it is in both a and b, but a has precedence. Coincidentally, identical z-tag is in b.
- b-tag is added because it is in b, not a.
Allow multiple tags: When allow-multiple-tags is equal to "c-tag".
Target: taskdef
<target name="taskdef">
<taskdef
name="xmlmerge"
classname="gchii.library.xmlbind2c.XmlMergeAntTask"
classpathref="cp1"/>
</target>
Target: xmlmerge
XmlMergeAntTask merges two XML documents.
<target name="xmlmerge">
<property name="xml1" location="a.xml"/>
<property name="xml2" location="b.xml"/>
<property name="destfile" location="build/c.xml"/>
<xmlmerge
xml1="${xml1}"
xml2="${xml2}"
destfile="${destfile}"
failonerror="true"
verbose="true">
<key part="item" property="name"/>
</xmlmerge>
</target>
- xml1: primary XML document.
- xml2: secondary XML document.
- destfile: target XML document (overwrite)
- failonerror: When true, a failure in the task stops the build process.
- verbose: When true, additional information messages are displayed.
|