5 // Created by Marc Liyanage on Mon Aug 04 2003.
6 // Copyright (c) 2003 __MyCompanyName__. All rights reserved.
9 //package ch.entropy.testxslt;
13 import java.util.regex.*;
14 import javax.xml.transform.*;
15 import javax.xml.transform.stream.*;
17 import javax.xml.parsers.*;
21 * JAXPWrapper is a helper class to make it easier to access JAXP transformers
22 * from the Obj-C code.
24 * @author Marc Liyanage
27 public class JAXPWrapper {
29 StringWriter resultWriter;
30 Exception transformException;
31 String errorMessage = "(no errors)";
35 public static final int XSLT_ERROR_SOURCE_XML = 1;
36 public static final int XSLT_ERROR_SOURCE_XSLT = 2;
41 public String getResult() {
43 return resultWriter.toString();
48 * Does something really useful.
50 * Blah blah {@link AnotherClass} blah blah.
52 * @param arg1 the first parameter
53 * @param arg2 the second parameter
54 * @return <code>true</code> if something,
55 * <code>false</code> otherwise.
57 public boolean transform(String processorClassName, String xml, String xslt, String parameters, String baseUri) {
59 java.lang.System.setProperty("javax.xml.transform.TransformerFactory", processorClassName);
61 System.err.println("java reached: xml: " + xml + " xsl: " + xslt + " param: " + parameters + " baseuri: " + baseUri);
65 TransformerFactory tf = TransformerFactory.newInstance();
66 Transformer t = tf.newTransformer(new StreamSource(new StringReader(xslt), baseUri));
69 if (parameters.length() > 0) {
71 * evil in-band separator tricks...
73 Pattern pairDelimiter = Pattern.compile("--_-!-_--");
74 Pattern keyValueDelimiter = Pattern.compile("==_=!=_==");
76 String [] pairs = pairDelimiter.split(parameters);
78 for (int i = 0; i < pairs.length; i++) {
79 System.err.println("after1");
80 String [] keyValue = keyValueDelimiter.split(pairs[i]);
82 if (keyValue.length > 0) {
83 System.err.println("after2");
84 String key = keyValue[0];
87 if (keyValue.length > 1) {
93 t.setParameter(key, value);
98 resultWriter = new StringWriter();
99 t.transform(new StreamSource(new StringReader(xml)), new StreamResult(resultWriter));
101 } catch (TransformerConfigurationException e) {
103 errorSource = XSLT_ERROR_SOURCE_XSLT; /* That's the assumption anyway */
104 errorMessage = getDeepestException(e).getMessage();
105 errorLine = getLineNumber(getDeepestException(e));
106 transformException = e;
110 } catch (TransformerException e) {
112 errorSource = XSLT_ERROR_SOURCE_XML; /* That's the assumption anyway */
113 errorMessage = getDeepestException(e).getMessage();
114 errorLine = getLineNumber(getDeepestException(e));
115 transformException = e;
118 } catch (Exception e) {
120 System.err.println("Exception: " + e);
121 transformException = e;
122 errorMessage = e.getMessage();
131 protected static Throwable getDeepestException(Throwable t) {
135 while (t.getCause() != null) {
137 System.err.println("found nested throwable: " + t);
143 protected static int getLineNumber(Throwable t) {
145 if (t instanceof TransformerException) {
147 TransformerException te = (TransformerException)t;
148 SourceLocator sl = te.getLocator();
150 return sl.getLineNumber();
154 } else if (t instanceof SAXParseException) {
155 SAXParseException spe = (SAXParseException)t;
156 return spe.getLineNumber();
165 public String getErrorMessage() {
169 public int getErrorLine() {
173 public int getErrorSource() {
180 * Main method for command-line invocation.
182 * @param argv the argument String array
184 public static void main (String argv[]) {
186 JAXPWrapper ref = new JAXPWrapper();
187 System.out.println("ref: " + ref);
188 String processorImpl = "com.icl.saxon.TransformerFactoryImpl";
190 System.out.println("\nParam Test\n");
191 String xml = "<?xml version='1.0'?>\n\n\n\n<root/>";
192 String xslt = "<?xml version='1.0' encoding='iso-8859-1'?>\n<xsl:stylesheet version='1.0' xmlns:xsl='http://www.w3.org/1999/XSL/Transform'><xsl:param name='param1' select=\"'wrong1'\"/><xsl:param name='param2' select=\"'wrong2'\"/><xsl:template match='/'>-<xsl:value-of select='$param1'/>--<xsl:value-of select='$param2'/>-</xsl:template></xsl:stylesheet>";
193 String parameters = "param1==_=!=_==right1--_-!-_--param2==_=!=_==right2";
194 ref.transform(processorImpl, xml, xslt, parameters, null);
195 System.out.println(ref.getResult());
196 System.out.println("source: " + ref.getErrorSource() + " line: " + ref.getErrorLine() + " msg: " + ref.getErrorMessage());
198 System.out.println("\nEmpty param test\n");
199 xml = "<?xml version='1.0'?>\n\n\n\n<root/>";
200 xslt = "<?xml version='1.0' encoding='iso-8859-1'?>\n<xsl:stylesheet version='1.0' xmlns:xsl='http://www.w3.org/1999/XSL/Transform'><xsl:param name='param1' select=\"'wrong1'\"/><xsl:param name='param2' select=\"'wrong2'\"/><xsl:template match='/'>-<xsl:value-of select='$param1'/>--<xsl:value-of select='$param2'/>-</xsl:template></xsl:stylesheet>";
202 ref.transform(processorImpl, xml, xslt, parameters, null);
203 System.out.println(ref.getResult());
204 System.out.println("source: " + ref.getErrorSource() + " line: " + ref.getErrorLine() + " msg: " + ref.getErrorMessage());
206 System.out.println("\nsystem-property() test\n");
207 xml = "<?xml version='1.0'?>\n\n\n<root/>";
208 xslt = "<?xml version='1.0' encoding='iso-8859-1'?>\n<xsl:stylesheet version='1.0' xmlns:xsl='http://www.w3.org/1999/XSL/Transform'><xsl:template match='/'>vendor: <xsl:value-of select=\"system-property('xsl:vendor')\"/></xsl:template></xsl:stylesheet>";
209 ref.transform(processorImpl, xml, xslt, parameters, null);
210 System.out.println(ref.getResult());
211 System.out.println("source: " + ref.getErrorSource() + " line: " + ref.getErrorLine() + " msg: " + ref.getErrorMessage());
213 System.out.println("\nBroken XML Test\n");
214 xml = "<?xml version='1.0'?>\n\n\n<root>\n\n\n</xyz>";
215 xslt = "<?xml version='1.0' encoding='iso-8859-1'?>\n<xsl:stylesheet version='1.0' xmlns:xsl='http://www.w3.org/1999/XSL/Transform'><xsl:template match='/'>match!</xsl:template></xsl:stylesheet>";
216 ref.transform(processorImpl, xml, xslt, parameters, null);
217 System.out.println("source: " + ref.getErrorSource() + " line: " + ref.getErrorLine() + " msg: " + ref.getErrorMessage());
219 System.out.println("\nBroken XSLT Test\n");
220 xml = "<?xml version='1.0'?>\n<root/>";
221 xslt = "<?xml version='1.0' encoding='iso-8859-1'?>\n<xsl:stylesheet version='1.0' xmlns:xsl='http://www.w3.org/1999/XSL/Transform'><xsl:template match='/'>match!</xsl:template></xsl:xyz>";
222 ref.transform(processorImpl, xml, xslt, parameters, null);
223 System.out.println("source: " + ref.getErrorSource() + " line: " + ref.getErrorLine() + " msg: " + ref.getErrorMessage());
225 System.out.println("\nBroken XML and XSLT Test\n");
226 xml = "<?xml version='1.0'?>\n<root></xyz>";
227 xslt = "<?xml version='1.0' encoding='iso-8859-1'?>\n<xsl:stylesheet version='1.0' xmlns:xsl='http://www.w3.org/1999/XSL/Transform'><xsl:template match='/'>match!</xsl:template></xsl:xyz>";
228 ref.transform(processorImpl, xml, xslt, parameters, null);
229 System.out.println("source: " + ref.getErrorSource() + " line: " + ref.getErrorLine() + " msg: " + ref.getErrorMessage());
231 System.out.println("\nBroken XML and XSLT Test 2\n");
232 xml = "<?xml version='1.0'?>\n<root></xyz>";
233 xslt = "<?xml version='1.0' encoding='iso-8859-1'?>\n\n<xsl:stylesheet version='1.0' xmlns:xsl='http://www.w3.org/1999/XSL/Transform'>\n\n\n<xsl:value-of select='1'/>\n\n\n<xsl:template match='/'>match!</xsl:template>\n\n\n</xsl:stylesheet>";
234 ref.transform(processorImpl, xml, xslt, parameters, null);
235 System.out.println("source: " + ref.getErrorSource() + " line: " + ref.getErrorLine() + " msg: " + ref.getErrorMessage());