Beispiel #1
0
  public void doGet(HttpServletRequest request, HttpServletResponse response)
      throws ServletException, IOException {
    String path = request.getPathInfo();
    boolean htmlOutput = true;
    String language = null;
    String contentType = "text/html; charset='utf-8'";

    if (path != null) {
      if (path.endsWith(".rdf")) {
        contentType = "application/rdf+xml; charset='utf-8'";
        htmlOutput = false;
        language = "RDF/XML";
      } else if (path.endsWith(".xml")) {
        contentType = "application/xml; charset='utf-8'";
        htmlOutput = false;
        language = "RDF/XML";
      } else if (path.endsWith(".n3")) {
        contentType = "text/n3; charset='utf-8'";
        htmlOutput = false;
        language = "N3";
      }
    }

    response.setContentType(contentType);
    response.setStatus(HttpServletResponse.SC_OK);

    Writer writer = response.getWriter();

    synchronized (board) {
      if (htmlOutput) {
        writer.write(
            "<!DOCTYPE html>\n"
                + "<html lang='en'>"
                + "<head><meta charset='utf-8'/><title>MATe model</title></head>"
                + "<body><ul>");
        StmtIterator it = model.listStatements();
        /* TODO: well, this could be prettier */
        while (it.hasNext()) writer.write("<li>" + it.nextStatement() + "</li>");
        writer.write("<ul></body></html>");
      } else model.write(writer, language);
    }
  }
  @Override
  protected void doPost(HttpServletRequest req, HttpServletResponse resp)
      throws ServletException, IOException {

    String diagramUXF = req.getParameter("diagramUXF");

    // Get an output stream suitable for writing binary
    // data to the client.
    ServletOutputStream respOutputStream = resp.getOutputStream();

    // Umlet should behave as if running from command line, e.g.
    // as if being called by
    // "-action=convert -format=svg -filename=inputfile.uxf"
    Program.RUNTIME_TYPE = RuntimeType.BATCH;

    // OutputHandler performs conversion from UXF to SVG and
    // requires DiagramHandler object for representing a UXF
    // document, which requires that the UXF is accessible
    // through a File interface.
    //
    // Create a temporary file and write UXF to it.

    // Take hash of UXF for good enough uniqueness.
    String tempSuffix = new Integer(diagramUXF.hashCode()).toString();
    File temp = File.createTempFile(tempSuffix, ".tmp");
    FileWriter tempWriter = new FileWriter(temp);
    tempWriter.write(diagramUXF);
    tempWriter.close();

    DiagramHandler diagramHandler = new DiagramHandler(temp);

    // Convert to SVG and write out binary data to client.
    try {
      OutputHandler.createToStream("svg", respOutputStream, diagramHandler);
    } catch (Exception e) {
      throw new ServletException(e);
    }

    // Flush to commit the response.
    respOutputStream.flush();
  }