Mike,

The whitespace and formatting really bugged me as well, so I wrote a servlet
filter to run the generated html through the Java port of Html tidy
(http://sourceforge.net/projects/jtidy/) to format it and clean up the
whitespace.  

[It does have the side effect of not displaying your page, if the html is
not standards compliant (http://www.webstandards.org/), but that could be
fixed.  I deliberately left it that way, so I knew which pages needed work.
]  

---------------------------

public class TidyOutput implements Filter
{
    //~ Static fields/initializers
---------------------------------------------
    static Logger log = Logger.getLogger(TidyOutput.class.getName());
    //~ Instance fields
--------------------------------------------------------
    private ServletContext ctx;
    private boolean isTextOut;
    //~ Inner Classes
----------------------------------------------------------
    private static class ByteArrayServletStream extends ServletOutputStream
    {
        ByteArrayOutputStream baos;
        ByteArrayServletStream(ByteArrayOutputStream baos)
        {
            this.baos = baos;
        }
        public void write(int param) throws java.io.IOException
        {
            baos.write(param);
        }
    }
    private static class ByteArrayPrintWriter
    {
        private ByteArrayOutputStream baos = new ByteArrayOutputStream();
        private PrintWriter pw = new PrintWriter(baos);
        private ServletOutputStream sos = new ByteArrayServletStream(baos);
        public PrintWriter getWriter()
        {
            return pw;
        }
        public ServletOutputStream getStream()
        {
            return sos;
        }
        byte[] toByteArray()
        {
            return baos.toByteArray();
        }
    }
    //~ Methods
----------------------------------------------------------------
    /**
     * Initializes the filter
     *
     * @param filterConfig
     *
     * @throws ServletException
     */
    public void init(FilterConfig filterConfig) throws ServletException
    {
        ctx = filterConfig.getServletContext();
    }
    /**
     * Performs the filtering action
     *
     * @param req
     * @param response
     * @param chain
     *
     * @throws IOException
     * @throws ServletException
     */
    public void doFilter(
        ServletRequest req,
        ServletResponse response,
        FilterChain chain)
        throws IOException, ServletException
    {
        HttpServletRequest hsr = (HttpServletRequest) req;
        final HttpServletResponse resp = (HttpServletResponse) response;
        final ByteArrayPrintWriter pw = new ByteArrayPrintWriter();
        HttpServletResponse wrappedResp = new
HttpServletResponseWrapper(resp)
        {
            public PrintWriter getWriter()
            {
                return pw.getWriter();
            }
            public ServletOutputStream getOutputStream()
            {
                return pw.getStream();
            }
        };
        chain.doFilter(req, wrappedResp);
        byte[] bytes = pw.toByteArray();
        String str = new String(bytes);
        if (str.indexOf("text/html") > 0)
        {
            setTextOut(true);
        }
        ServletOutputStream out = response.getOutputStream();
        String contentType = req.getContentType();
        //make sure that this is html 
        if ((bytes != null) && isTextOut)
        {
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
            Tidy tidy = new Tidy();
            tidy.setIndentContent(true);
            tidy.setSpaces(2);
            tidy.setQuiet(true);
            tidy.setSmartIndent(true);
            tidy.setXHTML(false);
            tidy.setWraplen(120);
            tidy.setMakeClean(true);
            tidy.parse(bais, baos);
            byte[] xformBytes = baos.toByteArray();
            resp.setContentLength(xformBytes.length);
            resp.getOutputStream().write(xformBytes);
        }
        else
        {
            resp.getOutputStream().write(bytes);
        }
    }
    /**
     * Cleanup when the filter ends
     */
    public void destroy()
    {
    }
    /**
     * Setter
     *
     * @return value
     */
    public boolean isTextOut()
    {
        return isTextOut;
    }
    /**
     * Getter
     *
     * @param b
     */
    public void setTextOut(boolean b)
    {
        isTextOut = b;
    }
}
-------- Original Message --------
Subject: white space on jsp compile
Date: Thu, 31 Jul 2003 20:21:08 +0100
From: Mike Whittaker <[EMAIL PROTECTED]>
Reply-To: Struts Users Mailing List <[EMAIL PROTECTED]>
Newsgroups: gmane.comp.jakarta.struts.user

Is there any way to control it? It seems a little excessive

I'd like to retain it in the JSP, and remove as much as possible in the html

--
Mike W

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to