I’ve tried to create my own xslt transformer that’s more lightweight than the built in service and that is also automatically embed the stringtobytes/bytetostring function.
Everything went fine until I tried a XSL file with Java extension, like :
<xsl:stylesheet xmlns:xsl = “XSLT Namespace”
xmlns:xalan = “Xalan-Java version 1”
xmlns:Output = “java://com.wm.pkg.xslt.extension.Output”
exclude-result-prefixes = “xalan xsl Output” version = “1.0”>
And then somewhere in the XML (just an exemple from an actual file, it runs OK in the built in transform service):
<xsl:value-of select = “Output:put($output, ‘mail’, string($mail))”/>
I get the exception:
"
java.lang.NoSuchMethodException: For extension function, could not find method java.lang.String.put([ExpressionContext,] #STRING, #STRING).
"
This is the java code I made so far:
//get input var
IDataHashCursor idc = pipeline.getHashCursor();
idc.first( "stylesheetSystemId" );
String stylesheetSystemId = (String)idc.getValue();
idc.first( "document" );
String document = (String)idc.getValue();
idc.first( "encoding" );
String encoding = (String)idc.getValue();
idc.first( "xslParamInput" );
IData xslParamInput = (IData)idc.getValue();
IDataHashCursor idcParamInput = null;
if(xslParamInput != null)
{
idcParamInput = xslParamInput.getHashCursor();
}
try{
//transform input string into bytes and then into InputStream and then into Source
byte[] documentBytes = document.getBytes(encoding);
java.io.ByteArrayInputStream documentInputStream = new java.io.ByteArrayInputStream(documentBytes);
javax.xml.transform.stream.StreamSource documentSource = new javax.xml.transform.stream.StreamSource(documentInputStream);
//create the object that will hold the output
java.io.CharArrayWriter transformedDocWriter = new java.io.CharArrayWriter();
javax.xml.transform.stream.StreamResult transformedDocResult = new javax.xml.transform.stream.StreamResult(transformedDocWriter);
//create the XSL factory
javax.xml.transform.TransformerFactory fabrique = javax.xml.transform.TransformerFactory.newInstance();
//create the XSL template file
java.io.File stylesheet = new java.io.File(stylesheetSystemId);
javax.xml.transform.stream.StreamSource stylesource = new javax.xml.transform.stream.StreamSource(stylesheet);
javax.xml.transform.Templates template = fabrique.newTemplates(stylesource);
//get the transformer for this template
javax.xml.transform.Transformer transformer = template.newTransformer();
transformer.setOutputProperty(javax.xml.transform.OutputKeys.INDENT, "yes");
transformer.setOutputProperty(javax.xml.transform.OutputKeys.ENCODING, encoding);
//set input parameters (if applicable)
if(idcParamInput != null)
{
idcParamInput.first();
do
{
transformer.setParameter(idcParamInput.getKey(), idcParamInput.getValue());
}
while(idcParamInput.next());
//destory the cursor, now useless:
idcParamInput.destroy();
}
//transform
transformer.transform(documentSource, transformedDocResult);
//get output parameters (if any); create a webMethods document
//and set it in the pipeline
java.util.Properties p = transformer.getOutputProperties();
if(p!=null)
{
java.util.Enumeration keys = p.keys();
while (keys.hasMoreElements()) {
Object key = keys.nextElement();
//just for test we insert the key of the output object in the pipeline
idc.first();
idc.insertAfter("transformedDocument", (String)key);
}
}
//access the result
String transformedDocument = ((java.io.CharArrayWriter)transformedDocResult.getWriter()).toString();
//set it in the pipeline
idc.first();
idc.insertAfter("transformedDocument", transformedDocument);
}
catch(Exception e){
idc.first();
idc.insertAfter("output", e.getMessage());
}
//destroy the pipeline cursor
idc.destroy();
Does someone know what the cause is?
Thanks!