1 package org.rundeck.api.parser;
2
3 import org.apache.commons.lang.StringUtils;
4 import org.dom4j.Node;
5
6 import org.rundeck.api.domain.RundeckOutputEntry;
7 import org.rundeck.api.domain.RundeckOutputEntry.RundeckLogLevel;
8
9 import java.text.ParseException;
10 import java.text.SimpleDateFormat;
11 import java.util.*;
12
13
14
15
16 public class OutputEntryParser extends BaseXpathParser<RundeckOutputEntry> {
17
18
19
20 public OutputEntryParser() {
21 super();
22 dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'", Locale.US);
23 dateFormat.setTimeZone(TimeZone.getTimeZone("GMT"));
24 }
25
26 private SimpleDateFormat dateFormat;
27
28
29
30 public OutputEntryParser(String xpath) {
31 super(xpath);
32 dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'", Locale.US);
33 dateFormat.setTimeZone(TimeZone.getTimeZone("GMT"));
34 }
35 static HashSet<String> nonMetaAttributes = new HashSet<String>();
36 static {
37 nonMetaAttributes.add("time");
38 nonMetaAttributes.add("level");
39 nonMetaAttributes.add("user");
40 nonMetaAttributes.add("node");
41 nonMetaAttributes.add("type");
42 nonMetaAttributes.add("log");
43 nonMetaAttributes.add("absolute_type");
44 }
45
46 @Override
47 public RundeckOutputEntry parse(Node entryNode) {
48
49 RundeckOutputEntry outputEntry = new RundeckOutputEntry();
50
51 outputEntry.setTime(StringUtils.trimToNull(entryNode.valueOf("@time")));
52 try {
53 outputEntry.setLevel(RundeckLogLevel.valueOf(StringUtils.upperCase(entryNode.valueOf("@level"))));
54 } catch (IllegalArgumentException e) {
55 outputEntry.setLevel(null);
56 }
57 if(null!=entryNode.valueOf("@absolute_time")) {
58 outputEntry.setAbsoluteTime(parseDate(StringUtils.trimToNull(entryNode.valueOf("@absolute_time"))));
59 }
60
61 outputEntry.setUser(StringUtils.trimToNull(entryNode.valueOf("@user")));
62 outputEntry.setNode(StringUtils.trimToNull(entryNode.valueOf("@node")));
63 outputEntry.setType(StringUtils.trimToNull(entryNode.valueOf("@type")));
64
65 HashMap<String, String> meta = new HashMap<String, String>();
66 List list = entryNode.selectNodes("@*");
67 for (Object node1 : list) {
68 if(node1 instanceof Node) {
69 Node child = (Node) node1;
70 if (!nonMetaAttributes.contains(child.getName())) {
71 meta.put(child.getName(), child.getText());
72 }
73 }
74 }
75 if(meta.size()>0){
76 outputEntry.setMetadata(meta);
77 }
78 outputEntry.setMessage(parseMessage(entryNode));
79
80 return outputEntry;
81 }
82
83 private Date parseDate(String s) {
84 if(null==s){
85 return null;
86 }
87 try {
88 Date parse = dateFormat.parse(s);
89 return parse;
90 } catch (ParseException e) {
91 return null;
92 }
93 }
94
95
96
97
98 protected String parseMessage(Node entryNode) {
99 return StringUtils.trimToNull(entryNode.valueOf("@log"));
100 }
101
102 }