Jorge,
For example, there's a shipped java class to perform xsl => html that
can be run as an executable from the command line. In brief, an RPGLE to
launch it uses a prototype to execute the main method of the executable,
like -
* Class = Process; method = main; returns void
D Process...
D PR Extproc(*JAVA:
D 'org.apache.xalan.xslt.Process'
D :'main')
D Static
D #args Like(jString)
D Dim(7) Const Options(*varsize)
...... then a bit of code to assign command line parameters to java
string objects in the args() array
Process(Args);
Nothing difficult about this.
But if the xalan class encounters an error while processing the XSL
stylesheet, it throws an exception which is critical since it kills the
JVM, which is a bummer as the JVM will never ( never, ever ) start again
so the whole job must be terminated.
So a java wrapper that traps exceptions would help here.
Turns out there's another shipped class in the form of
javax.xml.transform that I figure is better so I create a wrapper for
that and name it Xml2Html. In brief, the RPGLE prototype to launch it
would be -
* Class = Xml2Html method = xsl; returns void
D xsl...
D PR Extproc(*JAVA:
D 'Xml2Html'
D :'xsl')
D Static
D xmlin Like(jString)
D htmput Like(jString)
D xslin Like(jString)
....
* Assign java string parameters
xmlfile = jStr(%trim(InpFilename));
xslfile = jStr(%trim(StyleSheet));
htmfile = jStr(%trim(OutFilename));
* Execute wrapper class
Monitor;
xsl(xmlfile:xslfile:htmfile);
On-error;
EndMon;
....
Now in my case I'm not using an extra parameter for a msg since I
confirm that all went well in the class by a subsequent check from RPG
to see if the response file is empty.
For the java wrapper I simply surf the web to find a simple example of
usage for javax.xml.transform.
I paste it into a file for NotePad and change the class name to my own
Xml2Html.
I add a little diagnostic output into the exception handlers for good
measure then save it as /home/mystuff/java/Xml2Html.java.
So the code for my wrapper ends up looking like the block pasted at the
bottom of this response.
Then I compile my wrapper class -
STRQSH
cd /home/mystuff/java
javac Xml2Html.java
One simple way to install the resulting Xml2Html.class file is to now
make it a jar file and run it as a java extension, so while still in
QShell -
jar -cf Xml2Html.jar Xml2Html.class
Then use WRKLNK to copy Xml2Html.jar into /QIBM/UserData/Java400/ext
or drag it via Windows Explorer.
Compile the RPGLE then run it.
Well, at least for me it was as easy as that.
Cheers, Peter
.
.
.
.
.
//----------------------------------------------------------------------
-----------
import java.io.*;
import org.w3c.dom.*;
import org.xml.sax.*;
import javax.xml.parsers.*;
import javax.xml.transform.*;
import javax.xml.transform.dom.*;
import javax.xml.transform.stream.*;
public class Xml2Html {
// This method applies the xslFilename to inFilename and writes
// the output to outFilename.
public static void xsl(String inFilename, String xslFilename,
String outFilename) {
try {
// Create transformer factory
TransformerFactory factory =
TransformerFactory.newInstance();
// Use the factory to create a template containing the
xsl file
Templates template = factory.newTemplates(new
StreamSource(
new FileInputStream(xslFilename)));
// Use the template to create a transformer
Transformer xformer = template.newTransformer();
// Prepare the input and output files
Source source = new StreamSource(new
FileInputStream(inFilename));
Result result = new StreamResult(new
FileOutputStream(outFilename));
// Apply the xsl file to the source file and write the
result to the output file
xformer.transform(source, result);
} catch (FileNotFoundException e) {
System.out.println("An error occurred resolving a
filename argument");
} catch (TransformerConfigurationException e) {
// An error occurred in the XSL file
System.out.println("An error occurred in the XSL file");
} catch (TransformerException e) {
// An error occurred while applying the XSL file
// Get location of error in input file
SourceLocator locator = e.getLocator();
int col = locator.getColumnNumber();
int line = locator.getLineNumber();
String publicId = locator.getPublicId();
String systemId = locator.getSystemId();
String [] wrd = null;
wrd = inFilename.split("/");
System.out.println("<!-- XSL error at line " + line + "
of " + wrd[wrd.length-1] + " -->");
}
}
}
//----------------------------------------------------------------------
-----------
-----Original Message-----
From: rpg400-l-bounces@xxxxxxxxxxxx
[mailto:rpg400-l-bounces@xxxxxxxxxxxx] On Behalf Of
jmoreno@xxxxxxxxxxxxxxxx
Sent: Friday, 7 August 2009 8:42 a.m.
To: RPG programming on the IBM i / System i
Subject: RE: RPG Handshake with java program and capture of
"exceptionJava error"
Peter,
Great tool/utility
Do you have an example that I could look at ?
Thanks
Jorge A Moreno
Military Car Sales, Inchibited and may be unlawful.
rpg400-l-bounces@xxxxxxxxxxxx wrote on 08/06/2009 03:58:47 PM:
Jorge,
I have found that the best way is to compile a very simple java class
that works just like a CL program you would write to trap exceptions
in
an RPG app. Basically just a try block with an exception block that
As an Amazon Associate we earn from qualifying purchases.