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