|
1 |
| |
|
2 |
| |
|
3 |
| |
|
4 |
| package net.sourceforge.pmd.util; |
|
5 |
| |
|
6 |
| public class StringUtil { |
|
7 |
| |
|
8 |
| private static final String[] ENTITIES; |
|
9 |
| |
|
10 |
| static { |
|
11 |
3
| ENTITIES = new String[256 - 126];
|
|
12 |
3
| for (int i = 126; i <= 255; i++) {
|
|
13 |
390
| ENTITIES[i - 126] = "&#" + i + ';';
|
|
14 |
| } |
|
15 |
| } |
|
16 |
| |
|
17 |
3
| public static String replaceString(String d, char oldChar, String newString) {
|
|
18 |
3
| String fixedNew = newString;
|
|
19 |
3
| if (fixedNew == null) {
|
|
20 |
1
| fixedNew = "";
|
|
21 |
| } |
|
22 |
3
| StringBuffer desc = new StringBuffer();
|
|
23 |
3
| int index = d.indexOf(oldChar);
|
|
24 |
3
| int last = 0;
|
|
25 |
3
| while (index != -1) {
|
|
26 |
6
| desc.append(d.substring(last, index));
|
|
27 |
6
| desc.append(fixedNew);
|
|
28 |
6
| last = index + 1;
|
|
29 |
6
| index = d.indexOf(oldChar, last);
|
|
30 |
| } |
|
31 |
3
| desc.append(d.substring(last));
|
|
32 |
3
| return desc.toString();
|
|
33 |
| } |
|
34 |
| |
|
35 |
5
| public static String replaceString(String inputString, String oldString, String newString) {
|
|
36 |
5
| String fixedNew = newString;
|
|
37 |
5
| if (fixedNew == null) {
|
|
38 |
0
| fixedNew = "";
|
|
39 |
| } |
|
40 |
5
| StringBuffer desc = new StringBuffer();
|
|
41 |
5
| int index = inputString.indexOf(oldString);
|
|
42 |
5
| int last = 0;
|
|
43 |
5
| while (index != -1) {
|
|
44 |
1
| desc.append(inputString.substring(last, index));
|
|
45 |
1
| desc.append(fixedNew);
|
|
46 |
1
| last = index + oldString.length();
|
|
47 |
1
| index = inputString.indexOf(oldString, last);
|
|
48 |
| } |
|
49 |
5
| desc.append(inputString.substring(last));
|
|
50 |
5
| return desc.toString();
|
|
51 |
| } |
|
52 |
| |
|
53 |
| |
|
54 |
| |
|
55 |
| |
|
56 |
| |
|
57 |
| |
|
58 |
| |
|
59 |
| |
|
60 |
21
| public static void appendXmlEscaped(StringBuffer buf, String src) {
|
|
61 |
21
| appendXmlEscaped(buf, src, System.getProperty("net.sourceforge.pmd.supportUTF8", "no").equals("yes"));
|
|
62 |
| } |
|
63 |
| |
|
64 |
21
| private static void appendXmlEscaped(StringBuffer buf, String src, boolean supportUTF8) {
|
|
65 |
21
| char c;
|
|
66 |
21
| for (int i = 0; i < src.length(); i++) {
|
|
67 |
103
| c = src.charAt(i);
|
|
68 |
103
| if (c > '~') {
|
|
69 |
0
| if (!supportUTF8) {
|
|
70 |
0
| if (c <= 255) {
|
|
71 |
0
| buf.append(ENTITIES[c - 126]);
|
|
72 |
| } else { |
|
73 |
0
| buf.append("&u").append(Integer.toHexString(c)).append(';');
|
|
74 |
| } |
|
75 |
| } else { |
|
76 |
0
| buf.append(c);
|
|
77 |
| } |
|
78 |
103
| } else if (c == '&')
|
|
79 |
0
| buf.append("&");
|
|
80 |
103
| else if (c == '"')
|
|
81 |
0
| buf.append(""");
|
|
82 |
103
| else if (c == '<')
|
|
83 |
0
| buf.append("<");
|
|
84 |
103
| else if (c == '>')
|
|
85 |
0
| buf.append(">");
|
|
86 |
| else |
|
87 |
103
| buf.append(c);
|
|
88 |
| } |
|
89 |
| } |
|
90 |
| |
|
91 |
| |
|
92 |
| |
|
93 |
| |
|
94 |
| |
|
95 |
| |
|
96 |
| |
|
97 |
| |
|
98 |
| |
|
99 |
| |
|
100 |
| |
|
101 |
| |
|
102 |
| |
|
103 |
| |
|
104 |
| |
|
105 |
| |
|
106 |
| |
|
107 |
| |
|
108 |
| |
|
109 |
| |
|
110 |
| |
|
111 |
| |
|
112 |
| |
|
113 |
| |
|
114 |
| |
|
115 |
| |
|
116 |
| |
|
117 |
| |
|
118 |
| |
|
119 |
| } |