Hi Rob,

Here's an example of an XSLTC servlet using TrAX that 
may help you figure things out.

Tom

> import javax.servlet.*;
> import javax.servlet.http.*;
> import java.io.*;
> import java.util.Enumeration;
> import java.net.URL;
> 
> import org.xml.sax.*;
> import javax.xml.transform.TransformerFactory;
> import javax.xml.transform.TransformerConfigurationException;
> import javax.xml.transform.Templates;
> import javax.xml.transform.Transformer;
> import javax.xml.transform.Source;
> import javax.xml.transform.stream.StreamSource;
> import javax.xml.transform.stream.StreamResult;
> 
> public final class XSLTCTraxServlet extends HttpServlet {
> 
>     private static Templates templates = null;
>     private static TransformerFactory tFactory = null;
> 
>     public void init(ServletConfig config) throws ServletException {
> 
>       super.init(config);
> 
>       System.setProperty("javax.xml.transform.TransformerFactory",
>                          "org.apache.xalan.xsltc.trax.TransformerFactoryImpl");
> 
>       tFactory = TransformerFactory.newInstance();
>       String xsl = "search.xsl";
>       Source xslSource = null;
> 
>       try {
>           System.out.println("in the try");
>           // Get the stylesheet.
>           if (xsl != null && xsl.length()> 0)
>               xslSource = new StreamSource(xsl);
>           if (xslSource != null) { // Now do we have a stylesheet?
>               // if yes, create template for reuse
>               System.out.println("xslSource is " + xslSource);
>               System.out.println("tFactory is " + tFactory);
>               templates = tFactory.newTemplates(xslSource);
>               System.out.println("template is " + templates);
>           }
>           else {
>               System.out.println("No Stylesheet!");
>           }
>       }
>       catch (TransformerConfigurationException e) {
>           System.out.println("can't make templates, " + e.getMessage());
>           e.printStackTrace(System.err);
>       }
>       catch (Exception e) {
>           e.printStackTrace(System.out);    
>       }
>     } //end of init
> 
> 
>     public void doPost (HttpServletRequest request,
>                       HttpServletResponse response)
>       throws ServletException, IOException {
> 
>       // The servlet returns HTML; charset is UTF8.
>       // See ApplyXSLT.getContentType() to get output properties from <xsl:output>.
>       response.setContentType("text/html; charset=UTF-8"); 
>       PrintWriter out = response.getWriter();
>       Source xmlSource = null;
>       String xml = "http://glrr.east:8080/calendar/calendar.xml";;
>       try {
>           if (xml != null && xml.length()> 0) {
>               xmlSource = new StreamSource(new URL(xml).openStream());
>           }
>           else {
>               out.write("No XML Input Document!");
>           }
>           long start = System.currentTimeMillis();
>           Transformer transformer = null;
>           System.out.println("in doPost, templates is " + templates);
>           transformer = templates.newTransformer();
>           setParameters(transformer, request); // Set stylesheet params.
>           // Perform the transformation.
>           transformer.transform(xmlSource, new StreamResult(out));
>           long stop = System.currentTimeMillis();
>           out.println("<!-- transformed in "+
>                       (stop-start)+"ms -->");
>       }
>       catch (Exception e) {
>           e.printStackTrace(out);    
>       }
>       out.close();
>     } // end of doPost
>   
>     // Get parameters from the request URL.
>     String getRequestParam(HttpServletRequest request, String param) {
>       if (request != null) { 
>           String paramVal = request.getParameter(param); 
>           return paramVal;
>       }
>       return null;
>     }
>   
>     // Set stylesheet parameters from the request URL.
>     void setParameters(Transformer transformer, HttpServletRequest request) {
>       Enumeration paramNames = request.getParameterNames();
>       while (paramNames.hasMoreElements()) {
>           String name = (String) paramNames.nextElement();
>           String other;
>           try {
>               String value = request.getParameter(name);
>               if (!(name.equals("style") || name.equals("source") || 
>name.equals("action") || name.equals("class"))) {
>                   if(name.equals("states")){
>                       value = "";
>                       String [] values = request.getParameterValues("states");
>                       value = values[0];
>                       for (int i = 1; i < values.length; i++){
>                           value = value + " " + values[i];
>                       }
>                   } else {
>                       value = request.getParameter(name);
>                   } // set country to ocountry so stylesheet only deals with country
>                   if(name.equals("country") && value.equals("Other")){
>                       other = request.getParameter("ocountry");
>                       value = new String(other);
>                   }
>                   if(name.equals("town") && (value.length() < 2)){
>                       value = new String("");
>                   }
>                   if(name.equals("zip") && (value.length() < 2)){
>                       value = new String("");
>                   }
>                   if(name.equals("ldist") && (value != null)){
>                       String lval = request.getParameter("ldist");
>                       String ulval = request.getParameter("lunits");
>                       double ldistance = Double.parseDouble(lval) * 
>Double.parseDouble(ulval);
>                       value = new String(Double.toString(ldistance));
>                   }
>                   if(name.equals("gdist") && (value != null)){
>                       String gval = request.getParameter("gdist");
>                       String ugval = request.getParameter("gunits");
>                       double ldistance = Double.parseDouble(gval) * 
>Double.parseDouble(ugval);
>                       value = new String(Double.toString(ldistance));
>                   }
>                   if(value != null && value.length() >= 1){
>                       transformer.setParameter(name, value);
>                   }
>               }// end of if not style, source, action or class
>           }
> 
>           catch (Exception e) { }
> 
>       }
>     }
> }

Reply via email to