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.Arrays;
19  import org.apache.commons.lang.StringUtils;
20  import org.dom4j.Node;
21  import org.rundeck.api.domain.RundeckNode;
22  
23  /**
24   * Parser for a single {@link RundeckNode}
25   * 
26   * @author Vincent Behar
27   */
28  public class NodeParser implements XmlNodeParser<RundeckNode> {
29  
30      private String xpath;
31  
32      public NodeParser() {
33          super();
34      }
35  
36      /**
37       * @param xpath of the rundeck-node element if it is not the root xml-node
38       */
39      public NodeParser(String xpath) {
40          super();
41          this.xpath = xpath;
42      }
43  
44      @Override
45      public RundeckNode parseXmlNode(Node node) {
46          Node rundeckNodeNode = xpath != null ? node.selectSingleNode(xpath) : node;
47  
48          RundeckNode rundeckNode = new RundeckNode();
49  
50          rundeckNode.setName(StringUtils.trimToNull(rundeckNodeNode.valueOf("@name")));
51          rundeckNode.setType(StringUtils.trimToNull(rundeckNodeNode.valueOf("@type")));
52          rundeckNode.setDescription(StringUtils.trimToNull(rundeckNodeNode.valueOf("@description")));
53          rundeckNode.setHostname(StringUtils.trimToNull(rundeckNodeNode.valueOf("@hostname")));
54          rundeckNode.setOsArch(StringUtils.trimToNull(rundeckNodeNode.valueOf("@osArch")));
55          rundeckNode.setOsFamily(StringUtils.trimToNull(rundeckNodeNode.valueOf("@osFamily")));
56          rundeckNode.setOsName(StringUtils.trimToNull(rundeckNodeNode.valueOf("@osName")));
57          rundeckNode.setOsVersion(StringUtils.trimToNull(rundeckNodeNode.valueOf("@osVersion")));
58          rundeckNode.setUsername(StringUtils.trimToNull(rundeckNodeNode.valueOf("@username")));
59          rundeckNode.setEditUrl(StringUtils.trimToNull(rundeckNodeNode.valueOf("@editUrl")));
60          rundeckNode.setRemoteUrl(StringUtils.trimToNull(rundeckNodeNode.valueOf("@remoteUrl")));
61  
62          String tags = StringUtils.trimToEmpty(rundeckNodeNode.valueOf("@tags"));
63          rundeckNode.setTags(Arrays.asList(StringUtils.split(tags, ",")));
64  
65          return rundeckNode;
66      }
67  
68  }