register
other register

Tuesday, July 29, 2008

Groovlets

For more details please refer to: Groovlets


1. Put the following lines into web.xml in WEB-INF foler

=====================================

<servlet>
<servlet-name>Groovy</servlet-name>
<servlet-class>groovy.servlet.GroovyServlet</servlet-class>
</servlet>

<servlet-mapping>
<servlet-name>Groovy</servlet-name>
<url-pattern>*.groovy</url-pattern>
</servlet-mapping>

=====================================


2. Write your Groovlets hello.groovy like below, and put it into the webapps folder

=====================================

import java.util.Date
import groovy.xml.MarkupBuilder

if (session == null) {
session = request.getSession(true);
}

if (session.counter == null) {
session.counter = 1
}

html.html { // html is implicitly bound to new MarkupBuilder(out)
head {
title("Groovy Servlet")
}
body {
p("Hello, ${request.remoteHost}: ${session.counter}! ${new Date()}")
}
}

session.counter = session.counter + 1

====================================

3. Copy groovy-all-xyz.jar into WEB-INF/lib folder


Notes:

If your Groovlets is at somewhere else rather than $CATALINA_HOME/webapps, then you need to edit the server.xml in the $CATALINA_HOME/conf like below:

<Context path="/groovy" docBase="c:/groovy-servlet"/>

Monday, July 28, 2008

Set System Properties in Java

You can set system properties by using -D in the java command line:
java -Dfile_properties=file.properties HelloWorld

==========================================

String file_properties = System.getProperty("file_properties");

// load file.properties file which sets the files and directories
Properties properties = new Properties();
try {
//properties.load(new FileInputStream("file.properties"));
properties.load(new FileInputStream(System.getProperty("file_properties")));
} catch (FileNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (IOException e1)
{
// TODO Auto-generated catch block e1.printStackTrace();
}

// get properties File inputDir = new File(properties.getProperty("inputDir"));

==========================================

In Eclipse, you can set system properties in:

1. Right click the java program, choose Run As --> Open Run Dialog
2. Select Arguments tab
3. In "VM argument", write your system properties settings. (i.e. -Dfile_properties=file.properties).

Thursday, July 17, 2008

Dealing with namespace in XSLT

For more general knowledge about namespace, please refer to XML Namespaces and How They Affect XPath and XSLT


If you have an vanilla xml file like below:

<people xmlns="http://xmlns.blabla.com/name" xmlns:abc="http://xmlns-abc.blabla.com/abc"
abc:firstname="jianfeng"
abc:lastname="tian" />

In your xslt, you have to define namespaces in your xsl:stylesheet declaration to make it working:

xmlns:jet="http://xmlns.blabla.com/name"
xmlns:abc="http://xmlns-abc.blabla.com/abc"

<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:jet="http://xmlns.blabla.com/name"
xmlns:abc="http://xmlns-abc.blabla.com/abc"
exclude-result-prefixes="abc xs xsi xsl">

<xsl:output method="xml" encoding="UTF-8" indent="yes"/>

<xsl:template match="jet:people">
<person>

<xsl:attribute name="firstname">
<xsl:value-of select="@abc:firstname"/>
</xsl:attribute>

<xsl:attribute name="lastname">
<xsl:value-of select="@abc:lastname"/>
</xsl:attribute>

</person>

</xsl:template>
</xsl:stylesheet>

================================================================

If you have an xml file like below:

<people xmlns="http://www.somewhere.com/blabla" >
<person>
<address>
<postcode>110001</postcode>
</address>
<content group="firstname">John</person>
<content group="lastname">Smith</person>
</person>
</people>

In your xslt, you have to define namespaces in your xsl:stylesheet declaration to make it working:

xmlns:jet="http://www.somewhere.com/blabla"


<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:jet="http://www.somewhere.com/blabla"
exclude-result-prefixes="jet xsl xsi">
<xsl:output method="xml" indent="yes" omit-xml-declaration="no" encoding="ISO-8859-1"/>

<xsl:template match="jet:people">
<data>
<postcode>
<xsl:value-of select="jet:person/jet:address/jet:postcode">
</postcode>

<firstname><xsl:value-of select="jet:person/jet:content[@group='firstname']" /></login>
<lastname><xsl:value-of select="jet:person/jet:content[@group='lastname']" /></login>

</data>
</xsl:template>

</xsl:stylesheet>

Friday, July 04, 2008

Convert CSV to XML Using XSLT

Refer to: http://blogs.msdn.com/kaevans/archive/2003/04/17/5780.aspx

In your CSV file,

1. Surround the whole data with tag.
2. Add tag to the first row which contains the heading titles
3. Add tag to contain the whole data.

<root>
<head>firstname, lastname</content>
<content>bla, bla
bla, bla</conent>
</root>

<xsl:stylesheet version ="2.0" xmlns:xsl ="http://www.w3.org/1999/XSL/Transform" > <xsl:output method ="xml"/> <!-- template that matches the root node--> <xsl:template match ="/" > <root> <xsl:call-template name ="texttorows" > <xsl:with-param name ="StringToTransform" select ="/root/content" /> <xsl:with-param name="heading" select="/root/heading" /> </xsl:call-template> </root> </xsl:template> <!-- template that actually does the conversion--> <xsl:template name ="texttorows" > <!-- import $StringToTransform--> <xsl:param name ="StringToTransform" select ="''" /> <xsl:param name="heading" select="''" /> <!-- <xsl:variable name="CR" select="'&#013;'"/> --> <xsl:variable name="CR" select="'&#xA;'"/> <xsl:choose> <!-- string contains linefeed--> <xsl:when test ="contains($StringToTransform,$CR)" > <!-- Get everything up to the first carriage return--> <row> <xsl:call-template name ="csvtoxml" > <xsl:with-param name ="StringToTransform" select ="substring-before($StringToTransform,$CR)" /> <xsl:with-param name="heading" select="$heading" /> </xsl:call-template> </row> <!-- repeat for the remainder of the original string--> <xsl:call-template name ="texttorows" > <xsl:with-param name ="StringToTransform" > <xsl:value-of select ="substring-after($StringToTransform,$CR)" /> </xsl:with-param> <xsl:with-param name="heading"> <xsl:value-of select="$heading" /> </xsl:with-param> </xsl:call-template> </xsl:when> <!-- string does not contain newline, so just output it--> <xsl:otherwise> <row> <xsl:call-template name ="csvtoxml" > <xsl:with-param name ="StringToTransform" select ="$StringToTransform" /> <xsl:with-param name="heading" select="$heading" /> </xsl:call-template> </row> </xsl:otherwise> </xsl:choose> </xsl:template> <xsl:template name ="csvtoxml" > <!-- import $StringToTransform--> <xsl:param name ="StringToTransform" select ="''" /> <xsl:param name ="heading" select ="''" /> <xsl:choose> <!-- string contains linefeed--> <xsl:when test ="contains($StringToTransform,',')" > <!-- Get everything up to the first carriage return--> <elem> <xsl:value-of select ="substring-before($heading,',')" /><xsl:text> </xsl:text> <xsl:choose> <xsl:when test="starts-with($StringToTransform, '&quot;')"> <xsl:value-of select ="substring-before($StringToTransform,'&quot;,')" /> </xsl:when> <xsl:otherwise> <xsl:value-of select ="substring-before($StringToTransform,',')" /> </xsl:otherwise> </xsl:choose> </elem> <!-- repeat for the remainder of the original string--> <xsl:call-template name ="csvtoxml" > <xsl:with-param name ="heading" > <xsl:value-of select ="substring-after($heading,',')" /> </xsl:with-param> <xsl:with-param name ="StringToTransform" > <xsl:choose> <xsl:when test="starts-with($StringToTransform, '&quot;')"> <xsl:value-of select ="substring-after($StringToTransform,'&quot;,')" /> </xsl:when> <xsl:otherwise> <xsl:value-of select ="substring-after($StringToTransform,',')" /> </xsl:otherwise> </xsl:choose> </xsl:with-param> </xsl:call-template> </xsl:when> <!-- string does not contain newline, so just output it--> <xsl:otherwise> <elem> <xsl:value-of select ="$heading" /><xsl:text> </xsl:text> <xsl:value-of select ="$StringToTransform" /> </elem> </xsl:otherwise> </xsl:choose> </xsl:template> </xsl:stylesheet>

Convert CSV to XML in Excel

Refer to: http://www.meadinkent.co.uk/xl_xml1.htm

The easiest way of converting the data in Excel to XML is to use some VB script

You can create an Excel Macro with the code below. Then in your excel workbook, run the macro.


Sub MakeXML()
' create an XML file from an Excel table
Dim MyRow As Integer, MyCol As Integer, Temp As String, YesNo As Variant, DefFolder As String
Dim XMLFileName As String, XMLRecSetName As String, MyLF As String, RTC1 As Integer
Dim RangeOne As String, RangeTwo As String, Tt As String, FldName(99) As String

MyLF = Chr(10) & Chr(13) ' a line feed command
DefFolder = "C:\" 'change this to the location of saved XML files

YesNo = MsgBox("This procedure requires the following data:" & MyLF _
& "1 A filename for the XML file" & MyLF _
& "2 A groupname for an XML record" & MyLF _
& "3 A cellrange containing fieldnames (col titles)" & MyLF _
& "4 A cellrange containing the data table" & MyLF _
& "Are you ready to proceed?", vbQuestion + vbYesNo, "MakeXML CiM")

If YesNo = vbNo Then
Debug.Print "User aborted with 'No'"
Exit Sub
End If

XMLFileName = FillSpaces(InputBox("1. Enter the name of the XML file:", "MakeXML CiM", "xl_xml_data"))
If Right(XMLFileName, 4) <> ".xml" Then
XMLFileName = XMLFileName & ".xml"
End If

XMLRecSetName = FillSpaces(InputBox("2. Enter an identifying name of a record:", "MakeXML CiM", "record"))

RangeOne = InputBox("3. Enter the range of cells containing the field names (or column titles):", "MakeXML CiM", "A3:D3")
If MyRng(RangeOne, 1) <> MyRng(RangeOne, 2) Then
MsgBox "Error: names must be on a single row" & MyLF & "Procedure STOPPED", vbOKOnly + vbCritical, "MakeXML CiM"
Exit Sub
End If
MyRow = MyRng(RangeOne, 1)
For MyCol = MyRng(RangeOne, 3) To MyRng(RangeOne, 4)
If Len(Cells(MyRow, MyCol).Value) = 0 Then
MsgBox "Error: names range contains blank cell" & MyLF & "Procedure STOPPED", vbOKOnly + vbCritical, "MakeXML CiM"
Exit Sub
End If
FldName(MyCol - MyRng(RangeOne, 3)) = FillSpaces(Cells(MyRow, MyCol).Value)
Next MyCol

RangeTwo = InputBox("4. Enter the range of cells containing the data table:", "MakeXML CiM", "A4:D8")
If MyRng(RangeOne, 4) - MyRng(RangeOne, 3) <> MyRng(RangeTwo, 4) - MyRng(RangeTwo, 3) Then
MsgBox "Error: number of field names <> data columns" & MyLF & "Procedure STOPPED", vbOKOnly + vbCritical, "MakeXML CiM"
Exit Sub
End If
RTC1 = MyRng(RangeTwo, 3)

If InStr(1, XMLFileName, ":\") = 0 Then
XMLFileName = DefFolder & XMLFileName
End If

Open XMLFileName For Output As #1
Print #1, ""
Print #1, ""

For MyRow = MyRng(RangeTwo, 1) To MyRng(RangeTwo, 2)
Print #1, "<" & XMLRecSetName & ">"
For MyCol = RTC1 To MyRng(RangeTwo, 4)
' the next line uses the FormChk function to format dates and numbers
Print #1, "<" & FldName(MyCol - RTC1) & ">" & RemoveAmpersands(FormChk(MyRow, MyCol)) & ""
' the next line does not apply any formatting
' Print #1, "<" & FldName(MyCol - RTC1) & ">" & RemoveAmpersands(Cells(MyRow, MyCol).Value) & ""
Next MyCol
Print #1, ""

Next MyRow
Print #1, "
"
Close #1
MsgBox XMLFileName & " created." & MyLF & "Process finished", vbOKOnly + vbInformation, "MakeXML CiM"
Debug.Print XMLFileName & " saved"
End Sub
Function MyRng(MyRangeAsText As String, MyItem As Integer) As Integer
' analyse a range, where MyItem represents 1=TR, 2=BR, 3=LHC, 4=RHC

Dim UserRange As Range
Set UserRange = Range(MyRangeAsText)
Select Case MyItem
Case 1
MyRng = UserRange.Row
Case 2
MyRng = UserRange.Row + UserRange.Rows.Count - 1
Case 3
MyRng = UserRange.Column
Case 4
MyRng = UserRange.Columns(UserRange.Columns.Count).Column
End Select
Exit Function

End Function
Function FillSpaces(AnyStr As String) As String
' remove any spaces and replace with underscore character
Dim MyPos As Integer
MyPos = InStr(1, AnyStr, " ")
Do While MyPos > 0
Mid(AnyStr, MyPos, 1) = "_"
MyPos = InStr(1, AnyStr, " ")
Loop
FillSpaces = LCase(AnyStr)
End Function

Function FormChk(RowNum As Integer, ColNum As Integer) As String
' formats numeric and date cell values to comma 000's and DD MMM YY
FormChk = Cells(RowNum, ColNum).Value
If IsNumeric(Cells(RowNum, ColNum).Value) Then
FormChk = Format(Cells(RowNum, ColNum).Value, "#,##0 ;(#,##0)")
End If
If IsDate(Cells(RowNum, ColNum).Value) Then
FormChk = Format(Cells(RowNum, ColNum).Value, "dd mmm yy")
End If
End Function

Function RemoveAmpersands(AnyStr As String) As String
Dim MyPos As Integer
' replace Ampersands (&) with plus symbols (+)

MyPos = InStr(1, AnyStr, "&")
Do While MyPos > 0
Mid(AnyStr, MyPos, 1) = "+"
MyPos = InStr(1, AnyStr, "&")
Loop
RemoveAmpersands = AnyStr
End Function

Thursday, July 03, 2008

Increase Java heap size

Refer to: http://hausheer.osola.com/docs/5

"
If Java runs out of memory, the following error occurs:

Exception in thread "main" java.lang.OutOfMemoryError: Java heap space

This can have two reasons:

* Your Java application has a memory leak. There are tools like YourKit Java Profiler that help you to identify such leaks.
* Your Java application really needs a lot of memory (more than 128 MB by default!). In this case the Java heap size can be increased using the following runtime parameters:

java -Xms -Xmx

Defaults are:

java -Xms32m -Xmx128m

You can set this either in the Java Control Panel or on the command line, depending on the environment you run your application.

"