Question

I am working on a fork of Oryx Editor. In oryxRoot\editor\server\src\org\oryxeditor\server I added a Java servlet in which I try to apply an XSL file to a stream representing an XML document. The code is below:

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  //get AppML from POST
  InputStream inputStream = new ByteArrayInputStream(request.getParameter("content").getBytes("UTF-8"));
  inputStream.reset();
  try {  
      TransformerFactory tFactory = TransformerFactory.newInstance();
      Transformer transformer = tFactory.newTransformer(new StreamSource("lib/SorinCode.xsl"));
      transformer.transform(new StreamSource(inputStream), new StreamResult(new FileOutputStream("output.txt")));
      System.out.println("************* The result is in output.txt *************");
  } catch (Throwable t) {
      t.printStackTrace();
  }
}

The problem is that new StreamSource("lib/SorinCode.xsl") expects to find the XSL file in tomcat/bin/lib, and not in tomcat/webapp/oryx/lib as I would expect. I tried to change oryxRoot/editor/etc/context.xml and add baseDoc="oryx", but this didn't help.

Can someone please tell me why the app is looking for the XSL file in the bin folder and what should I do to make it look in webapps/oryx?

Was it helpful?

Solution

Use the InputStream constructor of StreamSource:

...new StreamSource(request.getServletContext().getResourceAsStream("lib/SorinCode.xsl"))

...if lib/SorinCode.xsl is in the web content. If it is in the classpath, use instead:

...new StreamSource(getClass().getResourceAsStream("lib/SorinCode.xsl"))

OTHER TIPS

The cwd (current working directory) is typically based on the path where a program is started from. That is, the Tomcat program, not your webapp. The cwd can be quite unpredictable. It may just be the bin directory because your shell was in that directory when you started Tomcat.

To get the path where the web app is deployed use request.getServletContext().getRealPath("/").

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top