-
Notifications
You must be signed in to change notification settings - Fork 192
/
ElementUtils.java
307 lines (282 loc) · 8.92 KB
/
ElementUtils.java
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
/*
* GeoServer-Manager - Simple Manager Library for GeoServer
*
* Copyright (C) 2007,2011 GeoSolutions S.A.S.
* http://www.geo-solutions.it
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package it.geosolutions.geoserver.rest.encoder.utils;
import java.io.IOException;
import java.io.Reader;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.filter.Filter;
import org.jdom.input.SAXBuilder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
*
* @author Carlo Cancellieri - carlo.cancellieri@geo-solutions.it
*
*/
public abstract class ElementUtils {
/**
* Default logger
*/
private final static Logger LOGGER = LoggerFactory.getLogger(ElementUtils.class);
private final static SAXBuilder builder = new SAXBuilder();
public static Document parseDocument(final String xml) throws JDOMException, IOException, IllegalArgumentException {
if (xml==null){
throw new IllegalArgumentException("Unable to parse a null xml string");
}
return (Document) builder.build(new StringReader(xml));
}
public static Document parseDocument(final Reader xmlReader) throws JDOMException, IOException, IllegalArgumentException {
if (xmlReader==null){
throw new IllegalArgumentException("Unable to parse a null xml string");
}
return (Document) builder.build(xmlReader);
}
/**
*
* @param root the root where to start searching to element to remove
* @param el the element to remove (will be set to null since this node is unusable after removal)
* @return true if the element EQUALS to the 'el' starting from root
* (including) is found, false if object 'el' is not found
*
*/
public static boolean remove(final Element root, Element el) throws IllegalArgumentException {
if (root == null || el == null) {
throw new IllegalArgumentException("Bad arguments: root=" + root
+ " element=" + el);
}
// root is the element to remove
// note: equals checks references on Element type
if (root.equals(el)) {
// detach
el.detach();
// removing all child
el.removeContent();
el=null;
return true;
}
// search for the element to remove
final Element search = ElementUtils.contains(root, el);
if (search != null) {
return remove(search, el);
}
return false;
}
/**
*
* @param root
* @param filter
* @param depth
* the max depth to search. Use {@link contains(final Element
* root, final Filter filter)} for an infinite depth search
* @return
* @throws IllegalArgumentException
*/
public static List<Element> search(final Element root,
final Filter filter, final int depth)
throws IllegalArgumentException {
if (root == null || filter == null) {
throw new IllegalArgumentException("Bad arguments: root=" + root
+ " filter=" + filter + " depth=" + depth);
}
final List<Element> ret = new ArrayList<Element>();
// if match add myself
if (filter.matches(root)) {
if (LOGGER.isDebugEnabled())
LOGGER.debug("LOCATED-> name:" + root.getName() + " text:"
+ root.getText());
ret.add(root);
}
// check my children
if (depth != 0) {
final List<?> childrenList = root.getContent();
final Iterator<?> it = childrenList.iterator();
while (it.hasNext()) {
final Object obj = it.next();
if (obj instanceof Element) {
final Element childEl = (Element) obj;
ret.addAll(search(childEl, filter, depth - 1));
}
}
}
return ret;
}
public static List<Element> search(final Element root, final Filter filter) {
if (root == null || filter == null) {
throw new IllegalArgumentException("Bad arguments: root=" + root
+ " filter=" + filter);
}
final List<Element> ret = new ArrayList<Element>();
// if match add myself
if (filter.matches(root)) {
if (LOGGER.isDebugEnabled())
LOGGER.debug("LOCATED-> name:" + root.getName() + " text:"
+ root.getText());
ret.add(root);
}
// navigate through children
final Iterator<?> it = root.getDescendants(filter);
while (it.hasNext()) {
Object obj = it.next();
if (obj instanceof Element) {
Element el = (Element) obj;
if (LOGGER.isDebugEnabled())
LOGGER.debug("LOCATED-> name:" + el.getName() + " text:"
+ el.getText());
ret.add(el);
}
}
return ret;
}
/**
* @param root
* @param name
* @param val
* @return the FIRST element matching the passed filter or null
*/
public static Element contains(final Element root, final Filter filter) throws IllegalArgumentException {
if (root == null || filter == null ) {
throw new IllegalArgumentException("Bad arguments: root=" + root
+ " name=" + filter);
}
final Iterator<Element> it = search(root, filter).iterator();
if (it.hasNext())
return it.next();
else
return null;
}
/**
* @param root
* @param name
* @param val
* @return the FIRST element EQUALS to a node with name and a text value
* starting from root or null
*/
public static Element contains(final Element root, final String name,
final String val) throws IllegalArgumentException {
if (root == null || name == null || val == null) {
throw new IllegalArgumentException("Bad arguments: root=" + root
+ " name=" + name + " val=" + val);
}
final Filter filter = new Filter() {
private static final long serialVersionUID = 1L;
public boolean matches(Object obj) {
if (obj instanceof Element) {
final Element el = ((Element) obj);
if (el.getName().equals(name) && el.getText().equals(val)) {
return true;
}
}
return false;
}
};
final Iterator<Element> it = search(root, filter).iterator();
if (it.hasNext())
return it.next();
else
return null;
}
/**
* return the FIRST element with name equals to the passed key
*
* @param root
* @param name
* @return
*/
public static Element contains(final Element root, final String name){
return contains(root, name, -1);
}
/**
* return the FIRST element with name equals to the passed key
*
* @param root
* @param name
* @return
*/
public static Element contains(final Element root, final String name, final int deep)
throws IllegalArgumentException {
if (root == null || name == null) {
throw new IllegalArgumentException("Bad arguments: root=" + root
+ " name=" + name);
}
final Filter filter = new Filter() {
private static final long serialVersionUID = 1L;
public boolean matches(Object obj) {
if (obj instanceof Element) {
final Element el = ((Element) obj);
if (el.getName().equals(name)) {
return true;
}
}
return false;
}
};
final Iterator<Element> it = search(root, filter, deep).iterator();
if (it.hasNext())
return it.next();
else
return null;
}
/**
* @param root
* @param el
* @return the FIRST element EQUALS to the 'el' starting from root or null
* This tests for equality of this Content object to the supplied
* object. Content items are considered equal only if they are
* referentially equal (i.e. the same object).
*/
public static Element contains(final Element root, final Element el)
throws IllegalArgumentException {
if (root == null || el == null) {
throw new IllegalArgumentException("Bad arguments: root=" + root
+ " element=" + el);
}
final Filter filter = new Filter() {
/**
*
*/
private static final long serialVersionUID = 1L;
public boolean matches(Object obj) {
if (obj instanceof Element) {
final Element element = ((Element) obj);
if (element.equals(el)) {
return true;
}
}
return false;
}
};
final Iterator<Element> it = search(root, filter).iterator();
if (it.hasNext())
return it.next();
else
return null;
}
}