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 { /** * <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); } 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); } } } }