1 package org.rundeck.api.parser;
2
3 import org.apache.commons.lang.StringUtils;
4 import org.dom4j.Node;
5 import org.rundeck.api.domain.RundeckExecution;
6 import org.rundeck.api.domain.RundeckOutput;
7 import org.rundeck.api.domain.RundeckOutputEntry;
8
9 import java.util.List;
10
11
12 public class OutputParser extends BaseXpathParser<RundeckOutput> {
13
14
15 XmlNodeParser<RundeckOutputEntry> parser;
16
17 public OutputParser(String xpath, XmlNodeParser<RundeckOutputEntry> parser) {
18 super(xpath);
19 if (null != parser) {
20 this.parser = parser;
21 }
22 }
23
24 @Override
25 public RundeckOutput parse(Node entryNode) {
26
27 RundeckOutput output = new RundeckOutput();
28
29
30
31
32
33 try {
34 output.setExecutionId(Long.valueOf(entryNode.valueOf("id")));
35 } catch (NumberFormatException e) {
36 output.setExecutionId(null);
37 }
38
39 try {
40 output.setOffset(Integer.valueOf(entryNode.valueOf("offset")));
41 } catch (NumberFormatException e) {
42 output.setOffset(-1);
43 }
44
45 output.setCompleted(Boolean.valueOf(entryNode.valueOf("completed")));
46 output.setExecCompleted(Boolean.valueOf(entryNode.valueOf("execCompleted")));
47 output.setHasFailedNodes(Boolean.valueOf(entryNode.valueOf("hasFailedNodes")));
48 output.setUnmodified(Boolean.valueOf(entryNode.valueOf("unmodified")));
49
50 try {
51 output.setStatus(RundeckExecution.ExecutionStatus
52 .valueOf(StringUtils.upperCase(entryNode.valueOf("execState"))));
53 } catch (IllegalArgumentException e) {
54 output.setStatus(null);
55 }
56
57 try {
58 output.setLastModified(Long.valueOf(entryNode.valueOf("lastModified")));
59 } catch (NumberFormatException e) {
60 output.setLastModified(null);
61 }
62
63 try {
64 output.setExecDuration(Long.valueOf(entryNode.valueOf("execDuration")));
65 } catch (NumberFormatException e) {
66 output.setExecDuration(null);
67 }
68
69 try {
70 output.setPercentLoaded(Float.valueOf(entryNode.valueOf("percentLoaded")));
71 } catch (NumberFormatException e) {
72 output.setPercentLoaded(null);
73 }
74
75 try {
76 output.setTotalSize(Integer.valueOf(entryNode.valueOf("totalSize")));
77 } catch (NumberFormatException e) {
78 output.setTotalSize(-1);
79 }
80 if(entryNode.selectSingleNode("filter")!=null){
81 output.setFilterNode(StringUtils.trimToNull(entryNode.valueOf("filter/@nodename")));
82 output.setFilterStep(StringUtils.trimToNull(entryNode.valueOf("filter/@stepctx")));
83 }
84
85 Node entriesListNode = entryNode.selectSingleNode("entries");
86
87 if (entriesListNode != null) {
88 @SuppressWarnings("unchecked")
89 List<Node> entries = entriesListNode.selectNodes("entry");
90
91 for (Node logEntryNode : entries) {
92 RundeckOutputEntry outputEntry = parser.parseXmlNode(logEntryNode);
93 output.addLogEntry(outputEntry);
94 }
95 }
96 return output;
97 }
98
99 }