2

The JTidyServlet project (mentioned in various answers) appears to be more or less defunct -- no browsable source, no downloads linked from the SourceForge project page apart from the main JTidy download (which doesn't include the servlet or filter), no Maven artifacts in any repositories I can find. Can anyone recommend a more up-to-date alternative?

Community
  • 1
  • 1
David Moles
  • 48,006
  • 27
  • 136
  • 235

3 Answers3

2

Looks like there's some source code here; https://github.com/vnc-biz/zcs-lib-jtidy/

Thor
  • 737
  • 7
  • 15
2

For aesthetics' sake -though admittedly pointless- I too recently have striven to deliver visually appealing HTML5 source originating from JSF (alongside with PrimeFaces) Facelets to user agents.

JTidy and its accompanying utils didn't really help me out; it radically attempted to "fix" my "broken" markup, e.g. by switching DTD and modifying various elements (as if there is such a thing as valid HTML in the real world anyway). XSLT would be a satisfying alternative, were the application/xhtml+xml Content-Type to be used (similarly in an ideal world). Then I stumbled upon jsoup and after a few attempts, combining its API with examples obtained from [3], [4], [5], [6], [7], the result was quite a surprise:


JSF output

<!DOCTYPE html>
<html lang="en" xmlns="http://www.w3.org/1999/xhtml"><head><link type="text/css" rel="stylesheet" href="/javax.faces.resource/theme.css.xhtml?ln=primefaces-bootstrap" /><link type="text/css" rel="stylesheet" href="/javax.faces.resource/primefaces.css.xhtml?ln=primefaces&amp;v=5.0" /><script type="text/javascript" src="/javax.faces.resource/jquery/jquery.js.xhtml?ln=primefaces&amp;v=5.0"></script><!-- ... -->
    <title>Some Page</title></head><body>
<form id="j_idt5" name="j_idt5" method="post" action="/somePage.xhtml" enctype="application/x-www-form-urlencoded">
<input type="hidden" name="j_idt5" value="j_idt5" />
<table id="j_idt5:j_idt6" class="ui-panelgrid ui-widget" role="grid"><tbody><tr class="ui-widget-content" role="row"><td role="gridcell" class="ui-panelgrid-cell"><label>Title : </label></td><td role="gridcell" class="ui-panelgrid-cell"><input id="j_idt5:j_idt8" name="j_idt5:j_idt8" type="text" class="ui-inputfield ui-inputtext ui-widget ui-state-default ui-corner-all" /><script id="j_idt5:j_idt8_s" type="text/javascript">PrimeFaces.cw("InputText","widget_j_idt5_j_idt8",{id:"j_idt5:j_idt8",widgetVar:"widget_j_idt5_j_idt8"});</script></td></tr><!-- ... --><tr class="ui-widget-content" role="row"><td role="gridcell" class="ui-panelgrid-cell"><button id="j_idt5:j_idt13" name="j_idt5:j_idt13" class="ui-button ui-widget ui-state-default ui-corner-all ui-button-text-icon-left" onclick="PrimeFaces.ab({s:'j_idt5:j_idt13'});return false;" type="submit"><span class="ui-button-icon-left ui-icon ui-c ui-icon-cancel"></span><span class="ui-button-text ui-c">Submit Item</span></button><script id="j_idt5:j_idt13_s" type="text/javascript">PrimeFaces.cw("CommandButton","widget_j_idt5_j_idt13",{id:"j_idt5:j_idt13",widgetVar:"widget_j_idt5_j_idt13"});</script></td></tbody></table><input type="hidden" name="javax.faces.ViewState" id="j_id1:javax.faces.ViewState:0" value="328014904185199862:2925470649195950066" autocomplete="off" />
</form></body>
</html>

After jsoup

<!DOCTYPE html>
<html lang="en" xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <link type="text/css" rel="stylesheet" href="/javax.faces.resource/theme.css.xhtml?ln=primefaces-bootstrap" />
    <link type="text/css" rel="stylesheet" href="/javax.faces.resource/primefaces.css.xhtml?ln=primefaces&amp;v=5.0" />
    <script type="text/javascript" src="/javax.faces.resource/jquery/jquery.js.xhtml?ln=primefaces&amp;v=5.0"></script>
    <!-- ... -->
    <title>Some Page</title>
  </head>
  <body> 
    <form id="j_idt5" name="j_idt5" method="post" action="/somePage.xhtml" enctype="application/x-www-form-urlencoded"> 
      <input type="hidden" name="j_idt5" value="j_idt5" /> 
      <table id="j_idt5:j_idt6" class="ui-panelgrid ui-widget" role="grid">
        <tbody>
          <tr class="ui-widget-content" role="row">
            <td role="gridcell" class="ui-panelgrid-cell">
              <label>Title : </label>
            </td>
            <td role="gridcell" class="ui-panelgrid-cell">
              <input id="j_idt5:j_idt8" name="j_idt5:j_idt8" type="text" class="ui-inputfield ui-inputtext ui-widget ui-state-default ui-corner-all" />
              <script id="j_idt5:j_idt8_s" type="text/javascript">PrimeFaces.cw("InputText","widget_j_idt5_j_idt8",{id:"j_idt5:j_idt8",widgetVar:"widget_j_idt5_j_idt8"});
              </script>
            </td>
          </tr>
          <!-- ... -->
          <tr class="ui-widget-content" role="row">
            <td role="gridcell" class="ui-panelgrid-cell">
              <button id="j_idt5:j_idt13" name="j_idt5:j_idt13" class="ui-button ui-widget ui-state-default ui-corner-all ui-button-text-icon-left" onclick="PrimeFaces.ab({s:'j_idt5:j_idt13'});return false;" type="submit">
                <span class="ui-button-icon-left ui-icon ui-c ui-icon-cancel"></span>
                <span class="ui-button-text ui-c">Submit Item</span>
              </button>
              <script id="j_idt5:j_idt13_s" type="text/javascript">PrimeFaces.cw("CommandButton","widget_j_idt5_j_idt13",{id:"j_idt5:j_idt13",widgetVar:"widget_j_idt5_j_idt13"});
              </script>
            </td>
          </tr>
        </tbody>
      </table>
      <input type="hidden" name="javax.faces.ViewState" id="j_id1:javax.faces.ViewState:0" value="328014904185199862:2925470649195950066" autocomplete="off" /> 
    </form> 
  </body>
</html>

No "well-thought-out XMLization" changes whatsoever (self-closing <script>s, <![CDATA[s, etc.), just line breaks and indentation. Of course the output remains XHTML invalid, but that shouldn't be an issue for the average browser. Lastly, I don't know if the outcome would be as positive in the case of a full-fledged Facelet.

References (can't post more than 2 links)

  [3]: http://stackoverflow.com/questions/3604248/jsf-prettify-beautify-html-output
  [4]: http://angelborroy.wordpress.com/2009/03/04/dump-request-and-response-using-javaxservletfilter/
  [5]: http://www.java2s.com/Tutorial/Java/0400__Servlet/Filterthatusesaresponsewrappertoconvertalloutputtouppercase.htm
  [6]: http://www.oracle.com/technetwork/java/filters-137243.html
  [7]: http://stackoverflow.com/questions/14736328/looking-for-an-example-for-inserting-content-into-the-response-using-a-servlet-f
Uux
  • 1,218
  • 1
  • 10
  • 21
1

Found the JTidyServlet at the following location

http://jtidy.sourceforge.net/nightly/

imranamans
  • 19
  • 2