001: package vqwiki.servlets;
002:
003: import java.io.IOException;
004: import java.util.ArrayList;
005: import java.util.Collection;
006: import java.util.Iterator;
007: import java.util.ResourceBundle;
008: import java.util.Vector;
009:
010: import javax.servlet.ServletException;
011: import javax.servlet.http.HttpServletRequest;
012: import javax.servlet.http.HttpServletResponse;
013:
014: import vqwiki.Environment;
015: import vqwiki.PrintableEntry;
016: import vqwiki.WikiBase;
017: import vqwiki.PseudoTopicHandler;
018: import vqwiki.utils.Utilities;
019: import vqwiki.utils.JSPUtils;
020:
021: /**
022: * Create a printable view of one servlet.
023: *
024: * @author garethc, Tobias Schulz-Hess (sourcefoge@schulz-hess.de)
025: * Date: Jan 8, 2003
026: */
027: public class PrintableServlet extends VQWikiServlet {
028:
029: /**
030: * Handle get request
031: *
032: * @param request The servlet request
033: * @param response The servlet response
034: *
035: * @throws ServletException If something went wrong with the servlet
036: * @throws IOException If the servlet cannot print
037: */
038: protected void doGet(HttpServletRequest request,
039: HttpServletResponse response) throws ServletException,
040: IOException {
041: String topic = request.getParameter("topic");
042: String virtualWiki = (String) request
043: .getAttribute("virtualWiki");
044: request.setAttribute("topic", topic);
045: request.setAttribute("title", topic);
046: String strDepth = request.getParameter("depth");
047: if (request.getParameter("hideform") != null) {
048: request.setAttribute("hideform", "true");
049: }
050: int depth = 0;
051: try {
052: depth = Integer.parseInt(strDepth);
053: } catch (NumberFormatException e1) {
054: depth = 0;
055: }
056: request.setAttribute("depth", String.valueOf(depth));
057: Environment en = Environment.getInstance();
058: String contextPath = request.getContextPath();
059: en.setBaseContext(contextPath);
060: ArrayList result = new ArrayList();
061: Vector alreadyVisited = new Vector();
062: try {
063: result.addAll(parsePage(ResourceBundle.getBundle(
064: "ApplicationResources", request.getLocale()), en,
065: virtualWiki, topic, depth, alreadyVisited));
066: } catch (Exception e) {
067: error(request, response, e);
068: return;
069: }
070: // now go through all pages and replace
071: // all href=Wiki? with href=# for the
072: // pages in the alreadyVisited vector
073: for (Iterator iter = result.iterator(); iter.hasNext();) {
074: PrintableEntry element = (PrintableEntry) iter.next();
075: for (Iterator visitedIterator = alreadyVisited.iterator(); visitedIterator
076: .hasNext();) {
077: String visitedTopic = (String) visitedIterator.next();
078: element.setContent(Utilities.replaceString(element
079: .getContent(), "href=\"Wiki?" + visitedTopic,
080: "href=\"#" + visitedTopic));
081: }
082: }
083: // put the result in the request
084: request.setAttribute("contentList", result);
085: dispatch("/jsp/printable.jsp", request, response);
086: }
087:
088: /**
089: * Parse page and supages
090: * @param virtualWiki The virutal wiki to use
091: * @param topic The topic to start with
092: * @param depth The depth to go into
093: * @return Collection of pages
094: */
095: private Collection parsePage(ResourceBundle messages,
096: Environment en, String virtualWiki, String topic,
097: int depth, Vector alreadyVisited) throws Exception {
098: WikiBase base = WikiBase.getInstance();
099: String onepage = base.readCooked(virtualWiki, topic);
100: Collection result = new ArrayList();
101: if (onepage != null) {
102: PrintableEntry entry = new PrintableEntry();
103: entry.setTopic(topic);
104: entry.setContent(onepage);
105: result.add(entry);
106: alreadyVisited.add(topic);
107: if (depth > 0) {
108: String searchfor = "href=\"Wiki?";
109: int iPos = onepage.indexOf(searchfor);
110: int iEndPos = onepage.indexOf(messages
111: .getString("topic.ismentionedon"));
112: if (iEndPos == -1)
113: iEndPos = Integer.MAX_VALUE;
114: while (iPos > -1 && iPos < iEndPos) {
115: String link = onepage.substring(iPos
116: + searchfor.length(), onepage.indexOf('"',
117: iPos + searchfor.length()));
118: if (link.indexOf('&') > -1) {
119: link = link.substring(0, link.indexOf('&'));
120: }
121: link = JSPUtils.decodeURL(link);
122: if (link.length() > 3
123: && !link.startsWith("topic=")
124: && !link.startsWith("action=")
125: && !alreadyVisited.contains(link)
126: && !PseudoTopicHandler.getInstance()
127: .isPseudoTopic(link)) {
128: result.addAll(parsePage(messages, en,
129: virtualWiki, link, (depth - 1),
130: alreadyVisited));
131: }
132: iPos = onepage.indexOf(searchfor, iPos + 10);
133: }
134: }
135: }
136: return result;
137: }
138: }
|