Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Copyright © 2012 jbundle.org. All rights reserved.
   */
  package org.jbundle.base.screen.control.xslservlet;

:
#)DBServlet.java 0.00 12-Feb-97 Don Corley Copyright © 2012 tourgeek.com. All Rights Reserved. don@tourgeek.com
 
 
 import java.io.File;
 import java.io.Writer;
 import java.util.Map;
 
 
XSLServlet This is the xsl servlet.
 
 public class XSLServlet extends XMLServlet
 {
 	private static final long serialVersionUID = 1L;
 
 	public XSLServlet()
 	{
 		super();
 	}
returns the servlet info
  
     public String getServletInfo()
     {
         return "This the xsl servlet";
     }
    
init method.

Throws:
javax.servlet.ServletException From inherited class.
 
     @Override
     public void init(ServletConfig configthrows ServletException
     {
         super.init(config);
     }
    
Destroy this Servlet and any active applications. This is only called when all users are done using this Servlet.
 
     @Override
     public void destroy()
     {
         super.destroy();
     }
    
This thread feeds data to the pipe.

Author(s):
don
 
     class ProcessOutputThread extends Thread
     {
         Writer outWriter = null;
         ServletTask servletTask = null;
         HttpServletRequest req = null;
         ScreenModel screen = null;
         BasicServlet servlet = null;
         boolean freeWhenDone = false;
         public ProcessOutputThread(BasicServlet servletWriter outWriterServletTask servletTaskHttpServletRequest reqScreenModel screenboolean freeWhenDone)
         {
            super();
            this. = outWriter;
            this. = servletTask;
            this. = req;
            this. = screen;
            this. = servlet;
            this. = freeWhenDone;
        }
        public void run()
        {
            PrintWriter writer = new PrintWriter();
            try {
                .doProcessOutput(nullwriter);
            } catch (ServletException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                writer.flush();
                writer.close();
                if ()
                    .free();
            }
            
        }        
    }
    
    
process an HTML get or post.

Throws:
javax.servlet.ServletException From inherited class.
java.io.IOException From inherited class.
    @Override
    public void service(HttpServletRequest reqHttpServletResponse res
        throws ServletExceptionIOException
    {
        ServletTask servletTask = new ServletTask(this..);
        try {
            this.addBrowserProperties(reqservletTask);
    		ScreenModel screen = servletTask.doProcessInput(thisreqnull);
    		
            Transformer transformer = getTransformer(reqservletTaskscreen); // Screen can't be freed when I call this.
    
            PipedReader in = new PipedReader();
            PipedWriter out = new PipedWriter(in);
            
            new ProcessOutputThread(thisoutservletTaskreqscreentrue).start();
            // Note: Print Thread frees the servlettask when it is done.
    
            StreamSource source = new StreamSource(in);
    
            ServletOutputStream outStream = res.getOutputStream();
            res.setContentType("text/html");	// Always for servlets/screens
            Result result = new StreamResult(outStream);
            synchronized (transformer)
            {
                transformer.transform(sourceresult);  // Get the data from the pipe (thread) and transform it to http
            }
        } catch (TransformerException ex) {
            ex.printStackTrace();
            servletTask.free();
        } catch (ServletException ex) {
            servletTask.free();
            throw ex;
        } catch (IOException ex) {
            servletTask.free();
            throw ex;
        } catch (Exception ex) {
            servletTask.free();
        }
    	//x Don't call super.service(req, res);
    }
    private int MAX_CACHED = 20;
    TransformerFactory tFact = null;
    @SuppressWarnings({ "deprecation""unchecked" })
Get or Create a transformer for the specified stylesheet.

Parameters:
req
servletTask
screen
Returns:
Throws:
javax.servlet.ServletException
java.io.IOException
    public Transformer getTransformer(HttpServletRequest reqServletTask servletTaskScreenModel screen)
            throws ServletExceptionIOException
    {
        String stylesheet = null;
        if (stylesheet == null)
            stylesheet = req.getParameter(.);
        if (stylesheet == null)
            if (screen != null)
                if (screen.getScreenFieldView() != null)
                    stylesheet = screen.getScreenFieldView().getStylesheetPath();
        if (stylesheet == null)
            stylesheet = req.getParameter("stylesheet");
        if (stylesheet == null)
            stylesheet = "org/jbundle/res/docs/styles/xsl/flat/base/menus";
        
        try {
            if (.get(stylesheet) != null)
                return .get(stylesheet);
            String stylesheetFixed = BaseServlet.fixStylesheetPath(stylesheetscreentrue);
            
            InputStream stylesheetStream = this.getFileStream(servletTaskstylesheetFixednull);
            if (stylesheetStream == null)
            {
                stylesheetFixed = BaseServlet.fixStylesheetPath(stylesheetscreenfalse);  // Try it without browser mod
                stylesheetStream = this.getFileStream(servletTaskstylesheetFixednull);
            }
            if (stylesheetStream == null)
                Utility.getLogger().warning("XmlFile not found " + stylesheetFixed);   // TODO - Display an error here
            StreamSource stylesheetSource = new StreamSource(stylesheetStream);
            
            if ( == null)
                 = TransformerFactory.newInstance();
            URIResolver resolver = new MyURIResolver(servletTaskstylesheetFixed);
            .setURIResolver(resolver);
            Transformer transformer = .newTransformer(stylesheetSource);
            //transformer.setOutputProperty(OutputKeys.INDENT, "yes");
            //transformer.setOutputProperty(OutputKeys.METHOD, "xml");
            .put(stylesheettransformer);
            return transformer;
        } catch (TransformerConfigurationException ex)    {
            ex.printStackTrace();
        }
        return null;
    }
    
Class to return base path of imports and includes.

Author(s):
don
    public class MyURIResolver
    	implements URIResolver
    {
    	
        public MyURIResolver(ServletTask servletTaskString mainStylesheet)
        {
        	super();
        	this. = mainStylesheet;
        	this. = servletTask;
        }

        
Using the main stylesheet, create a path to this stylesheet
		public Source resolve(String hrefString base)
			int lastSlash = .lastIndexOf('/');
			if (lastSlash == -1)
			if (lastSlash == -1)
				return null;	// Never
			if ((href.startsWith("/")) || (href.endsWith(.)))
				lastSlash--;
			String sourceStylesheet = .substring(0, lastSlash + 1) + href;
			try {
				return new StreamSource(getFileStream(sourceStylesheetnull));
catch (MalformedURLException e) {
				return null;
			}
		}
    	
    }
New to GrepCode? Check out our FAQ X