Class |
Line # |
Actions |
|||||
---|---|---|---|---|---|---|---|
Xpp3Utils | 39 | 21 | 0% | 10 | 4 |
1 | /* | |
2 | * Licensed to the Apache Software Foundation (ASF) under one | |
3 | * or more contributor license agreements. See the NOTICE file | |
4 | * distributed with this work for additional information | |
5 | * regarding copyright ownership. The ASF licenses this file | |
6 | * to you under the Apache License, Version 2.0 (the | |
7 | * "License"); you may not use this file except in compliance | |
8 | * with the License. You may obtain a copy of the License at | |
9 | * | |
10 | * http://www.apache.org/licenses/LICENSE-2.0 | |
11 | * | |
12 | * Unless required by applicable law or agreed to in writing, | |
13 | * software distributed under the License is distributed on an | |
14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | |
15 | * KIND, either express or implied. See the License for the | |
16 | * specific language governing permissions and limitations | |
17 | * under the License. | |
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 | * Utility class to manipulate {@link Xpp3Dom} model. | |
35 | * | |
36 | * @author Christophe Friederich | |
37 | * @since 2.0 | |
38 | */ | |
39 | public final class Xpp3Utils { | |
40 | ||
41 | 0 | private Xpp3Utils() { |
42 | 0 | throw new UnsupportedOperationException(); |
43 | } | |
44 | ||
45 | /** | |
46 | * Retrieves the child node. Tests both default name and with namespace. | |
47 | * | |
48 | * @param parentNode | |
49 | * the parent node | |
50 | * @param name | |
51 | * the child name of node to get | |
52 | * @param namespace | |
53 | * the namespace (can be empty or {@code null}). | |
54 | * @return Returns Returns {@link Xpp3Dom} representing the child of {@code parentNode} if exists, otherwise returns | |
55 | * {@code null}. | |
56 | */ | |
57 | 602 | @Nullable public static Xpp3Dom getFirstChild(@Nullable final Xpp3Dom parentNode, |
58 | @Nonnull final String name, | |
59 | @Nonnull final String namespace) { | |
60 | 602 | if (parentNode == null) { |
61 | 33 | return null; |
62 | } | |
63 | 569 | requireNonNull(namespace); |
64 | 569 | final Xpp3Dom child = parentNode.getChild(requireNonNull(name)); |
65 | 569 | if (child != null) { |
66 | 255 | return child; |
67 | } | |
68 | ||
69 | 314 | return parentNode.getChild(namespace + name); |
70 | } | |
71 | ||
72 | /** | |
73 | * Gets the list of all children name for the {@code parentNode}. | |
74 | * | |
75 | * @param parentNode | |
76 | * the parent node to use (can be {@code null}. | |
77 | * @return Returns a list of {@link String} representing the name of all children, which may be empty but never | |
78 | * {@code null}. | |
79 | * @since 1.3 | |
80 | */ | |
81 | 2 | @SuppressWarnings("null") |
82 | @Nonnull | |
83 | public static List<String> getChildren(@Nullable final Xpp3Dom parentNode) { | |
84 | 2 | return getChildrenNodes(parentNode, null).stream().map(Xpp3Dom::getName).collect(Collectors.toList()); |
85 | } | |
86 | ||
87 | /** | |
88 | * Gets children list filtered by name of {@code parentNode}. | |
89 | * | |
90 | * @param parentNode | |
91 | * the parent node to use. | |
92 | * @param name | |
93 | * the name of element to filter | |
94 | * @return Returns a list of {@link Xpp3Dom} representing all children element filtered by {@code name} of | |
95 | * {@code parentNode}. | |
96 | */ | |
97 | 33 | @SuppressWarnings("null") |
98 | @Nonnull | |
99 | public static List<Xpp3Dom> getChildrenNodes(@Nullable final Xpp3Dom parentNode, final String name) { | |
100 | 33 | if (parentNode == null) { |
101 | 2 | return Collections.emptyList(); |
102 | } | |
103 | 31 | final Xpp3Dom[] children = parentNode.getChildren(); |
104 | 31 | if (children == null) { |
105 | 0 | return Collections.emptyList(); |
106 | } | |
107 | 31 | final List<Xpp3Dom> list = Lists.newArrayListWithCapacity(children.length); |
108 | 31 | for (final Xpp3Dom child : children) { |
109 | 51 | if (name != null) { |
110 | 41 | if (name.equals(child.getName())) { |
111 | 31 | list.add(child); |
112 | } | |
113 | } else { | |
114 | 10 | list.add(child); |
115 | } | |
116 | } | |
117 | ||
118 | 31 | return list; |
119 | } | |
120 | } |