PageRenderTime 43ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

/src/com/atlassian/uwc/converters/socialtext/SpaceConverter.java

https://bitbucket.org/dodok1/uwc
Java | 164 lines | 95 code | 13 blank | 56 comment | 9 complexity | a112b26af804007cc63b94519dda6c33 MD5 | raw file
  1. package com.atlassian.uwc.converters.socialtext;
  2. import java.util.HashMap;
  3. import java.util.Iterator;
  4. import java.util.Properties;
  5. import java.util.regex.Matcher;
  6. import java.util.regex.Pattern;
  7. import org.apache.log4j.Logger;
  8. import com.atlassian.uwc.converters.BaseConverter;
  9. import com.atlassian.uwc.converters.tikiwiki.RegexUtil;
  10. import com.atlassian.uwc.ui.Page;
  11. /**
  12. * Transforms socialtext workspaces to confluence spacekeys using
  13. * config options in the converter properties file.
  14. *
  15. * There are a number of socialtext syntaxes that allow the user to specify
  16. * the workspace (links, images, recent changes macro, include macro).
  17. * Since socialtext workspaces are not necessarily going to be the same as your
  18. * corresponding confluence spacekey, you can specify a workspace to spacekey
  19. * map in the properties file for this class to use.
  20. */
  21. public class SpaceConverter extends BaseConverter {
  22. Logger log = Logger.getLogger(this.getClass());
  23. public void convert(Page page) {
  24. log.debug("Converting Spacenames - start");
  25. String input = page.getOriginalText();
  26. String converted = convertSpaces(input);
  27. page.setConvertedText(converted);
  28. log.info("Converting Spacenames - complete");
  29. }
  30. /**
  31. * transforms all uses of workspaces to spacekeys in the page text
  32. * @param input page text
  33. * @return page text with confluence spacekeys instead of
  34. * socialtext workspaces
  35. */
  36. protected String convertSpaces(String input) {
  37. HashMap spaces = getSpaceProperties();
  38. if (spaces == null || spaces.isEmpty()) return input;
  39. String converted = convertSpaceInLinks(input, spaces);
  40. converted = convertSpaceInImages(converted, spaces);
  41. converted = convertSpaceInRecentMacro(converted, spaces);
  42. converted = convertSpaceInIncludeMacro(converted, spaces);
  43. return converted;
  44. }
  45. Pattern linksWithSpace = Pattern.compile("" +
  46. "\\[" +
  47. "([^:\\]]+)");
  48. /**
  49. * transforms workspaces in link syntax to confluence spacekeys
  50. * @param input page text
  51. * @param spaces mapping of workspaces to spacekeys
  52. * @return
  53. */
  54. protected String convertSpaceInLinks(String input, HashMap spaces) {
  55. Matcher spaceFinder = linksWithSpace.matcher(input);
  56. return convertSpaces(input, spaces, spaceFinder, "[");
  57. }
  58. /**
  59. * transforms workspaces in a given syntax (represented by the regex Matcher) to
  60. * confluence spacekeys
  61. * @param input page text
  62. * @param spaces workspace to spacekey mapping
  63. * @param finder represents the regex syntax to identify the workspace. The associated pattern
  64. * must have at least one group which captures the socialtext workspace. It must not capture
  65. * anything after the workspace.
  66. * @param The replacement text for what comes before group 1 in the regex.
  67. * @return transformed text
  68. */
  69. private String convertSpaces(String input, HashMap spaces, Matcher finder, String delim) {
  70. StringBuffer sb = new StringBuffer();
  71. boolean found = false;
  72. while (finder.find()) {
  73. found = true;
  74. String rawSpace = finder.group(1);
  75. log.debug("rawSpace = " + rawSpace);
  76. String space = rawSpace;
  77. String alias = "";
  78. if (rawSpace.contains("|")) {
  79. String[] parts = rawSpace.split("\\|");
  80. alias = parts[0] + "|";
  81. space = parts[1];
  82. }
  83. if (!spaces.containsKey(space)) continue;
  84. String newspace = (String) spaces.get(space);
  85. log.debug("newspace = " + newspace);
  86. String replacement = delim + alias + newspace;
  87. replacement = RegexUtil.handleEscapesInReplacement(replacement);
  88. finder.appendReplacement(sb, replacement);
  89. }
  90. if (found) {
  91. finder.appendTail(sb);
  92. return sb.toString();
  93. }
  94. return input;
  95. }
  96. Pattern imagesWithSpace = Pattern.compile("" +
  97. "[!]" +
  98. "([^:!\\]]+)");
  99. /**
  100. * transforms workspaces in image syntax to confluence spacekeys
  101. * @param input page text
  102. * @param spaces mapping of workspaces to spacekeys
  103. * @return
  104. */
  105. protected String convertSpaceInImages(String input, HashMap spaces) {
  106. Matcher spaceFinder = imagesWithSpace.matcher(input);
  107. return convertSpaces(input, spaces, spaceFinder, "!");
  108. }
  109. Pattern recentWithSpace = Pattern.compile("" +
  110. "\\{recent_changes:\\s*([^}]+)");
  111. /**
  112. * transforms workspaces in recent changes macro syntax to confluence spacekeys
  113. * @param input page text
  114. * @param spaces mapping of workspaces to spacekeys
  115. * @return
  116. */
  117. protected String convertSpaceInRecentMacro(String input, HashMap spaces) {
  118. Matcher recentFinder = recentWithSpace.matcher(input);
  119. return convertSpaces(input, spaces, recentFinder, "{recent_changes: ");
  120. }
  121. Pattern includeWithSpace = Pattern.compile("" +
  122. "\\{include:\\s*([^}\\[\\s]+)");
  123. /**
  124. * transforms workspaces in include macro syntax to confluence spacekeys
  125. * @param input page text
  126. * @param spaces mapping of workspaces to spacekeys
  127. * @return
  128. */
  129. protected String convertSpaceInIncludeMacro(String input, HashMap spaces) {
  130. Matcher includeFinder = includeWithSpace.matcher(input);
  131. return convertSpaces(input, spaces, includeFinder, "{include: ");
  132. }
  133. /**
  134. * gets the workspace to spacekey misc properties and creates a map of them
  135. * @return map of workspace to spacekey maps. keys are socialtext workspaces.
  136. * values are confluence spacekeys.
  137. */
  138. protected HashMap getSpaceProperties() {
  139. Properties props = getProperties();
  140. HashMap spaces = new HashMap<String, String>();
  141. for (Iterator iter = props.keySet().iterator(); iter.hasNext();) {
  142. String key = (String) iter.next();
  143. if (key.startsWith("space-")) {
  144. String newkey = key.replaceFirst("^space-", "");
  145. String value = props.getProperty(key);
  146. spaces.put(newkey, value);
  147. }
  148. }
  149. return spaces;
  150. }
  151. }