Thursday, October 30, 2008

Division operator in Groovy

Groovy similar to Ruby fairly,in groovy language all of thing is object,that means you have to operate an object by invocation its method.For example 123.abs() will return the absolute value of this integer.Groovy support Integer and Floating point numeric.Indeed,Groovy implement every operator as a method,for example "+" implement as plus:123.plus(456) means 123+456the return value is 578,it's a integer,then on the other hand,for division operator,when you type and run 5/2 the result is 2.5,it's floating point numeric;now how about 6/2,what the result,I think the result is 3,an integer numeric,but I'm wrong,indeed all of the result of division operator is a floating point,even if the result look like an integer.so the result of 6/2 is 3.0,it's a floating point numeric.
And if you want to obtain an integer you should invoke the method intdiv:6.intdiv(2) then the result is an integer:3

Friday, September 19, 2008

An ant example for how to Validate XML

<?xml version="1.0" encoding="UTF-8"?>

<tns:root xmlns:tns="http://www.example.org/Simple" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xsi:schemaLocation="http://www.example.org/Simple Simple.xsd ">

This is an user test

<tns:user role="admin">

<tns:name>tnsname1</tns:name>

<tns:age>18</tns:age>

<tns:gender>Male</tns:gender>

<tns:password>sfds3555</tns:password>

<tns:title/>

</tns:user>

<tns:user role="admin">

<tns:name>tnsname1</tns:name>

<tns:age>18</tns:age>

<tns:gender>Male</tns:gender>

<tns:password>sfds3555</tns:password>

<tns:title/>

</tns:user>

<tns:cost>0.22</tns:cost>

<tns:cost>0</tns:cost>

</tns:root>

A XSD Schema file for example.

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"

targetNamespace="http://www.example.org/Simple" xmlns:tns="http://www.example.org/Simple"

elementFormDefault="qualified">

<xs:complexType name="user" mixed="true">



<xs:sequence>

<xs:element name="name" type="xs:string" default="Eric001" />

<xs:element name="age" type="tns:age" default="30" />

<xs:element name="gender" type="tns:gender" default="Male" />

<xs:element name="password" type="tns:password" />

<xs:any maxOccurs="unbounded"></xs:any>

</xs:sequence>

<xs:attribute name="role" type="xs:string"/>



</xs:complexType>

<xs:simpleType name="gender">

<xs:restriction base="xs:string">

<xs:enumeration value="Male" />

<xs:enumeration value="Female" />

</xs:restriction>

</xs:simpleType>

<xs:simpleType name="age">

<xs:restriction base="xs:integer">

<xs:minInclusive value="1" />

<xs:maxInclusive value="120" />

</xs:restriction>

</xs:simpleType>

<xs:simpleType name="letter">

<xs:restriction base="xs:string">

<xs:pattern value="[A-Za-z0-9]"></xs:pattern>

</xs:restriction>

</xs:simpleType>

<xs:simpleType name="password">

<xs:restriction base="xs:string">

<xs:pattern value="(([0-9]+[a-zA-Z]+)|([a-zA-Z]+[0-9]+))"></xs:pattern>

<xs:minLength value="5" />

<xs:maxLength value="8" />

</xs:restriction>

</xs:simpleType>

<xs:simpleType name="amount">

<xs:restriction base="xs:decimal">

<xs:totalDigits value="2"></xs:totalDigits>

</xs:restriction>

</xs:simpleType>

<xs:element name="root">

<xs:complexType mixed="true">

<xs:sequence>

<xs:any maxOccurs="unbounded"></xs:any>

</xs:sequence>

</xs:complexType>

</xs:element>

<xs:element name="user" type="tns:user" >



</xs:element>

<xs:element name="cost" type="tns:amount" />

<xs:element name="title"></xs:element>

</xs:schema>


Following is the XML file:
<?xml version="1.0" encoding="UTF-8"?>

<tns:root xmlns:tns="http://www.example.org/Simple" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xsi:schemaLocation="http://www.example.org/Simple Simple.xsd ">

This is an user test

<tns:user role="admin">

<tns:name>tnsname1</tns:name>

<tns:age>18</tns:age>

<tns:gender>Male</tns:gender>

<tns:password>sfds3555</tns:password>

<tns:title/>

</tns:user>

<tns:user role="admin">

<tns:name>tnsname1</tns:name>

<tns:age>18</tns:age>

<tns:gender>Male</tns:gender>

<tns:password>sfds3555</tns:password>

<tns:title/>

</tns:user>

<tns:cost>0.22</tns:cost>

<tns:cost>0</tns:cost>

</tns:root>

Wednesday, September 17, 2008

Add bookmark on docbook PDF

In this article I'll discuss two points about docbook that generate to PDF.I use the ant to generate docbook,following is my ant file.

<!--

- target: build-pdf

- description: Iterates through a directory and transforms

- .xml files into .fo files which can then be turned into DocBook XML

- files.

-->

<target name="build-pdf" depends="depends"

description="Generates PDF files from DocBook XML">

<xslt style="${fo.stylesheet}" extension=".fo"

basedir="${basedir}/src" destdir="${target.dir}/fo">

<classpath refid="xalan.classpath" />

<include name="**/*.xml" />

<exclude name="**/*chapter*.xml"/>

</xslt>





<property name="fop.home" value="${basedir}/lib/fop-0.95"/>

<taskdef name="fop" classname="org.apache.fop.tools.anttasks.Fop">

<classpath>

<fileset dir="${fop.home}/lib">

<include name="*.jar"/>

</fileset>

<fileset dir="${fop.home}/build">

<include name="fop.jar" />

</fileset>

</classpath>

</taskdef>



<fop format="application/pdf"

outdir="${target.dir}/pdf" basedir="${target.dir}/fo">

<fileset dir="${target.dir}/fo">

<include name="*.fo"/>

</fileset>

</fop>







</target>

following is my dobook segment:
<sect1>

<title>Section 1 title</title>

<para>

<sect2>

<title>section 2 title</title>

<para>

description...

</para>

<sect3>

<para>witing something...</para>

<sect3>

</sect2>

</sect1>

Now the question is;
1)I want to display the label of section.
2)I want to display the bookmark in PDF.

The final solution is change the param.xml file.
1)to display the label of section:
change following line
<xsl:param name="section.autolabel" select="0">

to this line

<xsl:param name="section.autolabel" select="1">



2)to display the bookmark in PDF.

change following line

<xsl:param name="fop1.extensions" select="0">

to this line

<xsl:param name="fop1.extensions" select="1">


Note:don't change following line otherwise you will get an exception like:
No element mapping definition found for (Namespace URI: "http://xml.apache.org/fop/extensions", Local Name: "destination")

<xsl:param name="fop.extensions" select="0">

Friday, September 12, 2008

How to change the locale in Grails.

It's simple to change the locale in Grail,just need put a param after you url.A better method is put a link in your template file,and include it in every your page,then write following code:

<span>

<g:link controller="${params.controller}" action="${params.action}" params="[lang:'en']" class="menuButton">English</g:link>

</span>

<span>

<g:link controller="${params.controller}" action="${params.action}" params="[lang:'de']" class="menuButton">German</g:link>

</span>



then you will have a change language in every page.or another way you can put this code in your layout.gsp file directly.

Error starting Sun's native2ascii: In Grails

If you run app in Grails and get this error message and app cannot run.This happened sometimes because you want to use i18n feature in your application.
Then try this method:
Copying %Java_Home%/lib/tools.jar to %Java_Home%/jre/lib/ext/tools.jar fixed this problem.
(a better approach is to set up JRE path to the directory JDK)

How to implement i18n message in controller

    In a Grails applicaton,actually it use spring message bundle for its i18n.let me give an example:
I have a Usercontroller and I want to display a message to our user when his user name or password is incorrectly and I want to show different language depend on the user's location.here is the code:

           if(user){
     session.userId=user.userId
     def redirectParams=session.originalRequestParams?session.originalRequestParams:[controler:'race']
     redirect(redirectParams)
     }else{    
                            //for now,it's a hard-code pattern.
     flash['message']='Please enter a valid user id and password'
     }

following we should do is add something in my message.propertis file.
in the file:grails-app/i18n/messages.properties we added English message
user.invilate.message=Please enter a valid user id and password
in the file:grails-app/i18n/messages_de.properties we added German message
user.invilate.message=Bitte geben Sie eine gültige Benutzer-ID und Passwort

then we change above code
flash['message']='${message(code:'user.invilate.message')}'