Fixing JBoss Seam XML Schema Errors in Eclipse

If you start developing with Jboss Seam in Eclipse you may get some weird errors like this:

cvc-elt.1: Cannot find the declaration of element 'components' (Resource: components.xml)
cvc-elt.1: Cannot find the declaration of element 'pages' (Resource: pages.xml)
Referenced file contains errors (http://jboss.com/products/seam/components-2.2.xsd)
Referenced file contains errors (http://jboss.com/products/seam/core-2.2.xsd)

Screenshot of Eclipse Problems

You can fix these errors easily by adding some User Sepcified Entries to the XML Catalog of Eclipse:

  1. Extract all XSD files from the JBoss Seam JAR file (jboss-seam-2.2.0.GA.jar for example) to your harddisk
  2. Open Window / Preferences / XML / XML Catalog
    Screenshot of Eclipse XML Catalog / User Specified Entries
  3. Add the first XSD file
    Screenshot of adding an Eclipse XML Catalog Entry
  4. Repeat the step for all other XSD files

After revalidating your project the errors should be fixed.

Parsing a Maven POM

Some days ago I needed some information from a Maven “pom.xml” file. First I thought about reading the information from that file using XPATH. But, after digging around with Google, I found it’s much simpler to use Maven’s own capabilities for that task.

You need only two JAR files:

  • maven-model-2.0.9.jar
  • plexus-utils-1.5.1.jar

And a small piece of code:

Reader reader = new FileReader(pomXmlFile);
try {
    MavenXpp3Reader xpp3Reader = new MavenXpp3Reader();
    Model model = xpp3Reader.read(reader);
} finally {
    reader.close();
}

The only thing that took a bit more time was to include an attached “parent POM”. If you’re interested in that, just let me know.

Add JAR files programmatically to the classpath

Everyone knows that starting a Java application from the command line is, to say the least, annoying. Ever got an application that contained several JAR files but no batch file or shell script to start the application?

You may have ended up creating a batch file like this:
java -classpath lib/abc.jar;lib/def.jar ...

A better approach is to collect the necessary JAR files with batch files or a shell script like this: http://www.fuin.org/files/create-classpath-scripts.zip (Thanks to the guys from iReport for the above code.)

If you are a lucky one, you may have had the opportunity to use Java 6, which introduced a wildcard option: java -classpath lib\* ...

But what if you create your own application and you have to load some libraries “on the fly”. At first this may seem difficult or impossible but, in fact, it’s easy:

  1. Start your application with a single main JAR file:
    java -classpath mymain.jar ...
  2. Add more JAR files programmatically:
    Utils4J.addToClasspath("file:///c:/myapp/lib/abc.jar");
    

Simple, isn’t it?

You can find the Utils4J library here.

Good source commenting is a kind of art

It’s my firm conviction that good inline source commenting is a kind of art. It’s not easy to find the right balance between too much and too little commenting in your code. And it’s even harder to write useful comments.

I recently came across the following code snippet:

// Number of pages
doc.setNoOfPages(t.getPageCount());
// State
doc.setState(t.getState());
// Text
doc.setText(t.getText().trim());
// Code
doc.setCode(t.getCode());
// GroupCode
doc.setGroupCode(t.getGroupCode());

This is a good example of completely useless comments. Even worse, the comments bloat the code!

I personally think most comments should be avoided in favor of good method names. In my experience, inline comments are often hints for possible code refactorings.

Another example from the same code:

// Rotate (Convert marker into boolean)
char rotate= t.getRotationMarker().charAt(0);
if (rotate == '1') {
    doc.setRotate(Boolean.TRUE);
} else {
    doc.setRotate(Boolean.FALSE);
}

Here we have, in fact, two separate operations and the comment states exactly that: “Rotate” means “setting the rotate property” (and is another example of a useless comment) and “Convert marker into Boolean”.

What about creating a small conversion method?

/**
* Converts a charcter value into a boolean.
*
* @param rotate Character to convert. A value of "1" will be
* interpreted as TRUE and all other values as FALSE.
*/
private Boolean toBoolean(char rotate) {
    return Boolean.valueOf(rotate == '1');
}

The calling code is then as follows:

boolean rotate = toBoolean(t.getRotationMarker().charAt(0));
doc.setRotate(rotate);

Now you have a simple JavaDoc comment and a separate setter with no comment.

Zooming Swing

There is a nice zoom function included in Firefox: You can use CTRL+ and CTRL- to zoom in and out of a website. Why isn’t such a function available for Swing applications? It shouldn’t be too complicated to create it!  So I started to create a new Scalable Layout Manager.

Here is a short preview of it: Flash Demo Video 

It already works with the following layouts:

  • Null (Absolute) Layout
  • Border Layout
  • Card Layout
  • Flow Layout
  • Gridbag Layout
  • JGoodies Layout

Still under work:

  • Box Layout
  • Grid Layout
  • Spring layout

Integration into existing applications is simple:

// Install scalable layout and CTRL+/CTRL- keys for scaling operations
ScalableLayoutUtils.installScalableLayoutAndKeys(new DefaultScalableLayoutRegistry(), frame, 0.1);

The source is available as part of the Utils4Swing5 library.

A better keytool

Have you ever used Java’s keytool? A simplistic command line tool that’s a pain if you use it only once in awhile.

But here comes KeyTool IUI to the rescue:

KeyToolIUI Screenshot 1
KeyToolIUI Screenshot 1KeyToolIUI Screenshot 2

A great UI to create and manage keys and certificates and more . . .
http://yellowcat1.free.fr/index_ktl.html

[Update 2011/03/13]
I’m sad to tell you that Bob (yellowcat) the developer of KeyTool IUI told me that it’s no longer available. He sold the software and all you can do is try to download an old copy of it somewhere from the internet.

[Update 2011/04/01]
Good news! The tool is now open source: http://code.google.com/p/keytool-iui/

Apache Batik with Maven 2

Today I just wanted to try some SVG graphics within a new Swing application.  The build is based on Maven 2 and so it should have been relatively easy to add the stuff to the project . . .

I checked the Fornax Respository and found about 18 Artifacts! Hmmm . . . Which one to use? Most of them don’t have any dependencies inside. Finally I discovered that it’s the Batik SVG Rasterizer that has the necessary declarations inside. 

Additionally I added Batik Swing because it sounds like I’d need it inside a Swing application 😉

I started the application and got a 

java.lang.NoClassDefFoundError: org/w3c/dom/svg/SVGSVGElement

Exception . . .

It took me some time to find the necessary XML APIs within the Maven respository but at least I succeeded…

Here are my Maven dependencies – Maybe this will save you some time:

<dependency>
    <groupId>xml-apis</groupId>
    <artifactId>xml-apis-ext</artifactId>
    <version>1.3.04</version>
</dependency>
<dependency>
    <groupId>xml-apis</groupId>
    <artifactId>xml-apis</artifactId>
    <version>1.3.04</version>
</dependency>
<dependency>
    <groupId>batik</groupId>
    <artifactId>batik-rasterizer</artifactId>
    <version>1.6</version>
</dependency>
<dependency>
    <groupId>batik</groupId>
    <artifactId>batik-swing</artifactId>
    <version>1.6</version>
</dependency>

Does your provider manipulate your Internet access?

Did you ever wonder why a search engine page is shown when you enter a wrong URL? This may be because your Internet access provider manipulates the results returned to your computer! My Internet provider Alice for example returns an AOL search result if a “HTTP/1.1 302” status is found.

You can check your own connection with a nice tool called Netalysr (developed at the University of California).