You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@velocity.apache.org by "Clifton C. Craig" <cc...@icsaward.com> on 2004/10/11 20:30:48 UTC
Learning Velocity/Anakia
Hello all,
I'm experimenting with Velocity/Anakia to create a SQL DDL script
generator. The purpose of the generator is to read an XML document and
xlat it into SQL DDL which will then be fed in through JDBC to the
database to create a database. I've started this project but I am having
difficulty adjusting to the syntax and use of Velocity. I will include
what I have accomplished so far to illustrate what I'm talking about.
Basically I'm struggling with white-space and formatting issues as well
as a way to externalize details of my XML schema. What I'm doing
currently is hard-coding the element and attribute names from my XML
document in my .vsl. I want to pass them in as properties or references
somehow. I tried setting up a velocity.properties file but once I did
that Velocity was no longer able to find my .vsl template. I tried
setting the path property in the velocity.properties file and that
became an issue because the templates are stored in a Windows directory
with spaces in the names. (eg. "Documents And Settings") I tried several
renditions of this but what it boiled down to was I had to use the
primitive DOS 8.3 naming convention in my path or velocity would lose
it. I believe this is due to the Commons collections external properties
stuff but I haven't tested it to confirm my suspicions. Anyhow, that's
my 1st major concern. My 2nd problem is formatting of my Velocity code
and/or it's output. I can get away with both being a little sloppy
because it's just me using it now and the SQL syntax is very generous
with whitespace. However both need serious improvement. It seems that
anything I do to help readability suffers in sloppy output and anything
I do to format the output suffers in readability. I am brand new to this
project so I know the following example will look silly to many but
understand that I am learning.
This is my .vsl template:
## This is the SQL DDL template
#*
M A C R O D E F S
*#
#macro( createTables )
#set ($allTables = $root.selectNodes("database/table"))
#foreach ( $table in $allTables )
#if( $table.getAttributeValue("hsqltype") )
#set ($type = "${table.getAttributeValue('hsqltype')} ")
#else
#set ($type = "")
#end
CREATE $!{type}TABLE $table.getAttributeValue("name")
#defineColumns($table);
#end
#end
#macro( defineColumns $table )
#set ($cols = $table.selectNodes("col"))
#set ( $more='' )
#foreach ($col in $cols)
#if( $col.getAttributeValue("size") )
#set ( $attr = "(${col.getAttributeValue('size')}" )
#if( $col.getAttributeValue("scale") )
#set ( $attr = "$!{attr},${col.getAttributeValue('scale')}" )
#end
#set ( $attr = "$!{attr})" )
#elseif( $col.getAttributeValue("precision") )
#set ( $attr = "(${col.getAttributeValue('precision')}" )
#if( $col.getAttributeValue("scale") )
#set ( $attr = "$!{attr},${col.getAttributeValue('scale')}" )
#end
#set ( $attr = "$!{attr})" )
#else
#set ( $attr = "" )
#end
$!{more}$col.getAttributeValue("name")
$col.getAttributeValue("type") $!{attr}
#if( $col.selectNodes("default") &&
$col.selectNodes("default").size() > 0 )
DEFAULT '$col.selectNodes("default").get(0).getText()'
#end
#if( $col.getAttribute("allownull") )
#if( $col.getAttributeValue("allownull")=='yes' ||
$col.getAttributeValue("allownull")=='true' ) NULL #else NOT NULL #end
#end
#set ( $more=',' )
#end
#end
#createTables()
This is some sample XML that I feed through it:
<data-model>
<database>
<table name="testtable">
<pk name="id"/>
<col name="id" type="int"/>
<col name="compositekey1" type="int"/>
<col name="compositekey2" type="dec" size="9" scale="3"/>
<col name="compositekey3" type="varchar" size="10"/>
<col name="uniquekey" type="varchar" size="30"/>
<col name="nonnullval" type="varchar" size="30" allownull="no"/>
<col name="nonnullval2" type="varchar" size="30" allownull="no">
<default>DEFAULT VALUE</default>
</col>
<col name="nullableval" type="varchar" allownull="yes"/>
<unique-key cols="compositekey1,compositekey2,compositekey3"/>
<unique-key cols="uniquekey"/>
</table>
</database>
</data-model>
And this is the output:
CREATE TABLE testtable
id int
,compositekey1 int
,compositekey2 dec (9,3)
,compositekey3 varchar (10)
,uniquekey varchar (30)
,nonnullval varchar (30)
NOT NULL ,nonnullval2 varchar (30)
NOT NULL DEFAULT DEFAULT VALUE
,nullableval varchar
NULL ;
Help!
--
Clifton C. Craig, Software Engineer
Intelligent Computer Systems - A Division of GBG
2101 Embassy Drive
Lancaster, PA 17603
Phone: 717-295-7977 ext. 621
Fax: 717-295-7683
ccc@icsaward.com
ccraig@gbg.com
---------------------------------------------------------------------
To unsubscribe, e-mail: velocity-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: velocity-user-help@jakarta.apache.org
Re: Learning Velocity/Anakia
Posted by Shinobu Kawai <sh...@gmail.com>.
Hi Clifton,
> I'm using Ant to manage the setting of the path. In other words I define
> a path in my build and I use the echoproperties task to generate
> velocity.properties under a temp directory. I then reference the
> generated velocity.properties in the Anakia task.
I see. I never thought of doing it that way.
If the template path is the only thing you are configuring, then the
"templatePath" attribute in the "anakia" task looks nice.
http://jakarta.apache.org/velocity/anakia.html?How%20does%20it%20work?
Best regards,
-- Shinobu Kawai
--
Shinobu Kawai <sh...@gmail.com>
---------------------------------------------------------------------
To unsubscribe, e-mail: velocity-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: velocity-user-help@jakarta.apache.org
Re: Learning Velocity/Anakia
Posted by "Clifton C. Craig" <cc...@icsaward.com>.
Shinobu,
I'm using Ant to manage the setting of the path. In other words I define
a path in my build and I use the echoproperties task to generate
velocity.properties under a temp directory. I then reference the
generated velocity.properties in the Anakia task. By the way I just
found out that I cannot use echoproperties this way even after using
pathconvert to replace backslashes with forward slashes because it
escapes the colon in the drive letter as well. This really blows because
where a normal properties object would convert the escape sequences
ExtendedProperties treats them verbatim.
Clifton C. Craig, Software Engineer
Intelligent Computer Systems - A Division of GBG
2101 Embassy Drive
Lancaster, PA 17603
Phone: 717-295-7977 ext. 621
Fax: 717-295-7683
ccc@icsaward.com
ccraig@gbg.com
Shinobu Kawai wrote:
>Hi Clifton,
>
>
>
>>To use foward slashes I'd have to change my path property in my Ant
>>build from a location property to a value property. While this would
>>work well it would feel like a dirty hack. What if I needed that path
>>later on and needed it formatted accordingly?
>>
>>
>Wait a minute, I thought we were talking about inside
>velocity.properties. How does that affect the Ant build file? Am I
>missing something?
>
>Best regards,
>-- Shinobu Kawai
>
>
>
---------------------------------------------------------------------
To unsubscribe, e-mail: velocity-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: velocity-user-help@jakarta.apache.org
Re: Learning Velocity/Anakia
Posted by Shinobu Kawai <sh...@gmail.com>.
Hi Clifton,
> To use foward slashes I'd have to change my path property in my Ant
> build from a location property to a value property. While this would
> work well it would feel like a dirty hack. What if I needed that path
> later on and needed it formatted accordingly?
Wait a minute, I thought we were talking about inside
velocity.properties. How does that affect the Ant build file? Am I
missing something?
Best regards,
-- Shinobu Kawai
--
Shinobu Kawai <sh...@gmail.com>
---------------------------------------------------------------------
To unsubscribe, e-mail: velocity-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: velocity-user-help@jakarta.apache.org
Re: Learning Velocity/Anakia
Posted by "Clifton C. Craig" <cc...@icsaward.com>.
Shinobu,
To use foward slashes I'd have to change my path property in my Ant
build from a location property to a value property. While this would
work well it would feel like a dirty hack. What if I needed that path
later on and needed it formatted accordingly? I do understand that the
likelyhood is slim and that I probably have no other choice in the
matter. I appreciate the idea of the SQL formatter. Thanx again.
Clifton C. Craig, Software Engineer
Intelligent Computer Systems - A Division of GBG
2101 Embassy Drive
Lancaster, PA 17603
Phone: 717-295-7977 ext. 621
Fax: 717-295-7683
ccc@icsaward.com
ccraig@gbg.com
Shinobu Kawai wrote:
>Hi Clifton,
>
>
>
>> ExtendedProperties cannot evaluate a real Java
>>properties file. It tries to offer convenience by not forcing you to
>>escape backslashes.
>>
>>
>Of course, you can just use forward slashes instead. That will work
>in both Extended and standard Properties. :)
>
>
>
>> I was
>>just wondering (hoping) that I could have my cake and eat it too because
>>when I go back to debug problems it helps to have formatted output that
>>I can use to trace problems.
>>
>>
>Maybe you can use one of those SQL formatters.
> http://www.google.com/search?q=sql+formatter
>
>Best regards,
>-- Shinobu Kawai
>
>
>
---------------------------------------------------------------------
To unsubscribe, e-mail: velocity-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: velocity-user-help@jakarta.apache.org
Re: Learning Velocity/Anakia
Posted by Shinobu Kawai <sh...@gmail.com>.
Hi Clifton,
> ExtendedProperties cannot evaluate a real Java
> properties file. It tries to offer convenience by not forcing you to
> escape backslashes.
Of course, you can just use forward slashes instead. That will work
in both Extended and standard Properties. :)
> I was
> just wondering (hoping) that I could have my cake and eat it too because
> when I go back to debug problems it helps to have formatted output that
> I can use to trace problems.
Maybe you can use one of those SQL formatters.
http://www.google.com/search?q=sql+formatter
Best regards,
-- Shinobu Kawai
--
Shinobu Kawai <sh...@gmail.com>
---------------------------------------------------------------------
To unsubscribe, e-mail: velocity-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: velocity-user-help@jakarta.apache.org
Re: Learning Velocity/Anakia
Posted by "Clifton C. Craig" <cc...@icsaward.com>.
Thank you Shinobu,
I initially considered using properties.xml but considered it overkill
for what I was attempting. I thought that I could pass in properties
through the velocity.properties file somehow. I now believe that
properties.xml may be the better approach here. My velocity.properties
is as follows (generated using echoproperties from my Ant build):
#Ant properties
#Tue Oct 12 08:19:14 EDT 2004
version=1.4
file.resource.loader.path=C\:\\Documents and Settings\\Clifton
Craig\\IdeaProjects\\ice\\DataAccess\\tools\\anakia\\stylesheets
home=D\:\\software\\velocity-1.4
The version and home properties are superfluous. (I couldn't quickly
find a clean way to filter them from my propertyset.) The important
property, file.resource.loader.path is definitely set to the correct
path as I use this same value in the Anakia task directly sans
velocity.properties and the templates are easily found. Also if I
convert the value to a DOS 8.3 path as below it works.
#Ant properties
#Tue Oct 12 08:19:14 EDT 2004
version=1.4
file.resource.loader.path=C\:\\DOCUME~1\\CLIFTO~1\\IDEAPR~1\\ice\\DATAAC~1\\tools\\anakia\\STYLES~1
home=D\:\\software\\velocity-1.4
I am convinced that it is a result of the commons ExtendedProperties
class that causes this. I don't believe that this class tokenized the
CSV list correctly. I believe it tokenized on white-space as well as
commas causing certain directory names to be broken into separate paths.
CANCEL WHAT I JUST WROTE ABOVE!! I just tried it and it doesn't work
either way. The real problem is related to ExtendedProperties but not as
I thought it would be. ExtendedProperties cannot evaluate a real Java
properties file. It tries to offer convenience by not forcing you to
escape backslashes. So my prior examples were being interpreted as is
with double backslashes and all. I initially found success by
accidentally coding the DOS 8.3 format without escaping the backslashes.
(Idea local VCS revealed this to me.) That's why I thought the class
wasn't tokenizing correctly. I just tried the following path and it worked.
file.resource.loader.path=C:\Documents and Settings\Clifton
Craig\IdeaProjects\ice\DataAccess\tools\anakia\stylesheets
The funny thing is I've been struggling with escaped backslashed in
properties files all year and I just figured out that the echoproperties
task handles this transparently with this project. (I used to copy an
existing properties file with ant properties which were expanded in
place.) Now my first use of echoproperties is with a task that expects
the paths in the properties file to be unescaped. Damned convenience
coding is making things inconvenient. I understand what you're saying
about the generated SQL and I'm going forward trying not to care. I was
just wondering (hoping) that I could have my cake and eat it too because
when I go back to debug problems it helps to have formatted output that
I can use to trace problems. Besides, it just doesn't feel quite right
doing it this way. I thank you for all of your suggestions.
Best Regards,
Clifton C. Craig, Software Engineer
Intelligent Computer Systems - A Division of GBG
2101 Embassy Drive
Lancaster, PA 17603
Phone: 717-295-7977 ext. 621
Fax: 717-295-7683
ccc@icsaward.com
ccraig@gbg.com
Shinobu Kawai wrote:
>Hi Craig,
>
>My first time with anakia, but let's see what I can figure out.
>
>
>
>>Basically I'm struggling with white-space and formatting issues as well
>>as a way to externalize details of my XML schema. What I'm doing
>>currently is hard-coding the element and attribute names from my XML
>>document in my .vsl. I want to pass them in as properties or references
>>somehow.
>>
>>
>Hmm, if I understand anakia correctly, it looks like the only way you
>can do that is via $project (project.xml).
>
>
>
>> I tried setting up a velocity.properties file but once I did
>>that Velocity was no longer able to find my .vsl template. I tried
>>setting the path property in the velocity.properties file and that
>>became an issue because the templates are stored in a Windows directory
>>with spaces in the names. (eg. "Documents And Settings") I tried several
>>renditions of this but what it boiled down to was I had to use the
>>primitive DOS 8.3 naming convention in my path or velocity would lose
>>it. I believe this is due to the Commons collections external properties
>>stuff but I haven't tested it to confirm my suspicions. Anyhow, that's
>>my 1st major concern.
>>
>>
>That's strange, the only special characters are commas. I put the vsl
>file in such a directory, but it worked. What does your properties
>file look like?
>
>
>
>> My 2nd problem is formatting of my Velocity code
>>and/or it's output. I can get away with both being a little sloppy
>>because it's just me using it now and the SQL syntax is very generous
>>with whitespace. However both need serious improvement. It seems that
>>anything I do to help readability suffers in sloppy output and anything
>>I do to format the output suffers in readability. I am brand new to this
>>project so I know the following example will look silly to many but
>>understand that I am learning.
>>
>>
>If I were you, I wouldn't care about how sloppy the SQL output is, as
>long as the DBMS can understand it. When you write java code, you
>don't write it so the class file is neat, do you? Make the vsl as
>readable as possible.
>
>Best regards,
>-- Shinobu Kawai
>
>
>
---------------------------------------------------------------------
To unsubscribe, e-mail: velocity-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: velocity-user-help@jakarta.apache.org
Re: Learning Velocity/Anakia
Posted by Shinobu Kawai <sh...@gmail.com>.
Hi Craig,
My first time with anakia, but let's see what I can figure out.
> Basically I'm struggling with white-space and formatting issues as well
> as a way to externalize details of my XML schema. What I'm doing
> currently is hard-coding the element and attribute names from my XML
> document in my .vsl. I want to pass them in as properties or references
> somehow.
Hmm, if I understand anakia correctly, it looks like the only way you
can do that is via $project (project.xml).
> I tried setting up a velocity.properties file but once I did
> that Velocity was no longer able to find my .vsl template. I tried
> setting the path property in the velocity.properties file and that
> became an issue because the templates are stored in a Windows directory
> with spaces in the names. (eg. "Documents And Settings") I tried several
> renditions of this but what it boiled down to was I had to use the
> primitive DOS 8.3 naming convention in my path or velocity would lose
> it. I believe this is due to the Commons collections external properties
> stuff but I haven't tested it to confirm my suspicions. Anyhow, that's
> my 1st major concern.
That's strange, the only special characters are commas. I put the vsl
file in such a directory, but it worked. What does your properties
file look like?
> My 2nd problem is formatting of my Velocity code
> and/or it's output. I can get away with both being a little sloppy
> because it's just me using it now and the SQL syntax is very generous
> with whitespace. However both need serious improvement. It seems that
> anything I do to help readability suffers in sloppy output and anything
> I do to format the output suffers in readability. I am brand new to this
> project so I know the following example will look silly to many but
> understand that I am learning.
If I were you, I wouldn't care about how sloppy the SQL output is, as
long as the DBMS can understand it. When you write java code, you
don't write it so the class file is neat, do you? Make the vsl as
readable as possible.
Best regards,
-- Shinobu Kawai
--
Shinobu Kawai <sh...@gmail.com>
---------------------------------------------------------------------
To unsubscribe, e-mail: velocity-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: velocity-user-help@jakarta.apache.org