1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
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 }