[prev in list] [next in list] [prev in thread] [next in thread] 

List:       xmlbeans-dev
Subject:    RE: Ideas from the ASF conference
From:       "Radu Preotiuc-Pietro" <radup () bea ! com>
Date:       2004-11-22 22:40:24
Message-ID: 4B2B4C417991364996F035E1EE39E2E124FC06 () uskiex01 ! amer ! bea ! com
[Download RAW message or body]

That would definitely make a cool sample! The only thing is that it requires jakarta, \
right? So we have to make it clear what's required on the classpath for it to run. As \
for your compilation problem, seems to me you are the victim of common error, which \
is you are trying to parse the name of the file as XmlSchema :-) You have to make a \
new File(schema) somewhere.

Radu

-----Original Message-----
From: Philip Mark Donaghy [mailto:philipmarkdonaghy@yahoo.com]
Sent: Monday, November 22, 2004 2:06 PM
To: dev@xmlbeans.apache.org
Subject: Ideas from the ASF conference


Hi everyone,

These are some things that I have learned from using
XmlBeans and talking to people at the ASF conference.

Combining XmlBeans and Velocity templates can result
in interesting xml schema related technologies.

Using XmlBeans and Velocity one can harness the xml
schema and generate documentation, a relational
database model, or even data access objects.

The idea that I am presenting here is an integration
between XmlBeans and Velocity which results in a
Jakarta commons-sql data model.

This is a description of the idea I came up with. It
is based on recent experience using xml schema,
XmlBeans, Velocity, and the participation at the ASF
conference.

Many projects need a database model that facilitates
the import and export of xml data from a particular
schema. One way to achieve this is to derive a
database model from the xml schema.

Information can be leveraged from the xml schema to
create a database schema capable of storing the xml
data.

So I created a Velocity template that will produce a
data model as defined in Jakarta commons-sql.

I think that this idea could make a good sample
application for XmlBeans. But for those who can't wait
to look under the hood. I have included the source
code which is one Java file, one Velocity template,
and a build file.

/*
 * Created on Nov 15, 2004
 */
package org.apache.xmlbeans.samples;

import java.io.File;
import java.io.FileWriter;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.Task;
import org.apache.velocity.Template;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.Velocity;
import org.apache.xmlbeans.SchemaTypeSystem;
import org.apache.xmlbeans.XmlBeans;
import org.apache.xmlbeans.XmlObject;

/**
 * @author phil
 */
public class VelocityXmlBeans extends Task {

    private static final Log log =
LogFactory.getLog(VelocityXmlBeans.class);

    private String template;

    private String output;

    private String schema;

    /**
     * @param output
     *            The output to set.
     */
    public void setOutput(String output) {
        this.output = output;
    }

    /**
     * @param schema
     *            The schema to set.
     */
    public void setSchema(String schema) {
        this.schema = schema;
    }

    /**
     * @param template
     *            The template to set.
     */
    public void setTemplate(String template) {
        this.template = template;
    }

    /**
     * Default Constructor
     */
    public VelocityXmlBeans() {
    }

    /**
     * Puts the XmlBeans SchemaTypeSystem into the
Velocity Context
     */
    public void execute() throws BuildException {
        VelocityContext ctx = new VelocityContext();
        Template template = null;
        FileWriter writer = null;
        SchemaTypeSystem schemaTypeSystem = null;
        try {
            Velocity.init();
            log.info("Using the Velocity template, " +
this.template);
            template =
Velocity.getTemplate(this.template);
            log.info("Using the output file, " +
this.output);
            writer = new FileWriter(new
File(this.output));
            log.info("Using the xml schema, " +
this.schema);
            schemaTypeSystem = XmlBeans.compileXsd(
                    new XmlObject[] {
XmlObject.Factory.parse(new File(
                            this.schema)) },
XmlBeans.getBuiltinTypeSystem(),
                    null);
            ctx.put("xsd", schemaTypeSystem);
            template.merge(ctx, writer);
            writer.close();
        } catch (Exception e) {
            throw new BuildException(e);
        }
    }

    /**
     * @param args
     */
    public static void main(String[] args) {
        VelocityXmlBeans beans = new
VelocityXmlBeans();
        beans.setSchema("easypo.xsd");
        beans.setTemplate("datamodel.vm");
        beans.setOutput("datamodel.xml");
        try {
            beans.execute();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

## Database template used by the sample XmlBeans
Velocity integration.
## The class VelocityXmlBeans produces an Jakarta
commons-sql datamodel.xml 
## file.
## This file can be used to create database ddl for
that 
## datamodel.
## This template expoits the SchemaTypeSystem XmlBeans
object
## present in the Velocity context.
<?xml version="1.0" encoding="ISO-8859-1"?>

<database name="sample">
#foreach($globalElement in $xsd.globalElements())##
SchemaGlobalElement[]

    <table name="$globalElement.name.localPart">

        <column name="pk" type="INTEGER"
required="true" primaryKey="true"/>

    #foreach($property in
$globalElement.type.properties)## SchemaProperty[]
       
#if($property.name.localPart.equals("line-item"))
       
#elseif($property.name.localPart.equals("shipper"))

        <column name="fk-$property.name.localPart"
type="INTEGER" />
       
#elseif($property.name.localPart.equals("customer"))

        <column name="fk-$property.name.localPart"
type="INTEGER" />
        #else

        <column name="$property.name.localPart"
type="INTEGER"/>
        #end
    #end

    </table>
#end
#foreach($globalType in $xsd.globalTypes())##
SchemaType[]

    <table name="$globalType.name.localPart">

        <column name="pk" type="INTEGER"
required="true" primaryKey="true"/>

    #foreach($property in $globalType.properties)##
SchemaProperty[]
        ## The name of this column is defined by the
local part of this 
        ## SchemaProperty.

        <column name="$property.name.localPart"
type="INTEGER" />
    #end
   
#if($globalType.name.localPart.equals("line-item"))

        <column name="fk-purchase-order"
type="INTEGER" />

        <foreign-key foreignTable="purchase-order">
            <reference local="fk-purchase-order"
foreign="pk"/>
        </foreign-key>
    #end

    </table>
#end

</database>

<?xml version="1.0"?>

<project name="vbx" default="run">

    <description>
            Integrating Velocity in XmlBeans
    </description>

    <!-- VelocityXmlBeans Task -->
    <taskdef name="vxb"
classname="org.apache.xmlbeans.samples.VelocityXmlBeans"
description="The task used to run the sample code.">
        <classpath>
            <pathelement path="build" />
            <pathelement location="lib/ant.jar" />
            <pathelement
location="lib/commons-collections-2.1.jar" />
            <pathelement
location="lib/commons-logging-1.0.3.jar" />
            <pathelement
location="lib/log4j-1.2.8.jar" />
            <pathelement
location="lib/velocity-1.4.jar" />
            <pathelement location="lib/xbean.jar" />
        </classpath>
    </taskdef>

    <!-- Jakarta Commons SQL Task-->
    <taskdef name="ddl"
classname="org.apache.commons.sql.task.DDLTask"
description="The task used to create a ddl from the
datamodel xml.">
        <classpath>
            <pathelement
location="lib/commons-sql-1.0-dev.jar" />
            <pathelement
location="lib/commons-betwixt-0.5.jar" />
            <pathelement
location="lib/commons-digester-1.5.jar" />
            <pathelement
location="lib/commons-logging-1.0.3.jar" />
            <pathelement
location="lib/commons-beanutils.jar" />
            <pathelement
location="lib/commons-collections-2.1.jar" />
            <pathelement location="lib/dom4j-1.4.jar"
/>
        </classpath>
    </taskdef>

    <target name="compile" description="Compile the
sample code">
        <javac srcdir="src/java" destdir="build" />
    </target>

    <target name="run" depends="compile"
description="Run the sample code">
        <vxb schema="easypo.xsd"
template="datamodel.vm" output="datamodel.xml" />
    </target>

    <target name="create-db-sql" description="Create
an sql script (DDL) from the xml datamodel">
        <ddl xmlFile="datamodel.xml"
targetDatabase="oracle" output="create.sql" />
    </target>

</project>

Remaining issues may be obvious to some people. The
jdbc type must be derived from the xml schema (piece
of cake). And foreign keys need to be handled either
in the xml schema itself, using a configuration file,
or as it is now (logical if else in the Velocity
template, yuk). And for some odd reason the ant target
used to run the sample yields an exception, yet the
main method works.

Exception :

Buildfile: /home/phil/soft/work/velex/build.xml
compile:
run:
      [vxb] 2004-11-23 00:04:51,447 INFO
org.apache.xmlbeans.samples.VelocityXmlBeans 73
      [vxb] Using the Velocity template, datamodel.vm
      [vxb] 2004-11-23 00:04:51,488 INFO
org.apache.xmlbeans.samples.VelocityXmlBeans 75
      [vxb] Using the output file, datamodel.xml
      [vxb] 2004-11-23 00:04:51,490 INFO
org.apache.xmlbeans.samples.VelocityXmlBeans 77
      [vxb] Using the xml schema, easypo.xsd
BUILD FAILED: /home/phil/soft/work/velex/build.xml:40:
org.apache.xmlbeans.XmlException: Thread main: The 0th
supplied input is not a schema or a config document:
its type is N=
Total time: 3 seconds

Rgds,

Philip Mark Donaghy


		
__________________________________ 
Do you Yahoo!? 
The all-new My Yahoo! - Get yours free! 
http://my.yahoo.com 
 


---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@xmlbeans.apache.org
For additional commands, e-mail: dev-help@xmlbeans.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@xmlbeans.apache.org
For additional commands, e-mail: dev-help@xmlbeans.apache.org


[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic