Skip to content

Commit e57b0ca

Browse files
authored
Merge pull request #1907 from idodeclare/feature/reuse_htmlize
Feature/reuse htmlize
2 parents 775b4ad + ee2bf1b commit e57b0ca

File tree

6 files changed

+83
-38
lines changed

6 files changed

+83
-38
lines changed

src/org/opensolaris/opengrok/analysis/JFlexXref.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -575,9 +575,7 @@ private String generateId(Scope scope) {
575575
* @return String with escaped html characters
576576
*/
577577
protected String htmlize(String raw) {
578-
return raw.replace("&", "&amp;").replace("<", "&lt;")
579-
.replace(">", "&gt;").replace("\"", "&quot;")
580-
.replace("'", "&apos;");
578+
return Util.prehtmlize(raw);
581579
}
582580

583581
/**

src/org/opensolaris/opengrok/search/Results.java

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,10 @@
1919

2020
/*
2121
* Copyright (c) 2005, 2017, Oracle and/or its affiliates. All rights reserved.
22-
*
2322
* Portions Copyright 2011 Jens Elkner.
23+
* Portions Copyright (c) 2017, Chris Fraire <[email protected]>.
2424
*/
25+
2526
package org.opensolaris.opengrok.search;
2627

2728
import java.io.BufferedReader;
@@ -165,11 +166,11 @@ public static void prettyPrint(Writer out, SearchHelper sh, int start,
165166
out.write(xrefPrefixE);
166167
out.write(Util.URIEncodePath(parent));
167168
out.write("/\">");
168-
out.write(parent); // htmlize ???
169+
out.write(htmlize(parent));
169170
out.write("/</a>");
170171
if (sh.desc != null) {
171172
out.write(" - <i>");
172-
out.write(sh.desc.get(parent)); // htmlize ???
173+
out.write(htmlize(sh.desc.get(parent)));
173174
out.write("</i>");
174175
}
175176
JSONArray messages;
@@ -208,7 +209,7 @@ public static void prettyPrint(Writer out, SearchHelper sh, int start,
208209
}
209210
}
210211
out.write(">");
211-
out.write(rpath.substring(rpath.lastIndexOf('/') + 1)); // htmlize ???
212+
out.write(htmlize(rpath.substring(rpath.lastIndexOf('/') + 1)));
212213
out.write("</a>");
213214
out.write("</td><td><tt class=\"con\">");
214215
if (sh.sourceContext != null) {
@@ -251,4 +252,8 @@ public static void prettyPrint(Writer out, SearchHelper sh, int start,
251252
}
252253
}
253254
}
255+
256+
private static String htmlize(String raw) {
257+
return Util.htmlize(raw);
258+
}
254259
}

src/org/opensolaris/opengrok/search/Summary.java

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
/*
22
* Copyright 2005 The Apache Software Foundation
3+
* Portions Copyright (c) 2017, Chris Fraire <[email protected]>.
34
*
45
* Licensed under the Apache License, Version 2.0 (the "License");
56
* you may not use this file except in compliance with the License.
@@ -17,26 +18,13 @@
1718

1819
import java.util.ArrayList;
1920
import java.util.List;
21+
import org.opensolaris.opengrok.web.Util;
2022

2123
/** A document summary dynamically generated to match a query. */
2224
public class Summary {
2325

24-
public static String htmlize(String q) {
25-
StringBuilder sb = new StringBuilder(q.length() * 2);
26-
char c;
27-
for(int i=0; i < q.length() ; i++) {
28-
c = q.charAt(i);
29-
if (c == '&') {
30-
sb.append("&amp;");
31-
} else if(c == '>') {
32-
sb.append("&gt;");
33-
} else if(c == '<') {
34-
sb.append("&lt;");
35-
} else {
36-
sb.append(c);
37-
}
38-
}
39-
return sb.toString();
26+
protected static String htmlize(String q) {
27+
return Util.prehtmlize(q);
4028
}
4129

4230
/** A fragment of text within a summary. */

src/org/opensolaris/opengrok/web/Util.java

Lines changed: 59 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,9 @@
2020
/*
2121
* Copyright (c) 2005, 2017, Oracle and/or its affiliates. All rights reserved.
2222
* Portions Copyright 2011 Jens Elkner.
23+
* Portions Copyright (c) 2017, Chris Fraire <[email protected]>.
2324
*/
25+
2426
package org.opensolaris.opengrok.web;
2527

2628
import java.io.File;
@@ -82,6 +84,27 @@ private Util() {
8284
// singleton
8385
}
8486

87+
/**
88+
* Calls
89+
* {@link #htmlize(java.lang.CharSequence, java.lang.Appendable, boolean)}
90+
* with {@code q}, a transient {@link StringBuilder}, and true.
91+
* @param q a character sequence
92+
* @return a string representing the character sequence in HTML
93+
*/
94+
public static String prehtmlize(CharSequence q) {
95+
StringBuilder sb = new StringBuilder(q.length() * 2);
96+
try {
97+
htmlize(q, sb, true);
98+
} catch (IOException ioe) {
99+
// IOException cannot happen when the destination is a
100+
// StringBuilder. Wrap in an AssertionError so that callers
101+
// don't have to check for an IOException that should never
102+
// happen.
103+
throw new AssertionError("StringBuilder threw IOException", ioe);
104+
}
105+
return sb.toString();
106+
}
107+
85108
/**
86109
* Return a string which represents a <code>CharSequence</code> in HTML.
87110
*
@@ -108,15 +131,31 @@ public static String htmlize(CharSequence q) {
108131
*
109132
* @param q a character sequence to escape
110133
* @param dest where to append the character sequence to
134+
* @param pre a value indicating whether the output is pre-formatted -- if
135+
* true then LFs will not be converted to &lt;br&gt; elements
111136
* @throws IOException if an error occurred when writing to {@code dest}
112137
*/
113-
public static void htmlize(CharSequence q, Appendable dest)
138+
public static void htmlize(CharSequence q, Appendable dest, boolean pre)
114139
throws IOException {
115140
for (int i = 0; i < q.length(); i++) {
116-
htmlize(q.charAt(i), dest);
141+
htmlize(q.charAt(i), dest, pre);
117142
}
118143
}
119144

145+
/**
146+
* Calls
147+
* {@link #htmlize(java.lang.CharSequence, java.lang.Appendable, boolean)}
148+
* with {@code q}, {@code dest}, and false.
149+
*
150+
* @param q a character sequence to escape
151+
* @param dest where to append the character sequence to
152+
* @throws IOException if an error occurred when writing to {@code dest}
153+
*/
154+
public static void htmlize(CharSequence q, Appendable dest)
155+
throws IOException {
156+
htmlize(q, dest, false);
157+
}
158+
120159
/**
121160
* Append a character array to the given destination whereby special
122161
* characters for HTML are escaped accordingly.
@@ -133,7 +172,7 @@ public static void htmlize(char[] cs, int length, Appendable dest)
133172
len = cs.length;
134173
}
135174
for (int i = 0; i < len; i++) {
136-
htmlize(cs[i], dest);
175+
htmlize(cs[i], dest, false);
137176
}
138177
}
139178

@@ -143,10 +182,19 @@ public static void htmlize(char[] cs, int length, Appendable dest)
143182
*
144183
* @param c the character to append
145184
* @param dest where to append the character to
185+
* @param pre a value indicating whether the output is pre-formatted -- if
186+
* true then LFs will not be converted to &lt;br&gt; elements
146187
* @throws IOException if an error occurred when writing to {@code dest}
147188
*/
148-
private static void htmlize(char c, Appendable dest) throws IOException {
189+
private static void htmlize(char c, Appendable dest, boolean pre)
190+
throws IOException {
149191
switch (c) {
192+
case '\'':
193+
dest.append("&apos;");
194+
break;
195+
case '"':
196+
dest.append("&quot;");
197+
break;
150198
case '&':
151199
dest.append("&amp;");
152200
break;
@@ -157,7 +205,11 @@ private static void htmlize(char c, Appendable dest) throws IOException {
157205
dest.append("&lt;");
158206
break;
159207
case '\n':
160-
dest.append("<br/>");
208+
if (pre) {
209+
dest.append(c);
210+
} else {
211+
dest.append("<br/>");
212+
}
161213
break;
162214
default:
163215
dest.append(c);
@@ -837,7 +889,7 @@ public static String[] diffline(StringBuilder line1, StringBuilder line2) {
837889
sb.append(Util.htmlize(line1.substring(m + 1, line1.length())));
838890
ret[0] = sb.toString();
839891
} else {
840-
ret[0] = line1.toString(); // no change
892+
ret[0] = Util.htmlize(line1.toString()); // no change
841893
}
842894

843895
// added
@@ -850,7 +902,7 @@ public static String[] diffline(StringBuilder line1, StringBuilder line2) {
850902
sb.append(Util.htmlize(line2.substring(n + 1, line2.length())));
851903
ret[1] = sb.toString();
852904
} else {
853-
ret[1] = line2.toString(); // no change
905+
ret[1] = Util.htmlize(line2.toString()); // no change
854906
}
855907

856908
return ret;

test/org/opensolaris/opengrok/analysis/python/sample_xref.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,6 @@
157157
<a class="l" name="149" href="#149">149</a> <a class="d intelliWindow-symbol" href="#f" data-definition-place="defined-in-file">f</a>.<a href="/source/s?defs=writelines" class="intelliWindow-symbol" data-definition-place="undefined-in-file">writelines</a>(<a class="d intelliWindow-symbol" href="#text" data-definition-place="defined-in-file">text</a>)
158158
<a class="hl" name="150" href="#150">150</a> <a class="d intelliWindow-symbol" href="#f" data-definition-place="defined-in-file">f</a>.<a href="/source/s?defs=close" class="intelliWindow-symbol" data-definition-place="undefined-in-file">close</a>()
159159
<a class="l" name="151" href="#151">151</a><b>print</b>(<span class="s">&apos;<a href="http://example.com?a=">http://example.com?a=</a>&apos;</span>)
160-
<a class="l" name="152" href="#152">152</a><b>print</b>(<span class="s">&apos;&apos;&apos;<a href="http://example.com?a='b'&amp;">http://example.com?a='b'&amp;</a>&apos;&apos;&apos;</span>)
160+
<a class="l" name="152" href="#152">152</a><b>print</b>(<span class="s">&apos;&apos;&apos;<a href="http://example.com?a='b'&amp;">http://example.com?a=&apos;b&apos;&amp;</a>&apos;&apos;&apos;</span>)
161161
<a class="l" name="153" href="#153">153</a></body>
162162
</html>

test/org/opensolaris/opengrok/web/UtilTest.java

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,9 @@
1919

2020
/*
2121
* Copyright (c) 2007, 2017, Oracle and/or its affiliates. All rights reserved.
22+
* Portions Copyright (c) 2017, Chris Fraire <[email protected]>.
2223
*/
24+
2325
package org.opensolaris.opengrok.web;
2426

2527
import java.io.ByteArrayInputStream;
@@ -211,19 +213,19 @@ public void diffline() {
211213
"\"(ses_id, mer_id, pass_id, \" + refCol +\" , mer_ref, amnt, "
212214
+ "cur, ps_id, ret_url, exp_url, d_req_time, d_req_mil, "
213215
+ "h_resp_time, h_resp_mil) \"",
214-
"\"(ses_id, mer_id, pass_id, \" + refCol +\" , mer_ref, amnt, "
216+
"&quot;(ses_id, mer_id, pass_id, &quot; + refCol +&quot; , mer_ref, amnt, "
215217
+ "cur, ps_id, ret_url, d_req_time, d_req_mil, h_resp_time, "
216-
+ "h_resp_mil) \"",
217-
"\"(ses_id, mer_id, pass_id, \" + refCol +\" , mer_ref, amnt, "
218+
+ "h_resp_mil) &quot;",
219+
"&quot;(ses_id, mer_id, pass_id, &quot; + refCol +&quot; , mer_ref, amnt, "
218220
+ "cur, ps_id, ret_url, <span class=\"a\">exp_url, "
219-
+ "</span>d_req_time, d_req_mil, h_resp_time, h_resp_mil) \""
221+
+ "</span>d_req_time, d_req_mil, h_resp_time, h_resp_mil) &quot;"
220222
},
221223
{
222224
"\"VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)\", values);",
223225
"\"VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)\", values);",
224-
"\"VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)\", values);",
225-
"\"VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?<span "
226-
+ "class=\"a\">, ?</span>)\", values);"
226+
"&quot;VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)&quot;, values);",
227+
"&quot;VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?<span "
228+
+ "class=\"a\">, ?</span>)&quot;, values);"
227229
},
228230
{
229231
"char *config_list = NULL;",

0 commit comments

Comments
 (0)