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.RundeckJob;
21  import org.rundeck.api.domain.RundeckJobsImportResult;
22  
23  /**
24   * Parser for a single {@link RundeckJobsImportResult}
25   * 
26   * @author Vincent Behar
27   */
28  public class JobsImportResultParser implements XmlNodeParser<RundeckJobsImportResult> {
29  
30      private String xpath;
31  
32      public JobsImportResultParser() {
33          super();
34      }
35  
36      /**
37       * @param xpath of the result element if it is not the root node
38       */
39      public JobsImportResultParser(String xpath) {
40          super();
41          this.xpath = xpath;
42      }
43  
44      @Override
45      public RundeckJobsImportResult parseXmlNode(Node node) {
46          Node resultNode = xpath != null ? node.selectSingleNode(xpath) : node;
47  
48          RundeckJobsImportResult result = new RundeckJobsImportResult();
49  
50          @SuppressWarnings("unchecked")
51          List<Node> succeededJobsNodes = resultNode.selectNodes("succeeded/job");
52          if (succeededJobsNodes != null) {
53              for (Node succeededJobNode : succeededJobsNodes) {
54                  RundeckJob job = new JobParser().parseXmlNode(succeededJobNode);
55                  result.addSucceededJob(job);
56              }
57          }
58  
59          @SuppressWarnings("unchecked")
60          List<Node> skippedJobsNodes = resultNode.selectNodes("skipped/job");
61          if (skippedJobsNodes != null) {
62              for (Node skippedJobNode : skippedJobsNodes) {
63                  RundeckJob job = new JobParser().parseXmlNode(skippedJobNode);
64                  result.addSkippedJob(job);
65              }
66          }
67  
68          @SuppressWarnings("unchecked")
69          List<Node> failedJobsNodes = resultNode.selectNodes("failed/job");
70          if (failedJobsNodes != null) {
71              for (Node failedJobNode : failedJobsNodes) {
72                  RundeckJob job = new JobParser().parseXmlNode(failedJobNode);
73                  result.addFailedJob(job, failedJobNode.valueOf("error"));
74              }
75          }
76  
77          return result;
78      }
79  
80  }