initial contribution
[phpeclipse.git] / archive / net.sourceforge.phpeclipse.wiki / src / net / sourceforge / phpeclipse / wiki / export / WikiExporter.java
diff --git a/archive/net.sourceforge.phpeclipse.wiki/src/net/sourceforge/phpeclipse/wiki/export/WikiExporter.java b/archive/net.sourceforge.phpeclipse.wiki/src/net/sourceforge/phpeclipse/wiki/export/WikiExporter.java
new file mode 100644 (file)
index 0000000..1463a25
--- /dev/null
@@ -0,0 +1,169 @@
+package net.sourceforge.phpeclipse.wiki.export;
+
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileOutputStream;
+import java.io.FileReader;
+import java.io.FileWriter;
+import java.io.IOException;
+import java.nio.MappedByteBuffer;
+import java.nio.channels.FileChannel;
+import java.util.TreeSet;
+
+import net.sourceforge.phpeclipse.wiki.builder.CreatePageAction;
+import net.sourceforge.phpeclipse.wiki.editor.WikiEditorPlugin;
+
+import org.eclipse.core.resources.IContainer;
+import org.eclipse.core.resources.IFile;
+import org.eclipse.core.resources.IFolder;
+import org.eclipse.core.resources.IResource;
+import org.eclipse.core.runtime.CoreException;
+import org.eclipse.core.runtime.IProgressMonitor;
+
+import de.java2html.converter.JavaSource2HTMLConverter;
+import de.java2html.javasource.JavaSource;
+import de.java2html.javasource.JavaSourceParser;
+import de.java2html.options.Java2HtmlConversionOptions;
+
+public final class WikiExporter {
+
+  public static final String HTML_EXTENSION = ".html";
+
+  public static final String WORKSPACE = "workspace";
+
+  //  private File exportDirectory;
+
+  //    private ExportLinkMaker exportLinkMaker;
+  private TreeSet index;
+
+  public WikiExporter() {
+    //        exportLinkMaker = new ExportLinkMaker();
+    index = new TreeSet(String.CASE_INSENSITIVE_ORDER);
+  }
+
+  public void export(IContainer folder, String exportDirectoryName, String srcBasePath, IProgressMonitor monitor) throws IOException, CoreException,
+      InstantiationException, IllegalAccessException, ClassNotFoundException {
+    //    exportDirectory = new File(exportDirectoryName);
+    IResource[] resources = folder.members(IResource.FILE);
+//    monitor.beginTask(WikiEditorPlugin.getResourceString("Export.wikiPages"), resources.length + 1);
+    for (int i = 0; i < resources.length; i++) {
+      if (resources[i] instanceof IFile) {
+        monitor.subTask(WikiEditorPlugin.getResourceString("Export.exportFile")+resources[i].getLocation());
+        CreatePageAction.createPage((IFile) resources[i], exportDirectoryName, srcBasePath);
+        monitor.worked(1);
+      } else if (resources[i] instanceof IFolder) {
+        monitor.subTask(WikiEditorPlugin.getResourceString("Export.exportFolder")+resources[i].getLocation());
+        export((IFolder) resources[i], exportDirectoryName, srcBasePath, monitor);
+        monitor.worked(1);
+      }
+    }
+    //    monitor.subTask(WikiEditorPlugin.getResourceString("Export.linkedResources"));
+    //        exportLinkedResources();
+    //    createIndex();
+//    monitor.worked(1);
+  }
+
+  /**
+   * TODO: This is a horrible hack for a quick solution.
+   */
+  //  private void createIndex() throws IOException {
+  //    File indexFile = createHtmlFile("index");
+  //
+  //    PrintWriter writer = new PrintWriter(new FileWriter(indexFile));
+  //    writer.println("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
+  //    writer.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01//EN\" \"http://www.w3.org/TR/html4/strict.dtd\">");
+  //    writer.println("<html>");
+  //    writer.println(" <head>");
+  //    writer.print(" <title>Index</title>");
+  //    writer.println(" </head>");
+  //    writer.println(" <body>");
+  //
+  //    Iterator iterator = index.iterator();
+  //    while (iterator.hasNext()) {
+  //      String name = (String) iterator.next();
+  //      writer.print(" <br/>");
+  //      writer.println("<a href=\"" + name + ".html\">" + name + "</a>");
+  //    }
+  //
+  //    writer.println(" </body>");
+  //    writer.println(" </html>");
+  //    writer.flush();
+  //    writer.close();
+  //  }
+  //    private void exportLinkedResources() throws IOException {
+  //        if (!exportLinkMaker.hasLinkedDocuments()) {
+  //            return;
+  //        }
+  //        File workspaceExport = new File(exportDirectory, WikiExporter.WORKSPACE);
+  //        if (!workspaceExport.exists()) {
+  //            workspaceExport.mkdir();
+  //        }
+  //        HashMap map = exportLinkMaker.getLinkedResources();
+  //        Iterator iterator = map.keySet().iterator();
+  //        while (iterator.hasNext()) {
+  //            IResource resource = (IResource) iterator.next();
+  //            String location = (String) map.get(resource);
+  //            export(resource, location);
+  //        }
+  //    }
+  //  private void export(IResource resource, String location) throws IOException {
+  //    File destination = new File(exportDirectory, location);
+  //
+  //    if (destination.isDirectory()) {
+  //      return;
+  //    }
+  //    if (!destination.exists()) {
+  //      destination.getParentFile().mkdirs();
+  //    }
+  //    File source = new File(resource.getLocation().toString());
+  //    if (isJavaResource(resource)) {
+  //      javaToHtml(source, new File(destination.getParentFile(), destination.getName()));
+  //    } else {
+  //      copy(source, destination);
+  //    }
+  //  }
+  private boolean isJavaResource(IResource resource) {
+    return "java".equals(resource.getFileExtension());
+  }
+
+  private void javaToHtml(File source, File destination) throws IOException {
+    JavaSource java = new JavaSourceParser().parse(new FileReader(source));
+    JavaSource2HTMLConverter converter = new JavaSource2HTMLConverter(java);
+    Java2HtmlConversionOptions options = Java2HtmlConversionOptions.getDefault();
+    options.setShowLineNumbers(true);
+    options.setShowFileName(true);
+    options.setShowJava2HtmlLink(true);
+    converter.setConversionOptions(options);
+    FileWriter writer = new FileWriter(destination);
+    converter.convert(writer);
+    writer.flush();
+    writer.close();
+  }
+
+  private void copy(File source, File dest) throws IOException {
+    FileChannel in = null;
+    FileChannel out = null;
+    try {
+      in = new FileInputStream(source).getChannel();
+      out = new FileOutputStream(dest).getChannel();
+      long size = in.size();
+      MappedByteBuffer buf = in.map(FileChannel.MapMode.READ_ONLY, 0, size);
+      out.write(buf);
+    } finally {
+      if (in != null) {
+        in.close();
+      }
+      if (out != null) {
+        out.close();
+      }
+    }
+  }
+
+  private boolean isWikiFile(IResource resource) {
+    return resource instanceof IFile && resource.getFileExtension().equals("wp");
+  }
+
+  //  private File createHtmlFile(String name) {
+  //    return new File(exportDirectory, name + WikiExporter.HTML_EXTENSION);
+  //  }
+}
\ No newline at end of file