From f5adf51f59628c210d6bc8838df99d9b64002ee5 Mon Sep 17 00:00:00 2001
From: ulrich
Date: Fri, 25 Jun 2021 09:55:05 +0000
Subject: [PATCH] Logik in den Actor ausgelagert

---
 src/de/uhilger/httpserver/adoc/AdocActor.java |   51 +++++++++++++++++++++++++++++++++++++++++----------
 1 files changed, 41 insertions(+), 10 deletions(-)

diff --git a/src/de/uhilger/httpserver/adoc/AdocActor.java b/src/de/uhilger/httpserver/adoc/AdocActor.java
index e918f00..94944f9 100644
--- a/src/de/uhilger/httpserver/adoc/AdocActor.java
+++ b/src/de/uhilger/httpserver/adoc/AdocActor.java
@@ -17,8 +17,12 @@
  */
 package de.uhilger.httpserver.adoc;
 
+import com.sun.net.httpserver.HttpExchange;
+import de.uhilger.httpserver.base.HttpResponder;
 import java.io.File;
+import java.io.IOException;
 import java.util.Map;
+import java.util.logging.Level;
 import java.util.logging.Logger;
 
 import static org.asciidoctor.Asciidoctor.Factory.create;
@@ -42,28 +46,48 @@
   private static final Logger logger = Logger.getLogger(AdocActor.class.getName());
   
   private static final String DOT = ".";
-  private static final String HTML = "html";
-  private static final String PDF = "pdf";
+  public static final String HTML = "html";
+  public static final String PDF = "pdf";
   
+  public void handle(HttpExchange e, String fileBase, String fileName) throws IOException {
+    File adocfile = new File(fileBase, fileName);
+    logger.fine("adocfile: " + adocfile.getAbsolutePath());
+    AdocActor actor = new AdocActor();
+    File outfile = actor.getTargetFile(adocfile, AdocActor.HTML);
+    logger.fine("outfile: " + outfile.getAbsolutePath());
+    HttpResponder fs = new HttpResponder();
+    fs.serveFile(e, outfile);
+  }
+  
+  public File getTargetFile(File adocfile, String ext) {
+    String nameext = adocfile.getName();
+    String fname = nameext.substring(0, nameext.lastIndexOf(DOT));
+    File outfile = new File(adocfile.getParentFile(), fname + DOT + ext);
+    logger.log(Level.FINE, "out: {0}", outfile.getAbsolutePath());
+    return outfile;
+  }
   
   public void processAdocFile(File adocfile, String pdf) {
     
     String absname = adocfile.getAbsolutePath();
-    logger.fine("in: " + absname);
+    logger.log(Level.FINE, "in: {0}", absname);
  
     // HTML-Datei ermitteln
-    String nameext = adocfile.getName();
-    String fname = nameext.substring(0, nameext.lastIndexOf(DOT));
-    File htmlfile = new File(adocfile.getParentFile(), fname + DOT + HTML);
-    File outfile = htmlfile; // Standardmaessig wird HTML zurueckgegeben
-    logger.fine("out: " + outfile.getAbsolutePath());
+    //String nameext = adocfile.getName();
+    //String fname = nameext.substring(0, nameext.lastIndexOf(DOT));
+    //File htmlfile = new File(adocfile.getParentFile(), fname + DOT + HTML);
+    //File outfile = htmlfile; // Standardmaessig wird HTML zurueckgegeben
+    //logger.fine("out: " + outfile.getAbsolutePath());
     //response.setCharacterEncoding("UTF-8");
+    File outfile = getTargetFile(adocfile, HTML);
+    File htmlfile = outfile;
     
     /*
       nach HTML transformieren, wenn die Quelle sich geandert hat oder 
       die HTML-Datei noch nicht existiert
     */
     if(!htmlfile.exists() || adocfile.lastModified() > htmlfile.lastModified()) {
+      logger.fine("calling transform for " + absname);
       transform(absname);
     }
  
@@ -74,7 +98,8 @@
     */
     
     if(null != pdf && pdf.equalsIgnoreCase(Boolean.TRUE.toString())) {
-      File pdffile = new File(adocfile.getParentFile(), fname + DOT + PDF);
+      //File pdffile = new File(adocfile.getParentFile(), fname + DOT + PDF);
+      File pdffile = getTargetFile(adocfile, PDF);
       outfile = pdffile; // PDF soll zurueckgegeben werden
       if(!pdffile.exists() || adocfile.lastModified() > pdffile.lastModified()) {
         //response.setContentType("application/pdf");
@@ -110,6 +135,7 @@
    * nach PDF transformiert werden soll
    */
   private void transform(String fileName, String backend) {    
+    logger.fine("fileName: " + fileName + ", backend: " + backend);
     Map<String, Object> attributes;
     File outFile = new File(fileName);
     String thisDirName = outFile.getParent();
@@ -139,9 +165,14 @@
               .attributes(attributes).asMap();
     }
     
+    File adcf = new File(fileName);
+    logger.fine("before asciidoctor create, adcf: " + adcf.getAbsolutePath());
     Asciidoctor asciidoctor = create();    
+    logger.fine("asciidoctor created.");
     asciidoctor.requireLibrary("asciidoctor-diagram");
-    asciidoctor.convertFile(new File(fileName), options);    
+    logger.fine("asciidoctor requireLibrary diagram passed.");
+    logger.fine("calling asciidoctor.convert for file " + adcf.getAbsolutePath());
+    asciidoctor.convertFile(adcf, options);    
   }
   
 }

--
Gitblit v1.9.3