1 11 package org.eclipse.help.internal.webapp.data; 12 13 import java.io.BufferedReader ; 14 import java.io.IOException ; 15 import java.io.InputStream ; 16 import java.io.InputStreamReader ; 17 import java.io.Reader ; 18 import java.io.Writer ; 19 import java.util.StringTokenizer ; 20 import java.util.regex.Matcher ; 21 import java.util.regex.Pattern ; 22 23 import javax.servlet.ServletContext ; 24 import javax.servlet.http.HttpServletRequest ; 25 import javax.servlet.http.HttpServletResponse ; 26 27 import org.eclipse.help.HelpSystem; 28 import org.eclipse.help.IToc; 29 import org.eclipse.help.ITopic; 30 import org.eclipse.help.internal.HelpPlugin; 31 import org.eclipse.help.internal.search.HTMLDocParser; 32 import org.eclipse.help.internal.webapp.HelpWebappPlugin; 33 34 37 public class PrintData extends RequestData { 38 39 private static final Pattern PATTERN_HEADING = Pattern.compile("<body.*?>[\\s]*?([\\w])", Pattern.MULTILINE | Pattern.DOTALL | Pattern.CASE_INSENSITIVE); 42 private static final Pattern PATTERN_LINK = Pattern.compile("(src|href)=\"(.*?\")", Pattern.MULTILINE | Pattern.DOTALL | Pattern.CASE_INSENSITIVE); 45 48 public PrintData(ServletContext context, HttpServletRequest request, HttpServletResponse response) { 49 super(context, request, response); 50 } 51 52 55 public String getTitle() { 56 return getTopic().getLabel(); 57 } 58 59 62 public String getTocHref() { 63 return getToc().getHref(); 64 } 65 66 69 public String getTopicHref() { 70 return getTopic().getHref(); 71 } 72 73 76 public void generateToc(Writer out) throws IOException { 77 out.write("<div id=\"toc\">"); out.write("<h1>"); out.write(getTitle()); 80 out.write("</h1>"); out.write("<h2>"); out.write(ServletResources.getString("TocHeading", request)); out.write("</h2>"); out.write("<div id=\"toc_content\">"); ITopic topic = getTopic(); 86 ITopic[] subtopics = topic.getSubtopics(); 87 for (int i=0;i<subtopics.length;++i) { 88 generateToc(subtopics[i], String.valueOf(i + 1), out); 89 } 90 out.write("</div>"); out.write("</div>"); } 93 94 97 private void generateToc(ITopic topic, String sectionId, Writer out) throws IOException { 98 out.write("<div class=\"toc_" + (sectionId.length() > 2 ? "sub" : "") + "entry\">"); out.write(sectionId + ". " + "<a HREF=\"#section" + sectionId + "\">" + topic.getLabel() + "</a>"); ITopic[] subtopics = topic.getSubtopics(); 101 for (int i=0;i<subtopics.length;++i) { 102 String subsectionId = sectionId + "." + (i + 1); generateToc(subtopics[i], subsectionId, out); 104 } 105 out.write("</div>"); } 107 108 111 public void generateContent(Writer out) throws IOException { 112 generateContent(getTopic(), null, out); 113 } 114 115 118 private void generateContent(ITopic topic, String sectionId, Writer out) throws IOException { 119 String href = topic.getHref(); 120 if (href != null) { 121 String pathHref = href.substring(0, href.lastIndexOf('/') + 1); 123 String baseHref = "http://" + request.getServerName() + ":" + request.getServerPort() + request.getContextPath() + "/topic" + pathHref; String content = getContent(href, locale); 125 126 if (sectionId != null) { 128 content = injectHeading(content, sectionId); 129 } 130 content = normalizeHrefs(content, baseHref); 131 out.write(content); 132 } 133 ITopic[] subtopics = topic.getSubtopics(); 134 for (int i=0;i<subtopics.length;++i) { 135 String subsectionId = (sectionId != null ? sectionId + "." : "") + (i + 1); generateContent(subtopics[i], subsectionId, out); 137 } 138 } 139 140 143 private String injectHeading(String content, String sectionId) { 144 Matcher matcher = PATTERN_HEADING.matcher(content); 145 if (matcher.find()) { 146 String heading = "<a id=\"section" + sectionId + "\">" + sectionId + ". </a>"; return content.substring(0, matcher.start(1)) + heading + content.substring(matcher.start(1)); 148 } 149 return content; 150 } 151 152 156 private String normalizeHrefs(String content, String baseHref) { 157 StringBuffer buf = new StringBuffer (); 158 Matcher matcher = PATTERN_LINK.matcher(content); 159 int prev = 0; 160 while (matcher.find()) { 161 buf.append(content.substring(prev, matcher.start(2))); 162 buf.append(baseHref); 163 buf.append(matcher.group(2)); 164 prev = matcher.end(); 165 } 166 buf.append(content.substring(prev)); 167 return buf.toString(); 168 } 169 170 173 private String getContent(String href, String locale) { 174 InputStream in = HelpSystem.getHelpContent(href, locale); 175 StringBuffer buf = new StringBuffer (); 176 if (in != null) { 177 try { 178 String charset = HTMLDocParser.getCharsetFromHTML(in); 179 if (charset == null) { 180 charset = "UTF-8"; } 182 in = HelpSystem.getHelpContent(href, locale); 183 Reader reader = new BufferedReader (new InputStreamReader (in, charset)); 184 char[] cbuf = new char[4096]; 185 int num; 186 while ((num = reader.read(cbuf)) > 0) { 187 buf.append(cbuf, 0, num); 188 } 189 } 190 catch (Exception e) { 191 String msg = "Error retrieving print preview content for " + href; HelpWebappPlugin.logError(msg, e); 193 } 194 finally { 195 try { 196 in.close(); 197 } 198 catch (Exception e) {} 199 } 200 } 201 return buf.toString(); 202 } 203 204 207 private IToc getToc() { 208 String tocParam = request.getParameter("toc"); if (tocParam != null && tocParam.length() > 0) { 210 return HelpPlugin.getTocManager().getToc(tocParam, getLocale()); 211 } 212 String topicParam = request.getParameter("topic"); if (topicParam != null && topicParam.length() > 0) { 214 if (topicParam.startsWith("/../nav/")) { String navPath = topicParam.substring(8); 216 StringTokenizer tok = new StringTokenizer (navPath, "_"); int index = Integer.parseInt(tok.nextToken()); 218 return HelpPlugin.getTocManager().getTocs(getLocale())[index]; 219 } 220 IToc[] tocs = HelpPlugin.getTocManager().getTocs(getLocale()); 221 for (int i=0;i<tocs.length;++i) { 222 if (tocs[i].getTopic(topicParam) != null) { 223 return tocs[i]; 224 } 225 } 226 } 227 return null; 228 } 229 230 233 private ITopic getTopic() { 234 String topicParam = request.getParameter("topic"); if (topicParam != null && topicParam.length() > 0) { 236 if (topicParam.startsWith("/../nav/")) { String navPath = topicParam.substring(8); 238 StringTokenizer tok = new StringTokenizer (navPath, "_"); int index = Integer.parseInt(tok.nextToken()); 240 ITopic topic = HelpPlugin.getTocManager().getTocs(getLocale())[index].getTopic(null); 241 while (tok.hasMoreTokens()) { 242 index = Integer.parseInt(tok.nextToken()); 243 topic = topic.getSubtopics()[index]; 244 } 245 return topic; 246 } 247 else { 248 IToc[] tocs = HelpPlugin.getTocManager().getTocs(getLocale()); 249 for (int i=0;i<tocs.length;++i) { 250 ITopic topic = tocs[i].getTopic(topicParam); 251 if (topic != null) { 252 return topic; 253 } 254 topic = tocs[i].getTopic(null); 256 if (topicParam.equals(topic.getHref())) { 257 return topic; 258 } 259 } 260 } 261 return null; 262 } 263 return getToc().getTopic(null); 264 } 265 } 266 | Popular Tags |