/hudson-core/src/main/java/hudson/AbstractMarkupText.java

http://github.com/hudson/hudson · Java · 166 lines · 59 code · 20 blank · 87 comment · 7 complexity · c128c3d31771d601d1d64555bd7467bd MD5 · raw file

  1. /*
  2. * The MIT License
  3. *
  4. * Copyright (c) 2004-2009, Sun Microsystems, Inc., Kohsuke Kawaguchi
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining a copy
  7. * of this software and associated documentation files (the "Software"), to deal
  8. * in the Software without restriction, including without limitation the rights
  9. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. * copies of the Software, and to permit persons to whom the Software is
  11. * furnished to do so, subject to the following conditions:
  12. *
  13. * The above copyright notice and this permission notice shall be included in
  14. * all copies or substantial portions of the Software.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  19. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  22. * THE SOFTWARE.
  23. */
  24. package hudson;
  25. import hudson.MarkupText.SubText;
  26. import java.util.List;
  27. import java.util.ArrayList;
  28. import java.util.regex.Pattern;
  29. import java.util.regex.Matcher;
  30. /**
  31. * Common part between {@link MarkupText} and {@link MarkupText.SubText}.
  32. *
  33. * <p>
  34. * See {@link MarkupText} for more discussion about what this class represents.
  35. *
  36. * @author Kohsuke Kawaguchi
  37. * @since 1.250
  38. */
  39. public abstract class AbstractMarkupText {
  40. /*package*/ AbstractMarkupText() {} // limit who can subclass this type.
  41. /**
  42. * Returns the plain text portion of this {@link MarkupText} without
  43. * any markup, nor any escape.
  44. */
  45. public abstract String getText();
  46. public char charAt(int idx) {
  47. return getText().charAt(idx);
  48. }
  49. /**
  50. * Length of the plain text.
  51. */
  52. public final int length() {
  53. return getText().length();
  54. }
  55. /**
  56. * Returns a subtext.
  57. *
  58. * @param end
  59. * If negative, -N means "trim the last N-1 chars". That is, (s,-1) is the same as (s,length)
  60. */
  61. public abstract MarkupText.SubText subText(int start, int end);
  62. /**
  63. * Adds a start tag and end tag at the specified position.
  64. *
  65. * <p>
  66. * For example, if the text was "abc", then <tt>addMarkup(1,2,"&lt;b>","&lt;/b>")</tt>
  67. * would generate <tt>"a&lt;b>b&lt;/b>c"</tt>
  68. */
  69. public abstract void addMarkup( int startPos, int endPos, String startTag, String endTag );
  70. /**
  71. * Inserts an A tag that surrounds the given position.
  72. *
  73. * @since 1.349
  74. */
  75. public void addHyperlink( int startPos, int endPos, String url ) {
  76. addMarkup(startPos,endPos,"<a href='"+url+"'>","</a>");
  77. }
  78. /**
  79. * Inserts an A tag that surrounds the given position.
  80. * But this hyperlink is less visible.
  81. *
  82. * @since 1.395
  83. */
  84. public void addHyperlinkLowKey( int startPos, int endPos, String url ) {
  85. addMarkup(startPos,endPos,"<a class='lowkey' href='"+url+"'>","</a>");
  86. }
  87. /**
  88. * Hides the given text.
  89. */
  90. public void hide( int startPos, int endPos ) {
  91. addMarkup(startPos,endPos,"<span style='display:none'>","</span>");
  92. }
  93. /**
  94. * Adds a start tag and end tag around the entire text
  95. */
  96. public final void wrapBy(String startTag, String endTag) {
  97. addMarkup(0,length(),startTag,endTag);
  98. }
  99. /**
  100. * Find the first occurrence of the given pattern in this text, or null.
  101. *
  102. * @since 1.349
  103. */
  104. public MarkupText.SubText findToken(Pattern pattern) {
  105. String text = getText();
  106. Matcher m = pattern.matcher(text);
  107. if(m.find())
  108. return createSubText(m);
  109. return null;
  110. }
  111. /**
  112. * Find all "tokens" that match the given pattern in this text.
  113. *
  114. * <p>
  115. * A token is like a substring, except that it's aware of word boundaries.
  116. * For example, while "bc" is a string of "abc", calling {@code findTokens}
  117. * with "bc" as a pattern on string "abc" won't match anything.
  118. *
  119. * <p>
  120. * This method is convenient for finding keywords that follow a certain syntax
  121. * from natural text. You can then use {@link MarkupText.SubText#surroundWith(String,String)}
  122. * to put mark up around such text.
  123. */
  124. public List<MarkupText.SubText> findTokens(Pattern pattern) {
  125. String text = getText();
  126. Matcher m = pattern.matcher(text);
  127. List<SubText> r = new ArrayList<SubText>();
  128. while(m.find()) {
  129. int idx = m.start();
  130. if(idx>0) {
  131. char ch = text.charAt(idx-1);
  132. if(Character.isLetter(ch) || Character.isDigit(ch))
  133. continue; // not at a word boundary
  134. }
  135. idx = m.end();
  136. if(idx<text.length()) {
  137. char ch = text.charAt(idx);
  138. if(Character.isLetter(ch) || Character.isDigit(ch))
  139. continue; // not at a word boundary
  140. }
  141. r.add(createSubText(m));
  142. }
  143. return r;
  144. }
  145. protected abstract SubText createSubText(Matcher m);
  146. }