1 /*
2 * Copyright 2011 Vincent Behar
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.rundeck.api.parser;
17
18 import java.util.ArrayList;
19 import java.util.List;
20 import org.dom4j.Node;
21
22 /**
23 * Parser for a {@link List} of elements
24 *
25 * @author Vincent Behar
26 */
27 public class ListParser<T> implements XmlNodeParser<List<T>> {
28
29 private final XmlNodeParser<T> parser;
30
31 private final String xpath;
32
33 /**
34 * @param parser for an individual element
35 * @param xpath of the elements
36 */
37 public ListParser(XmlNodeParser<T> parser, String xpath) {
38 super();
39 this.parser = parser;
40 this.xpath = xpath;
41 }
42
43 @Override
44 public List<T> parseXmlNode(Node node) {
45 List<T> elements = new ArrayList<T>();
46
47 @SuppressWarnings("unchecked")
48 List<Node> elementNodes = node.selectNodes(BaseXpathParser.unwrapXpath(node, xpath));
49
50 for (Node elementNode : elementNodes) {
51 T element = parser.parseXmlNode(elementNode);
52 elements.add(element);
53 }
54
55 return elements;
56 }
57
58 }