1 package net.sourceforge.phpeclipse.wiki.export.pdf;
3 import java.io.FileNotFoundException;
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.Document;
29 import com.lowagie.text.DocumentException;
30 import com.lowagie.text.html.HtmlParser;
31 import com.lowagie.text.pdf.PdfWriter;
33 //import de.java2html.converter.JavaSource2HTMLConverter;
34 //import de.java2html.javasource.JavaSource;
35 //import de.java2html.javasource.JavaSourceParser;
36 //import de.java2html.options.Java2HtmlConversionOptions;
38 public final class WikiPDFExporter implements IRunnableWithProgress {
39 //The constants for the overwrite 3 state
40 private static final int OVERWRITE_NOT_SET = 0;
42 private static final int OVERWRITE_NONE = 1;
44 private static final int OVERWRITE_ALL = 2;
46 private int overwriteState = OVERWRITE_NOT_SET;
48 private List errorTable = new ArrayList(1);
50 private List fResourcesToExport;
54 private IOverwriteQuery fOverwriteCallback;
56 public WikiPDFExporter(List resources, String destinationPath, IOverwriteQuery overwriteImplementor) {
58 fResourcesToExport = resources;
59 fPath = new Path(destinationPath);
60 fOverwriteCallback = overwriteImplementor;
63 // public void export(IContainer folder, String exportDirectoryName, String srcBasePath, IProgressMonitor monitor) throws
64 // IOException, CoreException,
65 // InstantiationException, IllegalAccessException, ClassNotFoundException {
66 // // exportDirectory = new File(exportDirectoryName);
67 // IResource[] resources = folder.members(IResource.FILE);
68 // String templateFileName = Util.getExportTemplate(folder);
69 //// monitor.beginTask(WikiEditorPlugin.getResourceString("Export.wikiPages"), resources.length + 1);
70 // for (int i = 0; i < resources.length; i++) {
71 // if (resources[i] instanceof IFile) {
72 // monitor.subTask(WikiEditorPlugin.getResourceString("Export.exportFile")+resources[i].getLocation());
73 // CreatePageAction.createPage(templateFileName, (IFile) resources[i], exportDirectoryName, srcBasePath);
75 // } else if (resources[i] instanceof IFolder) {
76 // monitor.subTask(WikiEditorPlugin.getResourceString("Export.exportFolder")+resources[i].getLocation());
77 // export((IFolder) resources[i], exportDirectoryName, srcBasePath, monitor);
81 // // monitor.subTask(WikiEditorPlugin.getResourceString("Export.linkedResources"));
82 // // exportLinkedResources();
84 //// monitor.worked(1);
88 * TODO: This is a horrible hack for a quick solution.
90 // private void createIndex() throws IOException {
91 // File indexFile = createHtmlFile("index");
93 // PrintWriter writer = new PrintWriter(new FileWriter(indexFile));
94 // writer.println("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
95 // writer.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01//EN\" \"http://www.w3.org/TR/html4/strict.dtd\">");
96 // writer.println("<html>");
97 // writer.println(" <head>");
98 // writer.print(" <title>Index</title>");
99 // writer.println(" </head>");
100 // writer.println(" <body>");
102 // Iterator iterator = index.iterator();
103 // while (iterator.hasNext()) {
104 // String name = (String) iterator.next();
105 // writer.print(" <br/>");
106 // writer.println("<a href=\"" + name + ".html\">" + name + "</a>");
109 // writer.println(" </body>");
110 // writer.println(" </html>");
114 // private void exportLinkedResources() throws IOException {
115 // if (!exportLinkMaker.hasLinkedDocuments()) {
118 // File workspaceExport = new File(exportDirectory, WikiPDFExporter.WORKSPACE);
119 // if (!workspaceExport.exists()) {
120 // workspaceExport.mkdir();
122 // HashMap map = exportLinkMaker.getLinkedResources();
123 // Iterator iterator = map.keySet().iterator();
124 // while (iterator.hasNext()) {
125 // IResource resource = (IResource) iterator.next();
126 // String location = (String) map.get(resource);
127 // export(resource, location);
130 // private void export(IResource resource, String location) throws IOException {
131 // File destination = new File(exportDirectory, location);
133 // if (destination.isDirectory()) {
136 // if (!destination.exists()) {
137 // destination.getParentFile().mkdirs();
139 // File source = new File(resource.getLocation().toString());
140 // if (isJavaResource(resource)) {
141 // javaToHtml(source, new File(destination.getParentFile(), destination.getName()));
143 // copy(source, destination);
146 // private boolean isJavaResource(IResource resource) {
147 // return "java".equals(resource.getFileExtension());
149 // private void javaToHtml(File source, File destination) throws IOException {
150 // JavaSource java = new JavaSourceParser().parse(new FileReader(source));
151 // JavaSource2HTMLConverter converter = new JavaSource2HTMLConverter(java);
152 // Java2HtmlConversionOptions options = Java2HtmlConversionOptions.getDefault();
153 // options.setShowLineNumbers(true);
154 // options.setShowFileName(true);
155 // options.setShowJava2HtmlLink(true);
156 // converter.setConversionOptions(options);
157 // FileWriter writer = new FileWriter(destination);
158 // converter.convert(writer);
162 // private void copy(File source, File dest) throws IOException {
163 // FileChannel in = null;
164 // FileChannel out = null;
166 // in = new FileInputStream(source).getChannel();
167 // out = new FileOutputStream(dest).getChannel();
168 // long size = in.size();
169 // MappedByteBuffer buf = in.map(FileChannel.MapMode.READ_ONLY, 0, size);
175 // if (out != null) {
183 * @see org.eclipse.jface.operation.IRunnableWithProgress#run(org.eclipse.core.runtime.IProgressMonitor)
185 public void run(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException {
187 WikiFilesVisitor visitor = new WikiFilesVisitor();
188 for (int i = 0; i < fResourcesToExport.size(); i++) {
190 ((IResource) fResourcesToExport.get(i)).accept(visitor);
191 } catch (CoreException e) {
196 List list = visitor.getList();
197 Collections.sort(list, new IFileComparator());
199 StringBuffer htmlBuffer = new StringBuffer();
200 htmlBuffer.append("<html><head></head><body>");
201 for (int i = 0; i < list.size(); i++) {
203 file = (IFile) list.get(i);
204 monitor.subTask(WikiEditorPlugin.getResourceString("Export.exportFile") + file.getLocation());
205 System.out.println(file.getLocation().toString());
206 // TODO add the real title here:
207 htmlBuffer.append("<h2>"+file.getName()+"</h2><br/>");
208 CreatePageAction.createFragmentPage(file, htmlBuffer);
209 if (i < list.size()-1) {
210 // TODO create a boolean flag to determine, if we would like a new page or only horizontal ruler
211 htmlBuffer.append("<hr/>");
213 System.out.println(htmlBuffer.toString());
214 // CreatePageAction.createPage(templateFileName, (IFile) resources[i], exportDirectoryName, srcBasePath);
216 } catch (Exception e) {
217 addError("PDF export exception", e);
220 htmlBuffer.append("</body></html>");
222 StringReader stream = null;
223 String pdffilename = fPath.toString();
224 FileOutputStream os = null;
227 HtmlParser parser = new HtmlParser();
228 Document document = new Document();
230 os = new FileOutputStream(pdffilename);
231 monitor.subTask("Generating PDF file: "+pdffilename);
232 stream = new StringReader(htmlBuffer.toString());
233 PdfWriter pdfWriter = PdfWriter.getInstance(document, os);
235 parser.go(document, stream);
236 } catch (FileNotFoundException e) {
238 } catch (Exception e) {
247 } catch (IOException e1) {
252 System.out.println(fPath.toString());
255 * Add a new entry to the error table with the passed information
257 protected void addError(String message, Throwable e) {
258 errorTable.add(new Status(IStatus.ERROR, PlatformUI.PLUGIN_ID, 0, message, e));
262 * Set this boolean indicating whether exported resources should automatically overwrite existing files when a conflict occurs. If
263 * not query the user.
268 public void setOverwriteFiles(boolean value) {
270 overwriteState = OVERWRITE_ALL;
274 * Returns the status of the export operation. If there were any errors, the result is a status object containing individual
275 * status objects for each error. If there were no errors, the result is a status object with error code <code>OK</code>.
279 public IStatus getStatus() {
280 IStatus[] errors = new IStatus[errorTable.size()];
281 errorTable.toArray(errors);
282 return new MultiStatus(PlatformUI.PLUGIN_ID, IStatus.OK, errors, "PDF export problems occured", //$NON-NLS-1$
287 // public static void main(String[] args) {
289 // HtmlParser parser = new HtmlParser();
290 // Document document = new Document();
291 // String htmlfilename = "C:/eclipse/wikibooks/wpbin/Synästhesie.html";
292 // String pdffilename = "C:/eclipse/wikibooks/wpbin/code_java1.pdf";
294 // PdfWriter.getInstance(document, new FileOutputStream(pdffilename));
295 // parser.go(document, htmlfilename);
296 // } catch (FileNotFoundException e) {
297 // e.printStackTrace();
298 // } catch (DocumentException e) {
299 // e.printStackTrace();