1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32 package com.generationjava.io.xml;
33
34 /***
35 * XML helping static methods.
36 *
37 * @author <a href="mailto:bayard@apache.org">Henri Yandell</a>
38 * @version 1.0
39 */
40 public final class XmlUtils {
41
42 public static String escapeXml(String str) {
43 str = str.replaceAll("&","&");
44 str = str.replaceAll("<","<");
45 str = str.replaceAll(">",">");
46 str = str.replaceAll("\"",""");
47 str = str.replaceAll("'","'");
48 return str;
49 }
50
51 public static String unescapeXml(String str) {
52 str = str.replaceAll("&","&");
53 str = str.replaceAll("<","<");
54 str = str.replaceAll(">",">");
55 str = str.replaceAll(""","\"");
56 str = str.replaceAll("'","'");
57 return str;
58 }
59
60 /***
61 * Remove any xml tags from a String.
62 * Same as HtmlW's method.
63 */
64 public static String removeXml(String str) {
65 int sz = str.length();
66 StringBuffer buffer = new StringBuffer(sz);
67 boolean inString = false;
68 boolean inTag = false;
69 for(int i=0; i<sz; i++) {
70 char ch = str.charAt(i);
71 if(ch == '<') {
72 inTag = true;
73 } else
74 if(ch == '>') {
75 inTag = false;
76 continue;
77 }
78 if(!inTag) {
79 buffer.append(ch);
80 }
81 }
82 return buffer.toString();
83 }
84
85 public static String getContent(String tag, String text) {
86 int idx = XmlUtils.getIndexOpeningTag(tag, text);
87 if(idx == -1) {
88 return "";
89 }
90 text = text.substring(idx);
91 int end = XmlUtils.getIndexClosingTag(tag, text);
92 idx = text.indexOf('>');
93 if(idx == -1) {
94 return "";
95 }
96 return text.substring(idx+1, end);
97 }
98
99 public static int getIndexOpeningTag(String tag, String text) {
100 return getIndexOpeningTag(tag, text, 0);
101 }
102 private static int getIndexOpeningTag(String tag, String text, int start) {
103
104 int idx = text.indexOf("<"+tag, start);
105 if(idx == -1) {
106 return -1;
107 }
108 char next = text.charAt(idx+1+tag.length());
109 if( (next == '>') || Character.isWhitespace(next) ) {
110 return idx;
111 } else {
112 return getIndexOpeningTag(tag, text, idx+1);
113 }
114 }
115
116
117
118
119 public static int getIndexClosingTag(String tag, String text) {
120 return getIndexClosingTag(tag, text, 0);
121 }
122 public static int getIndexClosingTag(String tag, String text, int start) {
123 String open = "<"+tag;
124 String close = "</"+tag+">";
125
126
127 int closeSz = close.length();
128 int nextCloseIdx = text.indexOf(close, start);
129
130 if(nextCloseIdx == -1) {
131 return -1;
132 }
133 int count = XmlUtils.countMatches(text.substring(start, nextCloseIdx), open);
134
135 if(count == 0) {
136 return -1;
137 }
138 int expected = 1;
139 while(count != expected) {
140 nextCloseIdx = text.indexOf(close, nextCloseIdx+closeSz);
141 if(nextCloseIdx == -1) {
142 return -1;
143 }
144 count = XmlUtils.countMatches(text.substring(start, nextCloseIdx), open);
145 expected++;
146 }
147 return nextCloseIdx;
148 }
149
150 public static String getAttribute(String attribute, String text) {
151 return getAttribute(attribute, text, 0);
152 }
153 public static String getAttribute(String attribute, String text, int idx) {
154 int close = text.indexOf(">", idx);
155 int attrIdx = text.indexOf(attribute+"=\"", idx);
156 if(attrIdx == -1) {
157 return null;
158 }
159 if(attrIdx > close) {
160 return null;
161 }
162 int attrStartIdx = attrIdx + attribute.length() + 2;
163 int attrCloseIdx = text.indexOf("\"", attrStartIdx);
164 if(attrCloseIdx > close) {
165 return null;
166 }
167 return unescapeXml(text.substring(attrStartIdx, attrCloseIdx));
168 }
169
170
171 private static int countMatches(String str, String sub) {
172 if (str == null || str.length() == 0 || sub == null || sub.length() == 0) {
173 return 0;
174 }
175 int count = 0;
176 int idx = 0;
177 while ((idx = str.indexOf(sub, idx)) != -1) {
178 count++;
179 idx += sub.length();
180 }
181 return count;
182 }
183
184
185 }