WebBox Klassenbibliothek
ulrich@undisclosed
2020-04-21 3bbfbc4952bf689a2f9024edc542ef609bc5c672
src/de/uhilger/wbx/web/AdocServlet.java
@@ -24,6 +24,7 @@
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.HashMap;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
@@ -34,18 +35,22 @@
import static org.asciidoctor.OptionsBuilder.options;
import static org.asciidoctor.Asciidoctor.Factory.create;
import org.asciidoctor.Asciidoctor;
import org.asciidoctor.OptionsBuilder;
/**
 * Das AdocServlet wandelt AsciiDoc-Inhalte (*.adoc) 
 * zu HTML-Seiten
 * zu HTML-Seiten und PDF-Dokumenten
 * 
 * ?pdf=true im URL fuegt PDF-Ausgabe hinzu
 */
public class AdocServlet extends HttpServlet  {
  
  private static final String DOT = ".";
  private static final String HTMLEXT = ".html";
  private static final String HTML = "html";
  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>
@@ -70,16 +75,30 @@
      // HTML-Datei ermitteln
      String nameext = adocfile.getName();
      String fname = nameext.substring(0, nameext.lastIndexOf(DOT));
      File htmlfile = new File(adocfile.getParentFile(), fname + HTMLEXT);
      File htmlfile = new File(adocfile.getParentFile(), fname + DOT + HTML);
      
      /*
        transformieren, wenn die Quelle sich geandert hat oder
        nach HTML transformieren, wenn die Quelle sich geandert hat oder
        die HTML-Datei noch nicht existiert
      */
      if(!htmlfile.exists() || adocfile.lastModified() > htmlfile.lastModified()) {
        transform(absname);
      }
      /*
        nach PDF transformieren, wenn der Parameter pdf=true existiert und
        wenn die Quelle sich geandert hat oder
        die PDF-Datei noch nicht existiert
      */
      String pdf = request.getParameter(PDF);
      if(null != pdf && pdf.length() > 0 && pdf.equalsIgnoreCase(Boolean.TRUE.toString())) {
        File pdffile = new File(adocfile.getParentFile(), fname + DOT + PDF);
        if(!pdffile.exists() || adocfile.lastModified() > pdffile.lastModified()) {
          transform(absname, PDF);
        }
      }
      
      // HTML-Datei ausgeben
      FileInputStream in = new FileInputStream(htmlfile);
      BufferedReader reader = new BufferedReader(new InputStreamReader(in));
      String line;
@@ -89,50 +108,26 @@
    }
  }
  
  private void transform(String absoluteFileName) {
    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);
  private void transform(String fileName) {
    transform(fileName, null);
  }  
  private void transform(String fileName, String backend) {
    Map<String, Object> attributes = new HashMap<>();
    attributes.put("no_footer", false);
    attributes.put("source_highlighter", "highlightjs");
  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>");
    Map<String, Object> options = new HashMap<>();
    options.put("attributes", attributes);
    options.put("in_place", false);
    if(null != backend) {
      options.put("backend", backend);
    }
    
    Asciidoctor asciidoctor = create();
    asciidoctor.convertFile(new File(fileName), options);
  }
  // <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 +163,7 @@
   */
  @Override
  public String getServletInfo() {
    return "AdocServlet";
  }// </editor-fold>
    return SERVLET_NAME;
  }
}