WebBox Klassenbibliothek
ulrich@undisclosed
2020-04-20 70be19081e67ce8013bacc1d778629d09867f882
src/de/uhilger/wbx/web/AdocServlet.java
@@ -39,13 +39,15 @@
/**
 * Das AdocServlet wandelt AsciiDoc-Inhalte (*.adoc) 
 * zu HTML-Seiten
 *
 * zu HTML-Seiten und PDF-Dokumenten
 */
public class AdocServlet extends HttpServlet  {
  
  private static final String DOT = ".";
  private static final String HTMLEXT = ".html";
  private static final String PDFEXT = ".pdf";
  private static final String PDF = "pdf";
  private static final String SERVLET_NAME = "AdocServlet";
  
  /**
   * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
@@ -77,9 +79,18 @@
        die HTML-Datei noch nicht existiert
      */
      if(!htmlfile.exists() || adocfile.lastModified() > htmlfile.lastModified()) {
        transform(absname);
        transform(absname, htmlfile);
      }
      String pdf = request.getParameter(PDF);
      if(null != pdf && pdf.length() > 0 && pdf.equalsIgnoreCase(Boolean.TRUE.toString())) {
        File pdffile = new File(adocfile.getParentFile(), fname + PDFEXT);
        if(!pdffile.exists() || adocfile.lastModified() > pdffile.lastModified()) {
          transform(absname, pdffile);
        }
      }
      
      // HTML-Datei ausgeben
      FileInputStream in = new FileInputStream(htmlfile);
      BufferedReader reader = new BufferedReader(new InputStreamReader(in));
      String line;
@@ -89,50 +100,32 @@
    }
  }
  
  private void transform(String absoluteFileName) {
  private void transform(String absoluteFileName, File target) {
    Map<String, Object> options = null;
    Map<String, Object> attributes;
    Asciidoctor asciidoctor = create();    
    Map<String, Object> attributes = attributes()
                                        .noFooter(false)
                                        //.tableOfContents(true)
                                        //.sectionNumbers(true)
                                        .asMap();
    Map<String, Object> options = options().inPlace(false)
                                       .attributes(attributes)
                                       .asMap();
    asciidoctor.convertFile(new File(absoluteFileName), options);
    String fname = target.getName().toLowerCase();
    if(fname.endsWith(HTMLEXT)) {
      attributes = attributes().noFooter(false)
                                .sourceHighlighter("highlightjs")
                                //.tableOfContents(true)
                                //.sectionNumbers(true)
                                .asMap();
      options = options().inPlace(false)
                                .attributes(attributes)
                                .asMap();
    } else if(fname.endsWith(PDFEXT)) {
      attributes = attributes().noFooter(false)
                                .asMap();
      options = options().inPlace(false)
                          .attributes(attributes)
                          .backend("pdf")
                          .asMap();
    }
    asciidoctor.convertFile(new File(absoluteFileName), options);
  }  
  private void test(HttpServletRequest request, HttpServletResponse response)
          throws IOException
  {
    response.setContentType("text/html;charset=UTF-8");
    try (PrintWriter out = response.getWriter()) {
      /* TODO output your page here. You may use following sample code. */
      out.println("<!DOCTYPE html>");
      out.println("<html>");
      out.println("<head>");
      out.println("<title>Servlet AdocServlet</title>");
      out.println("</head>");
      out.println("<body>");
      out.println("<h1>Servlet AdocServlet at " + request.getContextPath() + "</h1>");
      out.println("<p>request.getRequestURI(): " + request.getRequestURI() + "</p>");
      out.println("<p>request.getRequestURL(): " + request.getRequestURL() + "</p>");
      out.println("<p>request.getServletPath(): " + request.getServletPath() + "</p>");
      out.println("<p>request.getPathInfo(): " + request.getPathInfo() + "</p>");
      String vPath = request.getServletPath();
      out.println("<p>vPath: " + vPath);
      out.println("<p>getServletContext().getRealPath(vPath): " +
              getServletContext().getRealPath(vPath) + "</p>");
      out.println("</body>");
      out.println("</html>");
    }
  }
  // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
  /**
   * Handles the HTTP <code>GET</code> method.
   *
@@ -168,7 +161,7 @@
   */
  @Override
  public String getServletInfo() {
    return "AdocServlet";
  }// </editor-fold>
    return SERVLET_NAME;
  }
}