| 1 |
/* |
| 2 |
* Copyright (C) 2010 Ryszard Wiśniewski <[email protected]> |
| 3 |
* Copyright (C) 2010 Connor Tumbleson <[email protected]> |
| 4 |
* |
| 5 |
* Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 |
* you may not use this file except in compliance with the License. |
| 7 |
* You may obtain a copy of the License at |
| 8 |
* |
| 9 |
* https://www.apache.org/licenses/LICENSE-2.0 |
| 10 |
* |
| 11 |
* Unless required by applicable law or agreed to in writing, software |
| 12 |
* distributed under the License is distributed on an "AS IS" BASIS, |
| 13 |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 |
* See the License for the specific language governing permissions and |
| 15 |
* limitations under the License. |
| 16 |
*/ |
| 17 |
package brut.androlib.res.xml; |
| 18 |
|
| 19 |
import brut.util.Duo; |
| 20 |
import org.apache.commons.lang3.StringUtils; |
| 21 |
|
| 22 |
import java.awt.event.KeyEvent; |
| 23 |
import java.util.ArrayList; |
| 24 |
import java.util.List; |
| 25 |
|
| 26 |
public final class ResXmlEncoders { |
| 27 |
|
| 28 |
public static String escapeXmlChars(String str) { |
| 29 |
return StringUtils.replace(StringUtils.replace(str, "&", "&"), "<", "<"); |
| 30 |
} |
| 31 |
|
| 32 |
public static String encodeAsResXmlAttr(String str) { |
| 33 |
if (str == null || str.isEmpty()) { |
| 34 |
return str; |
| 35 |
} |
| 36 |
|
| 37 |
char[] chars = str.toCharArray(); |
| 38 |
StringBuilder out = new StringBuilder(str.length() + 10); |
| 39 |
|
| 40 |
switch (chars[0]) { |
| 41 |
case '#': |
| 42 |
case '@': |
| 43 |
case '?': |
| 44 |
out.append('\\'); |
| 45 |
} |
| 46 |
|
| 47 |
for (char c : chars) { |
| 48 |
switch (c) { |
| 49 |
case '\\': |
| 50 |
out.append('\\'); |
| 51 |
break; |
| 52 |
case '"': |
| 53 |
out.append("""); |
| 54 |
continue; |
| 55 |
case '\n': |
| 56 |
out.append("\\n"); |
| 57 |
continue; |
| 58 |
default: |
| 59 |
if (!isPrintableChar(c)) { |
| 60 |
out.append(String.format("\\u%04x", (int) c)); |
| 61 |
continue; |
| 62 |
} |
| 63 |
} |
| 64 |
out.append(c); |
| 65 |
} |
| 66 |
|
| 67 |
return out.toString(); |
| 68 |
} |
| 69 |
|
| 70 |
public static String encodeAsXmlValue(String str) { |
| 71 |
if (str == null || str.isEmpty()) { |
| 72 |
return str; |
| 73 |
} |
| 74 |
|
| 75 |
char[] chars = str.toCharArray(); |
| 76 |
StringBuilder out = new StringBuilder(str.length() + 10); |
| 77 |
|
| 78 |
switch (chars[0]) { |
| 79 |
case '#': |
| 80 |
case '@': |
| 81 |
case '?': |
| 82 |
out.append('\\'); |
| 83 |
} |
| 84 |
|
| 85 |
boolean isInStyleTag = false; |
| 86 |
int startPos = 0; |
| 87 |
boolean enclose = false; |
| 88 |
boolean wasSpace = true; |
| 89 |
for (char c : chars) { |
| 90 |
if (isInStyleTag) { |
| 91 |
if (c == '>') { |
| 92 |
isInStyleTag = false; |
| 93 |
startPos = out.length() + 1; |
| 94 |
enclose = false; |
| 95 |
} |
| 96 |
} else if (c == ' ') { |
| 97 |
if (wasSpace) { |
| 98 |
enclose = true; |
| 99 |
} |
| 100 |
wasSpace = true; |
| 101 |
} else { |
| 102 |
wasSpace = false; |
| 103 |
switch (c) { |
| 104 |
case '\\': |
| 105 |
case '"': |
| 106 |
out.append('\\'); |
| 107 |
break; |
| 108 |
case '\'': |
| 109 |
case '\n': |
| 110 |
enclose = true; |
| 111 |
break; |
| 112 |
case '<': |
| 113 |
isInStyleTag = true; |
| 114 |
if (enclose) { |
| 115 |
out.insert(startPos, '"').append('"'); |
| 116 |
} |
| 117 |
break; |
| 118 |
default: |
| 119 |
if (!isPrintableChar(c)) { |
| 120 |
|
| 121 |
// lets not write trailing \u0000 if we are at end of string |
| 122 |
if ((out.length() + 1) == str.length() && c == '\u0000') { |
| 123 |
continue; |
| 124 |
} |
| 125 |
out.append(String.format("\\u%04x", (int) c)); |
| 126 |
continue; |
| 127 |
} |
| 128 |
} |
| 129 |
} |
| 130 |
out.append(c); |
| 131 |
} |
| 132 |
|
| 133 |
if (enclose || wasSpace) { |
| 134 |
out.insert(startPos, '"').append('"'); |
| 135 |
} |
| 136 |
return out.toString(); |
| 137 |
} |
| 138 |
|
| 139 |
public static boolean hasMultipleNonPositionalSubstitutions(String str) { |
| 140 |
Duo<List<Integer>, List<Integer>> tuple = findSubstitutions(str, 4); |
| 141 |
return ! tuple.m1.isEmpty() && tuple.m1.size() + tuple.m2.size() > 1; |
| 142 |
} |
| 143 |
|
| 144 |
public static String enumerateNonPositionalSubstitutionsIfRequired(String str) { |
| 145 |
Duo<List<Integer>, List<Integer>> tuple = findSubstitutions(str, 4); |
| 146 |
if (tuple.m1.isEmpty() || tuple.m1.size() + tuple.m2.size() < 2) { |
| 147 |
return str; |
| 148 |
} |
| 149 |
List<Integer> subs = tuple.m1; |
| 150 |
|
| 151 |
StringBuilder out = new StringBuilder(); |
| 152 |
int pos = 0; |
| 153 |
int count = 0; |
| 154 |
for (Integer sub : subs) { |
| 155 |
out.append(str, pos, ++sub).append(++count).append('$'); |
| 156 |
pos = sub; |
| 157 |
} |
| 158 |
out.append(str.substring(pos)); |
| 159 |
|
| 160 |
return out.toString(); |
| 161 |
} |
| 162 |
|
| 163 |
/** |
| 164 |
* It returns a tuple of: |
| 165 |
* - a list of offsets of non positional substitutions. non-pos is defined as any "%" which isn't "%%" nor "%\d+\$" |
| 166 |
* - a list of offsets of positional substitutions |
| 167 |
*/ |
| 168 |
private static Duo<List<Integer>, List<Integer>> findSubstitutions(String str, int nonPosMax) { |
| 169 |
if (nonPosMax == -1) { |
| 170 |
nonPosMax = Integer.MAX_VALUE; |
| 171 |
} |
| 172 |
int pos; |
| 173 |
int pos2 = 0; |
| 174 |
List<Integer> nonPositional = new ArrayList<>(); |
| 175 |
List<Integer> positional = new ArrayList<>(); |
| 176 |
|
| 177 |
if (str == null) { |
| 178 |
return new Duo<>(nonPositional, positional); |
| 179 |
} |
| 180 |
|
| 181 |
int length = str.length(); |
| 182 |
|
| 183 |
while ((pos = str.indexOf('%', pos2)) != -1) { |
| 184 |
pos2 = pos + 1; |
| 185 |
if (pos2 == length) { |
| 186 |
nonPositional.add(pos); |
| 187 |
break; |
| 188 |
} |
| 189 |
char c = str.charAt(pos2++); |
| 190 |
if (c == '%') { |
| 191 |
continue; |
| 192 |
} |
| 193 |
if (c >= '0' && c <= '9' && pos2 < length) { |
| 194 |
while ((c = str.charAt(pos2++)) >= '0' && c <= '9' && pos2 < length); |
| 195 |
if (c == '$') { |
| 196 |
positional.add(pos); |
| 197 |
continue; |
| 198 |
} |
| 199 |
} |
| 200 |
|
| 201 |
nonPositional.add(pos); |
| 202 |
if (nonPositional.size() >= nonPosMax) { |
| 203 |
break; |
| 204 |
} |
| 205 |
} |
| 206 |
|
| 207 |
return new Duo<>(nonPositional, positional); |
| 208 |
} |
| 209 |
|
| 210 |
private static boolean isPrintableChar(char c) { |
| 211 |
Character.UnicodeBlock block = Character.UnicodeBlock.of(c); |
| 212 |
return !Character.isISOControl(c) && c != KeyEvent.CHAR_UNDEFINED |
| 213 |
&& block != null && block != Character.UnicodeBlock.SPECIALS; |
| 214 |
} |
| 215 |
} |