Views

API Sample Code

These are the XAware components, test files, java files, and scripts used in the Java API samples.

Contents

HellowWorld.xbd

<?xml version="1.0" encoding="UTF-8"?>
<HelloWorld xmlns:xa="http://xaware.org/xas/ns1" xa:version="5.0"
  xa:on_error="xa-doc::/HelloWorld/Error" xa:visible="yes">
   <xa:description />
   <xa:input>
       <xa:param xa:name="from" xa:datatype="string" xa:default="anonymous"
         xa:description="" />
   </xa:input>
   <From>%from%</From>
   <Hello xa:bizcomp="XAFiles/hello.xbc" xa:remove="yes"
     xa:input="xainput::/ContactList/Contact" />
   <Error xa:include="no">
       <detail>$xavar:error_stack$</detail>
   </Error>
</HelloWorld>


hello.xbc

<?xml version="1.0" encoding="UTF-8"?>
<XMLMapper xmlns:xa="http://xaware.org/xas/ns1" xa:bizcomptype="XMLMapper" 
  xa:response_type="Hello">
   <xa:input />
   <xa:description />
   <xa:request />
   <xa:response>
       <Hello>%xa-input::/Contact/FirstName%</Hello>
   </xa:response>
</XMLMapper>


test.xml

<ContactList>
   <Contact>
      <FirstName>John</FirstName>
      <LastName>Smith</LastName>
   </Contact>
   <Contact>
      <FirstName>Jane</FirstName>
      <LastName>Doe</LastName>
   </Contact>
</ContactList>


SayHello.java

package com.xaware.test;

import org.xaware.api.BizViewResponse;
import org.xaware.api.XABizView;
import org.xaware.shared.util.XAwareException;

public class SayHello {
  /**
   * Example using the XAware API High Level Interface. All BizDoc execution
   * options are provided in a single method.
   * 
   * This example demonstrates passing inputXML as a string, with no input
   * parameters; and returning the result XML as a string. The XML is displayed.
   * 
   * @param args
   */
   public static void main(String[] args) {
       String sBizViewName = "XAFiles/HelloWorld.xbd";
       String sInputXML = "<ContactList><Contact><FirstName>Mike</FirstName> 
                 <LastName>Brown</LastName></Contact></ContactList>";
       String sRscPath = "";

       System.out.println("---------- SayHello ----------");

       if (args.length > 0) {
           sRscPath = args[0];
       } else {
           System.out.println("You must provide the .xar resource path for this example.");
           return;
       }

   try {
       // initialize the XAware API and engine
       XABizView.initialize(XABizView.MODE_BATCH);

       // invoke the BizDoc with input XML as a String and no input
       // parameters, returning the result as a Response, and
       // displaying the XML as pretty-printed String
       BizViewResponse response = (BizViewResponse) XABizView.loadAndExecute(
           sBizViewName, sInputXML, "", XABizView.RESULT_TYPE_RESPONSE,
           null, null, sRscPath);
       String output = response.getResultingDocumentAsStringPrettyFormat();

       System.out.println("XML RESULT:");
       System.out.println(output);

       } catch (XAwareException e) {
           System.out.println("XAware Exception: " + e.getMessage());
       } catch (Exception e) {
           System.out.println("Exception: " + e.getMessage());
       }
   }
}

SayHello2.java

package com.xaware.test;

import java.util.HashMap;
import java.util.Iterator;

import org.jdom.Document;
import org.jdom.Element;
import org.xaware.api.BizViewRequestOptions;
import org.xaware.api.IBizViewRequestOptions;
import org.xaware.api.XABizView;
import org.xaware.shared.util.XAwareException;

public class SayHello2 {
  /**
   * Example using the XAware API Detailed Level Interface.
   * BizDoc execution options are set as needed, and the options
   * are passed to the BizDoc execution.
   * 
   * This example demonstrates passing inputXML from a file, 
   * an input parameter; and returning the result XML as a JDOM
   * document.  The JDOM is traversed and the values are displayed.
   * 
   * @param args
   */
   public static void main(String[] args) {
       String sBizViewName = "XAFiles/HelloWorld.xbd";
       String sInputXMLFile = "data/test.xml";
       String sRscPath = "";

       System.out.println("---------- SayHello2 ----------");

       if (args.length >0) {
           sRscPath = args[0];
       }
       else {
           System.out.println("You must provide the .xar resource path for this example.");
       return;
       }

       try {
           // instantiate and initialize the XAware API and engine
           XABizView invoker = new XABizView(XABizView.MODE_BATCH);

           // instantiate an options object and set each option
           IBizViewRequestOptions options = new BizViewRequestOptions();
           options.setBizViewName(sBizViewName);
           options.setResourcePath(sRscPath);
           options.setInputXmlResourceName(sInputXMLFile);

           // set input parameters (can also be set as an XML string)
           if (args.length >1) {
               HashMap<String,Object> params = new HashMap<String,Object>(2);
               params.put( "from", args[1] );
               options.setInputParams( params );
           }

           // invoke the BizDoc, returning the result as a JDOM object
           Document doc = (Document)invoker.executeBizView( 
           options,XABizView.RESULT_TYPE_DOCUMENT);

           // traverse the document and print the element results
           Iterator iter = doc.getRootElement().getChildren().iterator();
           while (iter.hasNext()) {
               Element elem = (Element)iter.next();
           	String elemName = elem.getName();
           	if (elemName.compareTo("Error")==0) {
                   System.out.println(elem.getChildText("detail"));
           	}
           	else if (elemName.compareTo("From")==0) {
                   System.out.println("Hello from: " +elem.getText());
           	}
           	else if (elemName.compareTo("Hello")==0) {
                   System.out.println("Hi: " +elem.getText());
           	}
           }
       } catch (XAwareException e) {
           System.out.println("XAware Exception: " + e.getMessage());
       } catch (Exception e) {
           System.out.println("Exception: " + e.getMessage());
       }
   }
}


SayHello.cmd

@echo off
REM set environment to use this installation of XAware
set XAWARE_HOME=C:\xaware5-938

IF NOT "%JAVA_HOME%" == "" GOTO SETCP
set JAVA_HOME=%XAWARE_HOME%\jdk150

:SETCP
call "%XAWARE_HOME%"\client\setClientClasspath.cmd
set CLIENT_CLASSPATH=%CLIENT_CLASSPATH%;"xa-apitest.jar"

:SHOWENV
@echo XAWARE_HOME=%XAWARE_HOME%
@echo JAVA_HOME=%JAVA_HOME%
REM @echo CP=%CLIENT_CLASSPATH%

@echo off
REM set classpath and execute java program

"%JAVA_HOME%\bin\java" -mx1400M -classpath %CLIENT_CLASSPATH%
   -Dxaware.home=%XAWARE_HOME% com.xaware.test.SayHello %1

"%JAVA_HOME%\bin\java" -mx1400M -classpath %CLIENT_CLASSPATH%
   -Dxaware.home=%XAWARE_HOME% com.xaware.test.SayHello2 %1 %2


build.xml

<?xml version="1.0" encoding="UTF-8"?>
<project name="XAAPITest" default="" basedir="C:\QATest">
   <description>Build script for XAware API example project</description>
   <property name="xawarehome.dir" value="C:\xaware5-938"/>
   <property name="compile.outdir" value="${ant.project.name}/buildclasses"/>
   <property name="deploy.dir" value="${ant.project.name}/deploy"/>
   <property name="javadoc.outdir" value="${ant.project.name}/javadoc"/>
   <property name="package.name" value="com.xaware.test"/>
   <path id="classpath">
       <fileset dir="${xawarehome.dir}/client">
           <include name="**/*.jar"/>
       </fileset>
   </path>
   <path id="srcpath">
       <pathelement location="${ant.project.name}/src"/>
   </path>
   <target name="all" depends="build,jar"/>
   <target name="build" depends="compile"/>
   <target name="clean" description="delete local copy of compiled code">
       <delete dir="${compile.outdir}"/>
       <delete dir="${deploy.dir}"/>
   </target>
   <target name="compile">
       <mkdir dir="${compile.outdir}"/>
       <javac destdir="${compile.outdir}" debug="on" nowarn="yes">
           <classpath refid="classpath"/>
           <src refid="srcpath"/>
           <include name="**/**.java"/>
       </javac>
   </target>
   <target name="jar" depends="compile">
       <mkdir dir="${deploy.dir}"/>
       <jar destfile="${deploy.dir}/xa-apitest.jar" 
           basedir="${compile.outdir}" excludes="" includes=""/>
   </target>
</project>