''Open Wiki link'' and ''Create Files for Wiki link'' every new created file creates...
[phpeclipse.git] / archive / net.sourceforge.phpeclipse.wiki / src / net / sourceforge / phpeclipse / wiki / xml / Page.java
1 package net.sourceforge.phpeclipse.wiki.xml;
2
3 import java.io.ByteArrayInputStream;
4 import java.util.ArrayList;
5
6 import net.sourceforge.phpeclipse.wiki.editor.WikiEditorPlugin;
7 import net.sourceforge.phpeclipse.wiki.preferences.Util;
8
9 import org.eclipse.core.resources.IContainer;
10 import org.eclipse.core.resources.IFile;
11 import org.eclipse.core.resources.IFolder;
12 import org.eclipse.core.resources.ResourcesPlugin;
13 import org.eclipse.core.runtime.CoreException;
14 import org.eclipse.core.runtime.IPath;
15 import org.eclipse.core.runtime.Path;
16
17 //
18
19 public class Page {
20   /**
21    * &lt;page&gt; XML data from Wikipedia Special:Export pages may be <code>null</code>
22    *  
23    */
24
25   /* package private */String title = null;
26
27   /* package private */ArrayList listOfRevisions = null;
28
29   //  Revision revision = null;
30
31   /* package private */Page() {
32   }
33
34   public Page(String timeStamp, String title, String body) {
35     listOfRevisions = new ArrayList();
36     Revision revision = new Revision(timeStamp, body);
37     listOfRevisions.add(revision);
38     this.title = title;
39   }
40
41   public void add(Revision revision) {
42     listOfRevisions.add(revision);
43   }
44
45   /*
46    * (non-Javadoc)
47    * 
48    * @see java.lang.Object#toString()
49    */
50   public String toString() {
51     StringBuffer buffer = new StringBuffer();
52
53     if (title != null) {
54       buffer.append("==>Title: ");
55       buffer.append(title);
56       buffer.append("\n");
57     }
58
59     //    if (revision != null) {
60     //      buffer.append("==>Revision:\n");
61     //      buffer.append(revision);
62     //      buffer.append("\n");
63     //    }
64     for (int i = 0; i < listOfRevisions.size(); i++) {
65       Revision revision = (Revision) listOfRevisions.get(i);
66       if (revision != null) {
67         buffer.append("==>Revision:\n");
68         buffer.append(revision);
69         buffer.append("\n");
70       }
71     }
72     return buffer.toString();
73   }
74
75   /**
76    * @return Returns the title.
77    */
78   public String getTitle() {
79     return title;
80   }
81
82   public String getURLTitle() {
83     return title.replaceAll(" ", "_");
84   }
85
86   /**
87    * @return
88    */
89   public boolean isEmpty() {
90     return listOfRevisions.isEmpty();
91   }
92
93   /**
94    * @return
95    */
96   public int size() {
97     return listOfRevisions.size();
98   }
99
100   /**
101    * @param index
102    * @return
103    */
104   public Revision get(int index) {
105     return (Revision) listOfRevisions.get(index);
106   }
107   
108   public void createXMLFile(IFile file, boolean modify) throws CoreException {
109     String srcBasePath = Util.getWikiTextsPath(file);
110     String binBasePath = Util.getProjectsWikiOutputPath(file.getProject(), WikiEditorPlugin.HTML_OUTPUT_PATH);
111
112     String filename = Util.getXMLFileName(file, binBasePath, srcBasePath);
113     IPath path = new Path(filename);
114     IFile xmlFile = ResourcesPlugin.getWorkspace().getRoot().getFileForLocation(path);
115     IContainer parent = xmlFile.getParent();
116     if (parent instanceof IFolder && (!((IFolder) parent).exists())) {
117       Util.createFolder((IFolder) parent, null);
118     }
119 //    Page page = new Page("", wikiTitle, "");
120     byte[] buffer = XStreamManager.toXML(this).getBytes();
121     ByteArrayInputStream source = new ByteArrayInputStream(buffer);
122     if (!xmlFile.exists()) {
123       // only if file doesn't exists
124       xmlFile.create(source, true, null);
125     } else {
126       if (modify) {
127         xmlFile.setContents(source, true, true, null);
128       }
129     }
130   }
131 }