View Javadoc
1   /*
2   * Copyright 2012-2025 Christophe Friederich
3   *
4   * Licensed under the Apache License, Version 2.0 (the "License");
5   * you may not use this file except in compliance with the License.
6   * You may obtain a copy of the License at
7   *
8   * http://www.apache.org/licenses/LICENSE-2.0
9   *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16  package org.devacfr.maven.skins.reflow;
17  
18  import static java.util.Objects.requireNonNull;
19  
20  import com.google.common.collect.Lists;
21  import java.util.Collections;
22  import java.util.List;
23  import java.util.stream.Collectors;
24  import javax.annotation.Nonnull;
25  import javax.annotation.Nullable;
26  import org.codehaus.plexus.util.xml.Xpp3Dom;
27  
28  /**
29   * Utility class to manipulate {@link Xpp3Dom} model.
30   *
31   * @author Christophe Friederich
32   * @since 2.0
33   */
34  public final class Xpp3Utils {
35  
36    private Xpp3Utils() {
37      throw new UnsupportedOperationException();
38    }
39  
40    /**
41     * Retrieves the child node. Tests both default name and with namespace.
42     *
43     * @param parentNode
44     *          the parent node
45     * @param name
46     *          the child name of node to get
47     * @param namespace
48     *          the namespace (can be empty or {@code null}).
49     * @return Returns Returns {@link Xpp3Dom} representing the child of {@code parentNode} if exists, otherwise returns
50     *         {@code null}.
51     */
52    @Nullable public static Xpp3Dom getFirstChild(@Nullable final Xpp3Dom parentNode,
53      @Nonnull final String name,
54      @Nonnull final String namespace) {
55      if (parentNode == null) {
56        return null;
57      }
58      requireNonNull(namespace);
59      final Xpp3Dom child = parentNode.getChild(requireNonNull(name));
60      if (child != null) {
61        return child;
62      }
63  
64      return parentNode.getChild(namespace + name);
65    }
66  
67    /**
68     * Gets the list of all children name for the {@code parentNode}.
69     *
70     * @param parentNode
71     *          the parent node to use (can be {@code null}.
72     * @return Returns a list of {@link String} representing the name of all children, which may be empty but never
73     *         {@code null}.
74     * @since 1.3
75     */
76    @Nonnull
77    public static List<String> getChildren(@Nullable final Xpp3Dom parentNode) {
78      return getChildrenNodes(parentNode, null).stream().map(Xpp3Dom::getName).collect(Collectors.toList());
79    }
80  
81    /**
82     * Gets children list filtered by name of {@code parentNode}.
83     *
84     * @param parentNode
85     *          the parent node to use.
86     * @param name
87     *          the name of element to filter
88     * @return Returns a list of {@link Xpp3Dom} representing all children element filtered by {@code name} of
89     *         {@code parentNode}.
90     */
91    @Nonnull
92    public static List<Xpp3Dom> getChildrenNodes(@Nullable final Xpp3Dom parentNode, final String name) {
93      if (parentNode == null) {
94        return Collections.emptyList();
95      }
96      final Xpp3Dom[] children = parentNode.getChildren();
97      if (children == null) {
98        return Collections.emptyList();
99      }
100     final List<Xpp3Dom> list = Lists.newArrayListWithCapacity(children.length);
101     for (final Xpp3Dom child : children) {
102       if (name != null) {
103         if (name.equals(child.getName())) {
104           list.add(child);
105         }
106       } else {
107         list.add(child);
108       }
109     }
110 
111     return list;
112   }
113 }