<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Adams Bros Blog &#187; Java</title>
	<atom:link href="http://blog.adamsbros.org/category/programming/java/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.adamsbros.org</link>
	<description></description>
	<lastBuildDate>Sat, 14 Aug 2010 07:30:32 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.1</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>What is JAXB and JAXB Example Code</title>
		<link>http://blog.adamsbros.org/2010/02/07/jaxb-example-code/</link>
		<comments>http://blog.adamsbros.org/2010/02/07/jaxb-example-code/#comments</comments>
		<pubDate>Mon, 08 Feb 2010 03:12:28 +0000</pubDate>
		<dc:creator>Trenton</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[XML]]></category>

		<guid isPermaLink="false">http://blog.adamsbros.org/?p=200</guid>
		<description><![CDATA[I thought it may be nice for those googlers that want to know about JAXB, to have a quick working example to use.  So, that is the purpose of this post.
First off, what is JAXB?  Well, simply put, JAXB is for converting Java objects to XML or from XML to Java objects.  With the advent [...]]]></description>
			<content:encoded><![CDATA[<p>I thought it may be nice for those <a title="Googlers" href="http://www.google.com">googlers</a> that want to know about JAXB, to have a quick working example to use.  So, that is the purpose of this post.</p>
<p><span id="more-200"></span>First off, what is JAXB?  Well, simply put, JAXB is for converting Java objects to XML or from XML to Java objects.  With the advent of Java 1.5, and annotations, this becomes extremely simple.</p>
<p>Download <a title="JAXB Download" href="http://www.google.com/search?q=jaxb+download">JAXB</a>, dependencies to your local computer, and put them in a folder called "libs". The dependencies JARs I have are...</p>
<ul>
<li>activation-1.1.jar</li>
<li>jaxb-api-2.0.jar</li>
<li>jaxb-impl-2.0.5.jar</li>
<li>jsr173_api-1.0.jar</li>
</ul>
<p>Copy the following Java source code to Employee.java, and proceed with the instructions below</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.xml.bind.JAXBContext</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.xml.bind.JAXBException</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.xml.bind.annotation.*</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.io.StringReader</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.io.StringWriter</span><span style="color: #339933;">;</span>
&nbsp;
@XmlRootElement<span style="color: #009900;">&#40;</span>name <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;employee&quot;</span><span style="color: #009900;">&#41;</span>
@XmlAccessorType<span style="color: #009900;">&#40;</span>XmlAccessType.<span style="color: #006633;">FIELD</span><span style="color: #009900;">&#41;</span>
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Employee
<span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000066; font-weight: bold;">void</span> main<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> args<span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> JAXBException
    <span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">final</span> Employee john <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Employee<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        john.<span style="color: #006633;">setId</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        john.<span style="color: #006633;">setFirstName</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;John&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        john.<span style="color: #006633;">setMiddleName</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Robert&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        john.<span style="color: #006633;">setLastName</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Doe&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #666666; font-style: italic;">// write it out as XML</span>
        <span style="color: #000000; font-weight: bold;">final</span> JAXBContext jaxbContext <span style="color: #339933;">=</span> JAXBContext.<span style="color: #006633;">newInstance</span><span style="color: #009900;">&#40;</span>Employee.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #003399;">StringWriter</span> writer <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">StringWriter</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        jaxbContext.<span style="color: #006633;">createMarshaller</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">marshal</span><span style="color: #009900;">&#40;</span>john, writer<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #666666; font-style: italic;">// read it from XML</span>
        <span style="color: #000000; font-weight: bold;">final</span> Employee johnRead <span style="color: #339933;">=</span>
            <span style="color: #009900;">&#40;</span>Employee<span style="color: #009900;">&#41;</span> jaxbContext.<span style="color: #006633;">createUnmarshaller</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">unmarshal</span><span style="color: #009900;">&#40;</span>
                <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">StringReader</span><span style="color: #009900;">&#40;</span>writer.<span style="color: #006633;">toString</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>john.<span style="color: #006633;">equals</span><span style="color: #009900;">&#40;</span>johnRead<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
        <span style="color: #009900;">&#123;</span>   <span style="color: #666666; font-style: italic;">// write the new object out as XML again.</span>
            writer <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">StringWriter</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            jaxbContext.<span style="color: #006633;">createMarshaller</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">marshal</span><span style="color: #009900;">&#40;</span>johnRead, writer<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span>
                <span style="color: #0000ff;">&quot;johnRead was identical to john: <span style="color: #000099; font-weight: bold;">\n</span>&quot;</span> <span style="color: #339933;">+</span> writer.<span style="color: #006633;">toString</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
        <span style="color: #000000; font-weight: bold;">else</span>
        <span style="color: #009900;">&#123;</span>
            <span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;john and johnRead are not equal&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    @XmlAttribute
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000066; font-weight: bold;">int</span> id<span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #008000; font-style: italic; font-weight: bold;">/**
     * Employee's first name
     */</span>
    @XmlElement
    <span style="color: #000000; font-weight: bold;">private</span>
    <span style="color: #003399;">String</span> firstName<span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #008000; font-style: italic; font-weight: bold;">/**
     * Employee's middle name
     */</span>
    @XmlElement
    <span style="color: #000000; font-weight: bold;">private</span>
    <span style="color: #003399;">String</span> middleName<span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #008000; font-style: italic; font-weight: bold;">/**
     * Employee's last name
     */</span>
    @XmlElement
    <span style="color: #000000; font-weight: bold;">private</span>
    <span style="color: #003399;">String</span> lastName<span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> Employee<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">int</span> getId<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">return</span> id<span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> setId<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span> id<span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">id</span> <span style="color: #339933;">=</span> id<span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">String</span> getLastName<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">return</span> lastName<span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> setLastName<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> lastName<span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">lastName</span> <span style="color: #339933;">=</span> lastName<span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">String</span> getMiddleName<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">return</span> middleName<span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> setMiddleName<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> middleName<span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">middleName</span> <span style="color: #339933;">=</span> middleName<span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">String</span> getFirstName<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">return</span> firstName<span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> setFirstName<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> firstName<span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">firstName</span> <span style="color: #339933;">=</span> firstName<span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    @Override
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">boolean</span> equals<span style="color: #009900;">&#40;</span><span style="color: #003399;">Object</span> o<span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">this</span> <span style="color: #339933;">==</span> o<span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000066; font-weight: bold;">true</span><span style="color: #339933;">;</span>
        <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>o <span style="color: #339933;">==</span> <span style="color: #000066; font-weight: bold;">null</span> <span style="color: #339933;">||</span> getClass<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">!=</span> o.<span style="color: #006633;">getClass</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000066; font-weight: bold;">false</span><span style="color: #339933;">;</span>
&nbsp;
        Employee employee <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>Employee<span style="color: #009900;">&#41;</span> o<span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>id <span style="color: #339933;">!=</span> employee.<span style="color: #006633;">id</span><span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000066; font-weight: bold;">false</span><span style="color: #339933;">;</span>
        <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>firstName <span style="color: #339933;">!=</span> <span style="color: #000066; font-weight: bold;">null</span> <span style="color: #339933;">?</span> <span style="color: #339933;">!</span>firstName.<span style="color: #006633;">equals</span><span style="color: #009900;">&#40;</span>employee.<span style="color: #006633;">firstName</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">:</span>
            employee.<span style="color: #006633;">firstName</span> <span style="color: #339933;">!=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000066; font-weight: bold;">false</span><span style="color: #339933;">;</span>
        <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>lastName <span style="color: #339933;">!=</span> <span style="color: #000066; font-weight: bold;">null</span> <span style="color: #339933;">?</span> <span style="color: #339933;">!</span>lastName.<span style="color: #006633;">equals</span><span style="color: #009900;">&#40;</span>employee.<span style="color: #006633;">lastName</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">:</span>
            employee.<span style="color: #006633;">lastName</span> <span style="color: #339933;">!=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000066; font-weight: bold;">false</span><span style="color: #339933;">;</span>
        <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>middleName <span style="color: #339933;">!=</span> <span style="color: #000066; font-weight: bold;">null</span> <span style="color: #339933;">?</span> <span style="color: #339933;">!</span>middleName.<span style="color: #006633;">equals</span><span style="color: #009900;">&#40;</span>employee.<span style="color: #006633;">middleName</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">:</span>
            employee.<span style="color: #006633;">middleName</span> <span style="color: #339933;">!=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000066; font-weight: bold;">false</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000066; font-weight: bold;">true</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    @Override
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">int</span> hashCode<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #000066; font-weight: bold;">int</span> result <span style="color: #339933;">=</span> id<span style="color: #339933;">;</span>
        result <span style="color: #339933;">=</span> <span style="color: #cc66cc;">31</span> <span style="color: #339933;">*</span> result <span style="color: #339933;">+</span> <span style="color: #009900;">&#40;</span>firstName <span style="color: #339933;">!=</span> <span style="color: #000066; font-weight: bold;">null</span> <span style="color: #339933;">?</span> firstName.<span style="color: #006633;">hashCode</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">:</span> <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        result <span style="color: #339933;">=</span> <span style="color: #cc66cc;">31</span> <span style="color: #339933;">*</span> result <span style="color: #339933;">+</span> <span style="color: #009900;">&#40;</span>middleName <span style="color: #339933;">!=</span> <span style="color: #000066; font-weight: bold;">null</span> <span style="color: #339933;">?</span> middleName.<span style="color: #006633;">hashCode</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">:</span> <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        result <span style="color: #339933;">=</span> <span style="color: #cc66cc;">31</span> <span style="color: #339933;">*</span> result <span style="color: #339933;">+</span> <span style="color: #009900;">&#40;</span>lastName <span style="color: #339933;">!=</span> <span style="color: #000066; font-weight: bold;">null</span> <span style="color: #339933;">?</span> lastName.<span style="color: #006633;">hashCode</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">:</span> <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000000; font-weight: bold;">return</span> result<span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    @Override
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">String</span> toString<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #0000ff;">&quot;Employee{&quot;</span> <span style="color: #339933;">+</span>
            <span style="color: #0000ff;">&quot;id=&quot;</span> <span style="color: #339933;">+</span> id <span style="color: #339933;">+</span>
            <span style="color: #0000ff;">&quot;, firstName='&quot;</span> <span style="color: #339933;">+</span> firstName <span style="color: #339933;">+</span> <span style="color: #0000ff;">'<span style="color: #000099; font-weight: bold;">\'</span>'</span> <span style="color: #339933;">+</span>
            <span style="color: #0000ff;">&quot;, middleName='&quot;</span> <span style="color: #339933;">+</span> middleName <span style="color: #339933;">+</span> <span style="color: #0000ff;">'<span style="color: #000099; font-weight: bold;">\'</span>'</span> <span style="color: #339933;">+</span>
            <span style="color: #0000ff;">&quot;, lastName='&quot;</span> <span style="color: #339933;">+</span> lastName <span style="color: #339933;">+</span> <span style="color: #0000ff;">'<span style="color: #000099; font-weight: bold;">\'</span>'</span> <span style="color: #339933;">+</span>
            <span style="color: #0000ff;">'}'</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>The following commands will compile and run the source, if under a unix OS.  If in windows, you'll need to form your own classpath.</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">javac <span style="color: #660033;">-classpath</span> .:$<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #000000; font-weight: bold;">for</span> jar <span style="color: #000000; font-weight: bold;">in</span> libs<span style="color: #000000; font-weight: bold;">/*</span>.jar; <span style="color: #000000; font-weight: bold;">do</span> <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #660033;">-n</span> <span style="color: #ff0000;">&quot;<span style="color: #007800;">$jar</span>:&quot;</span>; <span style="color: #000000; font-weight: bold;">done</span><span style="color: #7a0874; font-weight: bold;">&#41;</span> Employee.java
java <span style="color: #660033;">-classpath</span> .:$<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #000000; font-weight: bold;">for</span> jar <span style="color: #000000; font-weight: bold;">in</span> libs<span style="color: #000000; font-weight: bold;">/*</span>.jar; <span style="color: #000000; font-weight: bold;">do</span> <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #660033;">-n</span> <span style="color: #ff0000;">&quot;<span style="color: #007800;">$jar</span>:&quot;</span>; <span style="color: #000000; font-weight: bold;">done</span><span style="color: #7a0874; font-weight: bold;">&#41;</span> Employee</pre></div></div>

<p>The output should be as follows, but the XML will be on a single line...</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">johnRead was identical to john<span style="color: #339933;">:</span>
<span style="color: #339933;">&lt;?</span>xml version<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;1.0&quot;</span> encoding<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;UTF-8&quot;</span> standalone<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;yes&quot;</span><span style="color: #339933;">?&gt;</span>
<span style="color: #339933;">&lt;</span>employee id<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;1&quot;</span><span style="color: #339933;">&gt;</span>
 <span style="color: #339933;">&lt;</span>firstName<span style="color: #339933;">&gt;</span>John<span style="color: #339933;">&lt;/</span>firstName<span style="color: #339933;">&gt;</span>
 <span style="color: #339933;">&lt;</span>middleName<span style="color: #339933;">&gt;</span>Robert<span style="color: #339933;">&lt;/</span>middleName<span style="color: #339933;">&gt;</span>
 <span style="color: #339933;">&lt;</span>lastName<span style="color: #339933;">&gt;</span>Doe<span style="color: #339933;">&lt;/</span>lastName<span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;/</span>employee<span style="color: #339933;">&gt;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://blog.adamsbros.org/2010/02/07/jaxb-example-code/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
		<item>
		<title>ZK AJAX Status Bar</title>
		<link>http://blog.adamsbros.org/2010/01/31/zk-ajax-status-bar/</link>
		<comments>http://blog.adamsbros.org/2010/01/31/zk-ajax-status-bar/#comments</comments>
		<pubDate>Sun, 31 Jan 2010 08:45:43 +0000</pubDate>
		<dc:creator>Trenton</dc:creator>
				<category><![CDATA[AJAX]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://blog.adamsbros.org/?p=172</guid>
		<description><![CDATA[Seeing that my research project is a mini accounting system, I thought it necessary to be able to display items in a status bar, like a real application.  It is very slightly unfortunate that ZK doesn't have something integrated right in for this purpose.  But, given the versatility of ZK, it's easy enough to resolve.

Features
I [...]]]></description>
			<content:encoded><![CDATA[<p>Seeing that my research project is a mini accounting system, I thought it necessary to be able to display items in a status bar, like a real application.  It is very slightly unfortunate that ZK doesn't have something integrated right in for this purpose.  But, given the versatility of ZK, it's easy enough to resolve.</p>
<p><span id="more-172"></span></p>
<h2>Features</h2>
<p>I thought it important to have some useful features that make sense for what I've seen in status bars.</p>
<ul>
<li>a method to set the status, with a delay before the status bar is cleared</li>
<li>a method to simply set the status, and leave it as is.  It would be up to the developer to clear the status later, if need be.  Or, the next status item would take over.</li>
<li>a very simple interface abstraction in case we want to change the type of component we use later, or something of that nature.  This will help us prevent the need to refactor a lot of code that could be using the status bar; we would only need to refactor the implementing class of the interface, leaving all of the code accessing the status bar entirely intact.</li>
</ul>
<p>Future features might include...</p>
<ul>
<li>methods for setting the status with color text</li>
<li>methods for setting the status with html</li>
<li>any other suggestions are welcome.</li>
</ul>
<h2>Window Width Status Bar</h2>
<p>We first, of course, need to setup the actual status bar.  So, right before the end of the window, we do just that, with a "textbox".  Now, there's no reason you can't use some other type of control, if it makes sense to.</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;textbox</span> <span style="color: #000066;">width</span>=<span style="color: #ff0000;">&quot;100%&quot;</span> <span style="color: #000066;">height</span>=<span style="color: #ff0000;">&quot;1.2em&quot;</span> <span style="color: #000066;">sclass</span>=<span style="color: #ff0000;">&quot;status&quot;</span> <span style="color: #000066;">readonly</span>=<span style="color: #ff0000;">&quot;true&quot;</span></span>
<span style="color: #009900;">         <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;status&quot;</span> <span style="color: #000066;">use</span>=<span style="color: #ff0000;">&quot;com.example.system.StatusBar&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;timer</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;statusTimer&quot;</span> <span style="color: #000066;">running</span>=<span style="color: #ff0000;">&quot;false&quot;</span> <span style="color: #000066;">repeats</span>=<span style="color: #ff0000;">&quot;false&quot;</span></span>
<span style="color: #009900;">       <span style="color: #000066;">onTimer</span>=<span style="color: #ff0000;">'status.setText(&quot;&quot;);'</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span></pre></div></div>

<p>The text box is implemented by a custom class, which we'll delve into in a moment.  We also have a ZK timer, for implementing the automatic clearing feature, because it is illegal to access ZK elements from outside of a ZK event.  i.e. You cannot write your own thread to do it at a later time, as ZK will throw an <strong><span style="color: #3366ff;">IllegalStateException</span></strong>.</p>
<h2>The Code</h2>
<h3>Interface Abstraction</h3>
<p>We've kept the interface very simple, just two methods.  For more information, read the javadoc comments.</p>
<ul>
<li>setStatus(String) allows setting of the status bar permanently</li>
<li>setStatus(String, int) allows setting of the status bar, and automatic clearing of it after a specified delay</li>
</ul>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">package</span> <span style="color: #006699;">com.example.system</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #008000; font-style: italic; font-weight: bold;">/**
 * Handles setting of status bar messages.  All calls to this object must be
 * done from within the ZK framework, such as inside an event.
 *
 * Hides the details of what component is actually a status bar.  It could be a
 * textbox, or something else, but we don't want to be dependant on any specific
 * type of control, in case it changes in the future.
 *
 * Created :  Jan 31, 2010 1:39:37 AM MST
 *
 * Modified : $Date$ UTC
 *
 * Revision : $Revision$
 *
 * @author Trenton D. Adams
 */</span>
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">interface</span> IStatusBar
<span style="color: #009900;">&#123;</span>
    <span style="color: #008000; font-style: italic; font-weight: bold;">/**
     * Sets the status bar text for the time period indicated.
     *
     * @param statusText status text
     * @param timePeriod delay in seconds, before the status bar will be
     *                   cleared.
     */</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> setStatus<span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">final</span> <span style="color: #003399;">String</span> statusText, <span style="color: #000000; font-weight: bold;">final</span> <span style="color: #000066; font-weight: bold;">int</span> timePeriod<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #008000; font-style: italic; font-weight: bold;">/**
     * Sets the status bar text.  Pass null to clear.
     *
     * @param statusText status text or null to clear
     */</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> setStatus<span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">final</span> <span style="color: #003399;">String</span> statusText<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #008000; font-style: italic; font-weight: bold;">/**
     * @return the status text of the status bar.
     */</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">String</span> getStatus<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<h3>Implementation</h3>
<p>Essentially, all we are doing with the implement is as follows</p>
<ul>
<li>implement AfterCompose, so we can setup a session variable to access the status bar from any ZK code, without much trouble</li>
<li>implement the setStatus() methods as described in the feature section.  One takes an extra argument for a delay, indicating how long before the status bar should be cleared.</li>
</ul>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">package</span> <span style="color: #006699;">com.example.system</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.apache.log4j.Logger</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.zkoss.zk.ui.Session</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.zkoss.zk.ui.ext.AfterCompose</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.zkoss.zul.Textbox</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.zkoss.zul.Timer</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #008000; font-style: italic; font-weight: bold;">/**
 * StatusBar handles setting the status bar text, and clearing it after a given
 * timeout.  Implements IStatusBar, to hide the details of what a status bar is
 * from the client code.  We use a textbox, but we could change that, who knows.
 *
 * Created :  Jan 31, 2010 12:42:29 AM MST
 *
 * Modified : $Date$ UTC
 *
 * Revision : $Revision$
 *
 * @author Trenton D. Adams
 */</span>
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> StatusBar <span style="color: #000000; font-weight: bold;">extends</span> Textbox <span style="color: #000000; font-weight: bold;">implements</span> AfterCompose, IStatusBar
<span style="color: #009900;">&#123;</span>
    @SuppressWarnings<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span><span style="color: #0000ff;">&quot;ConstantNamingConvention&quot;</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000000; font-weight: bold;">final</span> Logger logger <span style="color: #339933;">=</span> Logger.<span style="color: #006633;">getLogger</span><span style="color: #009900;">&#40;</span>StatusBar.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> StatusBar<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> afterCompose<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        logger.<span style="color: #006633;">info</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;status bar running as &quot;</span> <span style="color: #339933;">+</span> getId<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #666666; font-style: italic;">// TODO find a better method, getFellow() didn't work</span>
        <span style="color: #000000; font-weight: bold;">final</span> Session session <span style="color: #339933;">=</span> getDesktop<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">getSession</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        session.<span style="color: #006633;">setAttribute</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;status&quot;</span>, <span style="color: #000000; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> setStatus<span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">final</span> <span style="color: #003399;">String</span> statusText, <span style="color: #000000; font-weight: bold;">final</span> <span style="color: #000066; font-weight: bold;">int</span> timePeriod<span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        setStatus<span style="color: #009900;">&#40;</span>statusText<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #666666; font-style: italic;">// the timer must be in the page, or the page is inactive and this</span>
        <span style="color: #666666; font-style: italic;">// call would never happen?</span>
        <span style="color: #000000; font-weight: bold;">final</span> <span style="color: #003399;">Timer</span> timer <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #003399;">Timer</span><span style="color: #009900;">&#41;</span> getFellow<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;statusTimer&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        timer.<span style="color: #006633;">setDelay</span><span style="color: #009900;">&#40;</span>timePeriod <span style="color: #339933;">*</span> <span style="color: #cc66cc;">1000</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        timer.<span style="color: #006633;">start</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> setStatus<span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">final</span> <span style="color: #003399;">String</span> statusText<span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        setText<span style="color: #009900;">&#40;</span>statusText<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">String</span> getStatus<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">return</span> getText<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<h3>Using the Status Bar</h3>
<p>The following code must be used inside of a ZK event handler, such as one that comes from the click of a button, or a mouse over event, etc.  Basically, all we're doing is grabbing the status bar object from the session, and request the status be set.  That's all there is to it.</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">final</span> Session session <span style="color: #339933;">=</span> getDesktop<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">getSession</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">final</span> IStatusBar statusBar <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>IStatusBar<span style="color: #009900;">&#41;</span> session.<span style="color: #006633;">getAttribute</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;status&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
statusBar.<span style="color: #006633;">setStatus</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Transaction Posted&quot;</span>, <span style="color: #cc66cc;">5</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<h2>Results</h2>
<p>I'm attaching a screen shot of the results of my status bar.</p>
<p><a href="http://blog.adamsbros.org/wp-content/uploads/2010/01/status-bar.png"><img class="alignnone size-full wp-image-177" title="status-bar" src="http://blog.adamsbros.org/wp-content/uploads/2010/01/status-bar.png" alt="ZK Status bar" width="530" height="148" /></a></p>
]]></content:encoded>
			<wfw:commentRss>http://blog.adamsbros.org/2010/01/31/zk-ajax-status-bar/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>ZK Exception Handling and Error Popup</title>
		<link>http://blog.adamsbros.org/2010/01/30/zk-exception-handling-and-error-popup/</link>
		<comments>http://blog.adamsbros.org/2010/01/30/zk-exception-handling-and-error-popup/#comments</comments>
		<pubDate>Sun, 31 Jan 2010 01:28:09 +0000</pubDate>
		<dc:creator>Trenton</dc:creator>
				<category><![CDATA[AJAX]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://blog.adamsbros.org/?p=160</guid>
		<description><![CDATA[Once I had some basic functionality in my mini accounting system that I'm writing (for research purposes), one of my goals was to have an adequate error display for the user, that was the same every time.  Suffice it to say, I was completely thrilled to find out that ZK could do exactly what I [...]]]></description>
			<content:encoded><![CDATA[<p>Once I had some basic functionality in my mini accounting system that I'm writing (for research purposes), one of my goals was to have an adequate error display for the user, that was the same every time.  Suffice it to say, I was completely thrilled to find out that ZK could do exactly what I wanted to do, and I didn't have to write an once of code.  All I did was write some ZUL, which wrapped a JSP inside of a ZK window.  I share how I did that in this post.</p>
<p><span id="more-160"></span></p>
<h2>Configuration</h2>
<p>The first thing to do is to configure ZK to handle your unhandled exceptions, and target a ZUL page for rendering.  This is done through the zk.xml file.</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;error-page<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
 <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;exception-type<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>java.lang.Throwable<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/exception-type<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
 <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;location<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>/error.zul<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/location<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/error-page<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<h2>ZK error.zul</h2>
<p>The second thing to do is create an error.zul page, which I pulled mostly from the ZK Developers Guide.  I added the bit where I include my error.jsp file.</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;?xml</span> <span style="color: #000066;">version</span>=<span style="color: #ff0000;">&quot;1.0&quot;</span> <span style="color: #000066;">encoding</span>=<span style="color: #ff0000;">&quot;UTF-8&quot;</span><span style="color: #000000; font-weight: bold;">?&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;zk</span> <span style="color: #000066;">xmlns</span>=<span style="color: #ff0000;">&quot;http://www.zkoss.org/2005/zul&quot;</span></span>
<span style="color: #009900;">    <span style="color: #000066;">xmlns:xsi</span>=<span style="color: #ff0000;">&quot;http://www.w3.org/2001/XMLSchema-instance&quot;</span></span>
<span style="color: #009900;">    <span style="color: #000066;">xsi:schemaLocation</span>=<span style="color: #ff0000;">&quot;http://www.zkoss.org/2005/zul http://www.zkoss.org/2005/zul/zul.xsd&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;window</span> <span style="color: #000066;">mode</span>=<span style="color: #ff0000;">&quot;modal&quot;</span> <span style="color: #000066;">sizable</span>=<span style="color: #ff0000;">&quot;true&quot;</span></span>
<span style="color: #009900;">          <span style="color: #000066;">title</span>=<span style="color: #ff0000;">&quot;Error ${requesScope['javax.servlet.error.status_code']}&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;vbox<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;hbox<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;include</span> <span style="color: #000066;">src</span>=<span style="color: #ff0000;">&quot;/jsp/error.jsp&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/hbox<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;hbox</span> <span style="color: #000066;">style</span>=<span style="color: #ff0000;">&quot;margin-left:auto; margin-right:auto; text-align: center;&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;button</span> <span style="color: #000066;">label</span>=<span style="color: #ff0000;">&quot;Okay&quot;</span> <span style="color: #000066;">onClick</span>=<span style="color: #ff0000;">&quot;spaceOwner.detach()&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;button</span> <span style="color: #000066;">label</span>=<span style="color: #ff0000;">&quot;Reload Page&quot;</span> <span style="color: #000066;">onClick</span>=<span style="color: #ff0000;">&quot;Executions.sendRedirect(null)&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/hbox<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/vbox<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/window<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/zk<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<h2>JSP error.jsp</h2>
<p>I basically pulled this error page from the standard error page that I normally use, and tailored it a bit.</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;">&lt;%@ page <span style="color: #000066;">import</span>=<span style="color: #ff0000;">&quot;java.io.PrintWriter&quot;</span> %<span style="color: #000000; font-weight: bold;">&gt;</span></span>
<span style="color: #009900;">&lt;%@ page <span style="color: #000066;">import</span>=<span style="color: #ff0000;">&quot;java.io.StringWriter&quot;</span> %<span style="color: #000000; font-weight: bold;">&gt;</span></span>
<span style="color: #009900;">&lt;%@ page <span style="color: #000066;">import</span>=<span style="color: #ff0000;">&quot;com.example.util.Util&quot;</span> %<span style="color: #000000; font-weight: bold;">&gt;</span></span>
<span style="color: #009900;">&lt;%@ page <span style="color: #000066;">isErrorPage</span>=<span style="color: #ff0000;">&quot;true&quot;</span> %<span style="color: #000000; font-weight: bold;">&gt;</span></span>
<span style="color: #009900;">&lt;%@ taglib <span style="color: #000066;">uri</span>=<span style="color: #ff0000;">&quot;http://java.sun.com/jsp/jstl/core&quot;</span> <span style="color: #000066;">prefix</span>=<span style="color: #ff0000;">&quot;ce&quot;</span> %<span style="color: #000000; font-weight: bold;">&gt;</span></span>
<span style="color: #009900;">&lt;%@ taglib <span style="color: #000066;">uri</span>=<span style="color: #ff0000;">&quot;http://java.sun.com/jsp/jstl/functions&quot;</span> <span style="color: #000066;">prefix</span>=<span style="color: #ff0000;">&quot;fn&quot;</span> %<span style="color: #000000; font-weight: bold;">&gt;</span></span>
<span style="color: #009900;">&lt;%@ taglib <span style="color: #000066;">uri</span>=<span style="color: #ff0000;">&quot;http://jakarta.apache.org/taglibs/string-1.1&quot;</span> <span style="color: #000066;">prefix</span>=<span style="color: #ff0000;">&quot;st&quot;</span> %<span style="color: #000000; font-weight: bold;">&gt;</span></span>
<span style="color: #808080; font-style: italic;">&lt;!-- BEGIN error.jsp --&gt;</span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;div</span> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;error&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;p</span> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;message&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span>&lt;%=exception.getMessage<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>%<span style="color: #000000; font-weight: bold;">&gt;</span><span style="color: #000000; font-weight: bold;">&lt;/p<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
&nbsp;
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;p<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>An error has occured. If you believe this error is a system problem, and you
  continue to get this error, please contact a special person via
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;a</span> <span style="color: #000066;">href</span>=<span style="color: #ff0000;">&quot;mailto:'me@example.com' &quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>email<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/a<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/p<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
&nbsp;
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;ce:if</span> <span style="color: #000066;">test</span>=<span style="color: #ff0000;">&quot;&lt;%=Util.getBooleanItem(\&quot;</span>/debug\<span style="color: #ff0000;">&quot;)%&gt;</span></span>&quot;&gt;
    <span style="color: #009900;">&lt;%</span>
<span style="color: #009900;">      StringWriter sw = new StringWriter<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;</span>
<span style="color: #009900;">      PrintWriter pw = new PrintWriter<span style="color: #66cc66;">&#40;</span>sw<span style="color: #66cc66;">&#41;</span>;</span>
<span style="color: #009900;">      exception.printStackTrace<span style="color: #66cc66;">&#40;</span>pw<span style="color: #66cc66;">&#41;</span>;</span>
<span style="color: #009900;">    %<span style="color: #000000; font-weight: bold;">&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;ce:set</span> <span style="color: #000066;">var</span>=<span style="color: #ff0000;">&quot;exceptionStack&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span>&lt;%=sw.toString<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>%<span style="color: #000000; font-weight: bold;">&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/ce:set<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;div</span> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;debug&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
      Message : <span style="color: #009900;">&lt;%=exception.getMessage<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>%<span style="color: #000000; font-weight: bold;">&gt;</span></span>
      Exception : <span style="color: #009900;">&lt;%=exception.getClass<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>.getName<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>%<span style="color: #000000; font-weight: bold;">&gt;</span></span>
      Stack : ${fn:escapeXml(exceptionStack)}
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/div<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/ce:if<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/div<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #808080; font-style: italic;">&lt;!-- END error.jsp --&gt;</span></pre></div></div>

<h2>Sample Error Page</h2>
<p>And, we have the result;  it looks quite nice.  If debug were enabled, it would have my stack trace displayed in a wrap-able fixed width font ; providing much easier diagnostics during development.</p>
<p><a href="http://blog.adamsbros.org/wp-content/uploads/2010/02/sample-error.png"><img class="alignnone size-full wp-image-186" title="ZK Sample Error" src="http://blog.adamsbros.org/wp-content/uploads/2010/02/sample-error.png" alt="ZK Sample Error Page" width="558" height="196" /></a></p>
]]></content:encoded>
			<wfw:commentRss>http://blog.adamsbros.org/2010/01/30/zk-exception-handling-and-error-popup/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>ZK AJAX Framework Introduction</title>
		<link>http://blog.adamsbros.org/2010/01/30/zk-ajax-framework-introduction/</link>
		<comments>http://blog.adamsbros.org/2010/01/30/zk-ajax-framework-introduction/#comments</comments>
		<pubDate>Sat, 30 Jan 2010 23:33:43 +0000</pubDate>
		<dc:creator>Trenton</dc:creator>
				<category><![CDATA[AJAX]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://blog.adamsbros.org/?p=139</guid>
		<description><![CDATA[I am in the process of working with and researching ZK as an AJAX framework to integrate into Java Web Development that I do.  So far, AJAX looks really amazing, and is fully integrated into the J2EE framework.  It is an event driven framework that has the option of hooking directly into existing JSP, or [...]]]></description>
			<content:encoded><![CDATA[<p>I am in the process of working with and researching ZK as an AJAX framework to integrate into Java Web Development that I do.  So far, AJAX looks really amazing, and is fully integrated into the J2EE framework.  It is an event driven framework that has the option of hooking directly into existing JSP, or entirely writing ZK event handlers to do the processing.  There is no wonder that ZK won the <a title="ZK - Simply Ajax and Mobile" href="http://sourceforge.net/blog/potm-200902/">Project of the Month Source Forge award</a>.</p>
<p><span id="more-139"></span></p>
<h2>Integration</h2>
<p>In the case of existing web applications that make heavy use of JSP for display, ZK can be easily integrated into specific controls on the page, such as drop downs, text boxes, etc, to provide AJAX for that individual component.  The component is then submitted using the normal GET/POST method.  You can thereby provide AJAX auto complete, or dynamic load functionality to your heart's content, without affecting the normal working of the page.  If your goal is to move toward entirely using AJAX for that application, you can easily do it incrementally, so as not to have an overly complex and long development cycle.  Just drop some of the ZUL includes directly into your JSP, write some ZSCRIPT or Java component/event classes, handle the processing, and you have yourself some event driven AJAX code.</p>
<h2>GUI Features</h2>
<p>ZUL files are essentially a derivative of XUL, defined for the purpose of AJAX handling in ZK.  You define your GUI layout in XML, and write code to handle each component as you choose.</p>
<p>One of many great benefits of ZK, is that it has many different ZUL components for the normal input text box.  You can make your text box a decimal box, a date box, int box, double box, etc, and AJAX will handle data validation for you, and wrapping it in the appropriate wrapper class such as BigDecimal, Integer, Double, etc.  In fact, in many cases, it won't even let you type in a character that is not allowed.  For example, if you have specified a number box of some type, such as decimal, int, double, etc, it won't even let you type an alpha character.  You can also place constraints on your inputs such as "<strong><span style="color: #3366ff;">no zero</span></strong>", or "<strong><span style="color: #3366ff;">no negative</span></strong>", which would indicate that the box cannot accept a negative or zero input, but could be empty.</p>
<p>Here is a quick example of a decimalbox in action.  I'll give two examples.  The first one has an implementing Java class, which you simply write a class that extends Decimalbox. The implementing class is just below that on the page. The second one achieves the exact same thing, but is not quite as flexible, for the simple reason that it's not as dynamic, because I you cannot go instantiating it from code.</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;decimalbox</span> <span style="color: #000066;">use</span>=<span style="color: #ff0000;">&quot;com.example.common.LedgerDecimalBox&quot;</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;credit-0&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span></pre></div></div>


<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;decimalbox</span> <span style="color: #000066;">constraint</span>=<span style="color: #ff0000;">&quot;no negative,no zero&quot;</span> <span style="color: #000066;">format</span>=<span style="color: #ff0000;">&quot;#,##0.#&quot;</span> <span style="color: #000066;">scale</span>=<span style="color: #ff0000;">&quot;2&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span></pre></div></div>


<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">package</span> <span style="color: #006699;">com.example.common</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">com.example.util.CVUtil</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.apache.log4j.Logger</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.zkoss.zk.ui.ext.AfterCompose</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.zkoss.zul.Decimalbox</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> LedgerDecimalBox <span style="color: #000000; font-weight: bold;">extends</span> Decimalbox <span style="color: #000000; font-weight: bold;">implements</span> AfterCompose
<span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">static</span> Logger logger <span style="color: #339933;">=</span> Logger.<span style="color: #006633;">getLogger</span><span style="color: #009900;">&#40;</span>LedgerDecimalBox.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000000; font-weight: bold;">public</span> LedgerDecimalBox<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>   <span style="color: #666666; font-style: italic;">// load configured options</span>
        setFormat<span style="color: #009900;">&#40;</span><span style="color: #003399;">Util</span>.<span style="color: #006633;">getStringItem</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;/inputs/decimalbox/format&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        setConstraint<span style="color: #009900;">&#40;</span><span style="color: #003399;">Util</span>.<span style="color: #006633;">getStringItem</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;/inputs/decimalbox/constraints&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        setScale<span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">2</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> afterCompose<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        logger.<span style="color: #006633;">info</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;id: &quot;</span> <span style="color: #339933;">+</span> getId<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>In all honesty, ZK seems to be absolutely amazing, very rich, very fast, very flexible, etc.  In my opinion, it is probably <strong>THE BEST</strong> AJAX framework on the planet, by a <strong>LONG</strong> shot.</p>
<p>You can totally expect me to write more on ZK, as I learn the framework, and blog about various ways of doing things.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.adamsbros.org/2010/01/30/zk-ajax-framework-introduction/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>RMI Pitfalls and Problems</title>
		<link>http://blog.adamsbros.org/2010/01/25/rmi-pitfalls/</link>
		<comments>http://blog.adamsbros.org/2010/01/25/rmi-pitfalls/#comments</comments>
		<pubDate>Tue, 26 Jan 2010 02:58:13 +0000</pubDate>
		<dc:creator>Trenton</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://blog.adamsbros.org/?p=107</guid>
		<description><![CDATA[In this post, I plan on laying out multiple common problems with RMI, that a developer can run into.  I hope that this will be a concise guide to fixing the common RMI problems that beginners run into.  As I come up with more, I will edit this post, rather than creating a new one.  [...]]]></description>
			<content:encoded><![CDATA[<p>In this post, I plan on laying out multiple common problems with RMI, that a developer can run into.  I hope that this will be a concise guide to fixing the common RMI problems that beginners run into.  As I come up with more, I will edit this post, rather than creating a new one.  I will then post a comment on this entry; if you are subscribed to the comments (RSS feed), you will get notification when there is an update.</p>
<p>Also, if you are having some sort of RMI trouble, post a comment, and I will let you know if I know the solution.  I may also add the solution directly to this post, if it happens to actually be an RMI related issue.</p>
<h2><span id="more-107"></span>no security manager: RMI class loader disabled</h2>
<p>This is a very common error, and can be a result of various things.  Generally speaking though, it is a result of a class not being accessible to the client machine, that is accessible to the RMI server.  A common developer mistake, is to nest Exception1  inside Exception2, and throw Exception2 to the client.  The client JVM knows nothing about Exception1, and therefore the above message is displayed, with more details as follows.</p>
<blockquote>
<pre>Caused by: java.rmi.UnmarshalException: Error unmarshaling return; nested exception is:
 java.lang.ClassNotFoundException: com.example.myapp.Exception1 (no security manager: RMI class loader disabled)</pre>
</blockquote>
<p>The solution to this problem is to either not wrap Exception1, to be propagated to the client side, or to make sure the proper jars are in the CLASSPATH of the client side.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.adamsbros.org/2010/01/25/rmi-pitfalls/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>EJB3 @RolesAllowed, annotation type not applicable to this kind of declaration</title>
		<link>http://blog.adamsbros.org/2010/01/21/ejb3-rolesallowed-annotation-type-not-applicable-to-this-kind-of-declaration/</link>
		<comments>http://blog.adamsbros.org/2010/01/21/ejb3-rolesallowed-annotation-type-not-applicable-to-this-kind-of-declaration/#comments</comments>
		<pubDate>Thu, 21 Jan 2010 18:30:26 +0000</pubDate>
		<dc:creator>Trenton</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://blog.adamsbros.org/?p=104</guid>
		<description><![CDATA[I'm just learning EJB3, and I'm stumbling here and there.  When developing an EJB object, I had a problem where the compiler was giving me an error that says "annotation type not applicable to this kind of declaration".  It was on a line like the following...


@RolesAllowed&#40;&#123;&#34;admin&#34;, &#34;entryclerk&#34;&#125;&#41;


Obviously this is normal to use on a method.  [...]]]></description>
			<content:encoded><![CDATA[<p>I'm just learning EJB3, and I'm stumbling here and there.  When developing an EJB object, I had a problem where the compiler was giving me an error that says "<span style="color: #0000ff;">annotation type not applicable to this kind of declaration</span>".  It was on a line like the following...</p>
<blockquote>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">@RolesAllowed<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span><span style="color: #0000ff;">&quot;admin&quot;</span>, <span style="color: #0000ff;">&quot;entryclerk&quot;</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span></pre></div></div>

</blockquote>
<p>Obviously this is normal to use on a method.  Unfortunately, I had it defined on not just any method, but the constructor method of my Java class.  It took me awhile to figure out why it was happening, so I thought would spare others a bit of grief.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.adamsbros.org/2010/01/21/ejb3-rolesallowed-annotation-type-not-applicable-to-this-kind-of-declaration/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>XSL Break or Wrap String on Word Boundary</title>
		<link>http://blog.adamsbros.org/2009/11/22/xsl-break-string-on-word-boundary/</link>
		<comments>http://blog.adamsbros.org/2009/11/22/xsl-break-string-on-word-boundary/#comments</comments>
		<pubDate>Sun, 22 Nov 2009 23:00:16 +0000</pubDate>
		<dc:creator>Trenton</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[XSL]]></category>

		<guid isPermaLink="false">http://blog.adamsbros.org/?p=93</guid>
		<description><![CDATA[I've searched all over the Internet for this, and was unable to find anything reasonable.  I found an example somewhere, of how to break a string at a specific location, but it breaks whether there is a word there or not.  So, either you have to re-compose the XML elements without a space, and hope [...]]]></description>
			<content:encoded><![CDATA[<p>I've searched all over the Internet for this, and was unable to find anything reasonable.  I found an example somewhere, of how to break a string at a specific location, but it breaks whether there is a word there or not.  So, either you have to re-compose the XML elements without a space, and hope every system you interact with does the same thing as you,  or re-compose them with a space, and a word may then be broken up in the end result.</p>
<p>In my example below, I break a string on a word boundary, outputting to an XML element called "NoteMessage" from the PESC standard.  This is dependant on Java, but you could use any language that has a useful lastIndexOf function.  In the case of Java, with zero based string index, we need to compensate for the one based string index that XSL has.  So, we add one to the result of the lastIndexOf() call.</p>
<blockquote>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">&lt;!--</span>
<span style="color: #808080; font-style: italic;">Example...</span>
<span style="color: #808080; font-style: italic;">java -cp target/dependency/xml-util-0.1.3-SNAPSHOT.jar:/usr/share/xalan/lib/xalan.jar \</span>
<span style="color: #808080; font-style: italic;">org.apache.xalan.xslt.Process -XSL target/classes/xsl/notemessage.xsl \</span>
<span style="color: #808080; font-style: italic;"> -PARAM testString &quot;This is a test to break a string into multiple note messages, \</span>
<span style="color: #808080; font-style: italic;">automatically, without programming.&quot;</span>
<span style="color: #808080; font-style: italic;">--&gt;</span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;xsl:stylesheet</span> <span style="color: #000066;">version</span>=<span style="color: #ff0000;">&quot;1.0&quot;</span></span>
<span style="color: #009900;">                <span style="color: #000066;">xmlns:String</span>=<span style="color: #ff0000;">&quot;http://xml.apache.org/xalan/java/java.lang.String&quot;</span></span>
<span style="color: #009900;">                <span style="color: #000066;">xmlns:xsl</span>=<span style="color: #ff0000;">&quot;http://www.w3.org/1999/XSL/Transform&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;xsl:output</span> <span style="color: #000066;">indent</span>=<span style="color: #ff0000;">&quot;${xml.indent}&quot;</span> <span style="color: #000066;">method</span>=<span style="color: #ff0000;">&quot;xml&quot;</span> <span style="color: #000066;">omit-xml-declaration</span>=<span style="color: #ff0000;">&quot;yes&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;xsl:param</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;testString&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;xsl:variable</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;break-at&quot;</span> <span style="color: #000066;">select</span>=<span style="color: #ff0000;">&quot;'76'&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
&nbsp;
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;xsl:template</span> <span style="color: #000066;">match</span>=<span style="color: #ff0000;">&quot;/&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;xsl:call-template</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;note-message&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;xsl:with-param</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;string&quot;</span> <span style="color: #000066;">select</span>=<span style="color: #ff0000;">&quot;$testString&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/xsl:call-template<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/xsl:template<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
&nbsp;
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;xsl:template</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;note-message&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;xsl:param</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;string&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;xsl:choose<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;xsl:when</span> <span style="color: #000066;">test</span>=<span style="color: #ff0000;">&quot;string-length($string) &lt;= $break-at&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;xsl:element</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;NoteMessage&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;xsl:value-of</span> <span style="color: #000066;">select</span>=<span style="color: #ff0000;">&quot;$string&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/xsl:element<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/xsl:when<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;xsl:otherwise<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #808080; font-style: italic;">&lt;!-- call method to find word boundary index --&gt;</span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;xsl:variable</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;truncString&quot;</span></span>
<span style="color: #009900;">                      <span style="color: #000066;">select</span>=<span style="color: #ff0000;">&quot;String:new(substring($string, 1, $break-at))&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;xsl:variable</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;lastSpaceIndex&quot;</span></span>
<span style="color: #009900;">                      <span style="color: #000066;">select</span>=<span style="color: #ff0000;">&quot;String:lastIndexOf($truncString, ' ') + 1&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;xsl:element</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;NoteMessage&quot;</span> <span style="color: #000066;">namespace</span>=<span style="color: #ff0000;">&quot;&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
          <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;xsl:value-of</span> <span style="color: #000066;">select</span>=<span style="color: #ff0000;">&quot;substring(string($truncString), 1, $lastSpaceIndex)&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/xsl:element<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;xsl:call-template</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;note-message&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
          <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;xsl:with-param</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;string&quot;</span></span>
<span style="color: #009900;">                          <span style="color: #000066;">select</span>=<span style="color: #ff0000;">&quot;substring($string, $lastSpaceIndex + 1)&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/xsl:call-template<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/xsl:otherwise<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/xsl:choose<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/xsl:template<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/xsl:stylesheet<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

</blockquote>
]]></content:encoded>
			<wfw:commentRss>http://blog.adamsbros.org/2009/11/22/xsl-break-string-on-word-boundary/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Java, Xalan, JAXP, xml transformations from Java String</title>
		<link>http://blog.adamsbros.org/2009/08/12/java-xalan-jaxp-xml-transformations-from-java-string/</link>
		<comments>http://blog.adamsbros.org/2009/08/12/java-xalan-jaxp-xml-transformations-from-java-string/#comments</comments>
		<pubDate>Wed, 12 Aug 2009 16:22:50 +0000</pubDate>
		<dc:creator>Trenton</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://blog.adamsbros.org/?p=76</guid>
		<description><![CDATA[I racked my head against the wall over and over again for several hours, unable to determine why I was getting a prolog error, when I knew dang well my XML was well formed.
I'm using JAXP, which is detecting and using xalan as my transformation implementation.  I'm not sure who's fault it is, but when [...]]]></description>
			<content:encoded><![CDATA[<p>I racked my head against the wall over and over again for several hours, unable to determine why I was getting a prolog error, when I knew dang well my XML was well formed.</p>
<p>I'm using JAXP, which is detecting and using xalan as my transformation implementation.  I'm not sure who's fault it is, but when I create a new StreamSource for my transformation, and I ask it to load the xml-stylesheet from the processing instruction, it simply doesn't work. I keep getting one of two errors, depending on the format of the java String.</p>
<p>The first error I was getting was "<code>javax.xml.transform.TransformerException: Content is not allowed in prolog</code>".  The other error I was getting, once I put my xml declaration at the beginning of the string, was "<code>javax.xml.transform.TransformerException: The markup in the document following the root element must be well-formed</code>".  Of course, none of these are meaningful in any way.  Neither one tells me that I'm not allowed to ask xalan to resolve my xml-stylesheet automatically, while using XML from a Java String object.</p>
<p><span id="more-76"></span></p>
<p>I started out with this...</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">xmlSource <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> StreamSource<span style="color: #009900;">&#40;</span>
 <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">ByteArrayInputStream</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span><span style="color: #009900;">&#41;</span> xml<span style="color: #009900;">&#41;</span>.<span style="color: #006633;">getBytes</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
xsltSource <span style="color: #339933;">=</span>
 transFact.<span style="color: #006633;">getAssociatedStylesheet</span><span style="color: #009900;">&#40;</span>xmlSource, <span style="color: #000066; font-weight: bold;">null</span>, <span style="color: #000066; font-weight: bold;">null</span>, <span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
...
<span style="color: #006633;">trans</span>.<span style="color: #006633;">transform</span><span style="color: #009900;">&#40;</span>xmlSource, result<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Of course, that completely fails with the miserable errors described.  But alas, after several ours of messing around, I decided to look into the source of "org.apache.xalan.xslt.Process".  It happens to use DOMSource, rather than StreamSource.  So, I tried that out, and it worked just fine.</p>
<p>And, just to help you out with a nice utility function for doing xalan transformations...</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">    <span style="color: #008000; font-style: italic; font-weight: bold;">/**
     * Runs a xalan transformation of the xml, with the specified xsl.
     *
     * @param xml        a String xml document, or a java.io.File object
     *                   pointing to the file
     * @param xsl        a String filename, or a java.io.File object pointing to
     *                   the file.  A null value indicates you want to resolve
     *                   the XML's &quot;xml-stylesheet&quot; processing instruction as
     *                   the XSL to use.  If it does not exist, it may fail.
     * @param parameters a map of parameters to pass to the XSL
     *
     * @return the String of the transformed xml
     *
     * @throws TransformerException if a transformation error occurs
     */</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #003399;">String</span> xalanTransformation<span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">final</span> <span style="color: #003399;">Object</span> xml, <span style="color: #000000; font-weight: bold;">final</span> <span style="color: #003399;">Object</span> xsl,
        <span style="color: #000000; font-weight: bold;">final</span> <span style="color: #003399;">Map</span> parameters<span style="color: #009900;">&#41;</span>
        <span style="color: #000000; font-weight: bold;">throws</span> TransformerException, ParserConfigurationException, <span style="color: #003399;">IOException</span>,
        SAXException
    <span style="color: #009900;">&#123;</span>
        <span style="color: #666666; font-style: italic;">// create an instance of TransformerFactory</span>
        <span style="color: #000000; font-weight: bold;">final</span> <span style="color: #003399;">ByteArrayOutputStream</span> byteArray<span style="color: #339933;">;</span>
        <span style="color: #000000; font-weight: bold;">final</span> Result result<span style="color: #339933;">;</span>
        <span style="color: #000000; font-weight: bold;">final</span> TransformerFactory transFact<span style="color: #339933;">;</span>
        <span style="color: #000000; font-weight: bold;">final</span> Transformer trans<span style="color: #339933;">;</span>
        <span style="color: #000000; font-weight: bold;">final</span> <span style="color: #003399;">Set</span> keys<span style="color: #339933;">;</span>
        <span style="color: #000000; font-weight: bold;">final</span> <span style="color: #003399;">Iterator</span> keyIt<span style="color: #339933;">;</span>
        <span style="color: #000000; font-weight: bold;">final</span> ExceptionErrorListener errorListener<span style="color: #339933;">;</span>
&nbsp;
        errorListener <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> ExceptionErrorListener<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        byteArray <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">ByteArrayOutputStream</span><span style="color: #009900;">&#40;</span>BUFFER_CAPACITY<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        result <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> StreamResult<span style="color: #009900;">&#40;</span>byteArray<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        transFact <span style="color: #339933;">=</span> TransformerFactory.<span style="color: #006633;">newInstance</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        transFact.<span style="color: #006633;">setErrorListener</span><span style="color: #009900;">&#40;</span>errorListener<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000000; font-weight: bold;">final</span> Source xmlSource<span style="color: #339933;">;</span>
        <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>xml <span style="color: #000000; font-weight: bold;">instanceof</span> <span style="color: #003399;">String</span><span style="color: #009900;">&#41;</span>
        <span style="color: #009900;">&#123;</span>
            <span style="color: #000000; font-weight: bold;">final</span> <span style="color: #003399;">Document</span> doc <span style="color: #339933;">=</span> stringToDocument<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span><span style="color: #009900;">&#41;</span> xml<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            logger.<span style="color: #006633;">debug</span><span style="color: #009900;">&#40;</span>documentToString<span style="color: #009900;">&#40;</span>doc<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            xmlSource <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> DOMSource<span style="color: #009900;">&#40;</span>doc<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
        <span style="color: #000000; font-weight: bold;">else</span> <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>xml <span style="color: #000000; font-weight: bold;">instanceof</span> <span style="color: #003399;">File</span><span style="color: #009900;">&#41;</span>
        <span style="color: #009900;">&#123;</span>
            xmlSource <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> StreamSource<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span><span style="color: #003399;">File</span><span style="color: #009900;">&#41;</span> xml<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
        <span style="color: #000000; font-weight: bold;">else</span>
        <span style="color: #009900;">&#123;</span>
            <span style="color: #000000; font-weight: bold;">throw</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">IllegalArgumentException</span><span style="color: #009900;">&#40;</span>
                <span style="color: #0000ff;">&quot;Only java.lang.String and java.io.File xml are supported &quot;</span> <span style="color: #339933;">+</span>
                    <span style="color: #0000ff;">&quot;for the xml parameter&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
&nbsp;
        <span style="color: #000000; font-weight: bold;">final</span> Source xsltSource<span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>xsl <span style="color: #339933;">==</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span>
        <span style="color: #009900;">&#123;</span>   <span style="color: #666666; font-style: italic;">// grab the XSL defined by the XML's xml-stylesheet instruction</span>
            xsltSource <span style="color: #339933;">=</span>
                transFact.<span style="color: #006633;">getAssociatedStylesheet</span><span style="color: #009900;">&#40;</span>xmlSource, <span style="color: #000066; font-weight: bold;">null</span>, <span style="color: #000066; font-weight: bold;">null</span>, <span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
        <span style="color: #000000; font-weight: bold;">else</span> <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>xsl <span style="color: #000000; font-weight: bold;">instanceof</span> <span style="color: #003399;">String</span><span style="color: #009900;">&#41;</span>
        <span style="color: #009900;">&#123;</span>
            <span style="color: #000000; font-weight: bold;">final</span> <span style="color: #003399;">InputStream</span> xsltResource <span style="color: #339933;">=</span> <span style="color: #003399;">Util</span>.<span style="color: #000000; font-weight: bold;">class</span>.<span style="color: #006633;">getResourceAsStream</span><span style="color: #009900;">&#40;</span>
                <span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span><span style="color: #009900;">&#41;</span> xsl<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>xsltResource <span style="color: #339933;">==</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span>
            <span style="color: #009900;">&#123;</span>
                <span style="color: #000000; font-weight: bold;">throw</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">IllegalArgumentException</span><span style="color: #009900;">&#40;</span>
                    xsl <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot; is an invalid XSL file&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #009900;">&#125;</span>
            xsltSource <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> StreamSource<span style="color: #009900;">&#40;</span>xsltResource<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
        <span style="color: #000000; font-weight: bold;">else</span> <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>xsl <span style="color: #000000; font-weight: bold;">instanceof</span> <span style="color: #003399;">File</span><span style="color: #009900;">&#41;</span>
        <span style="color: #009900;">&#123;</span>
            xsltSource <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> StreamSource<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span><span style="color: #003399;">File</span><span style="color: #009900;">&#41;</span> xsl<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
        <span style="color: #000000; font-weight: bold;">else</span>
        <span style="color: #009900;">&#123;</span>
            <span style="color: #000000; font-weight: bold;">throw</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">IllegalArgumentException</span><span style="color: #009900;">&#40;</span>
                <span style="color: #0000ff;">&quot;Only java.lang.String xsl filenames, or java.io.File &quot;</span> <span style="color: #339933;">+</span>
                    <span style="color: #0000ff;">&quot;are supported for the xsl parameter&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
&nbsp;
        trans <span style="color: #339933;">=</span> transFact.<span style="color: #006633;">newTransformer</span><span style="color: #009900;">&#40;</span>xsltSource<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        trans.<span style="color: #006633;">setErrorListener</span><span style="color: #009900;">&#40;</span>errorListener<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        keys <span style="color: #339933;">=</span> parameters.<span style="color: #006633;">keySet</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        keyIt <span style="color: #339933;">=</span> keys.<span style="color: #006633;">iterator</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000000; font-weight: bold;">while</span> <span style="color: #009900;">&#40;</span>keyIt.<span style="color: #006633;">hasNext</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
        <span style="color: #009900;">&#123;</span>
            <span style="color: #000000; font-weight: bold;">final</span> <span style="color: #003399;">String</span> key<span style="color: #339933;">;</span>
            key <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span><span style="color: #009900;">&#41;</span> keyIt.<span style="color: #006633;">next</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>    <span style="color: #666666; font-style: italic;">// assume string key</span>
            trans.<span style="color: #006633;">setParameter</span><span style="color: #009900;">&#40;</span>key, parameters.<span style="color: #006633;">get</span><span style="color: #009900;">&#40;</span>key<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
        trans.<span style="color: #006633;">transform</span><span style="color: #009900;">&#40;</span>xmlSource, result<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000000; font-weight: bold;">return</span> byteArray.<span style="color: #006633;">toString</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #008000; font-style: italic; font-weight: bold;">/**
     * Example for retrieving the APAS institutions list
     *
     * @param xml the string representation of the XML
     *
     * @return the Document object created from the XML string representation
     *
     * @throws IOException  if an I/O error occurs
     * @throws SAXException if an XML parsing exception occurs.
     */</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #003399;">Document</span> stringToDocument<span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">final</span> <span style="color: #003399;">String</span> xml<span style="color: #009900;">&#41;</span>
        <span style="color: #000000; font-weight: bold;">throws</span> SAXException, <span style="color: #003399;">IOException</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">return</span> loadXMLFrom<span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">ByteArrayInputStream</span><span style="color: #009900;">&#40;</span>xml.<span style="color: #006633;">getBytes</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #003399;">Document</span> loadXMLFrom<span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">final</span> <span style="color: #003399;">InputStream</span> is<span style="color: #009900;">&#41;</span>
        <span style="color: #000000; font-weight: bold;">throws</span> SAXException, <span style="color: #003399;">IOException</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">final</span> DocumentBuilderFactory factory <span style="color: #339933;">=</span>
            DocumentBuilderFactory.<span style="color: #006633;">newInstance</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        factory.<span style="color: #006633;">setNamespaceAware</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        DocumentBuilder builder <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #339933;">;</span>
        <span style="color: #000000; font-weight: bold;">try</span>
        <span style="color: #009900;">&#123;</span>
            builder <span style="color: #339933;">=</span> factory.<span style="color: #006633;">newDocumentBuilder</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
        <span style="color: #000000; font-weight: bold;">catch</span> <span style="color: #009900;">&#40;</span>ParserConfigurationException ignored<span style="color: #009900;">&#41;</span>
        <span style="color: #009900;">&#123;</span>
            <span style="color: #666666; font-style: italic;">// throw something here if you like.</span>
        <span style="color: #009900;">&#125;</span>
        <span style="color: #000000; font-weight: bold;">assert</span> builder <span style="color: #339933;">!=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #339933;">;</span>
        <span style="color: #000000; font-weight: bold;">final</span> <span style="color: #003399;">Document</span> doc <span style="color: #339933;">=</span> builder.<span style="color: #006633;">parse</span><span style="color: #009900;">&#40;</span>is<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        is.<span style="color: #006633;">close</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000000; font-weight: bold;">return</span> doc<span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span></pre></div></div>

<p>Example usage is below.  Take note how I pass in null for the XSL, as the XSL can be loaded from the "xml-stylesheet" processing instruction in the XML document.  The utility method above requests resolution of the stylesheet from the XML, if the "xsl" parameter is null.</p>
<pre>    System.out.println(Util.xalanTransformation(xml, null, new HashMap()));</pre>
]]></content:encoded>
			<wfw:commentRss>http://blog.adamsbros.org/2009/08/12/java-xalan-jaxp-xml-transformations-from-java-string/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
