View Javadoc

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.List;
19  import org.dom4j.Node;
20  import org.rundeck.api.domain.RundeckEvent;
21  import org.rundeck.api.domain.RundeckHistory;
22  
23  /**
24   * Parser for a single {@link RundeckHistory}
25   * 
26   * @author Vincent Behar
27   */
28  public class HistoryParser implements XmlNodeParser<RundeckHistory> {
29  
30      private String xpath;
31  
32      public HistoryParser() {
33          super();
34      }
35  
36      /**
37       * @param xpath of the history element if it is not the root node
38       */
39      public HistoryParser(String xpath) {
40          super();
41          this.xpath = xpath;
42      }
43  
44      @Override
45      public RundeckHistory parseXmlNode(Node node) {
46          Node eventsNode = xpath != null ? node.selectSingleNode(xpath) : node;
47  
48          RundeckHistory history = new RundeckHistory();
49  
50          history.setCount(Integer.valueOf(eventsNode.valueOf("@count")));
51          history.setTotal(Integer.valueOf(eventsNode.valueOf("@total")));
52          history.setMax(Integer.valueOf(eventsNode.valueOf("@max")));
53          history.setOffset(Integer.valueOf(eventsNode.valueOf("@offset")));
54  
55          @SuppressWarnings("unchecked")
56          List<Node> eventNodes = eventsNode.selectNodes("event");
57          EventParser eventParser = new EventParser();
58  
59          for (Node eventNode : eventNodes) {
60              RundeckEvent event = eventParser.parseXmlNode(eventNode);
61              history.addEvent(event);
62          }
63  
64          return history;
65      }
66  
67  }