From 70be19081e67ce8013bacc1d778629d09867f882 Mon Sep 17 00:00:00 2001
From: ulrich@undisclosed
Date: Mon, 20 Apr 2020 13:26:13 +0000
Subject: [PATCH] PDF-Ausgabe zu AdocServlet hinzugefuegt

---
 src/de/uhilger/wbx/web/AdocServlet.java |   85 +++++++++++++++++++-----------------------
 1 files changed, 39 insertions(+), 46 deletions(-)

diff --git a/src/de/uhilger/wbx/web/AdocServlet.java b/src/de/uhilger/wbx/web/AdocServlet.java
index 7b219b6..bd13c31 100644
--- a/src/de/uhilger/wbx/web/AdocServlet.java
+++ b/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;
+  }
 
 }

--
Gitblit v1.9.3