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 createFragmentPage(IFile file, StringBuffer htmlBuffer) {
91 BufferedInputStream stream = null;
93 // String templateFileName = Util.getLocalTemplate(file);
94 // String cssUrl = Util.getLocalCssUrl(file);
95 String srcBasePath = Util.getWikiTextsPath(file);
96 String binBasePath = Util.getProjectsWikiOutputPath(file.getProject(), WikiEditorPlugin.HTML_OUTPUT_PATH);
97 IContentRenderer renderer = RendererFactory.createContentRenderer(file.getProject());
98 stream = new BufferedInputStream(file.getContents());
100 String fileName = Util.getHTMLFileName(file, binBasePath, srcBasePath);
101 String content = new String(getInputStreamAsCharArray(stream, -1, "utf-8"));
102 String filePath = file.getLocation().toString(); // file.getProjectRelativePath().toString()
103 if (filePath.startsWith(srcBasePath)) {
104 filePath = filePath.substring(srcBasePath.length() + 1);
106 // calculate the <i>depth</i> of the file (i.e. ../../../ as much as needed)
110 index = fileName.indexOf('/', index);
116 renderer.render(null, content, htmlBuffer, level, false);
118 } catch (Exception e) {
122 if (stream != null) {
125 } catch (IOException e) {
131 public static void createPage(IFile file) {
132 String templateFileName = Util.getLocalTemplate(file);
133 String cssUrl = Util.getLocalCssUrl(file);
134 String srcBasePath = Util.getWikiTextsPath(file);
135 String binBasePath = Util.getProjectsWikiOutputPath(file.getProject(), WikiEditorPlugin.HTML_OUTPUT_PATH);
136 createPage(templateFileName, file, binBasePath, srcBasePath);
139 public static void createPage(String templateFileName, IFile file, String binBasePath, String srcBasePath) {
140 // only interested in files with the "wp" extension
141 if ("wp".equalsIgnoreCase(file.getFileExtension())) {
143 IContentRenderer renderer = RendererFactory.createContentRenderer(file.getProject());
144 convertWikiFile(templateFileName, file, binBasePath, srcBasePath, renderer);
145 } catch (InstantiationException e) {
146 // TODO Auto-generated catch block
148 } catch (IllegalAccessException e) {
149 // TODO Auto-generated catch block
151 } catch (ClassNotFoundException e) {
152 // TODO Auto-generated catch block
154 } catch (CoreException e1) {
155 // TODO Auto-generated catch block
156 e1.printStackTrace();
157 } catch (Exception e) {
158 // TODO Auto-generated catch block
162 String fname = file.getName().toLowerCase();
163 if ((fname.charAt(0) == '.') || "project.index".equals(fname) || "cvs".equals(fname) || "entries".equals(fname)
164 || "repository".equals(fname) || "root".equals(fname)) {
165 // ignore meta information
169 FileOutputStream output = null;
170 InputStream contentStream = null;
173 String filename = Util.getHTMLFileName(file, binBasePath, srcBasePath);
174 if (filename != null) {
175 int index = filename.lastIndexOf('/');
177 File ioFile = new File(filename.substring(0, index));
178 if (!ioFile.isDirectory()) {
182 output = new FileOutputStream(filename);
184 contentStream = file.getContents(false);
185 int chunkSize = contentStream.available();
186 byte[] readBuffer = new byte[chunkSize];
187 int n = contentStream.read(readBuffer);
190 output.write(readBuffer);
191 n = contentStream.read(readBuffer);
194 } catch (Exception e) {
200 if (contentStream != null)
201 contentStream.close();
202 } catch (IOException e1) {
208 public static void convertWikiFile(String templateFileName, IFile file, String binBasePath, String srcBasePath,
209 IContentRenderer renderer) throws CoreException {
210 StringBuffer htmlBuffer = new StringBuffer();
211 convertWikiBuffer(templateFileName, htmlBuffer, file, renderer, true);
212 String htmlName = Util.getHTMLFileName(file, binBasePath, srcBasePath);
213 if (htmlName != null) {
214 writeHTMLFile(htmlBuffer, htmlName);
218 public static void getWikiBuffer(StringBuffer htmlBuffer, IFile file) throws CoreException {
219 BufferedInputStream stream = new BufferedInputStream(file.getContents());
221 htmlBuffer.append(getInputStreamAsCharArray(stream, -1, null));
223 //new String(getInputStreamAsCharArray(stream, -1, null));
224 } catch (IOException e) {
228 if (stream != null) {
231 } catch (IOException e) {
237 public static void convertWikiBuffer(String templateFileName, StringBuffer htmlBuffer, IFile file, IContentRenderer renderer,
238 boolean completeHTML) throws CoreException {
239 BufferedInputStream stream = new BufferedInputStream(file.getContents());
241 String content = new String(getInputStreamAsCharArray(stream, -1, null));
242 String srcPath = Util.getWikiTextsPath(file);
243 String filePath = file.getLocation().toString(); // file.getProjectRelativePath().toString()
244 if (filePath.startsWith(srcPath)) {
245 filePath = filePath.substring(srcPath.length() + 1);
247 createWikiBuffer(templateFileName, htmlBuffer, filePath, content, renderer, completeHTML);
248 } catch (IOException e) {
252 if (stream != null) {
255 } catch (IOException e) {
266 public static void createWikiBuffer(String templateFileName, StringBuffer htmlBuffer, String fileName, String content,
267 IContentRenderer renderer, boolean completeHTML) {
268 // calculate the <i>depth</i> of the file (i.e. ../../../ as much as needed)
272 index = fileName.indexOf('/', index);
278 renderer.render(templateFileName, content, htmlBuffer, level, completeHTML);
281 public static void writeHTMLFile(StringBuffer buffer, String filename) {
282 int index = filename.lastIndexOf('/');
284 File file = new File(filename.substring(0, index));
285 if (!file.isDirectory()) {
289 FileWriter fileWriter;
291 fileWriter = new FileWriter(filename);
292 fileWriter.write(buffer.toString());
294 } catch (FileNotFoundException e) {
295 // ignore exception; project is deleted by fUser
296 } catch (IOException e) {
297 // TODO Auto-generated catch block
303 * Returns the given input stream's contents as a character array. If a length is specified (ie. if length != -1), only length
304 * chars are returned. Otherwise all chars in the stream are returned. Note this doesn't close the stream.
306 * @throws IOException
307 * if a problem occured reading the stream.
309 public static char[] getInputStreamAsCharArray(InputStream stream, int length, String encoding) throws IOException {
310 InputStreamReader reader = null;
311 reader = encoding == null ? new InputStreamReader(stream) : new InputStreamReader(stream, encoding);
315 int contentsLength = 0;
318 int amountRequested = Math.max(stream.available(), DEFAULT_READING_SIZE); // read at least 8K
320 // resize contents if needed
321 if (contentsLength + amountRequested > contents.length) {
322 System.arraycopy(contents, 0, contents = new char[contentsLength + amountRequested], 0, contentsLength);
325 // read as many chars as possible
326 amountRead = reader.read(contents, contentsLength, amountRequested);
328 if (amountRead > 0) {
329 // remember length of contents
330 contentsLength += amountRead;
332 } while (amountRead != -1);
334 // resize contents if necessary
335 if (contentsLength < contents.length) {
336 System.arraycopy(contents, 0, contents = new char[contentsLength], 0, contentsLength);
339 contents = new char[length];
342 while ((readSize != -1) && (len != length)) {
344 // We record first the read size. In this case len is the actual
347 readSize = reader.read(contents, len, length - len);
350 // Now we need to resize in case the default encoding used more than
354 System.arraycopy(contents, 0, (contents = new char[len]), 0, len);