1 package net.sourceforge.phpeclipse.wiki.export;
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;
13 import net.sourceforge.phpeclipse.wiki.builder.CreatePageAction;
14 import net.sourceforge.phpeclipse.wiki.editor.WikiEditorPlugin;
15 import net.sourceforge.phpeclipse.wiki.preferences.Util;
17 import org.eclipse.core.resources.IContainer;
18 import org.eclipse.core.resources.IFile;
19 import org.eclipse.core.resources.IFolder;
20 import org.eclipse.core.resources.IResource;
21 import org.eclipse.core.runtime.CoreException;
22 import org.eclipse.core.runtime.IProgressMonitor;
24 import de.java2html.converter.JavaSource2HTMLConverter;
25 import de.java2html.javasource.JavaSource;
26 import de.java2html.javasource.JavaSourceParser;
27 import de.java2html.options.Java2HtmlConversionOptions;
29 public final class WikiExporter {
31 public static final String HTML_EXTENSION = ".html";
33 public static final String WORKSPACE = "workspace";
35 // private File exportDirectory;
37 // private ExportLinkMaker exportLinkMaker;
38 private TreeSet index;
40 public WikiExporter() {
41 // exportLinkMaker = new ExportLinkMaker();
42 index = new TreeSet(String.CASE_INSENSITIVE_ORDER);
45 public void export(IContainer folder, String exportDirectoryName, String srcBasePath, IProgressMonitor monitor) throws IOException, CoreException,
46 InstantiationException, IllegalAccessException, ClassNotFoundException {
47 // exportDirectory = new File(exportDirectoryName);
48 IResource[] resources = folder.members(IResource.FILE);
49 String templateFileName = Util.getExportTemplate(folder);
50 // monitor.beginTask(WikiEditorPlugin.getResourceString("Export.wikiPages"), resources.length + 1);
51 for (int i = 0; i < resources.length; i++) {
52 if (resources[i] instanceof IFile) {
53 monitor.subTask(WikiEditorPlugin.getResourceString("Export.exportFile")+resources[i].getLocation());
54 CreatePageAction.createPage(templateFileName, (IFile) resources[i], exportDirectoryName, srcBasePath);
56 } else if (resources[i] instanceof IFolder) {
57 monitor.subTask(WikiEditorPlugin.getResourceString("Export.exportFolder")+resources[i].getLocation());
58 export((IFolder) resources[i], exportDirectoryName, srcBasePath, monitor);
62 // monitor.subTask(WikiEditorPlugin.getResourceString("Export.linkedResources"));
63 // exportLinkedResources();
69 * TODO: This is a horrible hack for a quick solution.
71 // private void createIndex() throws IOException {
72 // File indexFile = createHtmlFile("index");
74 // PrintWriter writer = new PrintWriter(new FileWriter(indexFile));
75 // writer.println("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
76 // writer.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01//EN\" \"http://www.w3.org/TR/html4/strict.dtd\">");
77 // writer.println("<html>");
78 // writer.println(" <head>");
79 // writer.print(" <title>Index</title>");
80 // writer.println(" </head>");
81 // writer.println(" <body>");
83 // Iterator iterator = index.iterator();
84 // while (iterator.hasNext()) {
85 // String name = (String) iterator.next();
86 // writer.print(" <br/>");
87 // writer.println("<a href=\"" + name + ".html\">" + name + "</a>");
90 // writer.println(" </body>");
91 // writer.println(" </html>");
95 // private void exportLinkedResources() throws IOException {
96 // if (!exportLinkMaker.hasLinkedDocuments()) {
99 // File workspaceExport = new File(exportDirectory, WikiExporter.WORKSPACE);
100 // if (!workspaceExport.exists()) {
101 // workspaceExport.mkdir();
103 // HashMap map = exportLinkMaker.getLinkedResources();
104 // Iterator iterator = map.keySet().iterator();
105 // while (iterator.hasNext()) {
106 // IResource resource = (IResource) iterator.next();
107 // String location = (String) map.get(resource);
108 // export(resource, location);
111 // private void export(IResource resource, String location) throws IOException {
112 // File destination = new File(exportDirectory, location);
114 // if (destination.isDirectory()) {
117 // if (!destination.exists()) {
118 // destination.getParentFile().mkdirs();
120 // File source = new File(resource.getLocation().toString());
121 // if (isJavaResource(resource)) {
122 // javaToHtml(source, new File(destination.getParentFile(), destination.getName()));
124 // copy(source, destination);
127 private boolean isJavaResource(IResource resource) {
128 return "java".equals(resource.getFileExtension());
131 private void javaToHtml(File source, File destination) throws IOException {
132 JavaSource java = new JavaSourceParser().parse(new FileReader(source));
133 JavaSource2HTMLConverter converter = new JavaSource2HTMLConverter(java);
134 Java2HtmlConversionOptions options = Java2HtmlConversionOptions.getDefault();
135 options.setShowLineNumbers(true);
136 options.setShowFileName(true);
137 options.setShowJava2HtmlLink(true);
138 converter.setConversionOptions(options);
139 FileWriter writer = new FileWriter(destination);
140 converter.convert(writer);
145 private void copy(File source, File dest) throws IOException {
146 FileChannel in = null;
147 FileChannel out = null;
149 in = new FileInputStream(source).getChannel();
150 out = new FileOutputStream(dest).getChannel();
151 long size = in.size();
152 MappedByteBuffer buf = in.map(FileChannel.MapMode.READ_ONLY, 0, size);
164 private boolean isWikiFile(IResource resource) {
165 return resource instanceof IFile && resource.getFileExtension().equals("wp");
168 // private File createHtmlFile(String name) {
169 // return new File(exportDirectory, name + WikiExporter.HTML_EXTENSION);