1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.rundeck.api.parser;
17
18 import java.util.Date;
19 import org.apache.commons.lang.StringUtils;
20 import org.dom4j.Node;
21 import org.rundeck.api.domain.RundeckExecution;
22 import org.rundeck.api.domain.RundeckJob;
23 import org.rundeck.api.domain.RundeckExecution.ExecutionStatus;
24
25
26
27
28
29
30 public class ExecutionParser implements XmlNodeParser<RundeckExecution> {
31
32 private String xpath;
33
34 public ExecutionParser() {
35 super();
36 }
37
38
39
40
41 public ExecutionParser(String xpath) {
42 super();
43 this.xpath = xpath;
44 }
45
46 @Override
47 public RundeckExecution parseXmlNode(Node node) {
48 Node execNode = xpath != null ? node.selectSingleNode(xpath) : node;
49
50 RundeckExecution execution = new RundeckExecution();
51
52 execution.setId(Long.valueOf(execNode.valueOf("@id")));
53 execution.setUrl(StringUtils.trimToNull(execNode.valueOf("@href")));
54 try {
55 execution.setStatus(ExecutionStatus.valueOf(StringUtils.upperCase(execNode.valueOf("@status"))));
56 } catch (IllegalArgumentException e) {
57 }
58 execution.setDescription(StringUtils.trimToNull(execNode.valueOf("description")));
59 execution.setStartedBy(StringUtils.trimToNull(execNode.valueOf("user")));
60 execution.setAbortedBy(StringUtils.trimToNull(execNode.valueOf("abortedby")));
61 String startedAt = StringUtils.trimToNull(execNode.valueOf("date-started/@unixtime"));
62 if (startedAt != null) {
63 execution.setStartedAt(new Date(Long.valueOf(startedAt)));
64 }
65 String endedAt = StringUtils.trimToNull(execNode.valueOf("date-ended/@unixtime"));
66 if (endedAt != null) {
67 execution.setEndedAt(new Date(Long.valueOf(endedAt)));
68 }
69
70 Node jobNode = execNode.selectSingleNode("job");
71 if (jobNode != null) {
72 RundeckJob job = new JobParser().parseXmlNode(jobNode);
73 execution.setJob(job);
74 }
75
76 return execution;
77 }
78
79 }