1 package net.sourceforge.phpeclipse.wiki.builder;
3 import java.io.BufferedInputStream;
5 import java.io.FileNotFoundException;
6 import java.io.FileOutputStream;
7 import java.io.FileWriter;
8 import java.io.IOException;
9 import java.io.InputStream;
10 import java.io.InputStreamReader;
11 import java.util.Iterator;
13 import net.sourceforge.phpeclipse.wiki.editor.WikiEditorPlugin;
14 import net.sourceforge.phpeclipse.wiki.preferences.Util;
15 import net.sourceforge.phpeclipse.wiki.renderer.IContentRenderer;
16 import net.sourceforge.phpeclipse.wiki.renderer.RendererFactory;
18 import org.eclipse.core.resources.IFile;
19 import org.eclipse.core.resources.IResource;
20 import org.eclipse.core.runtime.CoreException;
21 import org.eclipse.jface.action.IAction;
22 import org.eclipse.jface.viewers.ISelection;
23 import org.eclipse.jface.viewers.ISelectionProvider;
24 import org.eclipse.jface.viewers.StructuredSelection;
25 import org.eclipse.ui.IActionDelegate;
26 import org.eclipse.ui.IObjectActionDelegate;
27 import org.eclipse.ui.IWorkbenchPart;
30 * Create a static HTML page
32 public class CreatePageAction implements IObjectActionDelegate {
34 * Constant for an empty char array
36 public static final char[] NO_CHAR = new char[0];
38 private static final int DEFAULT_READING_SIZE = 8192;
40 private IWorkbenchPart workbenchPart;
45 public CreatePageAction() {
50 * @see IObjectActionDelegate#setActivePart(IAction, IWorkbenchPart)
52 public void setActivePart(IAction action, IWorkbenchPart targetPart) {
53 workbenchPart = targetPart;
56 public void run(IAction action) {
57 ISelectionProvider selectionProvider = null;
58 selectionProvider = workbenchPart.getSite().getSelectionProvider();
60 StructuredSelection selection = null;
61 selection = (StructuredSelection) selectionProvider.getSelection();
64 Iterator iterator = null;
65 iterator = selection.iterator();
66 while (iterator.hasNext()) {
67 // obj => selected object in the view
68 Object obj = iterator.next();
71 if (obj instanceof IResource) {
72 IResource resource = (IResource) obj;
74 // check if it's a file resource
75 switch (resource.getType()) {
78 createPage((IFile) resource);
85 * @see IActionDelegate#selectionChanged(IAction, ISelection)
87 public void selectionChanged(IAction action, ISelection selection) {
90 public static void createPage(IFile file) {
91 String templateFileName = Util.getLocalTemplate(file);
92 String srcBasePath = Util.getWikiTextsPath(file);
93 String binBasePath = Util.getProjectsWikiOutputPath(file.getProject(), WikiEditorPlugin.HTML_OUTPUT_PATH);
94 createPage(templateFileName, file, binBasePath, srcBasePath);
97 public static void createPage(String templateFileName, IFile file, String binBasepath, String srcBasePath) {
98 // only interested in files with the "wp" extension
99 if ("wp".equalsIgnoreCase(file.getFileExtension())) {
101 IContentRenderer renderer = RendererFactory.createContentRenderer(file.getProject());
102 convertWikiFile(templateFileName, file, binBasepath, srcBasePath, renderer);
103 } catch (InstantiationException e) {
104 // TODO Auto-generated catch block
106 } catch (IllegalAccessException e) {
107 // TODO Auto-generated catch block
109 } catch (ClassNotFoundException e) {
110 // TODO Auto-generated catch block
112 } catch (CoreException e1) {
113 // TODO Auto-generated catch block
114 e1.printStackTrace();
117 String fname = file.getName().toLowerCase();
118 if ((fname.charAt(0) == '.') || "project.index".equals(fname) || "cvs".equals(fname) || "entries".equals(fname)
119 || "repository".equals(fname) || "root".equals(fname)) {
120 // ignore meta information
124 FileOutputStream output = null;
125 InputStream contentStream = null;
128 String filename = Util.getHTMLFileName(file, binBasepath, srcBasePath);
129 if (filename != null) {
130 int index = filename.lastIndexOf('/');
132 File ioFile = new File(filename.substring(0, index));
133 if (!ioFile.isDirectory()) {
137 output = new FileOutputStream(filename);
139 contentStream = file.getContents(false);
140 int chunkSize = contentStream.available();
141 byte[] readBuffer = new byte[chunkSize];
142 int n = contentStream.read(readBuffer);
145 output.write(readBuffer);
146 n = contentStream.read(readBuffer);
149 } catch (Exception e) {
155 if (contentStream != null)
156 contentStream.close();
157 } catch (IOException e1) {
163 public static void convertWikiFile(String templateFileName, IFile file, String binBasePath, String srcBasePath, IContentRenderer renderer)
164 throws CoreException {
165 StringBuffer htmlBuffer = new StringBuffer();
166 convertWikiBuffer(templateFileName, htmlBuffer, file, renderer, true);
167 String htmlName = Util.getHTMLFileName(file, binBasePath, srcBasePath);
168 if (htmlName != null) {
169 writeHTMLFile(htmlBuffer, htmlName);
173 public static void getWikiBuffer(StringBuffer htmlBuffer, IFile file) throws CoreException {
174 BufferedInputStream stream = new BufferedInputStream(file.getContents());
176 htmlBuffer.append(getInputStreamAsCharArray(stream, -1, null));
178 //new String(getInputStreamAsCharArray(stream, -1, null));
179 } catch (IOException e) {
183 if (stream != null) {
186 } catch (IOException e) {
192 public static void convertWikiBuffer(String templateFileName, StringBuffer htmlBuffer, IFile file, IContentRenderer renderer, boolean completeHTML)
193 throws CoreException {
194 BufferedInputStream stream = new BufferedInputStream(file.getContents());
196 String content = new String(getInputStreamAsCharArray(stream, -1, null));
197 String srcPath = Util.getWikiTextsPath(file);
198 String filePath = file.getLocation().toString(); // file.getProjectRelativePath().toString()
199 if (filePath.startsWith(srcPath)) {
200 filePath = filePath.substring(srcPath.length()+1);
202 createWikiBuffer(templateFileName, htmlBuffer, filePath, content, renderer, completeHTML);
203 } catch (IOException e) {
207 if (stream != null) {
210 } catch (IOException e) {
221 public static void createWikiBuffer(String templateFileName, StringBuffer htmlBuffer, String fileName, String content, IContentRenderer renderer,
222 boolean completeHTML) {
223 // calculate the <i>depth</i> of the file (i.e. ../../../ as much as needed)
227 index = fileName.indexOf('/', index);
233 renderer.render(templateFileName, content, htmlBuffer, level, completeHTML);
236 public static void writeHTMLFile(StringBuffer buffer, String filename) {
237 int index = filename.lastIndexOf('/');
239 File file = new File(filename.substring(0, index));
240 if (!file.isDirectory()) {
244 FileWriter fileWriter;
246 fileWriter = new FileWriter(filename);
247 fileWriter.write(buffer.toString());
249 } catch (FileNotFoundException e) {
250 // ignore exception; project is deleted by fUser
251 } catch (IOException e) {
252 // TODO Auto-generated catch block
258 * Returns the given input stream's contents as a character array. If a length is specified (ie. if length != -1), only length
259 * chars are returned. Otherwise all chars in the stream are returned. Note this doesn't close the stream.
261 * @throws IOException
262 * if a problem occured reading the stream.
264 public static char[] getInputStreamAsCharArray(InputStream stream, int length, String encoding) throws IOException {
265 InputStreamReader reader = null;
266 reader = encoding == null ? new InputStreamReader(stream) : new InputStreamReader(stream, encoding);
270 int contentsLength = 0;
273 int amountRequested = Math.max(stream.available(), DEFAULT_READING_SIZE); // read at least 8K
275 // resize contents if needed
276 if (contentsLength + amountRequested > contents.length) {
277 System.arraycopy(contents, 0, contents = new char[contentsLength + amountRequested], 0, contentsLength);
280 // read as many chars as possible
281 amountRead = reader.read(contents, contentsLength, amountRequested);
283 if (amountRead > 0) {
284 // remember length of contents
285 contentsLength += amountRead;
287 } while (amountRead != -1);
289 // resize contents if necessary
290 if (contentsLength < contents.length) {
291 System.arraycopy(contents, 0, contents = new char[contentsLength], 0, contentsLength);
294 contents = new char[length];
297 while ((readSize != -1) && (len != length)) {
299 // We record first the read size. In this case len is the actual
302 readSize = reader.read(contents, len, length - len);
305 // Now we need to resize in case the default encoding used more than
309 System.arraycopy(contents, 0, (contents = new char[len]), 0, len);