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

http://github.com/hudson/hudson · Java · 334 lines · 170 code · 44 blank · 120 comment · 23 complexity · cc9960cf9b36b9560bbab8358fc65017 MD5 · raw file

  1. /*
  2. * The MIT License
  3. *
  4. * Copyright (c) 2004-2010, 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 java.util.ArrayList;
  26. import java.util.Collections;
  27. import java.util.List;
  28. import java.util.regex.Matcher;
  29. import java.util.regex.Pattern;
  30. /**
  31. * Mutable representation of string with HTML mark up.
  32. *
  33. * <p>
  34. * This class is used to put mark up on plain text.
  35. * See <a href="https://github.com/hudson/hudson/blob/master/hudson-core/src/test/java/hudson/MarkupTextTest.java">
  36. * the test code</a> for a typical usage and its result.
  37. *
  38. * @author Kohsuke Kawaguchi
  39. * @since 1.70
  40. */
  41. public class MarkupText extends AbstractMarkupText {
  42. private final String text;
  43. /**
  44. * Added mark up tags.
  45. */
  46. private final List<Tag> tags = new ArrayList<Tag>();
  47. /**
  48. * Represents one mark up inserted into text.
  49. */
  50. private static final class Tag implements Comparable<Tag> {
  51. /**
  52. * Char position of this tag in {@link MarkupText#text}.
  53. * This tag is placed in front of the character of this index.
  54. */
  55. private final int pos;
  56. private final String markup;
  57. public Tag(int pos, String markup) {
  58. this.pos = pos;
  59. this.markup = markup;
  60. }
  61. public int compareTo(Tag that) {
  62. return this.pos-that.pos;
  63. }
  64. }
  65. /**
  66. * Represents a substring of a {@link MarkupText}.
  67. */
  68. public final class SubText extends AbstractMarkupText {
  69. private final int start,end;
  70. private final int[] groups;
  71. public SubText(Matcher m, int textOffset) {
  72. start = m.start() + textOffset;
  73. end = m.end() + textOffset;
  74. int cnt = m.groupCount();
  75. groups = new int[cnt*2];
  76. for( int i=0; i<cnt; i++ ) {
  77. groups[i*2 ] = m.start(i+1) + textOffset;
  78. groups[i*2+1] = m.end(i+1) + textOffset;
  79. }
  80. }
  81. public SubText(int start, int end) {
  82. this.start = start;
  83. this.end = end;
  84. groups = new int[0];
  85. }
  86. @Override
  87. public SubText subText(int start, int end) {
  88. return MarkupText.this.subText(this.start+start,
  89. end<0 ? this.end+1+end : this.start+end);
  90. }
  91. @Override
  92. public String getText() {
  93. return text.substring(start,end);
  94. }
  95. @Override
  96. public void addMarkup(int startPos, int endPos, String startTag, String endTag) {
  97. MarkupText.this.addMarkup(startPos+start, endPos+start, startTag, endTag);
  98. }
  99. /**
  100. * Surrounds this subtext with the specified start tag and the end tag.
  101. *
  102. * <p>
  103. * Start/end tag text can contain special tokens "$0", "$1", ...
  104. * and they will be replaced by their {@link #group(int) group match}.
  105. * "\$" can be used to escape characters.
  106. */
  107. public void surroundWith(String startTag, String endTag) {
  108. addMarkup(0,length(),replace(startTag),replace(endTag));
  109. }
  110. /**
  111. * Works like {@link #surroundWith(String, String)} except
  112. * that the token replacement is not performed on parameters.
  113. */
  114. public void surroundWithLiteral(String startTag, String endTag) {
  115. addMarkup(0,length(),startTag,endTag);
  116. }
  117. /**
  118. * Surrounds this subtext with &lt;a>...&lt;/a>.
  119. */
  120. public void href(String url) {
  121. addHyperlink(0,length(),url);
  122. }
  123. /**
  124. * Gets the start index of the captured group within {@link MarkupText#getText()}.
  125. *
  126. * @param groupIndex
  127. * 0 means the start of the whole subtext. 1, 2, ... are
  128. * groups captured by '(...)' in the regexp.
  129. */
  130. public int start(int groupIndex) {
  131. if(groupIndex==0) return start;
  132. return groups[groupIndex*2-2];
  133. }
  134. /**
  135. * Gets the start index of this subtext within {@link MarkupText#getText()}.
  136. */
  137. public int start() {
  138. return start;
  139. }
  140. /**
  141. * Gets the end index of the captured group within {@link MarkupText#getText()}.
  142. */
  143. public int end(int groupIndex) {
  144. if(groupIndex==0) return end;
  145. return groups[groupIndex*2-1];
  146. }
  147. /**
  148. * Gets the end index of this subtext within {@link MarkupText#getText()}.
  149. */
  150. public int end() {
  151. return end;
  152. }
  153. /**
  154. * Gets the text that represents the captured group.
  155. */
  156. public String group(int groupIndex) {
  157. if(start(groupIndex)==-1)
  158. return null;
  159. return text.substring(start(groupIndex),end(groupIndex));
  160. }
  161. /**
  162. * How many captured groups are in this subtext.
  163. * @since 1.357
  164. */
  165. public int groupCount() {
  166. return groups.length / 2;
  167. }
  168. /**
  169. * Replaces the group tokens like "$0", "$1", and etc with their actual matches.
  170. */
  171. public String replace(String s) {
  172. StringBuffer buf = new StringBuffer();
  173. for( int i=0; i<s.length(); i++) {
  174. char ch = s.charAt(i);
  175. if (ch == '\\') {// escape char
  176. i++;
  177. buf.append(s.charAt(i));
  178. } else if (ch == '$') {// replace by group
  179. i++;
  180. ch = s.charAt(i);
  181. // get the group number
  182. int groupId = ch - '0';
  183. if (groupId < 0 || groupId > 9) {
  184. buf.append('$').append(ch);
  185. } else {
  186. // add the group text
  187. String group = group(groupId);
  188. if (group != null)
  189. buf.append(group);
  190. }
  191. } else {
  192. // other chars
  193. buf.append(ch);
  194. }
  195. }
  196. return buf.toString();
  197. }
  198. @Override
  199. protected SubText createSubText(Matcher m) {
  200. return new SubText(m,start);
  201. }
  202. }
  203. /**
  204. *
  205. * @param text
  206. * Plain text. This shouldn't include any markup nor escape. Those are done later in {@link #toString(boolean)}.
  207. */
  208. public MarkupText(String text) {
  209. this.text = text;
  210. }
  211. @Override
  212. public String getText() {
  213. return text;
  214. }
  215. /**
  216. * Returns a subtext.
  217. *
  218. * @param end
  219. * If negative, -N means "trim the last N-1 chars". That is, (s,-1) is the same as (s,length)
  220. */
  221. public SubText subText(int start, int end) {
  222. return new SubText(start, end<0 ? text.length()+1+end : end);
  223. }
  224. @Override
  225. public void addMarkup( int startPos, int endPos, String startTag, String endTag ) {
  226. rangeCheck(startPos);
  227. rangeCheck(endPos);
  228. if(startPos>endPos) throw new IndexOutOfBoundsException();
  229. // when multiple tags are added to the same range, we want them to show up like
  230. // <b><i>abc</i></b>, not <b><i>abc</b></i>. Also, we'd like <b>abc</b><i>def</i>,
  231. // not <b>abc<i></b>def</i>. Do this by inserting them to different places.
  232. tags.add(new Tag(startPos, startTag));
  233. tags.add(0,new Tag(endPos,endTag));
  234. }
  235. public void addMarkup(int pos, String tag) {
  236. rangeCheck(pos);
  237. tags.add(new Tag(pos,tag));
  238. }
  239. private void rangeCheck(int pos) {
  240. if(pos<0 || pos>text.length())
  241. throw new IndexOutOfBoundsException();
  242. }
  243. /**
  244. * Returns the fully marked-up text.
  245. *
  246. * @deprecated as of 1.350.
  247. * Use {@link #toString(boolean)} to be explicit about the escape mode.
  248. */
  249. @Override
  250. public String toString() {
  251. return toString(false);
  252. }
  253. /**
  254. * Returns the fully marked-up text.
  255. *
  256. * @param preEscape
  257. * If true, the escaping is for the &lt;PRE> context. This leave SP and CR/LF intact.
  258. * If false, the escape is for the normal HTML, thus SP becomes &amp;nbsp; and CR/LF becomes &lt;BR>
  259. */
  260. public String toString(boolean preEscape) {
  261. if(tags.isEmpty())
  262. return preEscape? Util.xmlEscape(text) : Util.escape(text); // the most common case
  263. Collections.sort(tags);
  264. StringBuilder buf = new StringBuilder();
  265. int copied = 0; // # of chars already copied from text to buf
  266. for (Tag tag : tags) {
  267. if (copied<tag.pos) {
  268. String portion = text.substring(copied, tag.pos);
  269. buf.append(preEscape ? Util.xmlEscape(portion) : Util.escape(portion));
  270. copied = tag.pos;
  271. }
  272. buf.append(tag.markup);
  273. }
  274. if (copied<text.length()) {
  275. String portion = text.substring(copied, text.length());
  276. buf.append(preEscape ? Util.xmlEscape(portion) : Util.escape(portion));
  277. }
  278. return buf.toString();
  279. }
  280. // perhaps this method doesn't need to be here to remain binary compatible with past versions,
  281. // but having this seems to be safer.
  282. @Override
  283. public List<SubText> findTokens(Pattern pattern) {
  284. return super.findTokens(pattern);
  285. }
  286. @Override
  287. protected SubText createSubText(Matcher m) {
  288. return new SubText(m,0);
  289. }
  290. }