1 package net.sourceforge.phpeclipse.wiki.export.pdf;
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;
12 import net.sourceforge.phpeclipse.wiki.builder.CreatePageAction;
13 import net.sourceforge.phpeclipse.wiki.editor.WikiEditorPlugin;
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;
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;
36 //import de.java2html.converter.JavaSource2HTMLConverter;
37 //import de.java2html.javasource.JavaSource;
38 //import de.java2html.javasource.JavaSourceParser;
39 //import de.java2html.options.Java2HtmlConversionOptions;
41 public final class WikiPDFExporter implements IRunnableWithProgress {
42 //The constants for the overwrite 3 state
43 private static final int OVERWRITE_NOT_SET = 0;
45 private static final int OVERWRITE_NONE = 1;
47 private static final int OVERWRITE_ALL = 2;
49 private int overwriteState = OVERWRITE_NOT_SET;
51 private List errorTable = new ArrayList(1);
53 private List fResourcesToExport;
57 private IOverwriteQuery fOverwriteCallback;
59 public WikiPDFExporter(List resources, String destinationPath, IOverwriteQuery overwriteImplementor) {
61 fResourcesToExport = resources;
62 fPath = new Path(destinationPath);
63 fOverwriteCallback = overwriteImplementor;
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);
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);
84 // // monitor.subTask(WikiEditorPlugin.getResourceString("Export.linkedResources"));
85 // // exportLinkedResources();
87 //// monitor.worked(1);
91 * TODO: This is a horrible hack for a quick solution.
93 // private void createIndex() throws IOException {
94 // File indexFile = createHtmlFile("index");
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>");
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>");
112 // writer.println(" </body>");
113 // writer.println(" </html>");
117 // private void exportLinkedResources() throws IOException {
118 // if (!exportLinkMaker.hasLinkedDocuments()) {
121 // File workspaceExport = new File(exportDirectory, WikiPDFExporter.WORKSPACE);
122 // if (!workspaceExport.exists()) {
123 // workspaceExport.mkdir();
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);
133 // private void export(IResource resource, String location) throws IOException {
134 // File destination = new File(exportDirectory, location);
136 // if (destination.isDirectory()) {
139 // if (!destination.exists()) {
140 // destination.getParentFile().mkdirs();
142 // File source = new File(resource.getLocation().toString());
143 // if (isJavaResource(resource)) {
144 // javaToHtml(source, new File(destination.getParentFile(), destination.getName()));
146 // copy(source, destination);
149 // private boolean isJavaResource(IResource resource) {
150 // return "java".equals(resource.getFileExtension());
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);
165 // private void copy(File source, File dest) throws IOException {
166 // FileChannel in = null;
167 // FileChannel out = null;
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);
178 // if (out != null) {
186 * @see org.eclipse.jface.operation.IRunnableWithProgress#run(org.eclipse.core.runtime.IProgressMonitor)
188 public void run(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException {
190 WikiFilesVisitor visitor = new WikiFilesVisitor();
191 for (int i = 0; i < fResourcesToExport.size(); i++) {
193 ((IResource) fResourcesToExport.get(i)).accept(visitor);
194 } catch (CoreException e) {
199 List list = visitor.getList();
200 Collections.sort(list, new IFileComparator());
203 FileOutputStream os = null;
204 String pdffilename = fPath.toString();
206 Document document = new Document(PageSize.A4);
207 PdfWriter pdfWriter = null;
209 os = new FileOutputStream(pdffilename);
210 pdfWriter = PdfWriter.getInstance(document, os);
211 pdfWriter.setViewerPreferences(PdfWriter.PageModeUseOutlines);
214 document.resetPageCount();
215 int chapterNumber = 1;
216 for (int i = 0; i < list.size(); i++) {
218 file = (IFile) list.get(i);
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>");
225 boolean noContent = CreatePageAction.createFragmentPage(file, htmlBuffer);
226 htmlBuffer.append("</body></html>");
227 System.out.println(htmlBuffer.toString());
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);
239 } catch (Exception e) {
240 addError("PDF export exception", e);
243 } catch (Exception e) {
244 addError("PDF export exception", e);
247 if (pdfWriter != null) {
253 } catch (IOException e1) {
259 System.out.println(fPath.toString());
267 private void appendArticle(Document document, StringBuffer htmlBuffer) {
268 StringReader stream = null;
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);
276 if (stream != null) {
282 // public void run(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException {
284 // WikiFilesVisitor visitor = new WikiFilesVisitor();
285 // for (int i = 0; i < fResourcesToExport.size(); i++) {
287 // ((IResource) fResourcesToExport.get(i)).accept(visitor);
288 // } catch (CoreException e) {
289 // e.printStackTrace();
293 // List list = visitor.getList();
294 // Collections.sort(list, new IFileComparator());
297 // FileOutputStream os = null;
298 // String pdffilename = fPath.toString();
300 // os = new FileOutputStream(pdffilename);
301 // Document document = new Document(PageSize.A4);
302 // PdfWriter pdfWriter = PdfWriter.getInstance(document, os);
304 // document.resetPageCount();
305 // for (int i = 0; i < list.size(); i++) {
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/>");
318 //// System.out.println(htmlBuffer.toString());
321 // appendArticle(document, htmlBuffer);
323 // // CreatePageAction.createPage(templateFileName, (IFile) resources[i], exportDirectoryName, srcBasePath);
324 // monitor.worked(1);
325 // } catch (Exception e) {
326 // addError("PDF export exception", e);
330 // } catch (Exception e) {
331 // addError("PDF export exception", e);
336 // } catch (IOException e1) {
341 // System.out.println(fPath.toString());
346 // * @param htmlBuffer
349 // private void appendArticle(Document document, StringBuffer htmlBuffer) {
350 // StringReader stream = null;
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);
359 // if (stream != null) {
366 * Add a new entry to the error table with the passed information
368 protected void addError(String message, Throwable e) {
370 errorTable.add(new Status(IStatus.ERROR, PlatformUI.PLUGIN_ID, 0, message, e));
374 * Set this boolean indicating whether exported resources should automatically overwrite existing files when a conflict occurs. If
375 * not query the user.
380 public void setOverwriteFiles(boolean value) {
382 overwriteState = OVERWRITE_ALL;
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>.
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$
399 // public static void main(String[] args) {
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";
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();