XML.com: XML From the Inside Out
oreilly.comSafari Bookshelf.Conferences.

advertisement

XML Forms, Web Services and Apache Cocoon
by Ivelin Ivanov | Pages: 1, 2, 3

Transforming the group element

The group element is not transformed itself. Its ref attribute is used as a base for the child form controls. The ref attributes of the children are relative to the reference of the group.

Example 6. group element in the original document.

...
    <xf:group ref="/system" id="sysgroup">
      <xf:textbox ref="@ram">
        <xf:caption>RAM</xf:caption>
        <xf:violations class="error"/>
      </xf:textbox>
      ...
    </xf:group>
...

Example 7. group element after transformation

...
    <xf:group id="sysgroup">
      <xf:textbox ref="/system/@ram">
        <xf:value>512</xf:value>
        <xf:caption>RAM</xf:caption>
      </xf:textbox>
      ...
    </xf:group>
...

Notice how the model reference of the textbox changed from "@ram" to "/system/@ram" after the transformation. This way the reference is absolute in regard to the form model.

Transforming the repeat element

The transformation of the repeat element consists of two phases. In the first phase the content of the element is unrolled into a set of group tags, each with a unique reference to an individual node from the node set. The second phase recursively applies the original transformation algorithm to each of the group elements.

Example 8. repeat element in the original document.

...
      <xf:repeat nodeset="favorite[position() &lt;= 3]" id="favorites">
        <xf:textbox ref="." class="info">
          <xf:caption>URL: </xf:caption>
        </xf:textbox>
      </xf:repeat>
...

Example 9. repeat element after transformation

...
<xf:repeat nodeset="favorite[position() &lt;= 3]" id="favorites">
  <xf:group ref="/favorite[1]">
    <xf:textbox class="info" ref="/favorite[1]/.">
      <xf:value>http://xml.apache.org/cocoon</xf:value>
      <xf:caption>URL: </xf:caption>
    </xf:textbox>
  </xf:group>
  <xf:group ref="/favorite[2]">
    <xf:textbox class="info" ref="/favorite[2]/.">
      <xf:value>http://jakarta.apache.org</xf:value>
      <xf:caption>URL: </xf:caption>
    </xf:textbox>
  </xf:group>
</xf:repeat>
...

Notice that there are multiple group elements referencing the indexes of the favorite node set. Each of the children textbox elements has a absolute reference in regard to the form model.

Transforming XMLForm markup to HTML

The output of the XMLFormTransformer is valid XML which can be fed to any other XML consumer. The consumer can be a web services client or even a Portal Syndication component. This feature of XMLForm is a distinct advantage over HTML forms, which are exclusively targeted to HTML capable browsers.

Accessing Model properties

XMLForms uses Apache JXPath to address instance data nodes in binding expressions, to express constraints, and to specify calculations. JXPath is an implementation of W3C XPath which provides access to nodes in XML documents as well as access to properties of JavaBeans and even mixed objects like JavaBeans containing DOM nodes. XPath expressions which are not syntactically valid, including attempted calls to undefined functions, result in server side exceptions at runtime.

XPath Datatypes

XPath data types are used only in binding expressions. A binding expression is an XPath LocationPath expression that addresses a model property. XMLForm uses XPath datatypes boolean, string, number, and node-set. When addressing JavaBean properties, JXPath automatically applies conversion to and from string, which means that it will support any Java primitive type as well as complex types which provide conversion methods via the Java BeanInfo interface. Additionally JXPath automatically converts a Collection type or a primitive Java array into a node-set.

Extending XMLForm Applications to Web Services

The core functionality of the Web Service is the same as for the GUI application. It collects information about the usage of a Cocoon deployment. Whether submitted directly via browser or through a remote program, the application will receive the message and if the data is valid, it will process it in some fashion then notify the client of the outcome.

To make the Web Service public, we will have to present a WSDL file which defines the contract for the interface for remote users. To maximize reuse we will opt for the HTTP GET binding, instead of XML-RPC or SOAP. This is a very straightforward exercise, which simply lists the names of the request parameters.

Following is a snippet of the WSDL file describing the service:

<definitions>
   . . .
  <message name="UsageFeedbackHttpGetIn">
    <part name="firstName" type="xsd:string"/>
    <part name="lastName" type="xsd:string"/>
    <part name="email" type="xsd:string"/>
      . . .
  </message>
      . . .
  <portType name="UsageFeedbackHttpGet">
    <operation name="UsageFeedback">
      <input message="s0:HelloWorldHttpGetIn"/>
      <output message="s0:HelloWorldHttpGetOut"/>
    </operation>
  </portType>
  <binding name="UsageFeedbackHttpGet" type="s0:UsageFeedbackHttpGet">
    <http:binding verb="GET"/>
    <operation name="WSUsageFeedback">
      <http:operation location="/UsageFeedbackService"/>
      <input>
        <http:urlEncoded/>
      </input>
      <output>
        <mime:mimeXml part="Body"/>
      </output>
    </operation>
  </binding>
  <service name="UsageFeedback">
    <port name="UsageFeedbackHttpGet" binding="s0:UsageFeedbackHttpGet">
      <http:address location="http://localhost:8080/cocoon/samples/xmlform"/>
    </port>
  </service>
</definitions>

Since the data model is exactly the same as the one for the GUI version, we can readily reuse all validation rules.

Since the request mechanism is HTTP GET, we could reuse the existing XMLForm Action class to handle the Web Service requests. Apart from the WSDL file, there is hardly any extra work to do to extend the application to a Web Service. Well designed Cocoon applications can be turned into Web Services with little effort. We capitalized on the clean separation of logic, content, and presentation once again.

Conclusion

This article introduced a new perspective on form handling in web applications, a technique for connecting the business logic and the UI layer, while preserving a thin line which cleanly separates them. Programmers can now focus on the implementation of the application workflow without the burden of tedious HTML coding.

Web page authors on the other hand can work on the presentational aspects of the application without knowing how to code in Java or even run the application server. Usability experts can sketch UML activity diagrams and write the initial XML form documents. And quality assurance professionals can write regression tests against the web pages in their XML stage, rather than manually testing poorly structured HTML.

Additional Information

Look for the author's new book Extreme XML Publishing with Open Source Tools from Manning Publications. You can also follow the CocoonHive.org weblog which posts use cases, best practices and success stories on advanced Cocoon applications.

Resources



1 to 3 of 3
  1. Please remove
    2006-09-28 14:49:37 dmccreary
  2. XMLForm as a standalone servlet toolkit
    2003-03-27 05:45:50 Ivelin Ivanov
  3. usability
    2003-01-30 05:38:34 ed nixon
1 to 3 of 3