1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.devacfr.maven.skins.reflow;
20
21 import javax.annotation.Nonnull;
22 import javax.annotation.Nullable;
23
24 import java.util.Collections;
25 import java.util.List;
26 import java.util.stream.Collectors;
27
28 import com.google.common.collect.Lists;
29 import org.codehaus.plexus.util.xml.Xpp3Dom;
30
31 import static java.util.Objects.requireNonNull;
32
33
34
35
36
37
38
39 public final class Xpp3Utils {
40
41 private Xpp3Utils() {
42 throw new UnsupportedOperationException();
43 }
44
45
46
47
48
49
50
51
52
53
54
55
56
57 @Nullable public static Xpp3Dom getFirstChild(@Nullable final Xpp3Dom parentNode,
58 @Nonnull final String name,
59 @Nonnull final String namespace) {
60 if (parentNode == null) {
61 return null;
62 }
63 requireNonNull(namespace);
64 final Xpp3Dom child = parentNode.getChild(requireNonNull(name));
65 if (child != null) {
66 return child;
67 }
68
69 return parentNode.getChild(namespace + name);
70 }
71
72
73
74
75
76
77
78
79
80
81 @SuppressWarnings("null")
82 @Nonnull
83 public static List<String> getChildren(@Nullable final Xpp3Dom parentNode) {
84 return getChildrenNodes(parentNode, null).stream().map(Xpp3Dom::getName).collect(Collectors.toList());
85 }
86
87
88
89
90
91
92
93
94
95
96
97 @SuppressWarnings("null")
98 @Nonnull
99 public static List<Xpp3Dom> getChildrenNodes(@Nullable final Xpp3Dom parentNode, final String name) {
100 if (parentNode == null) {
101 return Collections.emptyList();
102 }
103 final Xpp3Dom[] children = parentNode.getChildren();
104 if (children == null) {
105 return Collections.emptyList();
106 }
107 final List<Xpp3Dom> list = Lists.newArrayListWithCapacity(children.length);
108 for (final Xpp3Dom child : children) {
109 if (name != null) {
110 if (name.equals(child.getName())) {
111 list.add(child);
112 }
113 } else {
114 list.add(child);
115 }
116 }
117
118 return list;
119 }
120 }