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.*;
19 import com.apple.cocoa.foundation.*;
22 * JAXPWrapper is a helper class to make it easier to access JAXP transformers
23 * from the Obj-C code.
25 * @author Marc Liyanage
28 public class JAXPWrapper {
30 ByteArrayOutputStream bos;
31 Exception transformException;
32 String errorMessage = "(no errors)";
36 public static final int XSLT_ERROR_SOURCE_XML = 1;
37 public static final int XSLT_ERROR_SOURCE_XSLT = 2;
39 public NSData getResult() {
40 return new NSData(bos.toByteArray());
44 * Does something really useful.
46 * Blah blah {@link AnotherClass} blah blah.
48 * @param arg1 the first parameter
49 * @param arg2 the second parameter
50 * @return <code>true</code> if something,
51 * <code>false</code> otherwise.
53 public boolean transform(String processorClassName, NSData xml, NSData xslt, String parameters, String baseUri) {
55 java.lang.System.setProperty("javax.xml.transform.TransformerFactory", processorClassName);
57 System.err.println("java reached: xml: " + xml + " xsl: " + xslt + " param: " + parameters + " baseuri: " + baseUri);
61 TransformerFactory tf = TransformerFactory.newInstance();
62 Transformer t = tf.newTransformer(new StreamSource(new ByteArrayInputStream(xslt.bytes(0, xslt.length())), baseUri));
65 if (parameters.length() > 0) {
67 * evil in-band separator tricks...
69 Pattern pairDelimiter = Pattern.compile("--_-!-_--");
70 Pattern keyValueDelimiter = Pattern.compile("==_=!=_==");
72 String [] pairs = pairDelimiter.split(parameters);
74 for (int i = 0; i < pairs.length; i++) {
75 System.err.println("after1");
76 String [] keyValue = keyValueDelimiter.split(pairs[i]);
78 if (keyValue.length > 0) {
79 System.err.println("after2");
80 String key = keyValue[0];
83 if (keyValue.length > 1) {
89 t.setParameter(key, value);
94 bos = new ByteArrayOutputStream();
95 t.transform(new StreamSource(new ByteArrayInputStream(xml.bytes(0, xml.length()))), new StreamResult(bos));
97 } catch (TransformerConfigurationException e) {
99 errorSource = XSLT_ERROR_SOURCE_XSLT; /* That's the assumption anyway */
100 errorMessage = getDeepestException(e).getMessage();
101 errorLine = getLineNumber(getDeepestException(e));
102 transformException = e;
106 } catch (TransformerException e) {
108 errorSource = XSLT_ERROR_SOURCE_XML; /* That's the assumption anyway */
109 errorMessage = getDeepestException(e).getMessage();
110 errorLine = getLineNumber(getDeepestException(e));
111 transformException = e;
114 } catch (Exception e) {
116 System.err.println("Exception: " + e);
117 transformException = e;
118 errorMessage = e.getMessage();
127 protected static Throwable getDeepestException(Throwable t) {
131 while (t.getCause() != null) {
133 System.err.println("found nested throwable: " + t);
139 protected static int getLineNumber(Throwable t) {
141 if (t instanceof TransformerException) {
143 TransformerException te = (TransformerException)t;
144 SourceLocator sl = te.getLocator();
146 return sl.getLineNumber();
150 } else if (t instanceof SAXParseException) {
151 SAXParseException spe = (SAXParseException)t;
152 return spe.getLineNumber();
161 public String getErrorMessage() {
165 public int getErrorLine() {
169 public int getErrorSource() {
176 * Main method for command-line invocation.
178 * @param argv the argument String array
180 public static void main (String argv[]) {
182 JAXPWrapper ref = new JAXPWrapper();
183 System.out.println("ref: " + ref);
184 String processorImpl = "com.icl.saxon.TransformerFactoryImpl";
186 System.out.println("\nParam Test\n");
187 String xml = "<?xml version='1.0'?>\n\n\n\n<root/>";
188 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>";
189 String parameters = "param1==_=!=_==right1--_-!-_--param2==_=!=_==right2";
190 ref.transform(processorImpl, new NSData(xml.getBytes()), new NSData(xslt.getBytes()), parameters, null);
191 System.out.println(ref.getResult());
192 System.out.println("source: " + ref.getErrorSource() + " line: " + ref.getErrorLine() + " msg: " + ref.getErrorMessage());
194 System.out.println("\nEmpty param test\n");
195 xml = "<?xml version='1.0'?>\n\n\n\n<root/>";
196 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>";
198 ref.transform(processorImpl, new NSData(xml.getBytes()), new NSData(xslt.getBytes()), parameters, null);
199 System.out.println(ref.getResult());
200 System.out.println("source: " + ref.getErrorSource() + " line: " + ref.getErrorLine() + " msg: " + ref.getErrorMessage());
202 System.out.println("\nsystem-property() test\n");
203 xml = "<?xml version='1.0'?>\n\n\n<root/>";
204 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>";
205 ref.transform(processorImpl, new NSData(xml.getBytes()), new NSData(xslt.getBytes()), parameters, null);
206 System.out.println(ref.getResult());
207 System.out.println("source: " + ref.getErrorSource() + " line: " + ref.getErrorLine() + " msg: " + ref.getErrorMessage());
209 System.out.println("\nBroken XML Test\n");
210 xml = "<?xml version='1.0'?>\n\n\n<root>\n\n\n</xyz>";
211 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>";
212 ref.transform(processorImpl, new NSData(xml.getBytes()), new NSData(xslt.getBytes()), parameters, null);
213 System.out.println("source: " + ref.getErrorSource() + " line: " + ref.getErrorLine() + " msg: " + ref.getErrorMessage());
215 System.out.println("\nBroken XSLT Test\n");
216 xml = "<?xml version='1.0'?>\n<root/>";
217 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>";
218 ref.transform(processorImpl, new NSData(xml.getBytes()), new NSData(xslt.getBytes()), parameters, null);
219 System.out.println("source: " + ref.getErrorSource() + " line: " + ref.getErrorLine() + " msg: " + ref.getErrorMessage());
221 System.out.println("\nBroken XML and XSLT Test\n");
222 xml = "<?xml version='1.0'?>\n<root></xyz>";
223 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>";
224 ref.transform(processorImpl, new NSData(xml.getBytes()), new NSData(xslt.getBytes()), parameters, null);
225 System.out.println("source: " + ref.getErrorSource() + " line: " + ref.getErrorLine() + " msg: " + ref.getErrorMessage());
227 System.out.println("\nBroken XML and XSLT Test 2\n");
228 xml = "<?xml version='1.0'?>\n<root></xyz>";
229 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>";
230 ref.transform(processorImpl, new NSData(xml.getBytes()), new NSData(xslt.getBytes()), parameters, null);
231 System.out.println("source: " + ref.getErrorSource() + " line: " + ref.getErrorLine() + " msg: " + ref.getErrorMessage());