}
private static String getWikiFile(IFile file) {
- return Util.getFileWikiName(file);
+ return Util.getURLWikiName(file);
}
public void init(IWorkbenchWindow window) {
}
private String getWikiFile(IFile file) {
- return Util.getFileWikiName(file);
+ return Util.getURLWikiName(file);
}
public void init(IWorkbenchWindow window) {
import net.sourceforge.phpeclipse.wiki.actions.mediawiki.exceptions.UnexpectedAnswerException;
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;
this.files = files;
this.configuration = configuration;
this.actionURL = actionURL;
- }
+ }
public IStatus runInWorkspace(IProgressMonitor monitor) {
ProblemConsole console = new ProblemConsole();
buffer.append("\n");
}
}
- MediaWikiConnector mwc = new MediaWikiConnector();
+ MediaWikiConnector mwConnector = new MediaWikiConnector();
String url = actionURL;
if (url == null) {
url = configuration.getActionUrl() + "/" + configuration.getSpecialNs() + ":Export";
}
// get a list of Parsed elements
monitor.subTask("Downloading (XML Import)");
- ArrayList list = mwc.loadXML(configuration, url, buffer.toString());
+ ArrayList list = mwConnector.loadXML(configuration, url, buffer.toString());
String body;
for (int i = 0; i < list.size(); i++) {
if (body != null) {
file = (IFile) map.get(wikiTitle);
if (file != null) {
- monitor.subTask("Modify file: "+file.getLocation().toString());
- updateFileContent(console, file, parsed, body, configuration, monitor);
+ // rearrange parsed data into a page for XStream hamdling:
+ Page page = new Page(parsed.getDateTimestamp(), wikiTitle, body);
+ monitor.subTask("Modify file: " + file.getLocation().toString());
+ updateFileContent(console, file, page, body, configuration, monitor);
}
}
}
return isModal.booleanValue();
}
- private static void updateFileContent(ProblemConsole console, IFile file, Parsed parsed, String body, IWikipedia wp,
+ private static void updateFileContent(ProblemConsole console, IFile file, Page page, String body, IWikipedia wp,
IProgressMonitor monitor) {
try {
if (file.exists()) {
Util.createFolder((IFolder) parent, monitor);
}
try {
- String xmlData = parsed.getXmlData();
- String charSet = "UTF-8";
- StringBuffer buf = new StringBuffer();
- int index = xmlData.indexOf("<page>");
- if (index<0) {
- console.println("File: " + xmlFile.getLocation().toString() + "\n==>Couldn't create xml file - <page> tag not found");
- return;
- }
- xmlData = xmlData.substring(index);
-// buf.setLength(xmlData.length() + WikiEditorPlugin.XML_START_1.length() + WikiEditorPlugin.XML_START_2.length()
-// + WikiEditorPlugin.XML_END.length() + charSet.length());
-//
- buf.append(WikiEditorPlugin.XML_START_1);
- buf.append(charSet);
- buf.append(WikiEditorPlugin.XML_START_2);
- buf.append(xmlData);
- buf.append(WikiEditorPlugin.XML_END);
+ // String xmlData = parsed.getXmlData();
+ // String charSet = "UTF-8";
+ // StringBuffer buf = new StringBuffer();
+ // int index = xmlData.indexOf("<page>");
+ // if (index<0) {
+ // console.println("File: " + xmlFile.getLocation().toString() + "\n==>Couldn't create xml file - <page> tag not found");
+ // return;
+ // }
+ // xmlData = xmlData.substring(index);
+ // buf.append(WikiEditorPlugin.XML_START_1);
+ // buf.append(charSet);
+ // buf.append(WikiEditorPlugin.XML_START_2);
+ // buf.append(xmlData);
+ // buf.append(WikiEditorPlugin.XML_END);
- byte[] buffer = buf.toString().getBytes();
+ // byte[] buffer = buf.toString().getBytes();
+ byte[] buffer = XStreamManager.toXML(page).getBytes();
ByteArrayInputStream source = new ByteArrayInputStream(buffer);
if (!xmlFile.exists()) {
xmlFile.create(source, true, monitor);
}
} catch (UnsupportedEncodingException e) {
console.println("File: " + file.getLocation().toString() + "\n==>UnsupportedEncodingException: " + e.getMessage());
- } catch (CoreException e) {
- console.println("File: " + file.getLocation().toString() + "\n==>CoreException: " + e.getMessage());
+ } catch (Exception e) {
+ console.println("File: " + file.getLocation().toString() + "\n==>Exception: " + e.getMessage());
}
}
}
\ No newline at end of file
package net.sourceforge.phpeclipse.wiki.actions.mediawiki.connect;
+import java.io.InputStream;
+
//Parts of this sources are copied and modified from the jEdit Wikipedia plugin:
//http://www.djini.de/software/wikipedia/index.html
//
public String getTimestamp() {
return timestamp;
}
+
+ public String getDateTimestamp() {
+ if (timestamp!=null) {
+ StringBuffer buffer = new StringBuffer();
+ // 2004-11-22T12:41:10Z
+ buffer.append(timestamp.substring(0,4)); //year
+ buffer.append(timestamp.substring(5,7)); //month
+ buffer.append(timestamp.substring(8,10)); //day
+ buffer.append(timestamp.substring(11,13));//hour
+ buffer.append(timestamp.substring(14,16));//minute
+ buffer.append(timestamp.substring(17,19));//second
+ return buffer.toString();
+ }
+ return "";
+ }
/**
* @return Returns the title.
*/
import java.io.IOException;
import java.io.InputStream;
+import java.io.InputStreamReader;
import java.io.StringWriter;
import java.util.HashMap;
import net.sourceforge.phpeclipse.wiki.actions.mediawiki.config.IWikipedia;
import net.sourceforge.phpeclipse.wiki.actions.mediawiki.connect.Content;
import net.sourceforge.phpeclipse.wiki.actions.mediawiki.connect.MediaWikiConnector;
-import net.sourceforge.phpeclipse.wiki.actions.mediawiki.connect.XMLReader;
import net.sourceforge.phpeclipse.wiki.actions.mediawiki.exceptions.MethodException;
import net.sourceforge.phpeclipse.wiki.actions.mediawiki.exceptions.PageNotEditableException;
import net.sourceforge.phpeclipse.wiki.actions.mediawiki.exceptions.UnexpectedAnswerException;
import net.sourceforge.phpeclipse.wiki.internal.Configuration;
import net.sourceforge.phpeclipse.wiki.preferences.Util;
import net.sourceforge.phpeclipse.wiki.velocity.EditorText;
+import net.sourceforge.phpeclipse.wiki.xml.Page;
+import net.sourceforge.phpeclipse.wiki.xml.XStreamManager;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.Velocity;
return template;
}
- private void selectWiki(String timestamp, String body, MediaWikiConnector connector, String actionUrl, String wikiName)
+ private void uploadWiki(String timestamp, String body, MediaWikiConnector connector, String actionUrl, String wikiName)
throws UnexpectedAnswerException, MethodException, PageNotEditableException, InterruptedException {
String url = generateUrl(configuration.getURL(), wikiName);
actionUrl = wikipedia.getActionUrl();
}
try {
- if (files.length>0) {
+ if (files.length > 0) {
// prefetch for error messages
file = files[0];
}
int work = 0;
StringBuffer buffer = new StringBuffer();
HashMap map = new HashMap();
- String wikiTitle;
+ String wikiURLTitle;
- monitor.subTask("Login user:"+user);
+ monitor.subTask("Login user:" + user);
connector = new MediaWikiConnector();
success = connector.login(wikipedia, actionUrl, user, password, false);
if (success) {
try {
file = files[i];
is = file.getContents();
- String wikiName = Util.getFileWikiName(file);
+ wikiURLTitle = Util.getURLWikiName(file);
String body = StoreWikipediaAction.getInputStreamAsString(is, wikipedia.getCharSet());
char ch;
boolean noContent = checkNoContent(body);
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);
+ String fileXMLName = Util.getXMLFileName(file, binBasePath, srcBasePath);
+ IPath path = new Path(fileXMLName);
IFile xmlFile = ResourcesPlugin.getWorkspace().getRoot().getFileForLocation(path);
- String timestamp = null;
+
+ String timestamp = "";
if (xmlFile.exists()) {
try {
- timestamp = XMLReader.getDateTimestamp(xmlFile.getContents());
+ Page page = XStreamManager.fromXML(xmlFile.getContents());
+ if (page != null) {
+ // we've stored information about the article at download time which
+ // may be more exactly as the standard information
+ if (!page.isEmpty()) {
+ // at least one revision exists:
+ timestamp = page.get(0).getTimestamp();
+ }
+ wikiURLTitle = page.getURLTitle();
+ }
+ // timestamp = XMLReader.getDateTimestamp(xmlFile.getContents());
} catch (Exception e2) {
}
}
- if (timestamp == null) {
- // Date d = new Date();
- // timestamp = String.valueOf(d.getTime());
- timestamp = "";
- }
if (noContent) {
console.println("File: " + file.getLocation().toString() + "\n==>upload not allowed; Wiki text contains no content");
} else {
monitor.subTask("Upload: " + file.getLocation().toString());
- selectWiki(timestamp, body, connector, actionUrl, wikiName);
+ uploadWiki(timestamp, body, connector, actionUrl, wikiURLTitle);
}
} catch (CoreException e1) {
}
private String getWikiFile(IFile file) {
- return Util.getFileWikiName(file);
+ return Util.getURLWikiName(file);
}
public void init(IWorkbenchWindow window) {
} catch (CoreException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
+ } catch (Exception e) {
+ // TODO Auto-generated catch block
+ e.printStackTrace();
}
} else {
String fname = file.getName().toLowerCase();
* @param currentFile
* @return
*/
- public static String getFileWikiName(IFile currentFile) {
- return getFileWikiName(currentFile, false);
+ public static String getURLWikiName(IFile currentFile) {
+ return getURLWikiName(currentFile, false);
}
/**
* @param replaceUnderscore
* @return
*/
- public static String getFileWikiName(IFile currentFile, boolean replaceUnderscore) {
+ public static String getURLWikiName(IFile currentFile, boolean replaceUnderscore) {
String filePath = currentFile.getLocation().toString();
String basePath = getWikiTextsPath(currentFile);
StringBuffer result = new StringBuffer();
default:
if (ch == '_' && replaceUnderscore) {
result.append(' ');
- } else {
+ } else {
result.append(ch);
}
}
* @return
*/
public static String getReadableWikiName(IFile currentFile) {
- return getFileWikiName(currentFile, true);
+ return getURLWikiName(currentFile, true);
}
public static String getWikiTitle(IFile currentFile) {
--- /dev/null
+package net.sourceforge.phpeclipse.wiki.xml;
+
+import java.util.ArrayList;
+
+//
+
+public class Page {
+ /**
+ * <page> XML data from Wikipedia Special:Export pages may be <code>null</code>
+ *
+ */
+
+ /* package private */String title = null;
+
+ /* package private */ArrayList listOfRevisions = null;
+
+ // Revision revision = null;
+
+ /* package private */Page() {
+ }
+
+ public Page(String timeStamp, String title, String body) {
+ listOfRevisions = new ArrayList();
+ Revision revision = new Revision(timeStamp, body);
+ listOfRevisions.add(revision);
+ this.title = title;
+ }
+
+ public void add(Revision revision) {
+ listOfRevisions.add(revision);
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see java.lang.Object#toString()
+ */
+ public String toString() {
+ StringBuffer buffer = new StringBuffer();
+
+ if (title != null) {
+ buffer.append("==>Title: ");
+ buffer.append(title);
+ buffer.append("\n");
+ }
+
+ // if (revision != null) {
+ // buffer.append("==>Revision:\n");
+ // buffer.append(revision);
+ // buffer.append("\n");
+ // }
+ for (int i = 0; i < listOfRevisions.size(); i++) {
+ Revision revision = (Revision) listOfRevisions.get(i);
+ if (revision != null) {
+ buffer.append("==>Revision:\n");
+ buffer.append(revision);
+ buffer.append("\n");
+ }
+ }
+ return buffer.toString();
+ }
+
+ /**
+ * @return Returns the title.
+ */
+ public String getTitle() {
+ return title;
+ }
+
+ public String getURLTitle() {
+ return title.replaceAll(" ", "_");
+ }
+
+ /**
+ * @return
+ */
+ public boolean isEmpty() {
+ return listOfRevisions.isEmpty();
+ }
+
+ /**
+ * @return
+ */
+ public int size() {
+ return listOfRevisions.size();
+ }
+
+ /**
+ * @param index
+ * @return
+ */
+ public Revision get(int index) {
+ return (Revision) listOfRevisions.get(index);
+ }
+}
\ No newline at end of file
--- /dev/null
+package net.sourceforge.phpeclipse.wiki.xml;
+
+import java.util.ArrayList;
+
+import com.thoughtworks.xstream.alias.ClassMapper;
+import com.thoughtworks.xstream.converters.ConversionException;
+import com.thoughtworks.xstream.converters.Converter;
+import com.thoughtworks.xstream.converters.MarshallingContext;
+import com.thoughtworks.xstream.converters.UnmarshallingContext;
+import com.thoughtworks.xstream.io.HierarchicalStreamReader;
+import com.thoughtworks.xstream.io.HierarchicalStreamWriter;
+
+/**
+ * Converts a Page object from and to xml
+ */
+public class PageConverter implements Converter {
+ protected ClassMapper classMapper;
+
+ protected String classAttributeIdentifier;
+
+ public PageConverter(ClassMapper classMapper, String classAttributeIdentifier) {
+ this.classMapper = classMapper;
+ this.classAttributeIdentifier = classAttributeIdentifier;
+ }
+
+ public boolean canConvert(Class type) {
+ return type.equals(Page.class);
+ }
+
+ public void marshal(Object source, HierarchicalStreamWriter writer, MarshallingContext context) {
+ Page page = (Page) source;
+ writer.startNode("title");
+ writer.setValue(page.title);
+ writer.endNode();
+
+ ArrayList list = page.listOfRevisions;
+ for (int i = 0; i < list.size(); i++) {
+ Object item = list.get(i);
+ writeItem(item, context, writer);
+ }
+ }
+
+ public Object unmarshal(HierarchicalStreamReader reader, UnmarshallingContext context) {
+ Page page = (Page) createPage(context.getRequiredType());
+ reader.moveDown();
+ page.title = (String) reader.getValue();
+ reader.moveUp();
+ populatePage(reader, context, page);
+ return page;
+ }
+
+ protected void populatePage(HierarchicalStreamReader reader, UnmarshallingContext context, Page page) {
+ ArrayList list = page.listOfRevisions;
+ if (list == null) {
+ page.listOfRevisions = new ArrayList();
+ list = page.listOfRevisions;
+ }
+ while (reader.hasMoreChildren()) {
+ reader.moveDown();
+ Object item = readItem(reader, context, page);
+ list.add(item);
+ reader.moveUp();
+ }
+ }
+
+ protected void writeItem(Object item, MarshallingContext context, HierarchicalStreamWriter writer) {
+ // PUBLISHED API METHOD! If changing signature, ensure backwards compatability.
+ if (item == null) {
+ // todo: this is duplicated in TreeMarshaller.start()
+ writer.startNode(classMapper.lookupName(ClassMapper.Null.class));
+ writer.endNode();
+ } else {
+ writer.startNode(classMapper.lookupName(item.getClass()));
+ context.convertAnother(item);
+ writer.endNode();
+ }
+ }
+
+ protected Object readItem(HierarchicalStreamReader reader, UnmarshallingContext context, Object current) {
+ // PUBLISHED API METHOD! If changing signature, ensure backwards compatability.
+ String classAttribute = reader.getAttribute(classAttributeIdentifier);
+ Class type;
+ if (classAttribute == null) {
+ type = classMapper.lookupType(reader.getNodeName());
+ } else {
+ type = classMapper.lookupType(classAttribute);
+ }
+ return context.convertAnother(current, type);
+ }
+
+ protected Object createPage(Class type) {
+ Class defaultType = classMapper.lookupDefaultType(type);
+ try {
+ return defaultType.newInstance();
+ } catch (InstantiationException e) {
+ throw new ConversionException("Cannot instantiate " + defaultType.getName(), e);
+ } catch (IllegalAccessException e) {
+ throw new ConversionException("Cannot instantiate " + defaultType.getName(), e);
+ }
+ }
+}
\ No newline at end of file
--- /dev/null
+package net.sourceforge.phpeclipse.wiki.xml;
+
+//
+
+public class Revision {
+ /**
+ * timeStamp represented in XML format from Wikipedia Special:Export pages
+ */
+ /* package private */String timestamp = null;
+
+ /* package private */String contributor = null;
+
+ /* package private */String text = null;
+
+ /* package private */Revision() {
+ }
+
+ public Revision(String timeStamp, String text, String contributor) {
+ this.timestamp = timeStamp;
+ this.contributor = contributor;
+ this.text = text;
+ }
+
+ public Revision(String timeStamp, String text) {
+ this(timeStamp, text, "");
+ }
+
+ public Revision(String text) {
+ this("", text, "");
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see java.lang.Object#toString()
+ */
+ public String toString() {
+ StringBuffer buffer = new StringBuffer();
+
+ if (timestamp != null) {
+ buffer.append(" ==>Timestamp: ");
+ buffer.append(timestamp);
+ buffer.append("\n");
+ }
+
+ if (contributor != null) {
+ buffer.append(" ==>Contributor: ");
+ buffer.append(contributor);
+ buffer.append("\n");
+ }
+
+ if (text != null) {
+ buffer.append(" ==>Body: ");
+ buffer.append(text);
+ buffer.append("\n");
+ }
+ return buffer.toString();
+ }
+
+ /**
+ * @return Returns the timestamp.
+ */
+ public String getTimestamp() {
+ return timestamp;
+ }
+
+ /**
+ * @return Returns the contributor.
+ */
+ public String getContributor() {
+ return contributor;
+ }
+
+ /**
+ * @return Returns the text.
+ */
+ public String getText() {
+ return text;
+ }
+}
\ No newline at end of file
--- /dev/null
+package net.sourceforge.phpeclipse.wiki.xml;
+
+import java.io.InputStream;
+import java.io.InputStreamReader;
+import java.io.Reader;
+
+import com.thoughtworks.xstream.XStream;
+import com.thoughtworks.xstream.io.xml.DomDriver;
+
+public class XStreamManager {
+
+ public static String toXML(Page page) {
+ XStream xstream = new XStream(new DomDriver());
+ xstream.alias("page", Page.class);
+ xstream.changeDefaultConverter(new PageConverter(xstream.getClassMapper(), "revision"));
+ xstream.alias("revision", Revision.class);
+ return xstream.toXML(page);
+ }
+
+ public static Page fromXML(String xmlString) {
+ XStream xstream = new XStream(new DomDriver());
+ xstream.alias("page", Page.class);
+ xstream.changeDefaultConverter(new PageConverter(xstream.getClassMapper(), "revision"));
+ xstream.alias("revision", Revision.class);
+ return (Page) xstream.fromXML(xmlString);
+ }
+
+ public static Page fromXML(Reader xmlReader) {
+ XStream xstream = new XStream(new DomDriver());
+ xstream.alias("page", Page.class);
+ xstream.changeDefaultConverter(new PageConverter(xstream.getClassMapper(), "revision"));
+ xstream.alias("revision", Revision.class);
+ return (Page) xstream.fromXML(xmlReader);
+ }
+
+ public static Page fromXML(InputStream iStream) {
+ InputStreamReader sReader = new InputStreamReader(iStream);
+ return fromXML(sReader);
+ }
+
+ public static void main(String[] args) {
+ Page page = new Page("20050129", "title", "body1");
+ page.add(new Revision("20050130", "body2", "Joe"));
+ page.add(new Revision("20050131", "body3"));
+ String xml = toXML(page);
+ System.out.println(xml);
+
+ Page newXML = (Page) fromXML(xml);
+ System.out.println(newXML);
+ }
+}
\ No newline at end of file