PageRenderTime 49ms CodeModel.GetById 13ms RepoModel.GetById 1ms app.codeStats 0ms

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

https://bitbucket.org/atlassianlabs/universal-wiki-connector
Java | 404 lines | 207 code | 49 blank | 148 comment | 22 complexity | 2269d8c80fb7555cb57a49d89feacb37 MD5 | raw file
  1. package com.atlassian.uwc.ui;
  2. import java.io.File;
  3. import java.io.FilenameFilter;
  4. import java.io.IOException;
  5. import java.util.List;
  6. import java.util.StringTokenizer;
  7. import java.util.TreeMap;
  8. import java.util.Vector;
  9. import java.util.regex.Matcher;
  10. import java.util.regex.Pattern;
  11. import org.apache.log4j.Logger;
  12. import com.atlassian.uwc.converters.tikiwiki.RegexUtil;
  13. import com.atlassian.uwc.ui.UWCUserSettings.Setting;
  14. import com.atlassian.uwc.ui.listeners.FeedbackHandler.Feedback;
  15. import com.atlassian.uwc.util.PropertyFileManager;
  16. /**
  17. * This object contains the underlying data for the UWC. It's essentially a gateway to allow
  18. * different classes to communicate with each other. For example, it:
  19. * - provides access to the file system for
  20. * -- getting saved and saving user settings
  21. * -- converter properties files
  22. * -- info about existance of properties files
  23. * - maintains user chosen wiki pages information
  24. * - provides access to the converter engine, for converting, and also getting error codes and feedback
  25. * from the ConverterEngine
  26. * Note: heavily influenced by UWCForm2 and ChooseWikiForm code from the
  27. * UWC GUI v.2
  28. */
  29. public class UWCGuiModel {
  30. Logger log = Logger.getLogger(this.getClass());
  31. /**
  32. * list of wiki converters, gathered from converter.xxx.properties files
  33. */
  34. private Vector<String> converterListModel = null;
  35. /**
  36. * list of wiki exporters, gathered from exporter.xxx.properties files
  37. */
  38. private Vector<String> exportListModel = null;
  39. /**
  40. * list of user chosen pages
  41. */
  42. private Vector<String> pageNames = null;
  43. /**
  44. * engine that runs the conversions
  45. */
  46. private ConverterEngine engine = null;
  47. /**
  48. * user settings
  49. */
  50. private UWCUserSettings userSettings = null;
  51. /**
  52. * which setting the user is currently changing, and has not been saved yet
  53. */
  54. private UWCUserSettings.Setting unsaved = null;
  55. public UWCGuiModel() {
  56. userSettings = new UWCUserSettings();
  57. loadSavedPages(userSettings);
  58. }
  59. public UWCGuiModel(UWCUserSettings settings) {
  60. this.userSettings = settings;
  61. loadSavedPages(userSettings);
  62. }
  63. /**
  64. * gets the list of available wiki types that can be converted
  65. * @param parentDir directory in which the wiki properties files can be found
  66. * Example: "conf"
  67. * @return list of wiki types
  68. */
  69. public Vector<String> getWikiTypesList(String parentDir) {
  70. FilenameFilter filter = new UWCConverterPropFileFilter();
  71. return getFromFileList(parentDir, filter, this.converterListModel);
  72. }
  73. /**
  74. * gets the list of available exportable wiki types
  75. * @param parentDir directory in which export properties files can be found
  76. * Example: "conf"
  77. * @return list of export wiki types
  78. */
  79. public Vector<String> getExportTypes(String parentDir) {
  80. FilenameFilter filter = new UWCConverterExportFileFilter();
  81. return getFromFileList(parentDir, filter, this.exportListModel);
  82. }
  83. /**
  84. * creates or gets a list of wiki types that
  85. * exist in the given parentDir,
  86. * conform to the given filter,
  87. * and are maintained in the given model
  88. * @param parentDir directory in which the files representing the desired data exist
  89. * @param filter file filter which restricts which files contain the desired data
  90. * @param model object that maintains the desired data. If it is not null, the return value
  91. * will be this object
  92. * @return model or newly created list of wiki types
  93. */
  94. public Vector<String> getFromFileList(String parentDir, FilenameFilter filter, Vector<String> model) {
  95. //return existing object, if it exists
  96. if (model != null)
  97. return model;
  98. File confDir = new File(parentDir);
  99. //check for errors
  100. if (!confDir.exists()) {
  101. log.error("confDir " + confDir.getAbsolutePath() + " does not exist");
  102. return new Vector<String>(); //return empty list
  103. }
  104. if (!confDir.isDirectory()) {
  105. log.error("confDir " + confDir.getAbsolutePath() + " is not a directory!");
  106. return new Vector<String>();
  107. }
  108. //create the list
  109. File[] files = confDir.listFiles(filter);
  110. model = new Vector<String>();
  111. for (File file : files) {
  112. String fileName = file.getName();
  113. StringTokenizer st = new StringTokenizer(fileName, ".");
  114. st.nextToken();
  115. fileName = st.nextToken();
  116. model.add(fileName);
  117. }
  118. return model;
  119. }
  120. /**
  121. * @param files adds the given files to the pages state
  122. * @return current list of pages after additions
  123. */
  124. public Vector<String> addWikiPages(File[] files) {
  125. pageNames = getPageNames();
  126. for (File file : files) {
  127. pageNames.add(file.getPath());
  128. }
  129. return this.pageNames;
  130. }
  131. /**
  132. * @return the directory that the user chose pages from most recently
  133. */
  134. public String getPageChooserDir() {
  135. return this.userSettings.getPageChooserDir();
  136. }
  137. /**
  138. * removes the given pages from the internal list of files
  139. * @param files
  140. * @return resulting list of pagenames after removals
  141. */
  142. public Vector<String> removeWikiPages(Object[] files) {
  143. for (Object file : files) {
  144. String item = (String) file;
  145. pageNames.remove(item);
  146. }
  147. return pageNames;
  148. }
  149. /**
  150. * run the conversion
  151. * @param propsPath path to the converter properties file that will
  152. * be used to run the conversion
  153. * @throws IOException, {@link IllegalArgumentException} if there are problems reading the given converter
  154. * properties files
  155. */
  156. public void convert(String propsPath) throws IOException, IllegalArgumentException {
  157. ConverterEngine engine = getConverterEngine();
  158. List<File> pages = getPageFiles();
  159. List<String> converters = getConverters(propsPath);
  160. engine.convert(pages, converters, this.userSettings);
  161. }
  162. /**
  163. * cancels the currently being run conversion
  164. */
  165. public void cancelConvert() {
  166. ConverterEngine engine = getConverterEngine();
  167. engine.cancel();
  168. }
  169. /**
  170. * gets a list of converter strings from the file at the given propsPath
  171. * @param propsPath converter.xxx.properties file representing this wiki
  172. * @return list of converter strings
  173. * @throws IOException if can't load the properties from the given file at propsPath
  174. */
  175. protected List<String> getConverters(String propsPath) throws IOException {
  176. File props = new File(propsPath);
  177. if (!props.exists()) {
  178. String message = "No property file at that location: " + propsPath;
  179. log.error(message);
  180. throw new IllegalArgumentException(message);
  181. }
  182. TreeMap<String,String> converters = null;
  183. converters = PropertyFileManager.loadPropertiesFile(propsPath);
  184. if (converters == null)
  185. throw new IllegalArgumentException(); //unlikely, as the error handling above should be sufficient
  186. return getConvertersAsStrings(converters);
  187. }
  188. /**
  189. * gets a list of syntax converters from the given converters map
  190. * @param converters
  191. * @return list of syntax converters
  192. */
  193. protected List<String> getConvertersAsStrings(TreeMap<String, String> converters) {
  194. Vector<String> converterStrings = new Vector<String>(converters.keySet().size());
  195. for (String converter : converters.keySet()) {
  196. String value = converters.get(converter);
  197. String converterString = converter + "=" + value;
  198. converterStrings.add(converterString);
  199. }
  200. return converterStrings;
  201. }
  202. /**
  203. * @return creates a list of File objects from the saved pagenames state
  204. */
  205. protected List<File> getPageFiles() {
  206. Vector<File> files = new Vector<File>();
  207. this.pageNames = getPageNames();
  208. for (String path : this.pageNames) {
  209. File file = new File(path);
  210. files.add(file);
  211. }
  212. return files;
  213. }
  214. /**
  215. * @return gets the converter engine used to run conversions
  216. */
  217. private ConverterEngine getConverterEngine() {
  218. if (this.engine == null) {
  219. this.engine = new ConverterEngine();
  220. }
  221. return this.engine;
  222. }
  223. /**
  224. * @return the page names representing the pages the user has chosen to convert
  225. */
  226. public Vector<String> getPageNames() {
  227. if (this.pageNames == null)
  228. this.pageNames = new Vector<String>();
  229. return this.pageNames;
  230. }
  231. Pattern paths = Pattern.compile("" +
  232. "(.*?)" + //everything until
  233. "(?>::|$)"); //double colon or end of string
  234. /**
  235. * loads the saved pages setting data into the model's pagenames object
  236. * @param settings
  237. */
  238. private void loadSavedPages(UWCUserSettings settings) {
  239. String pagestring = settings.getSetting(Setting.PAGES);
  240. Matcher pathFinder = paths.matcher(pagestring);
  241. Vector<String> pagenames = getPageNames();
  242. while (pathFinder.find()) {
  243. String path = pathFinder.group(1);
  244. if ("".equals(path)) continue;
  245. pagenames.add(path);
  246. }
  247. }
  248. /**
  249. * file filter that accepts converter properties files
  250. */
  251. public class UWCConverterPropFileFilter implements FilenameFilter {
  252. private static final String PROPFILE_PREFIX = "converter";
  253. private static final String PROPFILE_SUFFIX = ".properties";
  254. public boolean accept(File dir, String name) {
  255. if (name.equalsIgnoreCase(PROPFILE_PREFIX + PROPFILE_SUFFIX)) return false;
  256. if (name.startsWith(PROPFILE_PREFIX) &&
  257. name.endsWith(PROPFILE_SUFFIX)) {
  258. return true;
  259. }
  260. return false;
  261. }
  262. }
  263. /**
  264. * file filter that accepts exporter properties files
  265. */
  266. public class UWCConverterExportFileFilter implements FilenameFilter {
  267. private static final String FILE_PREFIX = "exporter";
  268. private static final String FILE_SUFFIX = ".properties";
  269. public boolean accept(File dir, String name) {
  270. if (name.equalsIgnoreCase(FILE_PREFIX + FILE_SUFFIX)) return false;
  271. if (name.startsWith(FILE_PREFIX) &&
  272. name.endsWith(FILE_SUFFIX)) {
  273. return true;
  274. }
  275. return false;
  276. }
  277. }
  278. /**
  279. * @return directory where the converted files will be saved before upload
  280. */
  281. public static String getOutputDir() {
  282. return "output" + File.separator + "output";
  283. }
  284. /**
  285. * sets the given setting with the given value. And
  286. * saves all the current settings to the settings file
  287. * @param setting
  288. * @param value
  289. * @return feedback on the process of saving the file
  290. */
  291. public Feedback saveSetting(Setting setting, String value) {
  292. this.userSettings.setOneSetting(setting, value);
  293. this.userSettings.saveSettingsToFile();
  294. return this.userSettings.feedback;
  295. }
  296. /**
  297. * saves all settings to the file system
  298. */
  299. public void saveAllSettings() {
  300. log.debug("Saving All Settings");
  301. this.userSettings.saveSettingsToFile();
  302. }
  303. /**
  304. * @param setting
  305. * @return gets the value for the given setting
  306. */
  307. public String getSetting(Setting setting) {
  308. return this.userSettings.getSetting(setting);
  309. }
  310. /**
  311. * registers the given feedback window by:
  312. * * getting the state from the engine
  313. * * assigning that state to the feedback window
  314. * @param feedbackWindow
  315. */
  316. public void registerFeedbackWindow(FeedbackWindow feedbackWindow) {
  317. ConverterEngine engine = getConverterEngine();
  318. State state = engine.getState(this.userSettings);
  319. feedbackWindow.setState(state);
  320. }
  321. /**
  322. * @return gets the feedback from the converter engine
  323. */
  324. public Feedback getConverterFeedback() {
  325. ConverterEngine engine = getConverterEngine();
  326. return engine.getConverterFeedback();
  327. }
  328. /**
  329. * @return gets and clears error messages
  330. */
  331. public ConverterErrors getErrors() {
  332. ConverterEngine engine = getConverterEngine();
  333. return engine.getErrors();
  334. }
  335. /**
  336. * @return true if the conversion generated errors
  337. */
  338. public boolean getHadConverterErrors() {
  339. ConverterEngine engine = getConverterEngine();
  340. return engine.hadConverterErrors();
  341. }
  342. /**
  343. * @return the setting that is currently being updated by the user
  344. */
  345. public Setting getUnsaved() {
  346. return this.unsaved;
  347. }
  348. /**
  349. * setter
  350. * @param setting
  351. */
  352. public void setUnsaved(Setting setting) {
  353. this.unsaved = setting;
  354. }
  355. }