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;
17 import net.sourceforge.phpeclipse.wiki.renderer.StringUtil;
19 import org.eclipse.core.resources.IFile;
20 import org.eclipse.core.resources.IResource;
21 import org.eclipse.core.runtime.CoreException;
22 import org.eclipse.jface.action.IAction;
23 import org.eclipse.jface.viewers.ISelection;
24 import org.eclipse.jface.viewers.ISelectionProvider;
25 import org.eclipse.jface.viewers.StructuredSelection;
26 import org.eclipse.ui.IActionDelegate;
27 import org.eclipse.ui.IObjectActionDelegate;
28 import org.eclipse.ui.IWorkbenchPart;
31 * Create a static HTML page
33 public class CreatePageAction implements IObjectActionDelegate {
35 * Constant for an empty char array
37 public static final char[] NO_CHAR = new char[0];
39 private static final int DEFAULT_READING_SIZE = 8192;
41 private IWorkbenchPart workbenchPart;
46 public CreatePageAction() {
51 * @see IObjectActionDelegate#setActivePart(IAction, IWorkbenchPart)
53 public void setActivePart(IAction action, IWorkbenchPart targetPart) {
54 workbenchPart = targetPart;
57 public void run(IAction action) {
58 ISelectionProvider selectionProvider = null;
59 selectionProvider = workbenchPart.getSite().getSelectionProvider();
61 StructuredSelection selection = null;
62 selection = (StructuredSelection) selectionProvider.getSelection();
65 Iterator iterator = null;
66 iterator = selection.iterator();
67 while (iterator.hasNext()) {
68 // obj => selected object in the view
69 Object obj = iterator.next();
72 if (obj instanceof IResource) {
73 IResource resource = (IResource) obj;
75 // check if it's a file resource
76 switch (resource.getType()) {
79 createPage((IFile) resource);
86 * @see IActionDelegate#selectionChanged(IAction, ISelection)
88 public void selectionChanged(IAction action, ISelection selection) {
91 public static boolean createFragmentPage(IFile file, StringBuffer htmlBuffer) {
92 BufferedInputStream stream = null;
93 boolean noContent = true;
95 // String templateFileName = Util.getLocalTemplate(file);
96 // String cssUrl = Util.getLocalCssUrl(file);
97 String srcBasePath = Util.getWikiTextsPath(file);
98 String binBasePath = Util.getProjectsWikiOutputPath(file.getProject(), WikiEditorPlugin.HTML_OUTPUT_PATH);
99 IContentRenderer renderer = RendererFactory.createContentRenderer(file.getProject());
100 stream = new BufferedInputStream(file.getContents());
102 String fileName = Util.getHTMLFileName(file, binBasePath, srcBasePath);
103 String content = new String(getInputStreamAsCharArray(stream, -1, "utf-8"));
104 noContent = StringUtil.checkNoContent(content);
105 String filePath = file.getLocation().toString(); // file.getProjectRelativePath().toString()
106 if (filePath.startsWith(srcBasePath)) {
107 filePath = filePath.substring(srcBasePath.length() + 1);
109 // calculate the <i>depth</i> of the file (i.e. ../../../ as much as needed)
113 index = fileName.indexOf('/', index);
119 renderer.render(null, content, htmlBuffer, level, false);
121 } catch (Exception e) {
125 if (stream != null) {
128 } catch (IOException e) {
134 public static void createPage(IFile file) {
135 String templateFileName = Util.getLocalTemplate(file);
136 String cssUrl = Util.getLocalCssUrl(file);
137 String srcBasePath = Util.getWikiTextsPath(file);
138 String binBasePath = Util.getProjectsWikiOutputPath(file.getProject(), WikiEditorPlugin.HTML_OUTPUT_PATH);
139 createPage(templateFileName, file, binBasePath, srcBasePath);
142 public static void createPage(String templateFileName, IFile file, String binBasePath, String srcBasePath) {
143 // only interested in files with the "wp" extension
144 if ("wp".equalsIgnoreCase(file.getFileExtension())) {
146 IContentRenderer renderer = RendererFactory.createContentRenderer(file.getProject());
147 convertWikiFile(templateFileName, file, binBasePath, srcBasePath, renderer);
148 } catch (InstantiationException e) {
149 // TODO Auto-generated catch block
151 } catch (IllegalAccessException e) {
152 // TODO Auto-generated catch block
154 } catch (ClassNotFoundException e) {
155 // TODO Auto-generated catch block
157 } catch (CoreException e1) {
158 // TODO Auto-generated catch block
159 e1.printStackTrace();
160 } catch (Exception e) {
161 // TODO Auto-generated catch block
165 String fname = file.getName().toLowerCase();
166 if ((fname.charAt(0) == '.') || "project.index".equals(fname) || "cvs".equals(fname) || "entries".equals(fname)
167 || "repository".equals(fname) || "root".equals(fname)) {
168 // ignore meta information
172 FileOutputStream output = null;
173 InputStream contentStream = null;
176 String filename = Util.getHTMLFileName(file, binBasePath, srcBasePath);
177 if (filename != null) {
178 int index = filename.lastIndexOf('/');
180 File ioFile = new File(filename.substring(0, index));
181 if (!ioFile.isDirectory()) {
185 output = new FileOutputStream(filename);
187 contentStream = file.getContents(false);
188 int chunkSize = contentStream.available();
189 byte[] readBuffer = new byte[chunkSize];
190 int n = contentStream.read(readBuffer);
193 output.write(readBuffer);
194 n = contentStream.read(readBuffer);
197 } catch (Exception e) {
203 if (contentStream != null)
204 contentStream.close();
205 } catch (IOException e1) {
211 public static void convertWikiFile(String templateFileName, IFile file, String binBasePath, String srcBasePath,
212 IContentRenderer renderer) throws CoreException {
213 StringBuffer htmlBuffer = new StringBuffer();
214 convertWikiBuffer(templateFileName, htmlBuffer, file, renderer, true);
215 String htmlName = Util.getHTMLFileName(file, binBasePath, srcBasePath);
216 if (htmlName != null) {
217 writeHTMLFile(htmlBuffer, htmlName);
221 public static void getWikiBuffer(StringBuffer htmlBuffer, IFile file) throws CoreException {
222 BufferedInputStream stream = new BufferedInputStream(file.getContents());
224 htmlBuffer.append(getInputStreamAsCharArray(stream, -1, null));
226 //new String(getInputStreamAsCharArray(stream, -1, null));
227 } catch (IOException e) {
231 if (stream != null) {
234 } catch (IOException e) {
240 public static void convertWikiBuffer(String templateFileName, StringBuffer htmlBuffer, IFile file, IContentRenderer renderer,
241 boolean completeHTML) throws CoreException {
242 BufferedInputStream stream = new BufferedInputStream(file.getContents());
244 String content = new String(getInputStreamAsCharArray(stream, -1, null));
245 String srcPath = Util.getWikiTextsPath(file);
246 String filePath = file.getLocation().toString(); // file.getProjectRelativePath().toString()
247 if (filePath.startsWith(srcPath)) {
248 filePath = filePath.substring(srcPath.length() + 1);
250 createWikiBuffer(templateFileName, htmlBuffer, filePath, content, renderer, completeHTML);
251 } catch (IOException e) {
255 if (stream != null) {
258 } catch (IOException e) {
269 public static void createWikiBuffer(String templateFileName, StringBuffer htmlBuffer, String fileName, String content,
270 IContentRenderer renderer, boolean completeHTML) {
271 // calculate the <i>depth</i> of the file (i.e. ../../../ as much as needed)
275 index = fileName.indexOf('/', index);
281 renderer.render(templateFileName, content, htmlBuffer, level, completeHTML);
284 public static void writeHTMLFile(StringBuffer buffer, String filename) {
285 int index = filename.lastIndexOf('/');
287 File file = new File(filename.substring(0, index));
288 if (!file.isDirectory()) {
292 FileWriter fileWriter;
294 fileWriter = new FileWriter(filename);
295 fileWriter.write(buffer.toString());
297 } catch (FileNotFoundException e) {
298 // ignore exception; project is deleted by fUser
299 } catch (IOException e) {
300 // TODO Auto-generated catch block
306 * Returns the given input stream's contents as a character array. If a length is specified (ie. if length != -1), only length
307 * chars are returned. Otherwise all chars in the stream are returned. Note this doesn't close the stream.
309 * @throws IOException
310 * if a problem occured reading the stream.
312 public static char[] getInputStreamAsCharArray(InputStream stream, int length, String encoding) throws IOException {
313 InputStreamReader reader = null;
314 reader = encoding == null ? new InputStreamReader(stream) : new InputStreamReader(stream, encoding);
318 int contentsLength = 0;
321 int amountRequested = Math.max(stream.available(), DEFAULT_READING_SIZE); // read at least 8K
323 // resize contents if needed
324 if (contentsLength + amountRequested > contents.length) {
325 System.arraycopy(contents, 0, contents = new char[contentsLength + amountRequested], 0, contentsLength);
328 // read as many chars as possible
329 amountRead = reader.read(contents, contentsLength, amountRequested);
331 if (amountRead > 0) {
332 // remember length of contents
333 contentsLength += amountRead;
335 } while (amountRead != -1);
337 // resize contents if necessary
338 if (contentsLength < contents.length) {
339 System.arraycopy(contents, 0, contents = new char[contentsLength], 0, contentsLength);
342 contents = new char[length];
345 while ((readSize != -1) && (len != length)) {
347 // We record first the read size. In this case len is the actual
350 readSize = reader.read(contents, len, length - len);
353 // Now we need to resize in case the default encoding used more than
357 System.arraycopy(contents, 0, (contents = new char[len]), 0, len);