|
18 | 18 | */ |
19 | 19 | package net.sourceforge.easyml.util; |
20 | 20 |
|
| 21 | +import java.util.regex.Matcher; |
| 22 | +import java.util.regex.Pattern; |
| 23 | + |
21 | 24 | /** |
22 | 25 | * XMLUtil utility class used to format the XML element and element attribute |
23 | 26 | * values by escaping and un-escaping illegal XML characters. |
24 | 27 | * |
25 | 28 | * @author Victor Cordis ( cordis.victor at gmail.com) |
26 | | - * @version 1.5.0 |
| 29 | + * @version 1.7.4 |
27 | 30 | * @since 1.0 |
28 | 31 | */ |
29 | 32 | public final class XMLUtil { |
30 | 33 |
|
31 | | - static final char XML_ILLEGAL_LT = '<'; |
32 | | - static final char XML_ILLEGAL_GT = '>'; |
33 | | - static final char XML_ILLEGAL_AMP = '&'; |
34 | | - static final char XML_ILLEGAL_QUOT = '"'; |
35 | | - static final char XML_ILLEGAL_APOS = '\''; |
36 | | - static final char XML_ILLEGAL_CR = '\r'; |
37 | | - static final String XML_LEGAL_LT = "<"; |
38 | | - static final String XML_LEGAL_GT = ">"; |
39 | | - static final String XML_LEGAL_AMP = "&"; |
40 | | - static final String XML_LEGAL_QUOT = """; |
41 | | - static final String XML_LEGAL_APOS = "'"; |
42 | | - static final String XML_LEGAL_CR = " "; |
| 34 | + /** |
| 35 | + * Constant holding the illegal <code>$</code>. |
| 36 | + */ |
| 37 | + public static final char XML_TAG_ILLEGAL_$ = '$'; |
| 38 | + /** |
| 39 | + * Constant holding the escaped form of <code>$</code>. |
| 40 | + */ |
| 41 | + public static final String XML_TAG_LEGAL_$ = "_-_"; |
| 42 | + private static final String XML_TAG_ILLEGAL_$_QUOTED = Matcher.quoteReplacement(Character.toString(XML_TAG_ILLEGAL_$)); |
| 43 | + private static final Pattern XML_TAG_LEGAL_$_PATTERN = Pattern.compile(Pattern.quote(XML_TAG_LEGAL_$)); |
| 44 | + private static final char XML_ILLEGAL_LT = '<'; |
| 45 | + private static final char XML_ILLEGAL_GT = '>'; |
| 46 | + private static final char XML_ILLEGAL_AMP = '&'; |
| 47 | + private static final char XML_ILLEGAL_QUOT = '"'; |
| 48 | + private static final char XML_ILLEGAL_APOS = '\''; |
| 49 | + private static final char XML_ILLEGAL_CR = '\r'; |
| 50 | + private static final String XML_LEGAL_LT = "<"; |
| 51 | + private static final String XML_LEGAL_GT = ">"; |
| 52 | + private static final String XML_LEGAL_AMP = "&"; |
| 53 | + private static final String XML_LEGAL_QUOT = """; |
| 54 | + private static final String XML_LEGAL_APOS = "'"; |
| 55 | + private static final String XML_LEGAL_CR = " "; |
43 | 56 |
|
44 | 57 | /** |
45 | 58 | * Returns true if the input text is free from illegal XML characters, false otherwise. |
@@ -130,6 +143,49 @@ private static void appendEscaped(StringBuilder destination, char c) { |
130 | 143 | } |
131 | 144 | } |
132 | 145 |
|
| 146 | + /** |
| 147 | + * Escapes <code>$</code> in the given tag. |
| 148 | + * |
| 149 | + * @param tag to escape |
| 150 | + * @return the escaped tag |
| 151 | + */ |
| 152 | + public static String escapeXMLTag(String tag) { |
| 153 | + final int symbolIdx = tag.indexOf(XML_TAG_ILLEGAL_$); |
| 154 | + if (symbolIdx == -1) { |
| 155 | + return tag; // all legal. |
| 156 | + } |
| 157 | + final int len = tag.length(); |
| 158 | + final StringBuilder sb = new StringBuilder(len + 10); |
| 159 | + // copy initial legal part: |
| 160 | + for (int i = 0; i < symbolIdx; i++) { |
| 161 | + sb.append(tag.charAt(i)); |
| 162 | + } |
| 163 | + // escape the remaining part: |
| 164 | + for (int i = symbolIdx; i < len; i++) { |
| 165 | + final char c = tag.charAt(i); |
| 166 | + if (c == XML_TAG_ILLEGAL_$) { |
| 167 | + sb.append(XML_TAG_LEGAL_$); |
| 168 | + } else { |
| 169 | + sb.append(c); |
| 170 | + } |
| 171 | + } |
| 172 | + return sb.toString(); |
| 173 | + } |
| 174 | + |
| 175 | + /** |
| 176 | + * Unescapes <code>$</code> in the given tag. |
| 177 | + * |
| 178 | + * @param tag to unescape |
| 179 | + * @return the unescaped tag |
| 180 | + */ |
| 181 | + public static String unescapeXMLTag(String tag) { |
| 182 | + final int escapedIdx = tag.indexOf(XML_TAG_LEGAL_$); |
| 183 | + if (escapedIdx == -1) { |
| 184 | + return tag; // all legal. |
| 185 | + } |
| 186 | + return XML_TAG_LEGAL_$_PATTERN.matcher(tag).replaceAll(XML_TAG_ILLEGAL_$_QUOTED); |
| 187 | + } |
| 188 | + |
133 | 189 | private XMLUtil() { |
134 | 190 | } |
135 | 191 | } |
0 commit comments