moved saxon and xalan to libs subdir
[TestXSLT.git] / JAXPWrapper.java
1 //
2 //  JAXPWrapper.java
3 //  TestXSLT
4 //
5 //  Created by Marc Liyanage on Mon Aug 04 2003.
6 //  Copyright (c) 2003 __MyCompanyName__. All rights reserved.
7 //
8
9 //package ch.entropy.testxslt;
10
11 import java.io.*;
12 import java.util.*;
13 import java.util.regex.*;
14 import javax.xml.transform.*;
15 import javax.xml.transform.stream.*;
16 import javax.xml.*;
17 import javax.xml.parsers.*;
18 import org.xml.sax.*;
19 import com.apple.cocoa.foundation.*;
20
21 /**
22  * JAXPWrapper is a helper class to make it easier to access JAXP transformers
23  * from the Obj-C code.
24  * 
25  * @author    Marc Liyanage
26  * @version   $Id$
27  */
28 public class JAXPWrapper {
29         
30         ByteArrayOutputStream bos;
31         Exception transformException;
32         String errorMessage = "(no errors)";
33         int errorSource = 0;
34         int errorLine = 0;
35
36         public static final int XSLT_ERROR_SOURCE_XML = 1;
37         public static final int XSLT_ERROR_SOURCE_XSLT = 2;
38
39         public NSData getResult() {
40                 return new NSData(bos.toByteArray());
41         }
42         
43         /** 
44         * Does something really useful.
45          * <p>
46          * Blah blah {@link AnotherClass} blah blah.
47          *
48          * @param arg1    the first parameter
49          * @param arg2    the second parameter
50          * @return        <code>true</code> if something,  
51          *                <code>false</code> otherwise.
52          */
53         public boolean transform(String processorClassName, NSData xml, NSData xslt, String parameters, String baseUri) {
54
55                 java.lang.System.setProperty("javax.xml.transform.TransformerFactory", processorClassName);
56                 
57                 //System.err.println("java reached: xml: " + xml + " xsl: " + xslt + " param: " + parameters + " baseuri: " + baseUri);
58
59                 try {
60                         
61                         TransformerFactory tf = TransformerFactory.newInstance();
62                         Transformer t = tf.newTransformer(new StreamSource(new ByteArrayInputStream(xslt.bytes(0, xslt.length())), baseUri));
63
64                         
65                         if (parameters.length() > 0) {
66                                 /*
67                                  * evil in-band separator tricks...
68                                  */
69                                 Pattern pairDelimiter = Pattern.compile("--_-!-_--");
70                                 Pattern keyValueDelimiter = Pattern.compile("==_=!=_==");
71                                 
72                                 String [] pairs = pairDelimiter.split(parameters);
73                                 
74                                 for (int i = 0; i < pairs.length; i++) {
75                                         System.err.println("after1");
76                                         String [] keyValue = keyValueDelimiter.split(pairs[i]);
77                                         
78                                         if (keyValue.length > 0) {
79                                                 System.err.println("after2");
80                                                 String key = keyValue[0];
81                                                 String value;
82                                                 
83                                                 if (keyValue.length > 1) {
84                                                         value = keyValue[1];
85                                                 } else {
86                                                         value = "";
87                                                 }
88                                                 
89                                                 t.setParameter(key, value);
90                                         }
91                                 }
92                         }
93                         
94                         bos = new ByteArrayOutputStream();
95                         t.transform(new StreamSource(new ByteArrayInputStream(xml.bytes(0, xml.length()))), new StreamResult(bos));
96
97                 } catch (TransformerConfigurationException e) {
98                         
99                         errorSource = XSLT_ERROR_SOURCE_XSLT; /* That's the assumption anyway */
100                         errorMessage = getDeepestException(e).getMessage();
101                         errorLine = getLineNumber(getDeepestException(e));
102                         transformException = e;
103                         
104                         return false;
105                         
106                 } catch (TransformerException e) {
107                         
108                         errorSource = XSLT_ERROR_SOURCE_XML; /* That's the assumption anyway */
109                         errorMessage = getDeepestException(e).getMessage();
110                         errorLine = getLineNumber(getDeepestException(e));
111                         transformException = e;
112                         return false;
113                         
114                 } catch (Exception e) {
115
116                         System.err.println("Exception: " + e);
117                         transformException = e;
118                         errorMessage = e.getMessage();
119                         return false;
120                 }
121                 
122                 return true;
123         }
124
125         
126         
127         protected static Throwable getDeepestException(Throwable t) {
128                 
129                 Throwable next = t;
130                 
131                 while (t.getCause() != null) {
132                         t = t.getCause();
133                         System.err.println("found nested throwable: " + t);
134                 }
135                 
136                 return t;
137         }
138         
139         protected static int getLineNumber(Throwable t) {
140                 
141                 if (t instanceof TransformerException) {
142                         
143                         TransformerException te = (TransformerException)t;
144                         SourceLocator sl = te.getLocator();
145                         if (sl != null) {
146                                 return sl.getLineNumber();
147                         }
148                         
149                         
150                 } else if (t instanceof SAXParseException) {
151                         SAXParseException spe = (SAXParseException)t;
152                         return spe.getLineNumber();
153                 }
154                 
155                 return 0;
156                 
157         }
158         
159         
160
161         public String getErrorMessage() {
162                 return errorMessage;
163         }
164         
165         public int getErrorLine() {
166                 return errorLine;
167         }
168         
169         public int getErrorSource() {
170                 return errorSource;
171         }
172         
173                 
174         
175         /** 
176          * Main method for command-line invocation.
177          *
178          * @param argv    the argument String array
179          */
180         public static void main (String argv[]) {
181                 
182                 JAXPWrapper ref = new JAXPWrapper();
183                 System.out.println("ref: " + ref);
184                 String processorImpl = "com.icl.saxon.TransformerFactoryImpl";
185
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());
193
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>";
197                 parameters = "";
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());
201                 
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());
208                 
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());
214                 
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());
220                 
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());
226                 
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());
232
233                 
234                 
235         }
236         
237 }