improved PDF export support (better Chapter titles)
[phpeclipse.git] / archive / net.sourceforge.phpeclipse.wiki / src / net / sourceforge / phpeclipse / wiki / export / pdf / WikiPDFExporter.java
1 package net.sourceforge.phpeclipse.wiki.export.pdf;
2
3 import java.awt.Color;
4 import java.io.FileOutputStream;
5 import java.io.IOException;
6 import java.io.StringReader;
7 import java.lang.reflect.InvocationTargetException;
8 import java.util.ArrayList;
9 import java.util.Collections;
10 import java.util.List;
11
12 import net.sourceforge.phpeclipse.wiki.builder.CreatePageAction;
13 import net.sourceforge.phpeclipse.wiki.editor.WikiEditorPlugin;
14
15 import org.eclipse.core.resources.IFile;
16 import org.eclipse.core.resources.IResource;
17 import org.eclipse.core.runtime.CoreException;
18 import org.eclipse.core.runtime.IPath;
19 import org.eclipse.core.runtime.IProgressMonitor;
20 import org.eclipse.core.runtime.IStatus;
21 import org.eclipse.core.runtime.MultiStatus;
22 import org.eclipse.core.runtime.Path;
23 import org.eclipse.core.runtime.Status;
24 import org.eclipse.jface.operation.IRunnableWithProgress;
25 import org.eclipse.ui.PlatformUI;
26 import org.eclipse.ui.dialogs.IOverwriteQuery;
27
28 import com.lowagie.text.Chapter;
29 import com.lowagie.text.Document;
30 import com.lowagie.text.Font;
31 import com.lowagie.text.FontFactory;
32 import com.lowagie.text.PageSize;
33 import com.lowagie.text.Paragraph;
34 import com.lowagie.text.pdf.PdfWriter;
35
36 //import de.java2html.converter.JavaSource2HTMLConverter;
37 //import de.java2html.javasource.JavaSource;
38 //import de.java2html.javasource.JavaSourceParser;
39 //import de.java2html.options.Java2HtmlConversionOptions;
40
41 public final class WikiPDFExporter implements IRunnableWithProgress {
42   //The constants for the overwrite 3 state
43   private static final int OVERWRITE_NOT_SET = 0;
44
45   private static final int OVERWRITE_NONE = 1;
46
47   private static final int OVERWRITE_ALL = 2;
48
49   private int overwriteState = OVERWRITE_NOT_SET;
50
51   private List errorTable = new ArrayList(1);
52
53   private List fResourcesToExport;
54
55   private IPath fPath;
56
57   private IOverwriteQuery fOverwriteCallback;
58
59   public WikiPDFExporter(List resources, String destinationPath, IOverwriteQuery overwriteImplementor) {
60     super();
61     fResourcesToExport = resources;
62     fPath = new Path(destinationPath);
63     fOverwriteCallback = overwriteImplementor;
64   }
65
66   //  public void export(IContainer folder, String exportDirectoryName, String srcBasePath, IProgressMonitor monitor) throws
67   // IOException, CoreException,
68   //      InstantiationException, IllegalAccessException, ClassNotFoundException {
69   //    // exportDirectory = new File(exportDirectoryName);
70   //    IResource[] resources = folder.members(IResource.FILE);
71   //    String templateFileName = Util.getExportTemplate(folder);
72   //// monitor.beginTask(WikiEditorPlugin.getResourceString("Export.wikiPages"), resources.length + 1);
73   //    for (int i = 0; i < resources.length; i++) {
74   //      if (resources[i] instanceof IFile) {
75   //        monitor.subTask(WikiEditorPlugin.getResourceString("Export.exportFile")+resources[i].getLocation());
76   //        CreatePageAction.createPage(templateFileName, (IFile) resources[i], exportDirectoryName, srcBasePath);
77   //        monitor.worked(1);
78   //      } else if (resources[i] instanceof IFolder) {
79   //        monitor.subTask(WikiEditorPlugin.getResourceString("Export.exportFolder")+resources[i].getLocation());
80   //        export((IFolder) resources[i], exportDirectoryName, srcBasePath, monitor);
81   //        monitor.worked(1);
82   //      }
83   //    }
84   //    // monitor.subTask(WikiEditorPlugin.getResourceString("Export.linkedResources"));
85   //    // exportLinkedResources();
86   //    // createIndex();
87   //// monitor.worked(1);
88   //  }
89
90   /**
91    * TODO: This is a horrible hack for a quick solution.
92    */
93   //  private void createIndex() throws IOException {
94   //    File indexFile = createHtmlFile("index");
95   //
96   //    PrintWriter writer = new PrintWriter(new FileWriter(indexFile));
97   //    writer.println("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
98   //    writer.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01//EN\" \"http://www.w3.org/TR/html4/strict.dtd\">");
99   //    writer.println("<html>");
100   //    writer.println(" <head>");
101   //    writer.print(" <title>Index</title>");
102   //    writer.println(" </head>");
103   //    writer.println(" <body>");
104   //
105   //    Iterator iterator = index.iterator();
106   //    while (iterator.hasNext()) {
107   //      String name = (String) iterator.next();
108   //      writer.print(" <br/>");
109   //      writer.println("<a href=\"" + name + ".html\">" + name + "</a>");
110   //    }
111   //
112   //    writer.println(" </body>");
113   //    writer.println(" </html>");
114   //    writer.flush();
115   //    writer.close();
116   //  }
117   //    private void exportLinkedResources() throws IOException {
118   //        if (!exportLinkMaker.hasLinkedDocuments()) {
119   //            return;
120   //        }
121   //        File workspaceExport = new File(exportDirectory, WikiPDFExporter.WORKSPACE);
122   //        if (!workspaceExport.exists()) {
123   //            workspaceExport.mkdir();
124   //        }
125   //        HashMap map = exportLinkMaker.getLinkedResources();
126   //        Iterator iterator = map.keySet().iterator();
127   //        while (iterator.hasNext()) {
128   //            IResource resource = (IResource) iterator.next();
129   //            String location = (String) map.get(resource);
130   //            export(resource, location);
131   //        }
132   //    }
133   //  private void export(IResource resource, String location) throws IOException {
134   //    File destination = new File(exportDirectory, location);
135   //
136   //    if (destination.isDirectory()) {
137   //      return;
138   //    }
139   //    if (!destination.exists()) {
140   //      destination.getParentFile().mkdirs();
141   //    }
142   //    File source = new File(resource.getLocation().toString());
143   //    if (isJavaResource(resource)) {
144   //      javaToHtml(source, new File(destination.getParentFile(), destination.getName()));
145   //    } else {
146   //      copy(source, destination);
147   //    }
148   //  }
149   //  private boolean isJavaResource(IResource resource) {
150   //    return "java".equals(resource.getFileExtension());
151   //  }
152   //  private void javaToHtml(File source, File destination) throws IOException {
153   //    JavaSource java = new JavaSourceParser().parse(new FileReader(source));
154   //    JavaSource2HTMLConverter converter = new JavaSource2HTMLConverter(java);
155   //    Java2HtmlConversionOptions options = Java2HtmlConversionOptions.getDefault();
156   //    options.setShowLineNumbers(true);
157   //    options.setShowFileName(true);
158   //    options.setShowJava2HtmlLink(true);
159   //    converter.setConversionOptions(options);
160   //    FileWriter writer = new FileWriter(destination);
161   //    converter.convert(writer);
162   //    writer.flush();
163   //    writer.close();
164   //  }
165   //  private void copy(File source, File dest) throws IOException {
166   //    FileChannel in = null;
167   //    FileChannel out = null;
168   //    try {
169   //      in = new FileInputStream(source).getChannel();
170   //      out = new FileOutputStream(dest).getChannel();
171   //      long size = in.size();
172   //      MappedByteBuffer buf = in.map(FileChannel.MapMode.READ_ONLY, 0, size);
173   //      out.write(buf);
174   //    } finally {
175   //      if (in != null) {
176   //        in.close();
177   //      }
178   //      if (out != null) {
179   //        out.close();
180   //      }
181   //    }
182   //  }
183   /*
184    * (non-Javadoc)
185    * 
186    * @see org.eclipse.jface.operation.IRunnableWithProgress#run(org.eclipse.core.runtime.IProgressMonitor)
187    */
188   public void run(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException {
189
190     WikiFilesVisitor visitor = new WikiFilesVisitor();
191     for (int i = 0; i < fResourcesToExport.size(); i++) {
192       try {
193         ((IResource) fResourcesToExport.get(i)).accept(visitor);
194       } catch (CoreException e) {
195         e.printStackTrace();
196       }
197     }
198
199     List list = visitor.getList();
200     Collections.sort(list, new IFileComparator());
201     IFile file;
202
203     FileOutputStream os = null;
204     String pdffilename = fPath.toString();
205
206     Document document = new Document(PageSize.A4);
207     PdfWriter pdfWriter = null;
208     try {
209       os = new FileOutputStream(pdffilename);
210       pdfWriter = PdfWriter.getInstance(document, os);
211       pdfWriter.setViewerPreferences(PdfWriter.PageModeUseOutlines);
212
213       document.open();
214       document.resetPageCount();
215       int chapterNumber = 1;
216       for (int i = 0; i < list.size(); i++) {
217         try {
218           file = (IFile) list.get(i);
219           if (file.exists()) {
220             monitor.subTask(WikiEditorPlugin.getResourceString("Export.exportFile") + file.getLocation());
221             
222             StringBuffer htmlBuffer = new StringBuffer();
223             htmlBuffer.append("<html><head></head><body>");
224
225             boolean noContent = CreatePageAction.createFragmentPage(file, htmlBuffer);
226             htmlBuffer.append("</body></html>");
227
228             if (!noContent) {
229               System.out.println(file.getLocation().toString());
230               System.out.println(htmlBuffer.toString());
231               String fileName = file.getName();
232               fileName = fileName.replaceAll("_", " ");
233               int dotIndex = fileName.lastIndexOf('.');
234               if (dotIndex>0) {
235                 fileName = fileName.substring(0,dotIndex);
236               }
237               Paragraph title = new Paragraph(fileName, FontFactory.getFont(FontFactory.HELVETICA, 18, Font.BOLDITALIC,
238                   Color.BLACK));
239               Chapter chapter = new Chapter(title, chapterNumber++);
240               document.add(chapter);
241               appendArticle(document, htmlBuffer);
242             }
243             monitor.worked(1);
244           }
245         } catch (Exception e) {
246           addError("PDF export exception", e);
247         }
248       }
249     } catch (Exception e) {
250       addError("PDF export exception", e);
251     } finally {
252       document.close();
253       if (pdfWriter != null) {
254         pdfWriter.close();
255       }
256       if (os != null) {
257         try {
258           os.close();
259         } catch (IOException e1) {
260         }
261       }
262
263     }
264
265     System.out.println(fPath.toString());
266   }
267
268   /**
269    * @param document
270    * @param htmlBuffer
271    * @param monitor
272    */
273   private void appendArticle(Document document, StringBuffer htmlBuffer) {
274     StringReader stream = null;
275     try {
276       WPHtmlParser parser = new WPHtmlParser();
277       stream = new StringReader(htmlBuffer.toString());
278       parser.go(document, stream);
279     } catch (Exception e) {
280       addError("PDF export exception", e);
281     } finally {
282       if (stream != null) {
283         stream.close();
284       }
285     }
286   }
287
288   //  public void run(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException {
289   //
290   //    WikiFilesVisitor visitor = new WikiFilesVisitor();
291   //    for (int i = 0; i < fResourcesToExport.size(); i++) {
292   //      try {
293   //        ((IResource) fResourcesToExport.get(i)).accept(visitor);
294   //      } catch (CoreException e) {
295   //        e.printStackTrace();
296   //      }
297   //    }
298   //
299   //    List list = visitor.getList();
300   //    Collections.sort(list, new IFileComparator());
301   //    IFile file;
302   //
303   //    FileOutputStream os = null;
304   //    String pdffilename = fPath.toString();
305   //    try {
306   //      os = new FileOutputStream(pdffilename);
307   //      Document document = new Document(PageSize.A4);
308   //      PdfWriter pdfWriter = PdfWriter.getInstance(document, os);
309   //      document.open();
310   //      document.resetPageCount();
311   //      for (int i = 0; i < list.size(); i++) {
312   //        try {
313   //          file = (IFile) list.get(i);
314   //          monitor.subTask(WikiEditorPlugin.getResourceString("Export.exportFile") + file.getLocation());
315   //// System.out.println(file.getLocation().toString());
316   //          StringBuffer htmlBuffer = new StringBuffer();
317   //          // TODO add the real title here:
318   //          htmlBuffer.append("<h2>" + file.getName() + "</h2><br/>");
319   //          boolean noContent = CreatePageAction.createFragmentPage(file, htmlBuffer);
320   //          if (i < list.size() - 1) {
321   //            // TODO create a boolean flag to determine, if we would like a new page or only horizontal ruler
322   //            htmlBuffer.append("<hr/>");
323   //          }
324   //// System.out.println(htmlBuffer.toString());
325   //
326   //          if (!noContent) {
327   //            appendArticle(document, htmlBuffer);
328   //          }
329   //          // CreatePageAction.createPage(templateFileName, (IFile) resources[i], exportDirectoryName, srcBasePath);
330   //          monitor.worked(1);
331   //        } catch (Exception e) {
332   //          addError("PDF export exception", e);
333   //        }
334   //      }
335   //      document.close();
336   //    } catch (Exception e) {
337   //      addError("PDF export exception", e);
338   //    } finally {
339   //      if (os != null) {
340   //        try {
341   //          os.close();
342   //        } catch (IOException e1) {
343   //        }
344   //      }
345   //    }
346   //
347   //    System.out.println(fPath.toString());
348   //  }
349   //
350   //  /**
351   //   * @param document
352   //   * @param htmlBuffer
353   //   * @param monitor
354   //   */
355   //  private void appendArticle(Document document, StringBuffer htmlBuffer) {
356   //    StringReader stream = null;
357   //    try {
358   //// WPHtmlParser parser = new WPHtmlParser();
359   //      stream = new StringReader(htmlBuffer.toString());
360   //// parser.go(document, stream);
361   //      new HTMLWorker(document).parse(stream);
362   //    } catch (Exception e) {
363   //      addError("PDF export exception", e);
364   //    } finally {
365   //      if (stream != null) {
366   //        stream.close();
367   //      }
368   //    }
369   //  }
370
371   /**
372    * Add a new entry to the error table with the passed information
373    */
374   protected void addError(String message, Throwable e) {
375     e.printStackTrace();
376     errorTable.add(new Status(IStatus.ERROR, PlatformUI.PLUGIN_ID, 0, message, e));
377   }
378
379   /**
380    * Set this boolean indicating whether exported resources should automatically overwrite existing files when a conflict occurs. If
381    * not query the user.
382    * 
383    * @param value
384    *          boolean
385    */
386   public void setOverwriteFiles(boolean value) {
387     if (value)
388       overwriteState = OVERWRITE_ALL;
389   }
390
391   /**
392    * Returns the status of the export operation. If there were any errors, the result is a status object containing individual
393    * status objects for each error. If there were no errors, the result is a status object with error code <code>OK</code>.
394    * 
395    * @return the status
396    */
397   public IStatus getStatus() {
398     IStatus[] errors = new IStatus[errorTable.size()];
399     errorTable.toArray(errors);
400     return new MultiStatus(PlatformUI.PLUGIN_ID, IStatus.OK, errors, "PDF export problems occured", //$NON-NLS-1$
401         null);
402   }
403
404   // test
405   //  public static void main(String[] args) {
406   //    try {
407   //      WPHtmlParser parser = new WPHtmlParser();
408   //      Document document = new Document();
409   //      String htmlfilename = "C:/eclipse/wikibooks/wpbin/Synästhesie.html";
410   //      String pdffilename = "C:/eclipse/wikibooks/wpbin/code_java1.pdf";
411   //
412   //      PdfWriter.getInstance(document, new FileOutputStream(pdffilename));
413   //      parser.go(document, htmlfilename);
414   //    } catch (FileNotFoundException e) {
415   //      e.printStackTrace();
416   //    } catch (DocumentException e) {
417   //      e.printStackTrace();
418   //    }
419   //  }
420 }