import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import org.apache.trax.Processor;
import org.apache.trax.Templates;
import org.apache.trax.Transformer;
import org.apache.trax.Result;
import org.xml.sax.InputSource;
import org.apache.xpath.objects.XString;
public class xml2htm extends HttpServlet implements SingleThreadModel {
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doIt(req, resp);
}
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doIt(req, resp);
}
private void doIt(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String xmlFileName = req.getParameter("XML");
String xslFileName = req.getParameter("XSL");
resp.setContentType("text/html; charset=UTF-8");
if (xmlFileName != null && xslFileName != null) {
try {
Processor p = Processor.newInstance("xslt");
Templates templates = p.process(new InputSource(xslFileName));
Transformer transformer = templates.newTransformer();
java.util.Enumeration e = req.getParameterNames();
while (e.hasMoreElements()) {
String s = (String)e.nextElement();
String[] paramVals = req.getParameterValues(s);
String param = parseString(paramVals.getBytes());
if (paramVals != null) {
transformer.setParameter(s, null, new XString(param);
}
}
transformer.transform(new InputSource(xmlFileName), new Result(resp.getOutputStream()));
} catch (Exception err) {
String mess = "<HTML><HEAD></HEAD><BODY><pre>"+err.getMessage()+"</pre></BODY></HTML>";
resp.sendError(HttpServletResponse.SC_OK, mess);
}
} else {
String mess = "<HTML><HEAD><TITLE>Error</TITLE></HEAD><BODY><H1>Error in using xml2htm</H1>XMLfile and XSLfile are both required to the servlet</BODY></HTML>";
resp.sendError(HttpServletResponse.SC_OK, mess);
}
}
public static String parseString(byte[] utf8buf) {
try {
return new String(utf8buf, "UTF8");
} catch (UnsupportedEncodingException e) {
System.out.println("Bad utf8 string "+utf8buf);
}
return null;
}
}