Servlet Runner


Here's the meat of a simple version based on FileRunner. The major differences are there to eliminate instance variables and avoid potential multi-threading issues. It requires the usual Java web application scaffolding to make it work. http://hostname/appname/fit/arithmetic.html will cause it to process the file arithmetic.html in the root directory of the web application. If the input file is saved as a JSP, the following line will generate the right path automagically:
   <a href="fit<%= request.getServletPath() %>">run</a>
--StevenNewton

 public class FitRunnerServlet extends HttpServlet {
  protected void doGet(
    HttpServletRequest request,
    HttpServletResponse response)
    throws ServletException, IOException {
    Fixture fixture = new Fixture();
    String input = read(request, fixture);
    Parse tables;
    ServletOutputStream sos = response.getOutputStream();
    PrintWriter out = new PrintWriter(sos);
    try {
      if (input.indexOf("<wiki>") >= 0) {
	tables = new Parse(input, new String[] { "wiki", "table", "tr", "td" });
	fixture.doTables(tables.parts);
      } else {
	tables = new Parse(input, new String[] { "table", "tr", "td" });
	fixture.doTables(tables);
      }
    } catch (Exception e) {
      tables =
	new Parse("body", "Unable to parse input. Input ignored.", null, null);
      exception(e, tables, fixture);
    }
    tables.print(out);
    out.flush();
    log(fixture.counts());
  }

/** * @param request * @return */ protected String read(HttpServletRequest request, Fixture fixture) throws IOException { String inputPath = request.getPathInfo(); String inputUrl = request.getPathTranslated(); log("pathTranslated=" + inputUrl); File inputFile = new File(inputUrl); fixture.summary.put("input file", inputPath); fixture.summary.put("input update", new Date(inputFile.lastModified())); long fileLength = inputFile.length(); if (fileLength > Integer.MAX_VALUE) { fileLength = Integer.MAX_VALUE; } char chars[] = new char[(int) fileLength]; FileReader in = new FileReader(inputFile); in.read(chars); in.close(); log("Read " + chars.length + " characters from URL " + inputUrl); return new String(chars); }

protected void exception(Exception e, Parse tables, Fixture fixture) { fixture.exception(tables, e); } }

 

Last edited August 29, 2005
Return to WelcomeVisitors