01: package org.uispec4j.xml;
02:
03: public class XmlEscape {
04: private static final int AMP_CHARACTER = '&';
05: private static final int LT_CHARACTER = '<';
06: private static final int GT_CHARACTER = '>';
07: private static final int QUOT_CHARACTER = '"';
08: private static final int APOS_CHARACTER = '\'';
09:
10: private static final String AMP_ENTITY = "&";
11: private static final String LT_ENTITY = "<";
12: private static final String GT_ENTITY = ">";
13: private static final String QUOT_ENTITY = """;
14: private static final String APOS_ENTITY = "'";
15:
16: public static String convertToXmlWithEntities(String s) {
17: if (s.indexOf(AMP_CHARACTER) != -1) {
18: s = s.replaceAll("&", AMP_ENTITY);
19: }
20: if (s.indexOf(LT_CHARACTER) != -1) {
21: s = s.replaceAll("<", LT_ENTITY);
22: }
23: if (s.indexOf(GT_CHARACTER) != -1) {
24: s = s.replaceAll(">", GT_ENTITY);
25: }
26: if (s.indexOf(QUOT_CHARACTER) != -1) {
27: s = s.replaceAll("\"", QUOT_ENTITY);
28: }
29: if (s.indexOf(APOS_CHARACTER) != -1) {
30: s = s.replaceAll("'", APOS_ENTITY);
31: }
32: return s;
33: }
34:
35: public static String convertXmlEntitiesToText(String s) {
36: if (s.indexOf(AMP_ENTITY) != -1) {
37: s = s.replaceAll(AMP_ENTITY, "&");
38: }
39: if (s.indexOf(LT_ENTITY) != -1) {
40: s = s.replaceAll(LT_ENTITY, "<");
41: }
42: if (s.indexOf(GT_ENTITY) != -1) {
43: s = s.replaceAll(GT_ENTITY, ">");
44: }
45: if (s.indexOf(QUOT_ENTITY) != -1) {
46: s = s.replaceAll(QUOT_ENTITY, "\"");
47: }
48: if (s.indexOf(APOS_ENTITY) != -1) {
49: s = s.replaceAll(APOS_ENTITY, "'");
50: }
51: return s;
52: }
53: }
|