PageRenderTime 41ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/src/com/atlassian/uwc/converters/dokuwiki/HierarchyLinkConverter.java

https://bitbucket.org/atlassianlabs/universal-wiki-connector
Java | 213 lines | 206 code | 7 blank | 0 comment | 6 complexity | c06ffaf510c2a1428161371d64bc65fc MD5 | raw file
  1. package com.atlassian.uwc.converters.dokuwiki;
  2. import java.io.File;
  3. import java.util.HashMap;
  4. import java.util.Vector;
  5. import java.util.regex.Matcher;
  6. import java.util.regex.Pattern;
  7. import org.apache.log4j.Logger;
  8. import com.atlassian.uwc.converters.tikiwiki.RegexUtil;
  9. import com.atlassian.uwc.ui.Page;
  10. public class HierarchyLinkConverter extends HierarchyTarget {
  11. private String origPageTitle;
  12. Logger log = Logger.getLogger(this.getClass());
  13. public void convert(Page page) {
  14. log.debug("Link Conversion --- Start");
  15. String input = page.getOriginalText();
  16. origPageTitle = page.getName();
  17. String current = getCurrentPath(page);
  18. String spacekey = getSpacekey(page);
  19. log.debug("current: '" + current + "' spacekey: '" + spacekey + "'");
  20. String relativePath = getRelativePath(page);
  21. String converted = convertLink(input, current, spacekey, relativePath);
  22. page.setConvertedText(converted);
  23. log.debug("Link Conversion --- Completed");
  24. }
  25. public String getSpacekey(Page page) {
  26. if (page != null && page.getSpacekey() != null) {
  27. return page.getSpacekey();
  28. }
  29. return getProperties().getProperty("spacekey", null);
  30. }
  31. Pattern link = Pattern.compile("(?<=\\[)\\[([^\\]]*)\\](?=\\])");
  32. Pattern onecolon = Pattern.compile("^([^:]*:)([^:]*)$");
  33. protected String convertLink(String input) {
  34. return convertLink(input, null, getSpacekey(null), "");
  35. }
  36. protected String convertLink(String input, String currentPath, String spacekey, String pagepath) {
  37. Matcher linkFinder = link.matcher(input);
  38. String currentSpacekey = spacekey;
  39. Vector<String> allspaces = getSpaces();
  40. HashMap<String,String> namespaces = getDokuDirectories();
  41. StringBuffer sb = new StringBuffer();
  42. boolean found = false;
  43. while (linkFinder.find()) {
  44. found = true;
  45. String target = linkFinder.group(1);
  46. log.debug("link target orig: " + target);
  47. String alias = null;
  48. if (target.startsWith("\\\\")) continue; //UNC link
  49. if (target.contains("|")) {
  50. String[] parts = target.split("\\|");
  51. if (parts.length > 1) {
  52. target = parts[0];
  53. alias = parts[1];
  54. }
  55. }
  56. //FIXME anchors? we're not ready to transform these, so just get rid of the anchor part
  57. if (target.contains("#")) target = target.replaceAll("#[^|]*", "");
  58. log.debug("target: '" + target + "' and alias: '" + alias + "'");
  59. //remove any opening colons (:namespace:page)
  60. target = target.trim();
  61. if (!isExternal(target)) {
  62. if (target.startsWith(".")) {
  63. target = target.replaceAll("^[.]*", "");
  64. if (currentPath != null && !currentPath.equals(currentSpacekey)) { //need to add hierarchy in
  65. String pre = currentPath.replaceAll("\\/", ":");
  66. // log.debug("pre = " +pre);
  67. if (pre.endsWith(".txt")) {
  68. pre = pre.replaceFirst("[.]txt$", "");
  69. String sibling = target.replaceFirst(":[^:]+$", "");
  70. // log.debug("sibling = " +sibling);
  71. if (pre.endsWith(sibling)) {
  72. target = target.replaceFirst("^:[^:]+:", "");
  73. }
  74. }
  75. target = pre + ":" + target;
  76. }
  77. }
  78. if (target.startsWith(":")) target = target.replaceFirst(":", "");
  79. //figure out if we've already got the space represented
  80. String targetPart1 = target.replaceFirst(":[^:]+$", "");
  81. boolean containsSpace = false;
  82. // log.debug("targetPart1 = " + targetPart1);
  83. String nsFromTarget = findNSFromTarget(namespaces, target);
  84. if (nsFromTarget != null) {
  85. targetPart1 = nsFromTarget;
  86. }
  87. if (allspaces.contains(targetPart1))
  88. containsSpace = true;
  89. // log.debug("--LinkConverter");
  90. // log.debug("targetPart1 =" + targetPart1);
  91. //get rid of unnecessary links to start
  92. //(start page content will be moved to parent in DokuwikiHierarchy
  93. //unless the start page is a top level page in the space)
  94. boolean isOne = (onecolon.matcher(target)).matches();
  95. if (!(containsSpace && isOne))
  96. target = target.replaceFirst(":start$", "");
  97. String hierarchy = target; //save for later
  98. //is there a meta title to be used?
  99. // log.debug("pagepath = " + pagepath);
  100. String origMetaFilename = getMetaFilename(pagepath, ".meta");
  101. String metaFilename = origMetaFilename;
  102. // log.debug("isOne = " + isOne + ", target = " + target + ", metaFilename = " + metaFilename);
  103. metaFilename = getTargetMetaFilename(target, metaFilename, isOne);
  104. // log.debug("metaFilename = " + metaFilename);
  105. String metatitle = HierarchyTitleConverter.getMetaTitle(metaFilename);
  106. log.debug("metatitle = " + metatitle);
  107. //get confluence page name and fix the case to match HierarchyTitleConverter
  108. if (metatitle == null || "".equals(metatitle))
  109. target = target.replaceFirst("^.*:", ""); //remove everything to the last colon
  110. else //title was set with metadata
  111. target = metatitle;
  112. target = HierarchyTitleConverter.fixTitle(target); //foo_bar becomes Foo Bar
  113. //fix collisions
  114. String linkSpacekey = currentSpacekey;
  115. targetPart1 = targetPart1.replaceAll(":+", File.separator);
  116. // log.debug("containsSpace: " + containsSpace + ", " +
  117. // "ns: "+ namespaces.containsKey(targetPart1)
  118. // +", tp1: '" + targetPart1+"'");
  119. if (!containsSpace && namespaces.containsKey(targetPart1)) {
  120. linkSpacekey = namespaces.get(targetPart1);
  121. log.debug("linkSpacekey = " + linkSpacekey);
  122. }
  123. if (containsSpace) linkSpacekey = targetPart1;
  124. target = fixCollisions(target, hierarchy, linkSpacekey, metaFilename);
  125. //underscores to spaces
  126. target = target.replaceAll("_", " ");
  127. //add spacekey to target if necessary
  128. if (!target.contains(":") || containsSpace)
  129. target = linkSpacekey + ":" + target;
  130. blogLinkReport(namespaces, targetPart1, origMetaFilename, spacekey, target);
  131. log.debug("link target = " + target);
  132. }
  133. //build complete link
  134. String replacement = (alias == null)?
  135. target:
  136. alias.trim() + "|" + target;
  137. //replace
  138. replacement = RegexUtil.handleEscapesInReplacement(replacement);
  139. linkFinder.appendReplacement(sb, replacement);
  140. }
  141. if (found) {
  142. linkFinder.appendTail(sb);
  143. return sb.toString();
  144. }
  145. return input;
  146. }
  147. protected String findNSFromTarget(HashMap<String, String> namespaces, String target) {
  148. target = target.replaceAll(":", "/");
  149. while (!"".equals(target)) {
  150. if (namespaces.containsKey(target)) { return target; }
  151. if (target.contains("/")) target = target.replaceAll("[/][^/]*$", "");
  152. else return null;
  153. }
  154. return null;
  155. }
  156. private void blogLinkReport(HashMap<String, String> namespaces,
  157. String ns, String pageMetaFilename,
  158. String spacekey, String linktarget) {
  159. if (namespaces.containsKey(ns)) {
  160. String nsString = getProperties().getProperty("blog-namespaces", null);
  161. if (BlogConverter.namespaceIsBlog(ns, nsString)) {
  162. String pagetitle = HierarchyTitleConverter.getMetaTitle(pageMetaFilename);
  163. if (pagetitle == null || "".equals(pagetitle))
  164. pagetitle = origPageTitle;
  165. pagetitle = HierarchyTitleConverter.fixTitle(pagetitle);
  166. log.info("Blog Link Report - page: '" + pagetitle + "' " +
  167. "in space: '" + spacekey + "' " +
  168. "linking to: '" + linktarget + "'");
  169. }
  170. }
  171. }
  172. protected String getTargetMetaFilename(String target, String metaFilename, boolean isOne) {
  173. target=target.replaceAll(":+", File.separator);
  174. if (!target.startsWith(File.separator)) target = File.separator + target;
  175. if (!isOne) {
  176. String metadir = getProperties().getProperty("meta-dir", null);
  177. return metadir + target + ".meta";
  178. }
  179. Matcher metaFinder = metaFile.matcher(metaFilename);
  180. StringBuffer sb = new StringBuffer();
  181. boolean found = false;
  182. if (metaFinder.find()) {
  183. found = true;
  184. String replacement = target + metaFinder.group(3);
  185. replacement = RegexUtil.handleEscapesInReplacement(replacement);
  186. metaFinder.appendReplacement(sb, replacement);
  187. }
  188. if (found) {
  189. metaFinder.appendTail(sb);
  190. return sb.toString();
  191. }
  192. return null;
  193. }
  194. Pattern protocol = Pattern.compile("(https?:)|(ftp:)");
  195. private boolean isExternal(String target) {
  196. Matcher protocolFinder = protocol.matcher(target);
  197. return protocolFinder.lookingAt();
  198. }
  199. }