From: axelcl Date: Sun, 30 Jan 2005 13:30:50 +0000 (+0000) Subject: Added xstream handiling for Wikipedia upload/download X-Git-Url: http://git.phpeclipse.com?hp=c7455fb47aadc31ac7c27f960896c810640dce6a Added xstream handiling for Wikipedia upload/download --- diff --git a/archive/net.sourceforge.phpeclipse.wiki/src/net/sourceforge/phpeclipse/wiki/actions/mediawiki/DownloadWikipediaAction.java b/archive/net.sourceforge.phpeclipse.wiki/src/net/sourceforge/phpeclipse/wiki/actions/mediawiki/DownloadWikipediaAction.java index 26b6fcc..1b26b4b 100644 --- a/archive/net.sourceforge.phpeclipse.wiki/src/net/sourceforge/phpeclipse/wiki/actions/mediawiki/DownloadWikipediaAction.java +++ b/archive/net.sourceforge.phpeclipse.wiki/src/net/sourceforge/phpeclipse/wiki/actions/mediawiki/DownloadWikipediaAction.java @@ -124,7 +124,7 @@ public class DownloadWikipediaAction implements IEditorActionDelegate { } private static String getWikiFile(IFile file) { - return Util.getFileWikiName(file); + return Util.getURLWikiName(file); } public void init(IWorkbenchWindow window) { diff --git a/archive/net.sourceforge.phpeclipse.wiki/src/net/sourceforge/phpeclipse/wiki/actions/mediawiki/LoadWikipediaSQLAction.java b/archive/net.sourceforge.phpeclipse.wiki/src/net/sourceforge/phpeclipse/wiki/actions/mediawiki/LoadWikipediaSQLAction.java index 3344736..aa84549 100644 --- a/archive/net.sourceforge.phpeclipse.wiki/src/net/sourceforge/phpeclipse/wiki/actions/mediawiki/LoadWikipediaSQLAction.java +++ b/archive/net.sourceforge.phpeclipse.wiki/src/net/sourceforge/phpeclipse/wiki/actions/mediawiki/LoadWikipediaSQLAction.java @@ -86,7 +86,7 @@ public class LoadWikipediaSQLAction implements IEditorActionDelegate { } private String getWikiFile(IFile file) { - return Util.getFileWikiName(file); + return Util.getURLWikiName(file); } public void init(IWorkbenchWindow window) { 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 321c369..bda1e0a 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 @@ -13,6 +13,8 @@ import net.sourceforge.phpeclipse.wiki.actions.mediawiki.exceptions.MethodExcept 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; @@ -40,7 +42,7 @@ public class RefreshJob extends WorkspaceJob { this.files = files; this.configuration = configuration; this.actionURL = actionURL; - } + } public IStatus runInWorkspace(IProgressMonitor monitor) { ProblemConsole console = new ProblemConsole(); @@ -62,14 +64,14 @@ public class RefreshJob extends WorkspaceJob { 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++) { @@ -80,8 +82,10 @@ public class RefreshJob extends WorkspaceJob { 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); } } } @@ -129,7 +133,7 @@ public class RefreshJob extends WorkspaceJob { 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()) { @@ -158,25 +162,23 @@ public class RefreshJob extends WorkspaceJob { Util.createFolder((IFolder) parent, monitor); } try { - String xmlData = parsed.getXmlData(); - String charSet = "UTF-8"; - StringBuffer buf = new StringBuffer(); - int index = xmlData.indexOf(""); - if (index<0) { - console.println("File: " + xmlFile.getLocation().toString() + "\n==>Couldn't create xml file - 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(""); + // if (index<0) { + // console.println("File: " + xmlFile.getLocation().toString() + "\n==>Couldn't create xml file - 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); @@ -190,8 +192,8 @@ public class RefreshJob extends WorkspaceJob { } } 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 diff --git a/archive/net.sourceforge.phpeclipse.wiki/src/net/sourceforge/phpeclipse/wiki/actions/mediawiki/connect/Parsed.java b/archive/net.sourceforge.phpeclipse.wiki/src/net/sourceforge/phpeclipse/wiki/actions/mediawiki/connect/Parsed.java index c8bb20d..8adf257 100644 --- a/archive/net.sourceforge.phpeclipse.wiki/src/net/sourceforge/phpeclipse/wiki/actions/mediawiki/connect/Parsed.java +++ b/archive/net.sourceforge.phpeclipse.wiki/src/net/sourceforge/phpeclipse/wiki/actions/mediawiki/connect/Parsed.java @@ -1,5 +1,7 @@ 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 // @@ -62,6 +64,21 @@ public class Parsed { 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. */ diff --git a/archive/net.sourceforge.phpeclipse.wiki/src/net/sourceforge/phpeclipse/wiki/actions/mediawiki/post/PostJob.java b/archive/net.sourceforge.phpeclipse.wiki/src/net/sourceforge/phpeclipse/wiki/actions/mediawiki/post/PostJob.java index 24f807e..a7c811f 100644 --- a/archive/net.sourceforge.phpeclipse.wiki/src/net/sourceforge/phpeclipse/wiki/actions/mediawiki/post/PostJob.java +++ b/archive/net.sourceforge.phpeclipse.wiki/src/net/sourceforge/phpeclipse/wiki/actions/mediawiki/post/PostJob.java @@ -2,6 +2,7 @@ package net.sourceforge.phpeclipse.wiki.actions.mediawiki.post; import java.io.IOException; import java.io.InputStream; +import java.io.InputStreamReader; import java.io.StringWriter; import java.util.HashMap; @@ -9,7 +10,6 @@ import net.sourceforge.phpeclipse.wiki.actions.ProblemConsole; 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; @@ -17,6 +17,8 @@ import net.sourceforge.phpeclipse.wiki.editor.WikiEditorPlugin; 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; @@ -81,7 +83,7 @@ public class PostJob extends WorkspaceJob { 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); @@ -104,7 +106,7 @@ public class PostJob extends WorkspaceJob { actionUrl = wikipedia.getActionUrl(); } try { - if (files.length>0) { + if (files.length > 0) { // prefetch for error messages file = files[0]; } @@ -113,9 +115,9 @@ public class PostJob extends WorkspaceJob { 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) { @@ -123,34 +125,40 @@ public class PostJob extends WorkspaceJob { 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) { diff --git a/archive/net.sourceforge.phpeclipse.wiki/src/net/sourceforge/phpeclipse/wiki/actions/mediawiki/post/StoreWikipediaAction.java b/archive/net.sourceforge.phpeclipse.wiki/src/net/sourceforge/phpeclipse/wiki/actions/mediawiki/post/StoreWikipediaAction.java index d54b064..85dc17d 100644 --- a/archive/net.sourceforge.phpeclipse.wiki/src/net/sourceforge/phpeclipse/wiki/actions/mediawiki/post/StoreWikipediaAction.java +++ b/archive/net.sourceforge.phpeclipse.wiki/src/net/sourceforge/phpeclipse/wiki/actions/mediawiki/post/StoreWikipediaAction.java @@ -134,7 +134,7 @@ public class StoreWikipediaAction implements IEditorActionDelegate { } private String getWikiFile(IFile file) { - return Util.getFileWikiName(file); + return Util.getURLWikiName(file); } public void init(IWorkbenchWindow window) { diff --git a/archive/net.sourceforge.phpeclipse.wiki/src/net/sourceforge/phpeclipse/wiki/builder/CreatePageAction.java b/archive/net.sourceforge.phpeclipse.wiki/src/net/sourceforge/phpeclipse/wiki/builder/CreatePageAction.java index e2b3408..02546eb 100644 --- a/archive/net.sourceforge.phpeclipse.wiki/src/net/sourceforge/phpeclipse/wiki/builder/CreatePageAction.java +++ b/archive/net.sourceforge.phpeclipse.wiki/src/net/sourceforge/phpeclipse/wiki/builder/CreatePageAction.java @@ -113,6 +113,9 @@ public class CreatePageAction implements IObjectActionDelegate { } 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(); 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 9508175..b48f838 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 @@ -211,8 +211,8 @@ public class Util { * @param currentFile * @return */ - public static String getFileWikiName(IFile currentFile) { - return getFileWikiName(currentFile, false); + public static String getURLWikiName(IFile currentFile) { + return getURLWikiName(currentFile, false); } /** @@ -223,7 +223,7 @@ public class Util { * @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(); @@ -241,7 +241,7 @@ public class Util { default: if (ch == '_' && replaceUnderscore) { result.append(' '); - } else { + } else { result.append(ch); } } @@ -256,7 +256,7 @@ public class Util { * @return */ public static String getReadableWikiName(IFile currentFile) { - return getFileWikiName(currentFile, true); + return getURLWikiName(currentFile, true); } public static String getWikiTitle(IFile currentFile) { 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 new file mode 100644 index 0000000..adafff1 --- /dev/null +++ b/archive/net.sourceforge.phpeclipse.wiki/src/net/sourceforge/phpeclipse/wiki/xml/Page.java @@ -0,0 +1,95 @@ +package net.sourceforge.phpeclipse.wiki.xml; + +import java.util.ArrayList; + +// + +public class Page { + /** + * <page> XML data from Wikipedia Special:Export pages may be null + * + */ + + /* 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 diff --git a/archive/net.sourceforge.phpeclipse.wiki/src/net/sourceforge/phpeclipse/wiki/xml/PageConverter.java b/archive/net.sourceforge.phpeclipse.wiki/src/net/sourceforge/phpeclipse/wiki/xml/PageConverter.java new file mode 100644 index 0000000..5bd9206 --- /dev/null +++ b/archive/net.sourceforge.phpeclipse.wiki/src/net/sourceforge/phpeclipse/wiki/xml/PageConverter.java @@ -0,0 +1,101 @@ +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 diff --git a/archive/net.sourceforge.phpeclipse.wiki/src/net/sourceforge/phpeclipse/wiki/xml/Revision.java b/archive/net.sourceforge.phpeclipse.wiki/src/net/sourceforge/phpeclipse/wiki/xml/Revision.java new file mode 100644 index 0000000..75e73df --- /dev/null +++ b/archive/net.sourceforge.phpeclipse.wiki/src/net/sourceforge/phpeclipse/wiki/xml/Revision.java @@ -0,0 +1,80 @@ +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 diff --git a/archive/net.sourceforge.phpeclipse.wiki/src/net/sourceforge/phpeclipse/wiki/xml/XStreamManager.java b/archive/net.sourceforge.phpeclipse.wiki/src/net/sourceforge/phpeclipse/wiki/xml/XStreamManager.java new file mode 100644 index 0000000..07ad09a --- /dev/null +++ b/archive/net.sourceforge.phpeclipse.wiki/src/net/sourceforge/phpeclipse/wiki/xml/XStreamManager.java @@ -0,0 +1,51 @@ +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