PageRenderTime 50ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/src/com/atlassian/uwc/ui/Page.java

https://bitbucket.org/dodok1/uwc
Java | 436 lines | 231 code | 61 blank | 144 comment | 24 complexity | 8bc8e099301f3ea3f4f7ae2ee7478a48 MD5 | raw file
  1. package com.atlassian.uwc.ui;
  2. import java.io.File;
  3. import java.util.Date;
  4. import java.util.HashMap;
  5. import java.util.HashSet;
  6. import java.util.Set;
  7. import java.util.Vector;
  8. /**
  9. * A class representing a wiki page that is being converted to Confluence.
  10. * This holds the meta data needed to convert a Page.
  11. *
  12. * User: Rex (Rolf Staflin)
  13. * Date: 2006-94-05
  14. * Time: 15:40:25
  15. */
  16. public class Page implements Comparable {
  17. /**
  18. * File the page represents
  19. */
  20. private File file;
  21. /**
  22. * text that the page has before each conversion
  23. */
  24. private String originalText;
  25. /**
  26. * original source text. This should never be changed once
  27. * it is set. Note: This is different from originalText in that
  28. * originalText will be updated to reflect the most current
  29. * text at the beginning of each syntax conversion.
  30. */
  31. private String unchangedSource;
  32. /**
  33. * text that the page has after the conversion
  34. */
  35. private String convertedText;
  36. /**
  37. * page name. Will be used by Confluence when the page is created in Confluence.
  38. */
  39. private String name;
  40. /**
  41. * filepath to the page
  42. */
  43. private String path;
  44. /**
  45. * set of attachments associated with this page
  46. */
  47. private Set<Attachment> attachments;
  48. /**
  49. * page version, used by the UWC Page History Framework.
  50. * @see http://confluence.atlassian.com/display/CONFEXT/UWC+Page+History+Framework
  51. */
  52. private int version = 1; //the default is 1
  53. /**
  54. * set of labels associated with this page
  55. */
  56. private Set<String> labels;
  57. /**
  58. * list of page comments associated with this page
  59. * NOTE: At this time, we're only passing in the comment contents,
  60. * as all other comment parameters either (a) can't be set with
  61. * the remote api or (b) will not need to be assocated seperately (page id)
  62. */
  63. private Vector<Comment> comments; //It's a vector instead of a set to preserver order
  64. /**
  65. * username of author who updated this page. If null, the administrator running the UWC will be used
  66. */
  67. private String author;
  68. /**
  69. * timestamp when the author updated this page.
  70. */
  71. private Date timestamp;
  72. /**
  73. * map providing information about which version is the latest for a given title.
  74. * There should only be data in this object if pages have been sorted by history in the ConverterEngine
  75. */
  76. private static HashMap<String, Integer> latestVersions = new HashMap<String, Integer>();
  77. /**
  78. * Optionally associate the page with a spacekey. The ConverterEngine will use this instead of the spacekey setting.
  79. */
  80. private String spacekey;
  81. /**
  82. * keep needed space data here so the ConverterEngine can create the space, if necessary
  83. * The key is the spacekey. The value is an array with name and description data in the 0th and 1st indexes,
  84. * respectively. The name and description data will only be used if the spacekey is not already in use, and
  85. * therefore a space needs to be created.
  86. */
  87. private static HashMap<String,String[]> spaces = new HashMap<String, String[]>(); //spacekey -> [name, description]
  88. private boolean isBlog = false;
  89. private boolean isPersonalSpace = false;
  90. private String personalSpaceUsername = null;
  91. /**
  92. * Basic constructor. Creates a page with an empty path.
  93. * @param file The file to be converted.
  94. */
  95. public Page(File file) {
  96. init(file, "");
  97. }
  98. /**
  99. * Basic constructor.
  100. * @param file The file to be converted.
  101. * @param path A path used to construct a page hierarchy for
  102. * some wikis.
  103. */
  104. public Page(File file, String path) {
  105. init(file, path);
  106. }
  107. /**
  108. * initializes the page
  109. * @param file
  110. * @param path
  111. */
  112. private void init(File file, String path) {
  113. this.file = file;
  114. attachments = new HashSet<Attachment>();
  115. labels = new HashSet<String>();
  116. comments = new Vector<Comment>();
  117. setPath(path);
  118. }
  119. /**
  120. * used when sorting pages.
  121. * Takes into account page name and page version
  122. * @see java.lang.Comparable#compareTo(java.lang.Object)
  123. */
  124. public int compareTo(Object o) {
  125. Page b = (Page) o;
  126. int versionA = this.version;
  127. int versionB = b.getVersion();
  128. String nameA = this.name;
  129. String nameB = b.getName();
  130. //make sure there's a default value so we don't get NPE
  131. if (nameA == null) nameA = "";
  132. if (nameB == null) nameB = "";
  133. //order by name - if name is the same, in order by version
  134. int compareValue = (nameA.compareTo(nameB));
  135. if (compareValue == 0)
  136. compareValue = versionA - versionB;
  137. return compareValue;
  138. }
  139. /**
  140. * Adds a single attachment to the page. If the attachment was already
  141. * added (e.g., an image that appears several times on the page) it is not added again.
  142. * @param newAttachment The file to be attached to the page. It must not be null.
  143. */
  144. public void addAttachment(File newAttachment) {
  145. assert newAttachment != null;
  146. attachments.add(new Attachment(newAttachment));
  147. }
  148. public void addAttachment(File attachment, String name) {
  149. attachments.add(new Attachment(attachment, name));
  150. }
  151. public void addAttachment(Attachment attachment) {
  152. attachments.add(attachment);
  153. }
  154. /* Getters and Setters */
  155. public File getFile() {
  156. return file;
  157. }
  158. public String getOriginalText() {
  159. return originalText;
  160. }
  161. public void setOriginalText(String originalText) {
  162. this.originalText = originalText;
  163. }
  164. public String getUnchangedSource() {
  165. return unchangedSource;
  166. }
  167. /**
  168. * sets the unchangedSource field. Cannot be set more than once.
  169. * @throws IllegalStateException if the source has already been set.
  170. */
  171. public void setUnchangedSource(String unchangedSource) {
  172. if (this.unchangedSource != null)
  173. throw new IllegalStateException("Source Text has already been set. It may not be changed further.");
  174. this.unchangedSource = unchangedSource;
  175. }
  176. public String getName() {
  177. return name;
  178. }
  179. public void setName(String name) {
  180. this.name = name;
  181. }
  182. /**
  183. * Returns the relative path of the page. This path is constructed by ConverterEngine.recurse().
  184. * Top-level (root) pages have an empty path, while other pages have paths with the elements
  185. * separated by <code>File.separator</code>, e.g. "/path/to/the/page.txt" on unix systems and
  186. * "\path\to\the\page.txt" on Windows systems.
  187. *
  188. * @return The path of the page.
  189. */
  190. public String getPath() {
  191. return path;
  192. }
  193. public void setPath(String path) {
  194. this.path = path;
  195. }
  196. public String getConvertedText() {
  197. return convertedText;
  198. }
  199. public void setConvertedText(String convertedText) {
  200. this.convertedText = convertedText;
  201. }
  202. public Set<File> getAttachments() {
  203. HashSet<File> justFiles = new HashSet<File>();
  204. for (Attachment att : this.attachments) {
  205. justFiles.add(att.getFile());
  206. }
  207. return justFiles;
  208. }
  209. public void setAttachments(Set<File> attachments) {
  210. assert attachments != null;
  211. this.attachments.clear();
  212. for (File file : attachments) {
  213. this.attachments.add(new Attachment(file));
  214. }
  215. }
  216. public Set<Attachment> getAllAttachmentData() {
  217. return this.attachments;
  218. }
  219. public void setVersion(int version) {
  220. //save latest version data
  221. if (getLatestVersion(getName()) <= version) {
  222. latestVersions.put(getName(), version);
  223. }
  224. this.version = version;
  225. }
  226. public int getVersion() {
  227. return this.version;
  228. }
  229. /**
  230. * @return map of title -> latest version data.
  231. */
  232. public static HashMap<String, Integer> getLatestVersions() {
  233. return latestVersions;
  234. }
  235. /**
  236. * Latest version data is noted when setVersion is called based on the name the object has
  237. * when setVersion is called.
  238. * @param name page name this version is associated with
  239. * @return latest version for title or 1 if no version set yet
  240. */
  241. public static int getLatestVersion(String name) {
  242. Integer latest = getLatestVersions().get(name);
  243. if (latest == null) return 1;
  244. return latest;
  245. }
  246. public Set<String> getLabels() {
  247. return labels;
  248. }
  249. public void setLabels(Set<String> labels) {
  250. this.labels = labels;
  251. }
  252. /**
  253. * @return set of labels as a comma delimited list. null if
  254. * labels is null or empty
  255. */
  256. public String getLabelsAsString() {
  257. if (this.labels == null) return null;
  258. if (this.labels.isEmpty()) return null;
  259. boolean first = true;
  260. String labelString = "";
  261. for (String label : this.labels) {
  262. if (first) first = false;
  263. else labelString += ", ";
  264. labelString += label;
  265. }
  266. return labelString;
  267. }
  268. /**
  269. * adds the given label to the set of labels associated with this page,
  270. * removes disallowed confluence label chars,
  271. * and toLowerCases the label to: (a) avoid remote api errors and
  272. * (b) better represent what the results of uploading a given label to Confluence will be.
  273. * If you don't want the labels to be processed, use the setLabels method instead.
  274. * @param label
  275. */
  276. public void addLabel(String label) {
  277. label = label.trim();
  278. label = label.replaceAll("[ !#&()*,.:;<>?@\\[\\]\\^]", ""); //remove disallowed confluence tag chars
  279. label = label.toLowerCase();
  280. this.labels.add(label);
  281. }
  282. public Vector<String> getComments() {
  283. Vector<String> commentStrings = new Vector<String>();
  284. for (Comment comment : this.comments) {
  285. commentStrings.add(comment.text);
  286. }
  287. return commentStrings;
  288. }
  289. public Vector<Comment> getAllCommentData() {
  290. return this.comments;
  291. }
  292. public void setComments(Vector <String> comments) {
  293. for (String comment : comments) {
  294. this.comments.add(new Comment(comment));
  295. }
  296. }
  297. public void addComment(String comment) {
  298. this.comments.add(new Comment(comment));
  299. }
  300. public void addComment(Comment comment) {
  301. this.comments.add(comment);
  302. }
  303. public void addComment(String comment, String creator, String date) {
  304. this.comments.add(new Comment(comment, creator, date));
  305. }
  306. public boolean hasComments() {
  307. return !this.comments.isEmpty();
  308. }
  309. public String getAuthor() {
  310. return author;
  311. }
  312. public void setAuthor(String author) {
  313. this.author = author;
  314. }
  315. public Date getTimestamp() {
  316. return timestamp;
  317. }
  318. public void setTimestamp(Date timestamp) {
  319. this.timestamp = timestamp;
  320. }
  321. /* Space methods */
  322. public String getSpacekey() {
  323. return spacekey;
  324. }
  325. public void setSpacekey(String key) {
  326. this.spacekey = key;
  327. }
  328. /**
  329. * sets the spacekey for this page, and also assigns name and description data
  330. * to a map so that if the space needs to be created, we have that information.
  331. * @param key
  332. * @param name
  333. * @param desc
  334. */
  335. public void setSpace(String key, String name, String desc) {
  336. setSpacekey(key);
  337. String[] newdata = {name, desc};
  338. this.spaces.put(key, newdata);
  339. }
  340. /**
  341. * gets the space data that's been saved for a particular spacekey, or null if no data exists for that key
  342. * @param key
  343. * @return array. 0th index is the name of the space, 1st index is the description
  344. */
  345. public String[] getSpaceData(String key) {
  346. if (spaces.containsKey(key)) return spaces.get(key);
  347. return null;
  348. }
  349. /**
  350. * true if the spaces data map has an entry for the given key
  351. * @param key
  352. * @return
  353. */
  354. public boolean hasSpace(String key) {
  355. return spaces.containsKey(key);
  356. }
  357. public boolean isBlog() {
  358. return isBlog;
  359. }
  360. public void setIsBlog(boolean isBlog) {
  361. this.isBlog = isBlog;
  362. }
  363. public boolean isPersonalSpace() {
  364. return isPersonalSpace;
  365. }
  366. public void setIsPersonalSpace(boolean isPersonalSpace) {
  367. this.isPersonalSpace = isPersonalSpace;
  368. }
  369. public String getPersonalSpaceUsername() {
  370. return this.personalSpaceUsername;
  371. }
  372. public void setPersonalSpaceUsername(String username) {
  373. this.personalSpaceUsername = username;
  374. }
  375. }