/** Reflow XML */ public void doFormat() { Vector parts = new Vector(); char[] chars = original.toCharArray(); int index = 0; int first = 0; String part = null; while (index < chars.length) { // Check for start of tag if (chars[index] == '<') { // Did we have data before this tag? if (first < index) { part = new String(chars, first, index - first); part = part.trim(); // Save non-whitespace data if (part.length() > 0) { parts.addElement(part); } } // Save the start of tag first = index; } // Check for end of tag if (chars[index] == '>') { // Save the tag part = new String(chars, first, index - first + 1); parts.addElement(part); first = index + 1; } // Check for end of line if ((chars[index] == '\n') || (chars[index] == '\r')) { // Was there data on this line? if (first < index) { part = new String(chars, first, index - first); part = part.trim(); // Save non-whitespace data if (part.length() > 0) { parts.addElement(part); } } first = index + 1; } index++; } // Reflow as XML StringBuffer buf = new StringBuffer(); Object[] list = parts.toArray(); int indent = 0; int pad = 0; index = 0; while (index < list.length) { part = (String) list[index]; if (buf.length() == 0) { // Just add first tag (should be XML header) buf.append(part); } else { // All other parts need to start on a new line buf.append('\n'); // If we're at an end tag then decrease indent if (part.startsWith("</")) { indent--; } // Add any indent for (pad = 0; pad < indent; pad++) { buf.append(" "); } // Add the tag or data buf.append(part); // If this is a start tag then increase indent if (part.startsWith("<") && !part.startsWith("</") && !part.endsWith("/>")) { indent++; // Check for special <tag>data</tag> case if ((index + 2) < list.length) { part = (String) list[index + 2]; if (part.startsWith("</")) { part = (String) list[index + 1]; if (!part.startsWith("<")) { buf.append(part); part = (String) list[index + 2]; buf.append(part); index = index + 2; indent--; } } } } } index++; } formatted = new String(buf); }
public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { response.setContentType("text/html"); PrintWriter out = response.getWriter(); ResultSet rs = null; try { // SET UP Context environment, to look for Data Pooling Resource Context initCtx = new InitialContext(); Context envCtx = (Context) initCtx.lookup("java:comp/env"); DataSource ds = (DataSource) envCtx.lookup("jdbc/TestDB"); Connection dbcon = ds.getConnection(); // ########### SEARCH INPUT PARAMETERS, EXECUTE search // ##################################################### Vector<String> params = new Vector<String>(); params.add(request.getParameter("fname")); params.add(request.getParameter("lname")); params.add(request.getParameter("title")); params.add(request.getParameter("year")); params.add(request.getParameter("director")); List<Movie> movies = new ArrayList<Movie>(); movies = SQLServices.getMovies(params.toArray(new String[params.size()]), dbcon); // ########## SET DEFAULT SESSION() PARAMETERS #################################### request.getSession().removeAttribute("movies"); request.getSession().removeAttribute("linkedListMovies"); request.getSession().removeAttribute("hasPaged"); request.getSession().setAttribute("hasPaged", "no"); request.getSession().setAttribute("movies", movies); request.getSession().setAttribute("currentIndex", "0"); request.getSession().setAttribute("defaultN", "5"); // ########## IF MOVIES FROM SEARCH NON-EMPTY ########################################### List<String> fields = Movie.fieldNames(); int count = 1; if (!movies.isEmpty()) { request.setAttribute("movies", movies); for (String field : fields) { request.setAttribute("f" + count++, field); } request.getRequestDispatcher("../movieList.jsp").forward(request, response); } else { out.println("<html><head><title>error</title></head>"); out.println("<body><h1>could not find any movies, try your search again.</h1>"); out.println("<p> we are terribly sorry, please go back. </p>"); out.println("<table border>"); out.println("</table>"); } dbcon.close(); } catch (SQLException ex) { while (ex != null) { System.out.println("SQL Exception: " + ex.getMessage()); ex = ex.getNextException(); } } catch (java.lang.Exception ex) { out.println( "<html>" + "<head><title>" + "moviedb: error" + "</title></head>\n<body>" + "<p>SQL error in doGet: " + ex.getMessage() + "</p></body></html>"); return; } out.close(); }