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;
56 /*
57 * Created on 31.10.2004 by vlads
58 */
59 import java.io.InputStream;
60 import java.io.*;
61 import javax.servlet.http.HttpSession;
62
63 import org.apache.commons.logging.Log;
64 import org.apache.commons.logging.LogFactory;
65 import org.w3c.tidy.servlet.properties.JTidyServletProperties;
66
67 /***
68 *
69 *
70 */
71 public class TidyServletHelper
72 {
73
74 /***
75 * Logger.
76 */
77 private static Log log = LogFactory.getLog(TidyServletHelper.class);
78
79 public static ResponseRecordRepository getRecordRepository(HttpSession httpSession)
80 {
81 return JTidyServletProperties.getInstance().getRepositoryInstance(httpSession);
82 }
83
84 public static String getResponseID(String keyString, HttpSession httpSession)
85 {
86 ResponseRecordRepository rep = getRecordRepository(httpSession);
87 if (rep == null)
88 {
89 return "";
90 }
91 Object key = rep.getResponseID(keyString);
92 if (key != null)
93 {
94 return key.toString();
95 }
96 else
97 {
98 return "";
99 }
100 }
101
102 public static String process(InputStream in, HttpSession httpSession) {
103 RepositoryFactory factory = JTidyServletProperties.getInstance().getRepositoryFactoryInstance();
104
105 TidyProcessor tidyProcessor = new TidyProcessor(
106 httpSession,
107 null,
108 null);
109 tidyProcessor.setValidateOnly(true);
110
111 Object requestID = factory.getResponseID(httpSession, null, null, true);
112
113 StringBuffer html = new StringBuffer();
114 BufferedReader buffer = new BufferedReader(new InputStreamReader(in));
115 String line;
116 try
117 {
118 while ((line = buffer.readLine()) != null)
119 {
120 html.append(line).append("\n");
121 }
122 buffer.close();
123 }
124 catch (IOException e)
125 {
126 log.error("Error Reading file", e);
127 }
128
129 ByteArrayInputStream processorIn = new ByteArrayInputStream(html.toString().getBytes());
130
131 tidyProcessor.parse(processorIn, null, html.toString(), requestID, factory);
132 return requestID.toString();
133 }
134 }