import javax.servlet.*; import javax.servlet.http.*; import java.sql.*; import java.util.*; /** * Servlet which displays a page of a text. It sends a query to the * database to get a particular sequence of words, then * formats them in HTML */ public class Reader extends LitsearchBase { public String drawPage(HttpServletRequest request, HttpServletResponse response) throws Exception { int work, page; Hashtable highlight = null; ConnectionWrapper wrapper = null; try { work = Integer.parseInt(request.getParameter("work")); page = Integer.parseInt(request.getParameter("page")); String hi = request.getParameter("highlight"); int end, start = 0; if (hi != null) { while ((end = hi.indexOf(",", start)) != -1) { if (highlight == null) highlight = new Hashtable(); highlight.put(hi.substring(start, end), ""); start = end + 1; } if (start < hi.length() - 1) highlight.put(hi.substring(start), ""); } } catch (Exception e) { return drawError("Invalid input", "Your request for this particular work and " + "page was not properly formatted."); } try { wrapper = DatabaseHook.getConnection(); Connection c = wrapper.connection(); Statement stmt = c.createStatement(); StringBuffer text = new StringBuffer(); String linkAhead = ""; String title = ""; ResultSet rs = stmt.executeQuery ("SELECT Work.title, WrittenBy.name " + "FROM Work, WrittenBy " + "WHERE WrittenBy.work = Work.id AND Work.id = " + work); if (rs.next()) { title = "" + rs.getString(1) + "
by " + rs.getString(2) + "

"; } rs = stmt.executeQuery ("SELECT MAX(page) FROM UniquePage WHERE work=" + work); if (rs.next()) { int lastPage = rs.getInt(1); linkAhead = "
"; linkAhead += "\npage " + page + " of " + lastPage + " | "; if (page > 1) linkAhead += "prev. page | "; if (page < lastPage) linkAhead += "next page"; linkAhead += "
\n"; } String query = "SELECT w.context " + "FROM WordDetails w, UniquePage u " + "WHERE u.work=" + work + " AND u.page=" + page + " AND w.gpos >= u.global_start AND w.gpos <= u.global_end " + "ORDER BY w.gpos"; rs = stmt.executeQuery(query); text.append(title); text.append(linkAhead); text.append("

\n"); while (rs.next()) { String token = rs.getString(1).replace('-', '\n'); Object good = null; if (highlight != null) { good = highlight.get(PorterStemmer.stem(token)); } if (good != null) text.append(""); text.append(token).append(" "); if (token.endsWith("\n")) { text.append("
"); } if (good != null) text.append("
"); } text.append("

\n").append(linkAhead); wrapper.checkIn(); return text.toString(); } catch (SQLException e) { wrapper.checkIn(); throw e; } } }