From b0670d1a8bb91761a423fb5a1ce65ec43efe5431 Mon Sep 17 00:00:00 2001 From: axelcl Date: Sun, 30 Jan 2005 21:27:21 +0000 Subject: [PATCH] ''Open Wiki link'' and ''Create Files for Wiki link'' every new created file creates an XML file with the correct extracted title. '/' in wiki names are not converted back to ':' anymore (but ':' are converted to '/', if there's no corresonding *.xml file) --- .../actions/CreateFilesFromLinksEditorAction.java | 21 +++-- .../phpeclipse/wiki/actions/CreateFilesJob.java | 9 ++- .../wiki/actions/OpenWikiLinkEditorAction.java | 96 +++++++++++++------- .../wiki/actions/mediawiki/RefreshJob.java | 25 +++++- .../phpeclipse/wiki/preferences/Util.java | 10 +- .../net/sourceforge/phpeclipse/wiki/xml/Page.java | 36 ++++++++ 6 files changed, 145 insertions(+), 52 deletions(-) diff --git a/archive/net.sourceforge.phpeclipse.wiki/src/net/sourceforge/phpeclipse/wiki/actions/CreateFilesFromLinksEditorAction.java b/archive/net.sourceforge.phpeclipse.wiki/src/net/sourceforge/phpeclipse/wiki/actions/CreateFilesFromLinksEditorAction.java index 1aa69ff..5f6739b 100644 --- a/archive/net.sourceforge.phpeclipse.wiki/src/net/sourceforge/phpeclipse/wiki/actions/CreateFilesFromLinksEditorAction.java +++ b/archive/net.sourceforge.phpeclipse.wiki/src/net/sourceforge/phpeclipse/wiki/actions/CreateFilesFromLinksEditorAction.java @@ -16,7 +16,7 @@ public final class CreateFilesFromLinksEditorAction extends OpenWikiLinkEditorAc // int pos = selection.getOffset(); ArrayList startPositionList = new ArrayList(); char[] text = doc.get().toCharArray(); - try { + try { char ch = ' '; int i = 0; int startPos = -1; @@ -46,27 +46,30 @@ public final class CreateFilesFromLinksEditorAction extends OpenWikiLinkEditorAc } HashSet wikiNames = new HashSet(); ArrayList filesList = new ArrayList(); - String str; + ArrayList wikiList = new ArrayList(); + String wikiTitle; Integer posInteger; IFile currentFile = ((IFileEditorInput) editor.getEditorInput()).getFile(); for (int i = 0; i < startPositionList.size(); i++) { posInteger = (Integer) startPositionList.get(i); - str = getWikiString(editor, doc, posInteger.intValue()); + wikiTitle = getWikiTitle(editor, doc, posInteger.intValue()); - if (str != null && !str.equals("")) { - if (!wikiNames.contains(str)) { - IFile file = getWikiFile(currentFile, str); + if (wikiTitle != null && !wikiTitle.equals("")) { + if (!wikiNames.contains(wikiTitle)) { + IFile file = getWikiFile(currentFile, wikiTitle); filesList.add(file); - wikiNames.add(str); + wikiList.add(wikiTitle); + wikiNames.add(wikiTitle); } } } if (filesList.size() > 0) { IFile[] files = new IFile[filesList.size()]; + String[] wikiTitles = new String[filesList.size()]; filesList.toArray(files); - - Job job = new CreateFilesJob(files); + wikiList.toArray(wikiTitles); + Job job = new CreateFilesJob(files, wikiTitles); // job.setRule(createRule(files)); job.setRule(null); job.setUser(true); diff --git a/archive/net.sourceforge.phpeclipse.wiki/src/net/sourceforge/phpeclipse/wiki/actions/CreateFilesJob.java b/archive/net.sourceforge.phpeclipse.wiki/src/net/sourceforge/phpeclipse/wiki/actions/CreateFilesJob.java index 1b384c7..c947d56 100644 --- a/archive/net.sourceforge.phpeclipse.wiki/src/net/sourceforge/phpeclipse/wiki/actions/CreateFilesJob.java +++ b/archive/net.sourceforge.phpeclipse.wiki/src/net/sourceforge/phpeclipse/wiki/actions/CreateFilesJob.java @@ -3,6 +3,7 @@ import java.io.ByteArrayInputStream; import net.sourceforge.phpeclipse.wiki.editor.WikiEditorPlugin; import net.sourceforge.phpeclipse.wiki.preferences.Util; +import net.sourceforge.phpeclipse.wiki.xml.Page; import org.eclipse.core.resources.IContainer; import org.eclipse.core.resources.IFile; @@ -17,16 +18,19 @@ import org.eclipse.ui.progress.IProgressConstants; public class CreateFilesJob extends WorkspaceJob { IFile[] files; + String[] wikiTitles; - public CreateFilesJob(IFile[] files) { + public CreateFilesJob(IFile[] files, String[] wikiTitles) { super("Create Wiki Files Job"); this.files = files; + this.wikiTitles = wikiTitles; } public IStatus runInWorkspace(IProgressMonitor monitor) { try { monitor.beginTask("Create Wiki Files", 100); IFile file; + String wikiTitle; int partOfWork = 100 / files.length; int work = 0; String newText = WikiEditorPlugin.AUTOMATICALLY_CREATED; @@ -35,6 +39,7 @@ public class CreateFilesJob extends WorkspaceJob { ProblemConsole console = new ProblemConsole(); for (int i = 0; i < files.length; i++) { file = files[i]; + wikiTitle = wikiTitles[i]; IContainer parent = file.getParent(); if (parent instanceof IFolder && (!((IFolder) parent).exists())) { @@ -44,6 +49,8 @@ public class CreateFilesJob extends WorkspaceJob { if (!file.exists()) { monitor.subTask("Create File: "+file.getLocation().toString()); file.create(source, true, monitor); + Page page = new Page("", wikiTitle, ""); + page.createXMLFile(file, false); } else { String message = "File: " + file.getLocation().toString() + "\n==>file already exists!"; monitor.subTask(message); diff --git a/archive/net.sourceforge.phpeclipse.wiki/src/net/sourceforge/phpeclipse/wiki/actions/OpenWikiLinkEditorAction.java b/archive/net.sourceforge.phpeclipse.wiki/src/net/sourceforge/phpeclipse/wiki/actions/OpenWikiLinkEditorAction.java index 8f38c48..c09b190 100644 --- a/archive/net.sourceforge.phpeclipse.wiki/src/net/sourceforge/phpeclipse/wiki/actions/OpenWikiLinkEditorAction.java +++ b/archive/net.sourceforge.phpeclipse.wiki/src/net/sourceforge/phpeclipse/wiki/actions/OpenWikiLinkEditorAction.java @@ -5,6 +5,8 @@ import java.io.ByteArrayInputStream; import net.sourceforge.phpeclipse.wiki.editor.WikiEditor; import net.sourceforge.phpeclipse.wiki.editor.WikiEditorPlugin; import net.sourceforge.phpeclipse.wiki.preferences.Util; +import net.sourceforge.phpeclipse.wiki.xml.Page; +import net.sourceforge.phpeclipse.wiki.xml.XStreamManager; import org.eclipse.core.resources.IContainer; import org.eclipse.core.resources.IFile; @@ -76,7 +78,7 @@ public class OpenWikiLinkEditorAction implements IEditorActionDelegate { } } - public static String getWikiString(AbstractTextEditor editor, IDocument document, int initialPos) { + public static String getWikiTitle(AbstractTextEditor editor, IDocument document, int initialPos) { try { int pos = initialPos; int line = document.getLineOfOffset(pos); @@ -116,7 +118,7 @@ public class OpenWikiLinkEditorAction implements IEditorActionDelegate { wikiLinkStart = i + 1; break; } - if (word.charAt(i) == '|') { // links wih different description + if (word.charAt(i) == '|') { // links with different description wikiLinkEnd = i; } if (word.charAt(i) == '#') { // for links with anchors @@ -141,49 +143,73 @@ public class OpenWikiLinkEditorAction implements IEditorActionDelegate { IDocument doc = getDocument(); ITextSelection selection = (ITextSelection) editor.getSelectionProvider().getSelection(); int pos = selection.getOffset(); - - String textRegion = getWikiString(editor, doc, pos); + + String wikiTitle = getWikiTitle(editor, doc, pos); IFileEditorInput ei = (IFileEditorInput) editor.getEditorInput(); - openWikiFile(ei.getFile(), textRegion, true); + openWikiFile(ei.getFile(), wikiTitle, true); } - public void openWikiUrl(IProject project, String word) { - if (word != null && !word.equals("")) { - IFile cfile = project.getFile("dummy.wp"); - IFile file = getWikiFile(cfile, word); + // public void openWikiUrl(IProject project, String wikiTitle) { + // if (wikiTitle != null && !wikiTitle.equals("")) { + // IFile cfile = project.getFile("dummy.wp"); + // IFile file = getWikiFile(cfile, wikiTitle); + // try { + // createNewFileIfNeeded(file, wikiTitle); + // + // IDE.openEditor(WikiEditorPlugin.getDefault().getActivePage(), file, true); + // } catch (Exception e) { + // // + // WikiEditorPlugin.getDefault().logAndReport(WikiEditorPlugin.getResourceString(WikiConstants.RESOURCE_WIKI_ERROR_DIALOGUE_OPEN_WIKI_FILE_TITLE), + // // WikiPlugin.getResourceString(WikiConstants.RESOURCE_WIKI_ERROR_DIALOGUE_OPEN_WIKI_FILE_TEXT), e); + // } + // } + // } + + protected void openWikiFile(IFile cfile, String wikiTitle, boolean openEditor) { + if (wikiTitle != null && !wikiTitle.equals("")) { + IFile file = getWikiFile(cfile, wikiTitle); try { - createNewFileIfNeeded(file, word); - // if (WikiEditorPlugin.getDefault().getPreferenceStore().getBoolean(WikiConstants.REUSE_EDITOR)) { - // saveIfNeeded(); - // getActivePage().reuseEditor(reusableEditor, new FileEditorInput(file)); - // } else { + createNewFileIfNeeded(file, wikiTitle); + + Page page = new Page("", wikiTitle, ""); + page.createXMLFile(file, false); +// createXMLFile(wikiTitle, file, false); + IDE.openEditor(WikiEditorPlugin.getDefault().getActivePage(), file, true); - // redrawText(); - // } } catch (Exception e) { // WikiEditorPlugin.getDefault().logAndReport(WikiEditorPlugin.getResourceString(WikiConstants.RESOURCE_WIKI_ERROR_DIALOGUE_OPEN_WIKI_FILE_TITLE), // WikiPlugin.getResourceString(WikiConstants.RESOURCE_WIKI_ERROR_DIALOGUE_OPEN_WIKI_FILE_TEXT), e); } - } - } - - protected void openWikiFile(IFile cfile, String word, boolean openEditor) { - if (word != null && !word.equals("")) { - IFile file = getWikiFile(cfile, word); - try { - createNewFileIfNeeded(file, word); - // if (WikiEditorPlugin.getDefault().getPreferenceStore().getBoolean(WikiConstants.REUSE_EDITOR)) { - // saveIfNeeded(); - // getActivePage().reuseEditor(reusableEditor, new FileEditorInput(file)); - // } else { - IDE.openEditor(WikiEditorPlugin.getDefault().getActivePage(), file, true); - // redrawText(); - // } - } catch (Exception e) { - // WikiEditorPlugin.getDefault().logAndReport(WikiEditorPlugin.getResourceString(WikiConstants.RESOURCE_WIKI_ERROR_DIALOGUE_OPEN_WIKI_FILE_TITLE), - // WikiPlugin.getResourceString(WikiConstants.RESOURCE_WIKI_ERROR_DIALOGUE_OPEN_WIKI_FILE_TEXT), e); + } + } + + /** + * @param wikiTitle + * @param file + * @throws CoreException + */ + private void createXMLFile(String wikiTitle, IFile file, boolean modify) throws CoreException { + String srcBasePath = Util.getWikiTextsPath(file); + String binBasePath = Util.getProjectsWikiOutputPath(file.getProject(), WikiEditorPlugin.HTML_OUTPUT_PATH); + + String filename = Util.getXMLFileName(file, binBasePath, srcBasePath); + IPath path = new Path(filename); + IFile xmlFile = ResourcesPlugin.getWorkspace().getRoot().getFileForLocation(path); + IContainer parent = xmlFile.getParent(); + if (parent instanceof IFolder && (!((IFolder) parent).exists())) { + Util.createFolder((IFolder) parent, null); + } + Page page = new Page("", wikiTitle, ""); + byte[] buffer = XStreamManager.toXML(page).getBytes(); + ByteArrayInputStream source = new ByteArrayInputStream(buffer); + if (!xmlFile.exists()) { + // only if file doesn't exists + xmlFile.create(source, true, null); + } else { + if (modify) { + xmlFile.setContents(source, true, true, null); } - } + } } protected void createNewFileIfNeeded(IFile file, String word) throws CoreException { diff --git a/archive/net.sourceforge.phpeclipse.wiki/src/net/sourceforge/phpeclipse/wiki/actions/mediawiki/RefreshJob.java b/archive/net.sourceforge.phpeclipse.wiki/src/net/sourceforge/phpeclipse/wiki/actions/mediawiki/RefreshJob.java index bda1e0a..e3e72e8 100644 --- a/archive/net.sourceforge.phpeclipse.wiki/src/net/sourceforge/phpeclipse/wiki/actions/mediawiki/RefreshJob.java +++ b/archive/net.sourceforge.phpeclipse.wiki/src/net/sourceforge/phpeclipse/wiki/actions/mediawiki/RefreshJob.java @@ -57,9 +57,30 @@ public class RefreshJob extends WorkspaceJob { HashMap map = new HashMap(); String wikiTitle; for (int i = 0; i < files.length; i++) { - wikiTitle = Util.getReadableWikiName(files[i]); + wikiTitle = null; + file = files[i]; + String srcBasePath = Util.getWikiTextsPath(file); + String binBasePath = Util.getProjectsWikiOutputPath(file.getProject(), WikiEditorPlugin.HTML_OUTPUT_PATH); + + String fileXMLName = Util.getXMLFileName(file, binBasePath, srcBasePath); + IPath path = new Path(fileXMLName); + IFile xmlFile = ResourcesPlugin.getWorkspace().getRoot().getFileForLocation(path); + if (xmlFile.exists()) { + try { + Page page = XStreamManager.fromXML(xmlFile.getContents()); + if (page != null) { + wikiTitle = page.getTitle(); + } + // timestamp = XMLReader.getDateTimestamp(xmlFile.getContents()); + } catch (Exception e2) { + } + } + if (wikiTitle==null) { + // if no XML file exists we create the name from the filename + wikiTitle = Util.getReadableWikiName(files[i]); + } buffer.append(wikiTitle); - map.put(wikiTitle, files[i]); + map.put(wikiTitle, file); if (i != files.length - 1) { buffer.append("\n"); } diff --git a/archive/net.sourceforge.phpeclipse.wiki/src/net/sourceforge/phpeclipse/wiki/preferences/Util.java b/archive/net.sourceforge.phpeclipse.wiki/src/net/sourceforge/phpeclipse/wiki/preferences/Util.java index b48f838..b986737 100644 --- a/archive/net.sourceforge.phpeclipse.wiki/src/net/sourceforge/phpeclipse/wiki/preferences/Util.java +++ b/archive/net.sourceforge.phpeclipse.wiki/src/net/sourceforge/phpeclipse/wiki/preferences/Util.java @@ -206,7 +206,7 @@ public class Util { } /** - * Extract the wiki name from the current filename, replaces '/' with ':' characters, but doesn't replace '_' characters + * Extract the wiki name from the current filename, but doesn't replace '_' characters * * @param currentFile * @return @@ -216,7 +216,7 @@ public class Util { } /** - * Extract the wiki name from the current filename, replaces '/' with ':' characters, and '_' with ' ' characters if + * Extract the wiki name from the current filename, replaces '_' with ' ' characters if * replaceUnderscore==true * * @param currentFile @@ -235,9 +235,9 @@ public class Util { for (int i = basePath.length() + 1; i < lastIndex; i++) { ch = filePath.charAt(i); switch (ch) { - case '/': - result.append(':'); - break; +// case '/': +// result.append(':'); +// break; default: if (ch == '_' && replaceUnderscore) { result.append(' '); diff --git a/archive/net.sourceforge.phpeclipse.wiki/src/net/sourceforge/phpeclipse/wiki/xml/Page.java b/archive/net.sourceforge.phpeclipse.wiki/src/net/sourceforge/phpeclipse/wiki/xml/Page.java index adafff1..2421ed0 100644 --- a/archive/net.sourceforge.phpeclipse.wiki/src/net/sourceforge/phpeclipse/wiki/xml/Page.java +++ b/archive/net.sourceforge.phpeclipse.wiki/src/net/sourceforge/phpeclipse/wiki/xml/Page.java @@ -1,7 +1,19 @@ package net.sourceforge.phpeclipse.wiki.xml; +import java.io.ByteArrayInputStream; import java.util.ArrayList; +import net.sourceforge.phpeclipse.wiki.editor.WikiEditorPlugin; +import net.sourceforge.phpeclipse.wiki.preferences.Util; + +import org.eclipse.core.resources.IContainer; +import org.eclipse.core.resources.IFile; +import org.eclipse.core.resources.IFolder; +import org.eclipse.core.resources.ResourcesPlugin; +import org.eclipse.core.runtime.CoreException; +import org.eclipse.core.runtime.IPath; +import org.eclipse.core.runtime.Path; + // public class Page { @@ -92,4 +104,28 @@ public class Page { public Revision get(int index) { return (Revision) listOfRevisions.get(index); } + + public void createXMLFile(IFile file, boolean modify) throws CoreException { + String srcBasePath = Util.getWikiTextsPath(file); + String binBasePath = Util.getProjectsWikiOutputPath(file.getProject(), WikiEditorPlugin.HTML_OUTPUT_PATH); + + String filename = Util.getXMLFileName(file, binBasePath, srcBasePath); + IPath path = new Path(filename); + IFile xmlFile = ResourcesPlugin.getWorkspace().getRoot().getFileForLocation(path); + IContainer parent = xmlFile.getParent(); + if (parent instanceof IFolder && (!((IFolder) parent).exists())) { + Util.createFolder((IFolder) parent, null); + } +// Page page = new Page("", wikiTitle, ""); + byte[] buffer = XStreamManager.toXML(this).getBytes(); + ByteArrayInputStream source = new ByteArrayInputStream(buffer); + if (!xmlFile.exists()) { + // only if file doesn't exists + xmlFile.create(source, true, null); + } else { + if (modify) { + xmlFile.setContents(source, true, true, null); + } + } + } } \ No newline at end of file -- 1.7.1