View Javadoc

1   package org.rundeck.api.parser;
2   
3   import org.dom4j.Element;
4   import org.dom4j.Node;
5   import org.rundeck.api.domain.BaseStorageResource;
6   import org.rundeck.api.domain.StorageResource;
7   
8   import java.util.HashMap;
9   
10  /**
11   * StorageResourceParser is ...
12   *
13   * @author Greg Schueler <greg@simplifyops.com>
14   * @since 2014-04-04
15   */
16  public class StorageResourceParser extends BaseXpathParser<StorageResource> {
17      private BaseStorageResource holder;
18      public StorageResourceParser() {
19  
20      }
21      public StorageResourceParser(BaseStorageResource holder){
22          this.holder=holder;
23      }
24  
25      public StorageResourceParser(String xpath) {
26          super(xpath);
27      }
28  
29      @Override
30      public StorageResource parse(Node node) {
31          String path = node.valueOf("@path");
32          String type = node.valueOf("@type");
33          String url = node.valueOf("@url");
34          BaseStorageResource storageResource = null == holder ? new BaseStorageResource() : holder;
35          storageResource.setDirectory("directory".equals(type));
36          storageResource.setPath(path);
37          storageResource.setUrl(url);
38  
39          if (storageResource.isDirectory()) {
40              if (node.selectSingleNode("contents") != null) {
41                  storageResource.setDirectoryContents(new ListParser<StorageResource>(new StorageResourceParser(),
42                          "contents/resource").parseXmlNode(node));
43              }
44          } else {
45              String name = node.valueOf("@name");
46              storageResource.setName(name);
47  
48              Node meta = node.selectSingleNode("resource-meta");
49              HashMap<String, String> metamap = new HashMap<String, String>();
50              if (null != meta) {
51                  Element metaEl = (Element) meta;
52                  for (Object o : metaEl.elements()) {
53                      Element sub = (Element) o;
54                      metamap.put(sub.getName(), sub.getText().trim());
55                  }
56              }
57              storageResource.setMetadata(metamap);
58          }
59          return storageResource;
60      }
61  }