// // 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