PageRenderTime 22ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/src/com/atlassian/uwc/converters/vqwiki/AttachmentConverter.java

https://bitbucket.org/atlassianlabs/universal-wiki-connector
Java | 202 lines | 124 code | 27 blank | 51 comment | 17 complexity | bb65e9d918422bbe73414722979574e5 MD5 | raw file
  1. package com.atlassian.uwc.converters.vqwiki;
  2. import java.io.File;
  3. import java.util.HashSet;
  4. import java.util.Set;
  5. import java.util.TreeSet;
  6. import java.util.Vector;
  7. import java.util.regex.Matcher;
  8. import java.util.regex.Pattern;
  9. import org.apache.log4j.Logger;
  10. import com.atlassian.uwc.converters.BaseConverter;
  11. import com.atlassian.uwc.ui.ConfluenceSettingsForm;
  12. import com.atlassian.uwc.ui.Page;
  13. //deprecated : import com.atlassian.uwc.ui.UWCForm2;
  14. /**
  15. * New 1.
  16. * @author P.Hunter
  17. *
  18. */
  19. public class AttachmentConverter extends BaseConverter {
  20. Logger log = Logger.getLogger(this.getClass());
  21. ConfluenceSettingsForm confSettings = null;
  22. //CONSTANTS - regex patterns
  23. protected static final String DOTALL = "(?s)"; // flag to enable dotall mode ("." includes newline in search)
  24. // - need to work out how to capture attachments in tables::
  25. // private static Pattern attachPattern = Pattern.compile("attach:([^#|]*)[#]*[\n]");
  26. //private static Pattern attachPattern = Pattern.compile("attach:(.*)[\n]");
  27. private static Pattern attachPattern = Pattern.compile("attach:(\\w.*\\w)");
  28. private static final String FILE_SEP = System.getProperty("file.separator");
  29. public void convert(Page vqwikiPage) {
  30. log.info("Converting VQwiki Attachments -- starting");
  31. String pageAsTextString = vqwikiPage.getOriginalText(); // input (original) page text
  32. String convertedPage = ""; // write page to this output string, after successful conversion
  33. Matcher vqAttachFinder = attachPattern.matcher( pageAsTextString );
  34. StringBuffer sb = new StringBuffer();
  35. String attachmentDir = this.getAttachmentDirectory();
  36. //Set<File> pageAttachments = new HashSet<File>();
  37. // scan the page and create a list of attachments
  38. // addAttachmentsToPage(vqwikiPage, attachmentDir);
  39. log.info("Converting VQwiki Attachments -- about to loop...");
  40. // for each attachment encountered ...
  41. while (vqAttachFinder.find()) {
  42. String attachmentName = vqAttachFinder.group(1);
  43. log.info("** -> attachment:" + attachmentName );
  44. // write some code to find the attachments for this page as Files
  45. File attachmentFile = new File(attachmentDir + FILE_SEP + attachmentName);
  46. // add the attachments to the page, this queues the files to be attached to the Confluence page upon conversion
  47. vqAttachFinder.appendReplacement(sb, "[^" + attachmentName + "]");
  48. //TODO
  49. vqwikiPage.addAttachment(attachmentFile);
  50. log.info("** -> attached:" + attachmentName );
  51. }
  52. // terminate converted text string and pass back to calling program
  53. vqAttachFinder.appendTail(sb);
  54. convertedPage = sb.toString();
  55. vqwikiPage.setConvertedText(convertedPage);
  56. log.info("Converting VQwiki Attachments -- complete");
  57. }
  58. /**
  59. * determines which attachments are sought by the page, and attaches them
  60. * @param page object to attach pages to
  61. */
  62. protected void addAttachmentsToPage(Page page, String attachmentDir) {
  63. //what attachments are we looking for?
  64. log.debug("Finding Attachments...");
  65. Vector soughtAttachments = getSoughtAttachmentNames(page);
  66. if (soughtAttachments == null || soughtAttachments.size() == 0)
  67. return;
  68. //get filelist from the attachment directory
  69. log.debug("Examining File Directory");
  70. File attachmentPageDir = new File(attachmentDir);
  71. File files[] = attachmentPageDir.listFiles();
  72. if (files==null) {
  73. log.info("no attachment files found in directory: "+attachmentDir);
  74. return;
  75. }
  76. // add sought after attachments. This is a recursive method.
  77. log.debug("Attaching files to page");
  78. addAttachments(page, files, soughtAttachments);
  79. }
  80. /**
  81. * recursively look through the images directory for the desired files
  82. * and attach them to the given page
  83. * @param page object files will be attached to
  84. * @param files Array of a directory's files
  85. * @param soughtFilenames filenames we are looking to attach
  86. */
  87. private void addAttachments(Page page, File[] files, Vector soughtFilenames) {
  88. for (File file : files) {
  89. //check for existence
  90. if (!file.exists()) continue;
  91. //check for recursion
  92. if (file.isDirectory()) {
  93. File moreFiles[] = file.listFiles();
  94. if (moreFiles == null)
  95. continue;
  96. addAttachments(page, moreFiles, soughtFilenames);
  97. continue;
  98. }
  99. //existing non-Directory file?
  100. String filename = file.getName();
  101. //is it a file we want to attach?
  102. if (foundFile(soughtFilenames, filename)) {
  103. //attach the file
  104. log.debug("adding attachment: " + file.getName());
  105. page.addAttachment(file);
  106. }
  107. }
  108. }
  109. /**
  110. * checks if the given filename can be found in the given vector.
  111. * The search is case insensitive.
  112. * @param soughtFilenames
  113. * @param filename
  114. * @return true if filename is found to be in soughtFilenames
  115. */
  116. protected boolean foundFile(Vector<String> soughtFilenames, String filename) {
  117. boolean found = soughtFilenames.contains(filename);
  118. if (found) return found;
  119. // check for case insensitivity
  120. Pattern caseInsensitiveFilename = Pattern.compile(filename, Pattern.CASE_INSENSITIVE);
  121. for (String soughtFile : soughtFilenames) {
  122. Matcher fileFinder = caseInsensitiveFilename.matcher(soughtFile);
  123. if (fileFinder.matches()) return true;
  124. }
  125. return false;
  126. }
  127. /**
  128. * Determine which images the page is going to need.
  129. * Mediawiki images are not associated with a particular page.
  130. * @param page the given page
  131. * @return String Vector of image names
  132. */
  133. protected Vector<String> getSoughtAttachmentNames(Page page) {
  134. Vector<String> names = new Vector<String>();
  135. Set<String> nameSet = new TreeSet<String>();
  136. String pageText = page.getOriginalText();
  137. nameSet = getNamesFromImageSyntax(nameSet, pageText);
  138. nameSet = getNamesFromLinkSyntax(nameSet, pageText);
  139. names.addAll(nameSet);
  140. log.debug("found attachment names: " +names.toString());
  141. return names;
  142. }
  143. protected Set<String> getNamesFromImageSyntax(Set<String> nameSet, String pageText) {
  144. Pattern image = Pattern.compile("!([^!|]+)(?:\\|[^!]+)?!");
  145. Matcher imageFinder = image.matcher(pageText);
  146. while (imageFinder.find()) {
  147. String name = imageFinder.group(1);
  148. nameSet.add(name);
  149. }
  150. return nameSet;
  151. }
  152. String linkSyntax =
  153. "\\[" + //opening left bracket
  154. "(" + //start group
  155. "?:" + //but don't capture it
  156. "[^^]" + //not a carat
  157. "[^|]+" + //anything but a pipe until
  158. "\\|" + //a pipe
  159. ")" + //end group
  160. "?" + //and make that group optional
  161. "\\^" + //a carat as a part of the string
  162. "(" + //start capturing (group1)
  163. "[^\\]]+" + //anything but a right bracket until
  164. ")" + //end capturing (group1)
  165. "\\]"; //right bracket
  166. protected Set<String> getNamesFromLinkSyntax(Set<String> nameSet, String pageText) {
  167. Pattern image = Pattern.compile(linkSyntax);
  168. Matcher imageFinder = image.matcher(pageText);
  169. while (imageFinder.find()) {
  170. String name = imageFinder.group(1);
  171. nameSet.add(name);
  172. }
  173. return nameSet;
  174. }
  175. }