-
Notifications
You must be signed in to change notification settings - Fork 27
/
Directives.java
587 lines (558 loc) · 17.4 KB
/
Directives.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
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
/*
* Copyright (c) 2013-2024, xembly.org
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met: 1) Redistributions of source code must retain the above
* copyright notice, this list of conditions and the following
* disclaimer. 2) 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. 3) Neither the name of the xembly.org 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 HOLDER 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 org.xembly;
import java.util.Collection;
import java.util.Collections;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.Map;
import lombok.EqualsAndHashCode;
import org.w3c.dom.Attr;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
/**
* Collection of {@link Directive}s, instantiable from {@link String}.
*
* <p>For example, to fetch directives from a string and apply to the
* DOM document:
*
* <pre> Document dom = DocumentBuilderFactory.newInstance()
* .newDocumentBuilder().newDocument();
* dom.appendChild(dom.createElement("root"));
* new Xembler(
* new Directives("XPATH 'root'; ADD 'employee';")
* ).apply(dom);</pre>
*
* <p>{@link Directives} can be used as a builder of Xembly script:
*
* <pre> Document dom = DocumentBuilderFactory.newInstance()
* .newDocumentBuilder().newDocument();
* dom.appendChild(dom.createElement("root"));
* new Xembler(
* new Directives()
* .xpath("/root")
* .addIf("employees")
* .add("employee")
* .attr("id", 6564)
* .up()
* .xpath("employee[@id='100']")
* .strict(1)
* .remove()
* ).apply(dom);</pre>
*
* <p>The class is mutable and thread-safe.
*
* @since 0.1
* @checkstyle ClassFanOutComplexity (500 lines)
*/
@EqualsAndHashCode(callSuper = false, of = "all")
@SuppressWarnings
(
{
"PMD.TooManyMethods",
"PMD.CyclomaticComplexity",
"PMD.GodClass",
"PMD.StdCyclomaticComplexity"
}
)
public final class Directives implements Iterable<Directive> {
/**
* List of directives.
*/
private final Collection<Directive> all;
/**
* Public ctor.
*/
public Directives() {
this(Collections.emptyList());
}
/**
* Public ctor.
* @param text Xembly script
*/
public Directives(final String text) {
this(new Verbs(text).directives());
}
/**
* Public ctor.
* @param dirs Directives
*/
@SuppressWarnings("PMD.ConstructorOnlyInitializesOrCallOtherConstructors")
public Directives(final Iterable<Directive> dirs) {
this.all = Directives.toCollection(dirs);
}
@Override
public String toString() {
return new Print(this.all).toString();
}
@Override
public Iterator<Directive> iterator() {
return this.all.iterator();
}
/**
* Create a collection of directives, which can create a copy
* of provided node.
*
* <p>For example, you already have a node in an XML document,
* which you'd like to add to another XML document:
*
* <pre> Document target = parse("<root/>");
* Node node = parse("<user name='Jeffrey'/>");
* new Xembler(
* new Directives()
* .xpath("/*")
* .add("jeff")
* .append(Directives.copyOf(node))
* ).apply(target);
* assert print(target).equals(
* "<root><jeff name='Jeffrey'></root>"
* );
* </pre>
*
* @param node Node to analyze
* @return Collection of directives
* @since 0.13
* @checkstyle CyclomaticComplexity (50 lines)
*/
@SuppressWarnings(
{
"PMD.StdCyclomaticComplexity",
"PMD.InefficientEmptyStringCheck",
"aibolit.P20_5"
}
)
public static Iterable<Directive> copyOf(final Node node) {
final Directives dirs = new Directives();
if (node.hasAttributes()) {
final NamedNodeMap attrs = node.getAttributes();
final int len = attrs.getLength();
for (int idx = 0; idx < len; ++idx) {
final Attr attr = Attr.class.cast(attrs.item(idx));
dirs.attr(attr.getNodeName(), attr.getNodeValue());
}
}
if (node.hasChildNodes()) {
final NodeList children = node.getChildNodes();
final int len = children.getLength();
for (int idx = 0; idx < len; ++idx) {
final Node child = children.item(idx);
switch (child.getNodeType()) {
case Node.ELEMENT_NODE:
dirs.add(child.getNodeName())
.append(Directives.copyOf(child))
.up();
break;
case Node.ATTRIBUTE_NODE:
dirs.attr(child.getNodeName(), child.getNodeValue());
break;
case Node.TEXT_NODE:
case Node.CDATA_SECTION_NODE:
if (len == 1) {
dirs.set(child.getTextContent());
} else if (!child.getTextContent().trim().isEmpty()) {
throw new IllegalArgumentException(
String.format(
"TEXT node #%d is not allowed together with other %d nodes in %s",
idx, len, child.getNodeName()
)
);
}
break;
case Node.PROCESSING_INSTRUCTION_NODE:
dirs.pi(child.getNodeName(), child.getNodeValue());
break;
case Node.ENTITY_NODE:
case Node.COMMENT_NODE:
break;
default:
throw new IllegalArgumentException(
String.format(
"Unsupported type %d of node %s",
child.getNodeType(), child.getNodeName()
)
);
}
}
}
return dirs;
}
/**
* Append all directives.
* @param dirs Directives to append
* @return This object
* @since 0.11
*/
public Directives append(final Iterable<Directive> dirs) {
synchronized (this.all) {
this.all.addAll(Directives.toCollection(dirs));
}
return this;
}
/**
* Appends the {@link Node node}.
* @param node The node to append
* @return This object
* @see #append(Iterable)
* @see Directives#copyOf(Node)
* @since 0.23
*/
public Directives append(final Node node) {
return this.append(Directives.copyOf(node));
}
/**
* Add node to all current nodes.
* @param name Name of the node to add
* @return This object
* @since 0.5
*/
public Directives add(final Object name) {
try {
this.all.add(new AddDirective(name.toString()));
} catch (final XmlContentException ex) {
throw new IllegalArgumentException(
String.format(
"Failed to understand XML content, ADD(%s)",
name
),
ex
);
}
return this;
}
/**
* Add multiple nodes and set their text values.
*
* <p>Every pair in the provided map will be treated as a new
* node name and value. It's a convenient utility method that simplifies
* the process of adding a collection of nodes with pre-set values. For
* example:
*
* <pre> new Directives()
* .add("first", "hello, world!")
* .add(
* new ArrayMap<String, Object>()
* .with("alpha", 1)
* .with("beta", "2")
* .with("gamma", new Date())
* )
* .add("second");
* </pre>
*
* <p>If a value provided contains illegal XML characters, a runtime
* exception will be thrown. To avoid this, it is recommended to use
* {@link Xembler#escape(String)}.
*
* @param nodes Names and values of nodes to add
* @param <K> Type of key
* @param <V> Type of value
* @return This object
* @since 0.8
*/
public <K, V> Directives add(final Map<K, V> nodes) {
for (final Map.Entry<K, V> entry : nodes.entrySet()) {
this.add(entry.getKey().toString())
.set(entry.getValue().toString())
.up();
}
return this;
}
/**
* Add node if it's absent.
* @param name Name of the node to add
* @return This object
* @since 0.5
*/
public Directives addIf(final Object name) {
try {
this.all.add(new AddIfDirective(name.toString()));
} catch (final XmlContentException ex) {
throw new IllegalArgumentException(
String.format(
"Failed to understand XML content, ADDIF(%s)",
name
),
ex
);
}
return this;
}
/**
* Remove all current nodes and move cursor to their parents.
* @return This object
* @since 0.5
*/
public Directives remove() {
this.all.add(new RemoveDirective());
return this;
}
/**
* Set attribute.
*
* <p>If a value provided contains illegal XML characters, a runtime
* exception will be thrown. To avoid this, it is recommended to use
* {@link Xembler#escape(String)}.
*
* @param name Name of the attribute
* @param value Value to set
* @return This object
* @since 0.5
*/
public Directives attr(final Object name, final Object value) {
try {
this.all.add(new AttrDirective(name.toString(), value.toString()));
} catch (final XmlContentException ex) {
throw new IllegalArgumentException(
String.format(
"Failed to understand XML content, ATTR(%s, %s)",
name, value
),
ex
);
}
return this;
}
/**
* Add processing instruction.
*
* <p>If a value provided contains illegal XML characters, a runtime
* exception will be thrown. To avoid this, it is recommended to use
* {@link Xembler#escape(String)}.
*
* @param target PI name
* @param data Data to set
* @return This object
* @since 0.9
* @checkstyle MethodName (3 lines)
*/
@SuppressWarnings("PMD.ShortMethodName")
public Directives pi(final Object target, final Object data) {
try {
this.all.add(new PiDirective(target.toString(), data.toString()));
} catch (final XmlContentException ex) {
throw new IllegalArgumentException(
String.format(
"Failed to understand XML content, PI(%s, %s)",
target, data
),
ex
);
}
return this;
}
/**
* Set text content.
*
* <p>If a value provided contains illegal XML characters, a runtime
* exception will be thrown. To avoid this, it is recommended to use
* {@link Xembler#escape(String)}.
*
* @param text Text to set
* @return This object
* @since 0.5
*/
public Directives set(final Object text) {
try {
this.all.add(new SetDirective(text.toString()));
} catch (final XmlContentException ex) {
throw new IllegalArgumentException(
String.format(
"Failed to understand XML content, SET(%s)",
text
),
ex
);
}
return this;
}
/**
* Set text content.
* @param text Text to set
* @return This object
* @since 0.7
*/
public Directives xset(final Object text) {
try {
this.all.add(new XsetDirective(text.toString()));
} catch (final XmlContentException ex) {
throw new IllegalArgumentException(
String.format(
"Failed to understand XML content, XSET(%s)",
text
),
ex
);
}
return this;
}
/**
* Set attribute.
* @param attr Attribute name
* @param text Text to set
* @return This object
* @since 0.28
*/
public Directives xattr(final Object attr, final Object text) {
try {
this.all.add(new XattrDirective(attr.toString(), text.toString()));
} catch (final XmlContentException ex) {
throw new IllegalArgumentException(
String.format(
"Failed to understand XML content, XATTR(%s, %s)",
attr, text
),
ex
);
}
return this;
}
/**
* Go one node/level up.
* @return This object
* @since 0.5
* @checkstyle MethodName (3 lines)
*/
@SuppressWarnings("PMD.ShortMethodName")
public Directives up() {
this.all.add(new UpDirective());
return this;
}
/**
* Go to XPath.
* @param path Path to go to
* @return This object
* @since 0.5
*/
public Directives xpath(final Object path) {
try {
this.all.add(new XpathDirective(path.toString()));
} catch (final XmlContentException ex) {
throw new IllegalArgumentException(
String.format(
"Failed to understand XML content, XPATH(%s)",
path
),
ex
);
}
return this;
}
/**
* Check that there is exactly this number of current nodes.
* @param number Number of expected nodes
* @return This object
* @since 0.5
*/
public Directives strict(final int number) {
this.all.add(new StrictDirective(number));
return this;
}
/**
* Push current cursor to stack.
* @return This object
* @since 0.16
*/
public Directives push() {
this.all.add(new PushDirective());
return this;
}
/**
* Pop cursor to stack and replace current cursor with it.
* @return This object
* @since 0.16
*/
public Directives pop() {
this.all.add(new PopDirective());
return this;
}
/**
* Set CDATA section.
*
* <p>If a value provided contains illegal XML characters, a runtime
* exception will be thrown. To avoid this, it is recommended to use
* {@link Xembler#escape(String)}.
*
* @param text Text to set
* @return This object
* @since 0.17
*/
public Directives cdata(final Object text) {
try {
this.all.add(new CdataDirective(text.toString()));
} catch (final XmlContentException ex) {
throw new IllegalArgumentException(
String.format(
"Failed to understand XML content, CDATA(%s)",
text
),
ex
);
}
return this;
}
/**
* Add an XML comment.
*
* <p>If a value provided contains illegal XML characters, a runtime
* exception will be thrown. To avoid this, it is recommended to use
* {@link Xembler#escape(String)}.
*
* @param text Text to set
* @return This object
* @since 0.23
*/
public Directives comment(final Object text) {
try {
this.all.add(new CommentDirective(text.toString()));
} catch (final XmlContentException ex) {
throw new IllegalArgumentException(
String.format(
"Failed to understand XML content, comment(%s)",
text
),
ex
);
}
return this;
}
/**
* Iterable to collection.
* @param itr Iterable
* @param <T> The type
* @return Collection
*/
private static <T> Collection<T> toCollection(final Iterable<T> itr) {
final Collection<T> col = new LinkedList<>();
for (final T item : itr) {
col.add(item);
}
return col;
}
}