0b387dd7d996b0cc569c85ca06e95dc50e054169
[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             System.out.println(file.getLocation().toString());
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             System.out.println(htmlBuffer.toString());
228
229             if (!noContent) {
230               // TODO add the real title from corresponding *.xml file here:
231               Paragraph title = new Paragraph(file.getName(), FontFactory.getFont(FontFactory.HELVETICA, 18, Font.BOLDITALIC,
232                   new Color(0, 0, 255)));
233               Chapter chapter = new Chapter(title, chapterNumber++);
234               document.add(chapter);
235               appendArticle(document, htmlBuffer);
236             }
237             monitor.worked(1);
238           }
239         } catch (Exception e) {
240           addError("PDF export exception", e);
241         }
242       }
243     } catch (Exception e) {
244       addError("PDF export exception", e);
245     } finally {
246       document.close();
247       if (pdfWriter != null) {
248         pdfWriter.close();
249       }
250       if (os != null) {
251         try {
252           os.close();
253         } catch (IOException e1) {
254         }
255       }
256
257     }
258
259     System.out.println(fPath.toString());
260   }
261
262   /**
263    * @param document
264    * @param htmlBuffer
265    * @param monitor
266    */
267   private void appendArticle(Document document, StringBuffer htmlBuffer) {
268     StringReader stream = null;
269     try {
270       WPHtmlParser parser = new WPHtmlParser();
271       stream = new StringReader(htmlBuffer.toString());
272       parser.go(document, stream);
273     } catch (Exception e) {
274       addError("PDF export exception", e);
275     } finally {
276       if (stream != null) {
277         stream.close();
278       }
279     }
280   }
281
282   //  public void run(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException {
283   //
284   //    WikiFilesVisitor visitor = new WikiFilesVisitor();
285   //    for (int i = 0; i < fResourcesToExport.size(); i++) {
286   //      try {
287   //        ((IResource) fResourcesToExport.get(i)).accept(visitor);
288   //      } catch (CoreException e) {
289   //        e.printStackTrace();
290   //      }
291   //    }
292   //
293   //    List list = visitor.getList();
294   //    Collections.sort(list, new IFileComparator());
295   //    IFile file;
296   //
297   //    FileOutputStream os = null;
298   //    String pdffilename = fPath.toString();
299   //    try {
300   //      os = new FileOutputStream(pdffilename);
301   //      Document document = new Document(PageSize.A4);
302   //      PdfWriter pdfWriter = PdfWriter.getInstance(document, os);
303   //      document.open();
304   //      document.resetPageCount();
305   //      for (int i = 0; i < list.size(); i++) {
306   //        try {
307   //          file = (IFile) list.get(i);
308   //          monitor.subTask(WikiEditorPlugin.getResourceString("Export.exportFile") + file.getLocation());
309   //// System.out.println(file.getLocation().toString());
310   //          StringBuffer htmlBuffer = new StringBuffer();
311   //          // TODO add the real title here:
312   //          htmlBuffer.append("<h2>" + file.getName() + "</h2><br/>");
313   //          boolean noContent = CreatePageAction.createFragmentPage(file, htmlBuffer);
314   //          if (i < list.size() - 1) {
315   //            // TODO create a boolean flag to determine, if we would like a new page or only horizontal ruler
316   //            htmlBuffer.append("<hr/>");
317   //          }
318   //// System.out.println(htmlBuffer.toString());
319   //
320   //          if (!noContent) {
321   //            appendArticle(document, htmlBuffer);
322   //          }
323   //          // CreatePageAction.createPage(templateFileName, (IFile) resources[i], exportDirectoryName, srcBasePath);
324   //          monitor.worked(1);
325   //        } catch (Exception e) {
326   //          addError("PDF export exception", e);
327   //        }
328   //      }
329   //      document.close();
330   //    } catch (Exception e) {
331   //      addError("PDF export exception", e);
332   //    } finally {
333   //      if (os != null) {
334   //        try {
335   //          os.close();
336   //        } catch (IOException e1) {
337   //        }
338   //      }
339   //    }
340   //
341   //    System.out.println(fPath.toString());
342   //  }
343   //
344   //  /**
345   //   * @param document
346   //   * @param htmlBuffer
347   //   * @param monitor
348   //   */
349   //  private void appendArticle(Document document, StringBuffer htmlBuffer) {
350   //    StringReader stream = null;
351   //    try {
352   //// WPHtmlParser parser = new WPHtmlParser();
353   //      stream = new StringReader(htmlBuffer.toString());
354   //// parser.go(document, stream);
355   //      new HTMLWorker(document).parse(stream);
356   //    } catch (Exception e) {
357   //      addError("PDF export exception", e);
358   //    } finally {
359   //      if (stream != null) {
360   //        stream.close();
361   //      }
362   //    }
363   //  }
364
365   /**
366    * Add a new entry to the error table with the passed information
367    */
368   protected void addError(String message, Throwable e) {
369     e.printStackTrace();
370     errorTable.add(new Status(IStatus.ERROR, PlatformUI.PLUGIN_ID, 0, message, e));
371   }
372
373   /**
374    * Set this boolean indicating whether exported resources should automatically overwrite existing files when a conflict occurs. If
375    * not query the user.
376    * 
377    * @param value
378    *          boolean
379    */
380   public void setOverwriteFiles(boolean value) {
381     if (value)
382       overwriteState = OVERWRITE_ALL;
383   }
384
385   /**
386    * Returns the status of the export operation. If there were any errors, the result is a status object containing individual
387    * status objects for each error. If there were no errors, the result is a status object with error code <code>OK</code>.
388    * 
389    * @return the status
390    */
391   public IStatus getStatus() {
392     IStatus[] errors = new IStatus[errorTable.size()];
393     errorTable.toArray(errors);
394     return new MultiStatus(PlatformUI.PLUGIN_ID, IStatus.OK, errors, "PDF export problems occured", //$NON-NLS-1$
395         null);
396   }
397
398   // test
399   //  public static void main(String[] args) {
400   //    try {
401   //      WPHtmlParser parser = new WPHtmlParser();
402   //      Document document = new Document();
403   //      String htmlfilename = "C:/eclipse/wikibooks/wpbin/Synästhesie.html";
404   //      String pdffilename = "C:/eclipse/wikibooks/wpbin/code_java1.pdf";
405   //
406   //      PdfWriter.getInstance(document, new FileOutputStream(pdffilename));
407   //      parser.go(document, htmlfilename);
408   //    } catch (FileNotFoundException e) {
409   //      e.printStackTrace();
410   //    } catch (DocumentException e) {
411   //      e.printStackTrace();
412   //    }
413   //  }
414 }