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;
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;
23 import de.java2html.converter.JavaSource2HTMLConverter;
24 import de.java2html.javasource.JavaSource;
25 import de.java2html.javasource.JavaSourceParser;
26 import de.java2html.options.Java2HtmlConversionOptions;
28 public final class WikiExporter {
30 public static final String HTML_EXTENSION = ".html";
32 public static final String WORKSPACE = "workspace";
34 // private File exportDirectory;
36 // private ExportLinkMaker exportLinkMaker;
37 private TreeSet index;
39 public WikiExporter() {
40 // exportLinkMaker = new ExportLinkMaker();
41 index = new TreeSet(String.CASE_INSENSITIVE_ORDER);
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);
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);
60 // monitor.subTask(WikiEditorPlugin.getResourceString("Export.linkedResources"));
61 // exportLinkedResources();
67 * TODO: This is a horrible hack for a quick solution.
69 // private void createIndex() throws IOException {
70 // File indexFile = createHtmlFile("index");
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>");
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>");
88 // writer.println(" </body>");
89 // writer.println(" </html>");
93 // private void exportLinkedResources() throws IOException {
94 // if (!exportLinkMaker.hasLinkedDocuments()) {
97 // File workspaceExport = new File(exportDirectory, WikiExporter.WORKSPACE);
98 // if (!workspaceExport.exists()) {
99 // workspaceExport.mkdir();
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);
109 // private void export(IResource resource, String location) throws IOException {
110 // File destination = new File(exportDirectory, location);
112 // if (destination.isDirectory()) {
115 // if (!destination.exists()) {
116 // destination.getParentFile().mkdirs();
118 // File source = new File(resource.getLocation().toString());
119 // if (isJavaResource(resource)) {
120 // javaToHtml(source, new File(destination.getParentFile(), destination.getName()));
122 // copy(source, destination);
125 private boolean isJavaResource(IResource resource) {
126 return "java".equals(resource.getFileExtension());
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);
143 private void copy(File source, File dest) throws IOException {
144 FileChannel in = null;
145 FileChannel out = null;
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);
162 private boolean isWikiFile(IResource resource) {
163 return resource instanceof IFile && resource.getFileExtension().equals("wp");
166 // private File createHtmlFile(String name) {
167 // return new File(exportDirectory, name + WikiExporter.HTML_EXTENSION);