Alignment: Chaotic Java

Saturday, January 07, 2006

New place, new lucks

This blog, much like my personal blog has finally moved to the domain of Crazy Red Panda. You can find the new blog, with the old posts, at http://javachaos.crazyredpanda.com/.

Blogger gave a great host and I appreciate what they're doing. Giving a free, reliable and easy to use platform for bloggers is very welcomed, and I can't stop directing people to it when they think about starting a blog, even though in Israel we have our own free blog platform (which I deem with a lesser respect).

Okay, that's it! I've spoken enough, just flock to the new site already!

Tuesday, January 03, 2006

When a framework uses itself

I think the most fun part of using a framework, except for having others use it, is when the framework or producing company is using the framework itself for some task.

Just like the Apache site running on an Apache HTTP Server, Atlassian's JIRA having their JIRA issues tracked by JIRA, and so on.

Soon I will be facing Feature Requests 1380359 and 1380356, namely generating XSD from Java classes and vice versa. Thinking about the task ahead, as I usually try to do, I realised that it should be fairly simple, at least from parsing the XSD document perspective.

You see, I could bind the XSD document schema to POJOs using X2J, read the XSD document into Java classes using X2JReader, and use these Java classes to create Java annotated code.

On the other way around, I could just use the already existing Analyser class to fill up the information in those Java classes describing the XSD, and then just write them down using the X2JWriter.

Ah... I love simplicity..

Related Topics
XML to POJOs: Alpha 0.1
XML to POJOs: Repeating Types
XML to POJOs Example: Contacts List

Bug in java.blogs?

I'm probably the only one to use the search option in java.blogs, which can explain the following image I received when searching for the term "ajax":



Interesting Topics
Contacts List Example
Converters in XML
Using the Mould

Sunday, January 01, 2006

Using the Mould

Lately I've realised that the Analyser code in X2J is messy, and wanted to treat it to a better design. For those numerous people out there who do not know the X2J code (yet!), I will offer some explanation:

The Analyser's task in X2J is to take a Plain Old Java Object (POJO) and analyse it into a ClassInfo instance. This instance represents an xs:complexType in XSD terms, and contains information regarding the type.

This information includes two collections: One for ElementInfo instances, and one for AttributeInfo instances (representing the xs:element and xs:attribute respectively).

These XXX-Info instances have only getter methods for their properties; They are much like the Class instances Java provides, in a way.

The problem with Analyser is that all the XXX-Info creation logic is located inside it; Whenever new properties are added to ClassInfo it would require changing Analyser itself.

An idea comes to mind and it is called Visitors. Visitors allow separation between the object's inspection and the object's creation. The problem is, once an XXX-Info is created, it can't be changed (getters only, remember?)

My proposition, and what I use in X2J, is something I call a "Mould". Internally, it's nothing more than a Properties instance.

What makes it special is that it is accompanied by a set of Visitors which change the mould information as they inspect the analysed class, and a Factory class which takes a mould and creates an XXX-Info instance from it.

What more is that the Factory contains a "default mould", so that if visitors did not change certain values they will have a default value set in a pretty convinient way, and outside the Factory's logic.

Any remarks? Did I miss anything? I'd love to hear responses!

Related Topics
XML to POJOs Advatanges: @Converter
XML to POJOs Advantages: @PrimaryKey
Interesting Topics
XML to POJOs Example: Contacts List
Class Enum<E extends Enum<E>>

Saturday, December 24, 2005

XML to POJOs Example - Contacts List

Showing off X2J I created the following XSD example, which is a simple definition for a contact list which can store connections between the contacts:


<?xml version="1.0" encoding="utf-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="contactList">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="contact" minOccurs="0" maxOccurs="unbounded">
          <xs:complexType>
            <xs:sequence>
              <xs:element name="friend-of" type="xs:string" minOccurs="0"
       maxOccurs="unbounded"/>
            </xs:sequence>
            <xs:attribute name="name" type="xs:string" />
          </xs:complexType>
        </xs:element>
      </xs:sequence>
      <xs:attribute name="owner" type="xs:string"/>
    </xs:complexType>
  </xs:element>
</xs:schema>



You can see I'm using "friend-of" as a list of names. When an XML binding generator creates classes from this, it is likely to produce a String[] getFriendOfArray() or something similar.

Check out the corresponding X2J code:






01 @Element
02 public class Contact {
03   @Attribute
04   @PrimaryKey
05   String getName() {..}
06   
07   @InnerElement
08   @ArrayElement(label=false)
09   Contact[] getFriendOf() {..}
10 
11 }



Java2html




You can see how I can access the Contact instances who are friends with a certain Contact instance immediately; Also, when an XML conforming to this X2J annotated class is parsed, any contact on the "friend-of" list will be checked for existence as part of validation testing, something that (to my humble knowledge) just wouldn't happen in a regular XSD generated class.

I'd love to hear comments, remarks, bashings, suggestions... Anything.

Related Topics
XML to POJOs alpha 0.1
Possible Advantages of X2J: Primary Keys
X2J on the firing line
Interesting Topics
XML to POJOs Advantages - @Converter