// // JAXPWrapper.java // TestXSLT // // Created by Marc Liyanage on Mon Aug 04 2003. // Copyright (c) 2003 __MyCompanyName__. All rights reserved. // //package ch.entropy.testxslt; import java.io.*; import java.util.*; import java.util.regex.*; import javax.xml.transform.*; import javax.xml.transform.stream.*; import javax.xml.*; import javax.xml.parsers.*; import org.xml.sax.*; import com.apple.cocoa.foundation.*; /** * JAXPWrapper is a helper class to make it easier to access JAXP transformers * from the Obj-C code. * * @author Marc Liyanage * @version $Id$ */ public class JAXPWrapper { ByteArrayOutputStream bos; Exception transformException; String errorMessage = "(no errors)"; int errorSource = 0; int errorLine = 0; public static final int XSLT_ERROR_SOURCE_XML = 1; public static final int XSLT_ERROR_SOURCE_XSLT = 2; public NSData getResult() { return new NSData(bos.toByteArray()); } /** * Does something really useful. *

* Blah blah {@link AnotherClass} blah blah. * * @param arg1 the first parameter * @param arg2 the second parameter * @return true if something, * false otherwise. */ public boolean transform(String processorClassName, NSData xml, NSData xslt, String parameters, String baseUri) { java.lang.System.setProperty("javax.xml.transform.TransformerFactory", processorClassName); //System.err.println("java reached: xml: " + xml + " xsl: " + xslt + " param: " + parameters + " baseuri: " + baseUri); try { TransformerFactory tf = TransformerFactory.newInstance(); Transformer t = tf.newTransformer(new StreamSource(new ByteArrayInputStream(xslt.bytes(0, xslt.length())), baseUri)); if (parameters.length() > 0) { /* * evil in-band separator tricks... */ Pattern pairDelimiter = Pattern.compile("--_-!-_--"); Pattern keyValueDelimiter = Pattern.compile("==_=!=_=="); String [] pairs = pairDelimiter.split(parameters); for (int i = 0; i < pairs.length; i++) { System.err.println("after1"); String [] keyValue = keyValueDelimiter.split(pairs[i]); if (keyValue.length > 0) { System.err.println("after2"); String key = keyValue[0]; String value; if (keyValue.length > 1) { value = keyValue[1]; } else { value = ""; } t.setParameter(key, value); } } } bos = new ByteArrayOutputStream(); t.transform(new StreamSource(new ByteArrayInputStream(xml.bytes(0, xml.length()))), new StreamResult(bos)); } catch (TransformerConfigurationException e) { errorSource = XSLT_ERROR_SOURCE_XSLT; /* That's the assumption anyway */ errorMessage = getDeepestException(e).getMessage(); errorLine = getLineNumber(getDeepestException(e)); transformException = e; return false; } catch (TransformerException e) { errorSource = XSLT_ERROR_SOURCE_XML; /* That's the assumption anyway */ errorMessage = getDeepestException(e).getMessage(); errorLine = getLineNumber(getDeepestException(e)); transformException = e; return false; } catch (Exception e) { System.err.println("Exception: " + e); transformException = e; errorMessage = e.getMessage(); return false; } return true; } protected static Throwable getDeepestException(Throwable t) { Throwable next = t; while (t.getCause() != null) { t = t.getCause(); System.err.println("found nested throwable: " + t); } return t; } protected static int getLineNumber(Throwable t) { if (t instanceof TransformerException) { TransformerException te = (TransformerException)t; SourceLocator sl = te.getLocator(); if (sl != null) { return sl.getLineNumber(); } } else if (t instanceof SAXParseException) { SAXParseException spe = (SAXParseException)t; return spe.getLineNumber(); } return 0; } public String getErrorMessage() { return errorMessage; } public int getErrorLine() { return errorLine; } public int getErrorSource() { return errorSource; } /** * Main method for command-line invocation. * * @param argv the argument String array */ public static void main (String argv[]) { JAXPWrapper ref = new JAXPWrapper(); System.out.println("ref: " + ref); String processorImpl = "com.icl.saxon.TransformerFactoryImpl"; System.out.println("\nParam Test\n"); String xml = "\n\n\n\n"; String xslt = "\n----"; String parameters = "param1==_=!=_==right1--_-!-_--param2==_=!=_==right2"; ref.transform(processorImpl, new NSData(xml.getBytes()), new NSData(xslt.getBytes()), parameters, null); System.out.println(ref.getResult()); System.out.println("source: " + ref.getErrorSource() + " line: " + ref.getErrorLine() + " msg: " + ref.getErrorMessage()); System.out.println("\nEmpty param test\n"); xml = "\n\n\n\n"; xslt = "\n----"; parameters = ""; ref.transform(processorImpl, new NSData(xml.getBytes()), new NSData(xslt.getBytes()), parameters, null); System.out.println(ref.getResult()); System.out.println("source: " + ref.getErrorSource() + " line: " + ref.getErrorLine() + " msg: " + ref.getErrorMessage()); System.out.println("\nsystem-property() test\n"); xml = "\n\n\n"; xslt = "\nvendor: "; ref.transform(processorImpl, new NSData(xml.getBytes()), new NSData(xslt.getBytes()), parameters, null); System.out.println(ref.getResult()); System.out.println("source: " + ref.getErrorSource() + " line: " + ref.getErrorLine() + " msg: " + ref.getErrorMessage()); System.out.println("\nBroken XML Test\n"); xml = "\n\n\n\n\n\n"; xslt = "\nmatch!"; ref.transform(processorImpl, new NSData(xml.getBytes()), new NSData(xslt.getBytes()), parameters, null); System.out.println("source: " + ref.getErrorSource() + " line: " + ref.getErrorLine() + " msg: " + ref.getErrorMessage()); System.out.println("\nBroken XSLT Test\n"); xml = "\n"; xslt = "\nmatch!"; ref.transform(processorImpl, new NSData(xml.getBytes()), new NSData(xslt.getBytes()), parameters, null); System.out.println("source: " + ref.getErrorSource() + " line: " + ref.getErrorLine() + " msg: " + ref.getErrorMessage()); System.out.println("\nBroken XML and XSLT Test\n"); xml = "\n"; xslt = "\nmatch!"; ref.transform(processorImpl, new NSData(xml.getBytes()), new NSData(xslt.getBytes()), parameters, null); System.out.println("source: " + ref.getErrorSource() + " line: " + ref.getErrorLine() + " msg: " + ref.getErrorMessage()); System.out.println("\nBroken XML and XSLT Test 2\n"); xml = "\n"; xslt = "\n\n\n\n\n\n\n\nmatch!\n\n\n"; ref.transform(processorImpl, new NSData(xml.getBytes()), new NSData(xslt.getBytes()), parameters, null); System.out.println("source: " + ref.getErrorSource() + " line: " + ref.getErrorLine() + " msg: " + ref.getErrorMessage()); } }