Go to content Go to sidebar

Browsing the "Java" category...

Using XSL Param

XSL is not just a transformational process, you can also add information content during the transformation. The following transform tweaks the identity transform to add a date element as the first child of the root.

Here are our changes:

  1. Access the EXSLT extensions for dates and times in Xalan-J by adding the date: namespace.
  2. Exclude the date: namespace from the results by adding it to the exclude-result-prefixes attribute.
  3. Define an XSL date param and initialize it to the current date. Note that the param value specified in the transform may be externally overridden.
  4. Explicitly match on the root element: match=”/*”
    1. Use the name() function to create an element with the same name as the root
    2. Apply the identity template to any attributes of the root element
    3. Add a new date element that contains the value of the date param
    4. Apply the identity template to any children of the root element

read more


Using soapUI Project Properties

So far, we haven’t done anything with soapUI that couldn’t be done with curl and a shell script. One of the advantages of soapUI over curl is the built-in support for groovy scripting .

The following example shows the use of a groovy load script to insure that properties are properly initialized. A project load script is specified in the lower load pane of the of the project panel. …

read more


Adding a REST service to soapUI

soapUI is a “open source functional testing tool for Web Services”. In this post, we’ll add the XSL Servlet as a soapUI project.

To help you get started, here is the XSL servlet as a java war file. This war requires the addition of serializer.jar, xalan.jar and xml-apis.jar from Xalan-J to the web-application WEB-INF/lib directory to run.

First, create a new project in soapUI. That bring up the dialog shown below. For this example, I’ve named the new service Ideoplex, specified the base url for the service as http://localhost:8080/xsl/transform and requested that we proceed to add a REST resource as our next step. …

read more


Simple XSL Transforms

Before we deploy our XSL servlet, let’s look at some simple transforms from the command line.

First, a simple transform that detects the root node and then outputs the xalan environment. Naturally, this will only work when you’re using xalan. …

read more


A Simple XSL Servlet

If you only have a hammer, you tend to see every problem as a nail.
Abraham Maslow

I try to restrain myself, but one of my more common workplace rants is:

If you start with xml and you end up with xml, then why aren’t you using xsl in between?

My colleagues commonly unmarshal xml to an object, manipulate the object, and marshal the object back to xml. The right choice when there are relatively complex manipulations in the middle. But xsl is a much better choice for simple manipulations:

  • Easier to review
  • Easier to test
  • Easier to modify
  • Easier to track change

Here’s a simple servlet that applies xsl transforms to posted request xml. First, the init method that reads in xsl tranform files located in /WEB-INF/xsl and stores the corresponding Templates objects in a HashMap …

read more


More on the Ant PropertyFile task

A reader writes in, requesting greater control over the behavior of the entry element of the ant PropertyFile Task:

I am having a problem where in, if the number in test.properties is more than 999 then the increment adds a comma. The incremented number after 999 will be 1,000.

The answer is to use the pattern attribute of the entry element:


<project default="run">
  <target name="run">
    <propertyfile file="test.properties">
      <entry key="count" type="int" operation="+" value="1" pattern="0">
    </propertyfile>
  </target>
</project>

With the addition of this attribute, the count property no longer includes the thousands separator:

$ cat test.properties
#Sat Apr 18 20:16:30 GMT 2009
count=999
$ ant
Buildfile: build.xml

run: [propertyfile] Updating property file: test.properties

BUILD SUCCESSFUL Total time: 0 seconds $ cat test.properties #Sat Apr 18 20:29:41 GMT 2009 count=1000

Building an executable jar with Ant

An executable jar file has a defined default class, allowing the default class to be invoked without explicitly identifying it via the command line. On a supporting OS (such as Mac OS/X or Microsoft Windows), this allows the class to be invoked by double-clicking the file.

I know that making a jar file executable requires an entry in the manifest file. I can usually remember that the attribute in question is Main-Class (although I'm usually fuzzy about the proper capitalization). The problem is remembering the correct command line options to insert my own MANIFEST.MF file into a jar file.

All in all, it is much simpler to just use ant to handle it for me. Let's start with the example from my Ant Hello World Revisited. The new ant build file follows, with the new lines shown in bold. Note that I've also designated the "jar" task as the default task.


$ cat hello.xml
<project default="jar">
  <target name="compile">
    <javac srcdir="." />
  </target>
  <target name="jar" depends="compile">
    <jar destfile="hello.jar"
         basedir="."
         includes="**/*.class">
      <manifest>
        <attribute name="Main-Class" value="hello" />
      </manifest>
    </jar>
  </target>
</project>
$ cat hello.java public class hello { public static void main( String[] args ) { System.out.println( "Hello World" ); } }

Now we simply build our jar file:


$ rm *.jar
$ ant -f hello.xml
Buildfile: hello.xml

compile:

jar: [jar] Building jar: /Tutorial/Ant/Jar/hello.jar

BUILD SUCCESSFUL Total time: 2 seconds

And execute the default class by using the -jar command line option to java:


$ java -jar hello.jar
Hello World

Disclaimer: I don't claim to be an expert on ant. Please send comments and corrections.


« Previous