PageRenderTime 34ms CodeModel.GetById 10ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://bitbucket.org/dodok1/uwc
Java | 130 lines | 81 code | 11 blank | 38 comment | 9 complexity | 11acedfbe208125738468ffc838aee47 MD5 | raw file
  1. package com.atlassian.uwc.converters.twiki;
  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 twiki webs to confluence spacekeys using
  13. * config options in the converter properties file.
  14. */
  15. public class SpaceConverter extends BaseConverter {
  16. Logger log = Logger.getLogger(this.getClass());
  17. public void convert(Page page) {
  18. log.debug("Converting Spacenames - start");
  19. String input = page.getOriginalText();
  20. String converted = convertSpaces(input);
  21. page.setConvertedText(converted);
  22. log.info("Converting Spacenames - complete");
  23. }
  24. /**
  25. * transforms all uses of webs to spacekeys in the page text
  26. * @param input page text
  27. * @return page text with confluence spacekeys instead of
  28. * twiki webs
  29. */
  30. protected String convertSpaces(String input) {
  31. HashMap spaces = getSpaceProperties();
  32. if (spaces == null || spaces.isEmpty()) return input;
  33. String converted = convertSpaceInLinks(input, spaces);
  34. converted = convertSpaceInImages(converted, spaces);
  35. return converted;
  36. }
  37. Pattern linksWithSpace = Pattern.compile("" +
  38. "\\[" +
  39. "([^:\\]]+)");
  40. /**
  41. * transforms webs in link syntax to confluence spacekeys
  42. * @param input page text
  43. * @param spaces mapping of webs to spacekeys
  44. * @return
  45. */
  46. protected String convertSpaceInLinks(String input, HashMap spaces) {
  47. Matcher spaceFinder = linksWithSpace.matcher(input);
  48. return convertSpaces(input, spaces, spaceFinder, "[");
  49. }
  50. /**
  51. * transforms webs in a given syntax (represented by the regex Matcher) to
  52. * confluence spacekeys
  53. * @param input page text
  54. * @param spaces web to spacekey mapping
  55. * @param finder represents the regex syntax to identify the web. The associated pattern
  56. * must have at least one group which captures the twiki web. It must not capture
  57. * anything after the web.
  58. * @param The replacement text for what comes before group 1 in the regex.
  59. * @return transformed text
  60. */
  61. private String convertSpaces(String input, HashMap spaces, Matcher finder, String delim) {
  62. StringBuffer sb = new StringBuffer();
  63. boolean found = false;
  64. while (finder.find()) {
  65. found = true;
  66. String rawSpace = finder.group(1);
  67. log.debug("rawSpace = " + rawSpace);
  68. String space = rawSpace;
  69. String alias = "";
  70. if (rawSpace.contains("|")) {
  71. String[] parts = rawSpace.split("\\|");
  72. alias = parts[0] + "|";
  73. space = parts[1];
  74. }
  75. if (!spaces.containsKey(space)) continue;
  76. String newspace = (String) spaces.get(space);
  77. log.debug("newspace = " + newspace);
  78. String replacement = delim + alias + newspace;
  79. replacement = RegexUtil.handleEscapesInReplacement(replacement);
  80. finder.appendReplacement(sb, replacement);
  81. }
  82. if (found) {
  83. finder.appendTail(sb);
  84. return sb.toString();
  85. }
  86. return input;
  87. }
  88. Pattern imagesWithSpace = Pattern.compile("" +
  89. "[!]" +
  90. "([^:!\\]]+)");
  91. /**
  92. * transforms webs in image syntax to confluence spacekeys
  93. * @param input page text
  94. * @param spaces mapping of webs to spacekeys
  95. * @return
  96. */
  97. protected String convertSpaceInImages(String input, HashMap spaces) {
  98. Matcher spaceFinder = imagesWithSpace.matcher(input);
  99. return convertSpaces(input, spaces, spaceFinder, "!");
  100. }
  101. /**
  102. * gets the web to spacekey misc properties and creates a map of them
  103. * @return map of web to spacekey maps. keys are twiki webs.
  104. * values are confluence spacekeys.
  105. */
  106. protected HashMap getSpaceProperties() {
  107. Properties props = getProperties();
  108. HashMap spaces = new HashMap<String, String>();
  109. for (Iterator iter = props.keySet().iterator(); iter.hasNext();) {
  110. String key = (String) iter.next();
  111. if (key.startsWith("space-")) {
  112. String newkey = key.replaceFirst("^space-", "");
  113. String value = props.getProperty(key);
  114. spaces.put(newkey, value);
  115. }
  116. }
  117. return spaces;
  118. }
  119. }