View Javadoc

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.*;
6   
7   import java.text.DateFormat;
8   import java.text.ParseException;
9   import java.text.SimpleDateFormat;
10  import java.util.*;
11  
12  /**
13   * $INTERFACE is ... User: greg Date: 1/16/14 Time: 5:44 PM
14   */
15  public class WorkflowStateParser extends BaseXpathParser<WorkflowState> {
16  
17  
18      private static final ThreadLocal<DateFormat> w3cDateFormat = new ThreadLocal<DateFormat>() {
19          protected DateFormat initialValue() {
20              SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'", Locale.US);
21              fmt.setTimeZone(TimeZone.getTimeZone("GMT"));
22              return fmt;
23          }
24      };
25  
26      public WorkflowStateParser(final String xpath) {
27          super(xpath);
28      }
29  
30      public static Date parseDate(String s) {
31          if (null == s) {
32              return null;
33          }
34          try {
35              Date parse = w3cDateFormat.get().parse(s);
36              return parse;
37          } catch (ParseException e) {
38              return null;
39          }
40      }
41  
42      private static int integerValue(final String value, final int defValue) {
43          int parseMax = defValue;
44          try {
45              parseMax = null != value ? Integer.parseInt(value) : defValue;
46          } catch (NumberFormatException e) {
47          }
48          return parseMax;
49      }
50  
51      @Override
52      public WorkflowState parse(Node targetNode) {
53          WorkflowState state = new WorkflowState();
54          parseWorkflowState(targetNode, state);
55  
56  
57          return state;
58      }
59  
60      /**
61       * Parse the workflow state components from the given dom node
62       * @param targetNode
63       * @param state
64       */
65      public static void parseWorkflowState(Node targetNode, WorkflowState state) {
66          BaseStateParser.parseBaseState(targetNode, state);
67  
68          state.setStepCount(integerValue(StringUtils.trimToNull(targetNode.valueOf("stepCount")), 0));
69  
70          final List<RundeckNode> rundeckNodes =
71                  new ListParser<RundeckNode>(new NodeParser(), "targetNodes/nodes/node").parseXmlNode(targetNode);
72          state.setTargetNodes(new HashSet<RundeckNodeIdentity>(rundeckNodes));
73  
74          //steps
75          state.setSteps(new ListParser<WorkflowStepState>(new WorkflowStepStateParser(),
76                  "steps/step").parseXmlNode(targetNode));
77      }
78  
79  }