Search code examples
javascriptjavaxsltrhinojaggery-js

How to process ServerSide XSLT using SOAP web service response from within Rhino?


I am trying to perform a ServerSide transform using the input of a SOAP web service response and file based XSLT from within Rhino. I have very little (but growing) JAVA / JS experience. Looking for some pointers on how to convert the sample I have found from using a file based xml input to the w/s SOAP stream.

My constraints:

  1. Transformation must be run ServerSide and not at the client
  2. must be done within Rhino
  3. Incoming XML is contained within a Web Service (SOAP) response that I am successfully getting into my JS
  4. XSLT is within a local file

I have been able to track down the following sources / pointer but cannot determine how to get the xml soap input from my server versus the file as in the example.

http://simonkissane.blogspot.ca/2013/06/xml-pretty-printing-and-xslt-processing.html

My JS is pretty simple, note that the portion getting the w/s response is using Jaggeryjs.org, which is simply Rhino

My soap response works, print(getBeerList()) returns the xml I expect.

I fail on: var dsDoc = new javax.xml.transform.dom.DOMSource(doc); within the function applyXSLT with the following error:

ERROR {org.jaggeryjs.scriptengine.engine.RhinoEngine} - org.mozilla.javascript.JavaScriptException: InternalError: Java constructor for "javax.xml.transform.dom.DOMSource" with arguments "string" not found. (/TestJaggery_1//index.jag#90) (/TestJaggery_1//index.jag#108)

JS Code:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
     "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    //stuff removed
</head>

<body>
        <!-- Le javascript
        ================================================== -->
        <!-- Placed at the end of the document so the pages load faster -->
        <script src="js/jquery.min.js"></script>
        <script src="js/bootstrap.js"></script>
        <script src="js/util.js"></script>
</body>

<%

function getBeerList() {
        var log = new Log();
        var ws = require('ws');

        var mylist = new ws.WSRequest();
        var options = new Array();
        options.useSOAP = 1.2;
        options.useWSA = 1.0;
        options.action = "getAll";
        var payload = null;
        var result;

        try {
            mylist.open(options, "http://192.168.1.204:9764/services/TestSheet/", false);
            mylist.send(payload);
            result = mylist.responseText;
        } catch (e)
        {
            log.error(e.toString());
            print(mylist.error)
            return e.toString();
        }
        return result;
    }

    print(getBeerList()); 

xmlToString = function(doc) {
   var domreg = org.w3c.dom.bootstrap.DOMImplementationRegistry.newInstance();
   var ls = domreg.getDOMImplementation("LS");
   var w = ls.createLSSerializer();
   w.getDomConfig().setParameter("format-pretty-print", true);
   var lsout = ls.createLSOutput();
   var out = new java.io.StringWriter();
   lsout.setCharacterStream(out);
   w.write(doc, lsout);
   out.close();
   return "" + out.toString();
};

getDocBuilder = function() {
   var dbf = javax.xml.parsers.DocumentBuilderFactory.newInstance();
   dbf.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);
   dbf.setNamespaceAware(true);
   return dbf.newDocumentBuilder();
};

loadXMLFile = function(file) {
   var inp = new java.io.File(file);
   return getDocBuilder().parse(inp);
};

applyXSLT = function(doc, xslt) {
   //the following line is failing
   var dsDoc = new javax.xml.transform.dom.DOMSource(doc);
   var dsXSLT = new javax.xml.transform.dom  .DOMSource(xslt);
   var tf = javax.xml.transform.TransformerFactory.newInstance();
   var xf = tf.newTransformer(dsXSLT);
   var out = getDocBuilder().newDocument();
   var drOut = new javax.xml.transform.dom.DOMResult(out);
   xf.transform(dsDoc, drOut);
   return out;
};

try {
   //doc is where I am unsure of how to insert my SOAP response 'getBeerList()'
   var doc = getBeerList();
   var xslt = loadXMLFile(PathtoMyLocalFile);
   var out = applyXSLT(doc, xslt);
   println(xmlToString(out));
} catch (e) {
   if (e.javaException)
      e.javaException.printStackTrace();
   throw e;
}   

%>

</html>

Solution

  • Instead of creating a DOMSource from your string with the XML I would suggest to create a StreamSource https://docs.oracle.com/javase/7/docs/api/javax/xml/transform/stream/StreamSource.html over a StringReader over you string variable. You can then pass that StreamSource to the Transformer.