PageRenderTime 46ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/src/main/java/com/atlassian/jira/plugins/bitbucket/IssueLinkerImpl.java

https://bitbucket.org/atlassian/jira-bitbucket-connector/
Java | 65 lines | 42 code | 16 blank | 7 comment | 2 complexity | 6b2b29f4777098bea8725f3868caaa20 MD5 | raw file
  1. package com.atlassian.jira.plugins.bitbucket;
  2. import java.text.MessageFormat;
  3. import java.util.regex.Matcher;
  4. import java.util.regex.Pattern;
  5. import org.apache.commons.lang.StringUtils;
  6. import com.atlassian.sal.api.ApplicationProperties;
  7. import com.opensymphony.util.TextUtils;
  8. import com.atlassian.templaterenderer.annotations.HtmlSafe;
  9. public class IssueLinkerImpl implements IssueLinker
  10. {
  11. // Copied from {@link RendererUtils} (applinks-linker plugin)
  12. public static final String PRE_LINK_PATTERN_STRING = "(?<![&=\\?>^!~/\\.\\[])\\b";// end if a blank or the end of line is found
  13. public static final String POST_LINK_PATTERN_STRING = "\\b";// end if a blank or the end of line is found
  14. public static final String LINK_JIRA_PATTERN_STRING = PRE_LINK_PATTERN_STRING + "(\\p{Lu}{2,}-\\p{Digit}+)" + POST_LINK_PATTERN_STRING;
  15. private static final String ISSUE_URL_PATTERN = "{0}/browse/{1}";
  16. private static final String ISSUE_LINK_PATTERN = "<a href=\"{0}\">{1}</a>";
  17. private final ApplicationProperties applicationProperties;
  18. public IssueLinkerImpl(ApplicationProperties applicationProperties)
  19. {
  20. this.applicationProperties = applicationProperties;
  21. }
  22. /**
  23. * Code copied mostly from {@link AbstractAppLinkRendererComponent#linkText} (applinks-linker plugin)
  24. * @param text
  25. * @return
  26. */
  27. @Override
  28. @HtmlSafe
  29. public String createLinks(String text)
  30. {
  31. text = TextUtils.htmlEncode(text);
  32. String baseUrl = applicationProperties.getBaseUrl();
  33. if (StringUtils.isBlank(text)) return "";
  34. StringBuffer buff = new StringBuffer();
  35. Matcher matcher;
  36. while ((matcher = Pattern.compile(LINK_JIRA_PATTERN_STRING).matcher(text)).find())
  37. {
  38. buff.append(text.substring(0, matcher.start(1)));
  39. String key = matcher.group(1);
  40. String url = MessageFormat.format(ISSUE_URL_PATTERN, baseUrl,key);
  41. String aLink = MessageFormat.format(ISSUE_LINK_PATTERN, url, key);
  42. buff.append(aLink);
  43. text = text.substring(matcher.end(1));
  44. }
  45. // append any remaining body (or the whole thing if no matches occurred)
  46. buff.append(text);
  47. return buff.toString();
  48. }
  49. }