initial contribution
[phpeclipse.git] / archive / net.sourceforge.phpeclipse.wiki / src / net / sourceforge / phpeclipse / wiki / export / WikiExporter.java
1 package net.sourceforge.phpeclipse.wiki.export;
2
3 import java.io.File;
4 import java.io.FileInputStream;
5 import java.io.FileOutputStream;
6 import java.io.FileReader;
7 import java.io.FileWriter;
8 import java.io.IOException;
9 import java.nio.MappedByteBuffer;
10 import java.nio.channels.FileChannel;
11 import java.util.TreeSet;
12
13 import net.sourceforge.phpeclipse.wiki.builder.CreatePageAction;
14 import net.sourceforge.phpeclipse.wiki.editor.WikiEditorPlugin;
15
16 import org.eclipse.core.resources.IContainer;
17 import org.eclipse.core.resources.IFile;
18 import org.eclipse.core.resources.IFolder;
19 import org.eclipse.core.resources.IResource;
20 import org.eclipse.core.runtime.CoreException;
21 import org.eclipse.core.runtime.IProgressMonitor;
22
23 import de.java2html.converter.JavaSource2HTMLConverter;
24 import de.java2html.javasource.JavaSource;
25 import de.java2html.javasource.JavaSourceParser;
26 import de.java2html.options.Java2HtmlConversionOptions;
27
28 public final class WikiExporter {
29
30   public static final String HTML_EXTENSION = ".html";
31
32   public static final String WORKSPACE = "workspace";
33
34   //  private File exportDirectory;
35
36   //    private ExportLinkMaker exportLinkMaker;
37   private TreeSet index;
38
39   public WikiExporter() {
40     //        exportLinkMaker = new ExportLinkMaker();
41     index = new TreeSet(String.CASE_INSENSITIVE_ORDER);
42   }
43
44   public void export(IContainer folder, String exportDirectoryName, String srcBasePath, IProgressMonitor monitor) throws IOException, CoreException,
45       InstantiationException, IllegalAccessException, ClassNotFoundException {
46     //    exportDirectory = new File(exportDirectoryName);
47     IResource[] resources = folder.members(IResource.FILE);
48 //    monitor.beginTask(WikiEditorPlugin.getResourceString("Export.wikiPages"), resources.length + 1);
49     for (int i = 0; i < resources.length; i++) {
50       if (resources[i] instanceof IFile) {
51         monitor.subTask(WikiEditorPlugin.getResourceString("Export.exportFile")+resources[i].getLocation());
52         CreatePageAction.createPage((IFile) resources[i], exportDirectoryName, srcBasePath);
53         monitor.worked(1);
54       } else if (resources[i] instanceof IFolder) {
55         monitor.subTask(WikiEditorPlugin.getResourceString("Export.exportFolder")+resources[i].getLocation());
56         export((IFolder) resources[i], exportDirectoryName, srcBasePath, monitor);
57         monitor.worked(1);
58       }
59     }
60     //    monitor.subTask(WikiEditorPlugin.getResourceString("Export.linkedResources"));
61     //        exportLinkedResources();
62     //    createIndex();
63 //    monitor.worked(1);
64   }
65
66   /**
67    * TODO: This is a horrible hack for a quick solution.
68    */
69   //  private void createIndex() throws IOException {
70   //    File indexFile = createHtmlFile("index");
71   //
72   //    PrintWriter writer = new PrintWriter(new FileWriter(indexFile));
73   //    writer.println("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
74   //    writer.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01//EN\" \"http://www.w3.org/TR/html4/strict.dtd\">");
75   //    writer.println("<html>");
76   //    writer.println(" <head>");
77   //    writer.print(" <title>Index</title>");
78   //    writer.println(" </head>");
79   //    writer.println(" <body>");
80   //
81   //    Iterator iterator = index.iterator();
82   //    while (iterator.hasNext()) {
83   //      String name = (String) iterator.next();
84   //      writer.print(" <br/>");
85   //      writer.println("<a href=\"" + name + ".html\">" + name + "</a>");
86   //    }
87   //
88   //    writer.println(" </body>");
89   //    writer.println(" </html>");
90   //    writer.flush();
91   //    writer.close();
92   //  }
93   //    private void exportLinkedResources() throws IOException {
94   //        if (!exportLinkMaker.hasLinkedDocuments()) {
95   //            return;
96   //        }
97   //        File workspaceExport = new File(exportDirectory, WikiExporter.WORKSPACE);
98   //        if (!workspaceExport.exists()) {
99   //            workspaceExport.mkdir();
100   //        }
101   //        HashMap map = exportLinkMaker.getLinkedResources();
102   //        Iterator iterator = map.keySet().iterator();
103   //        while (iterator.hasNext()) {
104   //            IResource resource = (IResource) iterator.next();
105   //            String location = (String) map.get(resource);
106   //            export(resource, location);
107   //        }
108   //    }
109   //  private void export(IResource resource, String location) throws IOException {
110   //    File destination = new File(exportDirectory, location);
111   //
112   //    if (destination.isDirectory()) {
113   //      return;
114   //    }
115   //    if (!destination.exists()) {
116   //      destination.getParentFile().mkdirs();
117   //    }
118   //    File source = new File(resource.getLocation().toString());
119   //    if (isJavaResource(resource)) {
120   //      javaToHtml(source, new File(destination.getParentFile(), destination.getName()));
121   //    } else {
122   //      copy(source, destination);
123   //    }
124   //  }
125   private boolean isJavaResource(IResource resource) {
126     return "java".equals(resource.getFileExtension());
127   }
128
129   private void javaToHtml(File source, File destination) throws IOException {
130     JavaSource java = new JavaSourceParser().parse(new FileReader(source));
131     JavaSource2HTMLConverter converter = new JavaSource2HTMLConverter(java);
132     Java2HtmlConversionOptions options = Java2HtmlConversionOptions.getDefault();
133     options.setShowLineNumbers(true);
134     options.setShowFileName(true);
135     options.setShowJava2HtmlLink(true);
136     converter.setConversionOptions(options);
137     FileWriter writer = new FileWriter(destination);
138     converter.convert(writer);
139     writer.flush();
140     writer.close();
141   }
142
143   private void copy(File source, File dest) throws IOException {
144     FileChannel in = null;
145     FileChannel out = null;
146     try {
147       in = new FileInputStream(source).getChannel();
148       out = new FileOutputStream(dest).getChannel();
149       long size = in.size();
150       MappedByteBuffer buf = in.map(FileChannel.MapMode.READ_ONLY, 0, size);
151       out.write(buf);
152     } finally {
153       if (in != null) {
154         in.close();
155       }
156       if (out != null) {
157         out.close();
158       }
159     }
160   }
161
162   private boolean isWikiFile(IResource resource) {
163     return resource instanceof IFile && resource.getFileExtension().equals("wp");
164   }
165
166   //  private File createHtmlFile(String name) {
167   //    return new File(exportDirectory, name + WikiExporter.HTML_EXTENSION);
168   //  }
169 }