View Javadoc

1   /*
2    *  Java HTML Tidy - JTidy
3    *  HTML parser and pretty printer
4    *
5    *  Copyright (c) 1998-2000 World Wide Web Consortium (Massachusetts
6    *  Institute of Technology, Institut National de Recherche en
7    *  Informatique et en Automatique, Keio University). All Rights
8    *  Reserved.
9    *
10   *  Contributing Author(s):
11   *
12   *     Dave Raggett <dsr@w3.org>
13   *     Andy Quick <ac.quick@sympatico.ca> (translation to Java)
14   *     Gary L Peskin <garyp@firstech.com> (Java development)
15   *     Sami Lempinen <sami@lempinen.net> (release management)
16   *     Fabrizio Giustina <fgiust at users.sourceforge.net>
17   *     Vlad Skarzhevskyy <vlads at users.sourceforge.net> (JTidy servlet  development)
18   *
19   *  The contributing author(s) would like to thank all those who
20   *  helped with testing, bug fixes, and patience.  This wouldn't
21   *  have been possible without all of you.
22   *
23   *  COPYRIGHT NOTICE:
24   *
25   *  This software and documentation is provided "as is," and
26   *  the copyright holders and contributing author(s) make no
27   *  representations or warranties, express or implied, including
28   *  but not limited to, warranties of merchantability or fitness
29   *  for any particular purpose or that the use of the software or
30   *  documentation will not infringe any third party patents,
31   *  copyrights, trademarks or other rights.
32   *
33   *  The copyright holders and contributing author(s) will not be
34   *  liable for any direct, indirect, special or consequential damages
35   *  arising out of any use of the software or documentation, even if
36   *  advised of the possibility of such damage.
37   *
38   *  Permission is hereby granted to use, copy, modify, and distribute
39   *  this source code, or portions hereof, documentation and executables,
40   *  for any purpose, without fee, subject to the following restrictions:
41   *
42   *  1. The origin of this source code must not be misrepresented.
43   *  2. Altered versions must be plainly marked as such and must
44   *     not be misrepresented as being the original source.
45   *  3. This Copyright notice may not be removed or altered from any
46   *     source or altered source distribution.
47   *
48   *  The copyright holders and contributing author(s) specifically
49   *  permit, without fee, and encourage the use of this source code
50   *  as a component for supporting the Hypertext Markup Language in
51   *  commercial products. If you use this source code in a product,
52   *  acknowledgment is not required but would be appreciated.
53   *
54   */
55  package org.w3c.tidy.servlet.jsp.tagext;
56  
57  import java.io.IOException;
58  
59  import javax.servlet.http.HttpServletRequest;
60  import javax.servlet.http.HttpServletResponse;
61  import javax.servlet.jsp.JspException;
62  import javax.servlet.jsp.tagext.TagSupport;
63  
64  import org.apache.commons.logging.LogFactory;
65  import org.w3c.tidy.servlet.Consts;
66  import org.w3c.tidy.servlet.RepositoryFactory;
67  import org.w3c.tidy.servlet.TidyServlet;
68  import org.w3c.tidy.servlet.properties.JTidyServletProperties;
69  import org.w3c.tidy.servlet.util.HTMLEncode;
70  
71  
72  /***
73   * Show Image base on JTidy HTML Validation See tagExample.jsp for usage example.
74   * @author Vlad Skarzhevskyy <a href="mailto:skarzhevskyy@gmail.com">skarzhevskyy@gmail.com </a>
75   * @version $Revision: 1.8 $ ($Author: fgiust $)
76   */
77  public class ValidationImageTag extends TagSupport
78  {
79  
80      /***
81       * Stable <code>serialVersionUID</code>.
82       */
83      private static final long serialVersionUID = 29137L;
84  
85      /***
86       * Just generate URI for image.
87       */
88      private boolean srcOnly;
89  
90      /***
91       * RequestID (number) used by Tidy to identify this page.
92       */
93      private String requestID;
94  
95      /***
96       * Name of img element in html, By default JTidyValidationImage.
97       */
98      private String imgName;
99  
100     /***
101      * javascript Event
102      */
103     private String onclick;
104 
105     /***
106      * @see javax.servlet.jsp.tagext.Tag#doEndTag()
107      */
108     public int doEndTag() throws JspException
109     {
110         if (pageContext.getAttribute(Consts.ATTRIBUTE_IGNORE) == null)
111         {
112             try
113             {
114                 String out = null;
115 
116                 String requestID = this.requestID;
117 
118                 JTidyServletProperties properties = JTidyServletProperties.getInstance();
119 
120                 if ((requestID == null) || (requestID.equalsIgnoreCase("this")))
121                 {
122                     RepositoryFactory factory = properties.getRepositoryFactoryInstance();
123                     Object key = factory.getResponseID(pageContext.getSession(), (HttpServletRequest) pageContext
124                         .getRequest(), (HttpServletResponse) pageContext.getResponse(), false);
125                     requestID = key.toString();
126                 }
127 
128                 if (srcOnly)
129                 {
130                     out = LinkTag.getImageLink(requestID, (HttpServletRequest) pageContext.getRequest());
131                 }
132                 else
133                 {
134                     out = getImageHTML(requestID, this.imgName, this.onclick, (HttpServletRequest) pageContext
135                         .getRequest());
136                 }
137                 pageContext.getOut().write(out);
138             }
139             catch (IOException e)
140             {
141                 LogFactory.getLog(this.getClass()).error("ValidationImageTag write error", e);
142                 throw new JspException(e.getMessage());
143             }
144         }
145         return EVAL_PAGE;
146     }
147 
148     /***
149      * Generates the html tag for jtidy image.
150      * @param requestID request id
151      * @param imgName image name/id
152      * @param onclick onclick javascript code
153      * @param request HttpServletRequest
154      * @return html tag for jtidy image
155      */
156     public static String getImageHTML(String requestID, String imgName, String onclick, HttpServletRequest request)
157     {
158         StringBuffer out = new StringBuffer(120);
159 
160         JTidyServletProperties properties = JTidyServletProperties.getInstance();
161 
162         String servletURI = request.getContextPath()
163             + properties.getProperty(JTidyServletProperties.JTIDYSERVLET_URI, Consts.DEFAULT_JTIDYSERVLET_URI);
164 
165         if (imgName == null)
166         {
167             imgName = "JTidyValidationImage";
168         }
169 
170         out.append("<a name=\"");
171         out.append(imgName);
172         out.append("Link\" ");
173         out.append("id=\"");
174         out.append(imgName);
175         out.append("Link\" href=\"");
176         out.append(HTMLEncode.encodeHREFQuery(servletURI, new String[]{
177             TidyServlet.PARAM_REQUEST_ID,
178             requestID,
179             TidyServlet.PARAM_ACTION,
180             TidyServlet.ACTION_REPORT,
181             TidyServlet.ACTION_REPORT_PARAM_SRC_ORG,
182             "1"}));
183 
184         out.append("\" ");
185 
186         if ((onclick != null) && (onclick.length() > 0))
187         {
188             out.append("onclick=\"").append(onclick).append("\"");
189         }
190         out.append(">");
191 
192         out.append("<img name=\"");
193         out.append(imgName);
194         out.append("\" id=\"");
195         out.append(imgName);
196         out.append("\" alt=\"Page Validation\" ");
197         out.append("src=\"");
198 
199         out.append(LinkTag.getImageLink(requestID, request));
200 
201         out.append("\" width=\"").append(
202             properties.getProperty(JTidyServletProperties.PROPERTY_STRING_IMAGE_WIDTH, "32"));
203         out.append("\" height=\"").append(
204             properties.getProperty(JTidyServletProperties.PROPERTY_STRING_IMAGE_HEIGHT, "26"));
205         out.append("\" ");
206         if (properties.getBooleanProperty(JTidyServletProperties.PROPERTY_BOOLEAN_XHTML, true))
207         {
208             out.append("/");
209         }
210         out.append("></a>");
211         return out.toString();
212     }
213 
214     /***
215      * @see javax.servlet.jsp.tagext.Tag#release()
216      */
217     public void release()
218     {
219         super.release();
220         this.srcOnly = false;
221         this.requestID = null;
222         this.onclick = null;
223         this.imgName = null;
224     }
225 
226     /***
227      * @param srcOnly The srcOnly to set.
228      */
229     public void setSrcOnly(boolean srcOnly)
230     {
231         this.srcOnly = srcOnly;
232     }
233 
234     /***
235      * @param requestID The requestID to set.
236      */
237     public void setRequestID(String requestID)
238     {
239         this.requestID = requestID;
240     }
241 
242     /***
243      * @param onclick The onclick to set.
244      */
245     public void setOnclick(String onclick)
246     {
247         this.onclick = onclick;
248     }
249 
250     /***
251      * @param imgName The imgName to set.
252      */
253     public void setImgName(String imgName)
254     {
255         this.imgName = imgName;
256     }
257 }