-
Notifications
You must be signed in to change notification settings - Fork 35
Expand file tree
/
Copy pathXmlUtils.java
More file actions
185 lines (174 loc) · 6.33 KB
/
XmlUtils.java
File metadata and controls
185 lines (174 loc) · 6.33 KB
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
/*
* Copyright (c) 2003, Henri Yandell
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or
* without modification, are permitted provided that the
* following conditions are met:
*
* + Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* + Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* + Neither the name of XmlWriter nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
package com.generationjava.io.xml;
/**
* XML helping static methods.
*
* @author <a href="mailto:bayard@apache.org">Henri Yandell</a>
* @version 1.0
*/
public final class XmlUtils {
public static String escapeXml(String str) {
str = str.replaceAll("&","&");
str = str.replaceAll("<","<");
str = str.replaceAll(">",">");
str = str.replaceAll("\"",""");
str = str.replaceAll("'","'");
return str;
}
public static String unescapeXml(String str) {
str = str.replaceAll("&","&");
str = str.replaceAll("<","<");
str = str.replaceAll(">",">");
str = str.replaceAll(""","\"");
str = str.replaceAll("'","'");
return str;
}
/**
* Remove any xml tags from a String.
* Same as HtmlW's method.
*/
public static String removeXml(String str) {
int sz = str.length();
StringBuffer buffer = new StringBuffer(sz);
// boolean inString = false;
boolean inTag = false;
for(int i=0; i<sz; i++) {
char ch = str.charAt(i);
if(ch == '<') {
inTag = true;
} else
if(ch == '>') {
inTag = false;
continue;
}
if(!inTag) {
buffer.append(ch);
}
}
return buffer.toString();
}
public static String getContent(String tag, String text) {
int idx = XmlUtils.getIndexOpeningTag(tag, text);
if(idx == -1) {
return "";
}
text = text.substring(idx);
int end = XmlUtils.getIndexClosingTag(tag, text);
idx = text.indexOf('>');
if(idx == -1) {
return "";
}
return text.substring(idx+1, end);
}
public static int getIndexOpeningTag(String tag, String text) {
return getIndexOpeningTag(tag, text, 0);
}
private static int getIndexOpeningTag(String tag, String text, int start) {
// consider whitespace?
int idx = text.indexOf("<"+tag, start);
if(idx == -1) {
return -1;
}
char next = text.charAt(idx+1+tag.length());
if( (next == '>') || Character.isWhitespace(next) ) {
return idx;
} else {
return getIndexOpeningTag(tag, text, idx+1);
}
}
// Pass in "para" and a string that starts with
// <para> and it will return the index of the matching </para>
// It assumes well-formed xml. Or well enough.
public static int getIndexClosingTag(String tag, String text) {
return getIndexClosingTag(tag, text, 0);
}
public static int getIndexClosingTag(String tag, String text, int start) {
String open = "<"+tag;
String close = "</"+tag+">";
// System.err.println("OPEN: "+open);
// System.err.println("CLOSE: "+close);
int closeSz = close.length();
int nextCloseIdx = text.indexOf(close, start);
// System.err.println("first close: "+nextCloseIdx);
if(nextCloseIdx == -1) {
return -1;
}
int count = XmlUtils.countMatches(text.substring(start, nextCloseIdx), open);
// System.err.println("count: "+count);
if(count == 0) {
return -1; // tag is never opened
}
int expected = 1;
while(count != expected) {
nextCloseIdx = text.indexOf(close, nextCloseIdx+closeSz);
if(nextCloseIdx == -1) {
return -1;
}
count = XmlUtils.countMatches(text.substring(start, nextCloseIdx), open);
expected++;
}
return nextCloseIdx;
}
public static String getAttribute(String attribute, String text) {
return getAttribute(attribute, text, 0);
}
public static String getAttribute(String attribute, String text, int idx) {
int close = text.indexOf(">", idx);
int attrIdx = text.indexOf(attribute+"=\"", idx);
if(attrIdx == -1) {
return null;
}
if(attrIdx > close) {
return null;
}
int attrStartIdx = attrIdx + attribute.length() + 2;
int attrCloseIdx = text.indexOf("\"", attrStartIdx);
if(attrCloseIdx > close) {
return null;
}
return unescapeXml(text.substring(attrStartIdx, attrCloseIdx));
}
// Taken from Commons Lang StringUtils 2.x
private static int countMatches(String str, String sub) {
if (str == null || str.length() == 0 || sub == null || sub.length() == 0) {
return 0;
}
int count = 0;
int idx = 0;
while ((idx = str.indexOf(sub, idx)) != -1) {
count++;
idx += sub.length();
}
return count;
}
}