Преглед на файлове

Refactoring and organizing

jhonnymertz преди 9 години
родител
ревизия
714adc2f18

+ 18 - 35
src/main/java/br/eti/mertz/wkhtmltopdf/wrapper/Pdf.java

@@ -5,13 +5,12 @@ import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.List;
 
-import br.eti.mertz.wkhtmltopdf.wrapper.options.GlobalOption;
-import lombok.Data;
+import br.eti.mertz.wkhtmltopdf.wrapper.params.Param;
+import br.eti.mertz.wkhtmltopdf.wrapper.params.Symbol;
 
-@Data
 public class Pdf implements PdfService {
 
-    static final String STDOUT = "-";
+    private static final String STDOUT = "-";
 
 	private String command;
 	private List<Param> params;
@@ -40,20 +39,6 @@ public class Pdf implements PdfService {
         this.htmlInput = input;
 	}
 
-	/**
-	 * TODO Add a HTML file, a HTML string or a page from a URL
-	 */
-	public void addCover(String cover) {
-		// TODO Auto-generated method stub
-	}
-
-	/**
-	 * TODO just the TOC option from wkhtmltopdf
-	 */
-	public void addToc() {
-		// TODO Auto-generated method stub
-	}
-
 	public void addParam(Param param) {
 		params.add(param);
 	}
@@ -68,13 +53,11 @@ public class Pdf implements PdfService {
 	 * @throws IOException
 	 * @throws InterruptedException
 	 */
-	public File saveAs(String path) throws IOException, InterruptedException {
-        File file = new File(path);
-        getPDF(path);
-        return file;
+	public void saveAs(String path) throws IOException, InterruptedException {
+        saveAs(path, getPDF());
 	}
 
-    public File saveAs(String path, byte[] document) throws IOException {
+    private File saveAs(String path, byte[] document) throws IOException {
         File file = new File(path);
 
         BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(new FileOutputStream(file));
@@ -85,18 +68,26 @@ public class Pdf implements PdfService {
         return file;
     }
 
-    public byte[] getPDF(String path) throws IOException, InterruptedException {
-        Runtime runtime = Runtime.getRuntime();
+    public byte[] getPDF() throws IOException, InterruptedException {
+        return getPDF(STDOUT);
+    }
+
+    private byte[] getPDF(String path) throws IOException, InterruptedException {
+
         if(htmlFromString && !this.params.contains(new Param("-"))) {
             this.addParam(new Param("-"));
         }
-        String command = this.commandWithParameters() + Symbol.separator + path;
+        String command = this.commandParameters() + Symbol.separator + path;
+
+        Runtime runtime = Runtime.getRuntime();
         Process process = runtime.exec(command);
+
         if(htmlFromString) {
             OutputStream stdInStream = process.getOutputStream();
             stdInStream.write(htmlInput.getBytes("UTF-8"));
             stdInStream.close();
         }
+
         InputStream stdOutStream = process.getInputStream();
         InputStream stdErrStream = process.getErrorStream();
         process.waitFor();
@@ -120,11 +111,7 @@ public class Pdf implements PdfService {
         return stdOut.toByteArray();
     }
 
-    public byte[] getPDF() throws IOException, InterruptedException {
-        return getPDF(STDOUT);
-    }
-
-	public String commandWithParameters() {
+	public String commandParameters() {
 		StringBuilder sb = new StringBuilder();
 		for (Param param : params) {
 			sb.append(param);
@@ -133,8 +120,4 @@ public class Pdf implements PdfService {
 		return command + sb.toString();
 	}
 
-	public void addParam(GlobalOption option) {
-		addParam(new Param(option.toString()));
-	}
-
 }

+ 7 - 5
src/main/java/br/eti/mertz/wkhtmltopdf/wrapper/PdfService.java

@@ -1,17 +1,19 @@
 package br.eti.mertz.wkhtmltopdf.wrapper;
 
+import br.eti.mertz.wkhtmltopdf.wrapper.params.Param;
+
 import java.io.File;
 import java.io.IOException;
 
 public interface PdfService {
 	
 	void addHtmlInput(String page);
+
+    void addParam(Param param);
+
+    void addParam(Param... params);
 	
-	void addCover(String cover);
-	
-	void addToc();
-	
-	File saveAs(String path) throws IOException, InterruptedException;
+	void saveAs(String path) throws IOException, InterruptedException;
 
 	byte[] getPDF() throws IOException, InterruptedException;
 }

+ 1 - 6
src/main/java/br/eti/mertz/wkhtmltopdf/wrapper/Param.java → src/main/java/br/eti/mertz/wkhtmltopdf/wrapper/params/Param.java

@@ -1,12 +1,7 @@
-package br.eti.mertz.wkhtmltopdf.wrapper;
+package br.eti.mertz.wkhtmltopdf.wrapper.params;
 
-import lombok.Data;
-import lombok.NonNull;
-
-@Data
 public class Param {
 
-	@NonNull
 	private String key;
 
 	private String value;

+ 2 - 5
src/main/java/br/eti/mertz/wkhtmltopdf/wrapper/Params.java → src/main/java/br/eti/mertz/wkhtmltopdf/wrapper/params/Params.java

@@ -1,14 +1,11 @@
-package br.eti.mertz.wkhtmltopdf.wrapper;
+package br.eti.mertz.wkhtmltopdf.wrapper.params;
 
 import java.util.ArrayList;
 import java.util.List;
 
-import lombok.Data;
-
-@Data
 public class Params {
 
-	List<Param> params;
+	private List<Param> params;
 
 	public Params() {
 		this.params = new ArrayList<Param>();

+ 1 - 1
src/main/java/br/eti/mertz/wkhtmltopdf/wrapper/Symbol.java → src/main/java/br/eti/mertz/wkhtmltopdf/wrapper/params/Symbol.java

@@ -1,4 +1,4 @@
-package br.eti.mertz.wkhtmltopdf.wrapper;
+package br.eti.mertz.wkhtmltopdf.wrapper.params;
 
 public enum Symbol {
 

+ 8 - 0
src/tests/java/br/eti/mertz/wkhtmltopdf/wrapper/PdfTest.java

@@ -1,5 +1,6 @@
 package br.eti.mertz.wkhtmltopdf.wrapper;
 
+import br.eti.mertz.wkhtmltopdf.wrapper.params.Param;
 import org.apache.pdfbox.pdfparser.PDFParser;
 import org.apache.pdfbox.pdmodel.PDDocument;
 import org.apache.pdfbox.util.PDFTextStripper;
@@ -12,6 +13,13 @@ import static org.hamcrest.core.StringContains.containsString;
 
 public class PdfTest {
 
+    @Test
+    public void testCommand() throws Exception {
+        Pdf pdf = new Pdf();
+        pdf.addParam(new Param("--enable-javascript"), new Param("--html-header", "file:///example.html"));
+        Assert.assertThat("command params should contain the --enable-javascript and --html-header", pdf.commandParameters(), containsString("wkhtmltopdf --enable-javascript --html-header file:///example.html"));
+    }
+
     @Test
     public void testPdfFromStringTo() throws Exception {