1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.jclouds.trmk.vcloud_0_8.xml;
20
21 import static org.jclouds.trmk.vcloud_0_8.util.Utils.newReferenceType;
22 import static org.jclouds.trmk.vcloud_0_8.util.Utils.putReferenceType;
23 import static org.jclouds.util.SaxUtils.cleanseAttributes;
24 import static org.jclouds.util.SaxUtils.currentOrNull;
25 import static org.jclouds.util.SaxUtils.equalsOrSuffix;
26
27 import java.util.Map;
28
29 import org.jclouds.http.functions.ParseSax;
30 import org.jclouds.trmk.vcloud_0_8.TerremarkVCloudMediaType;
31 import org.jclouds.trmk.vcloud_0_8.domain.ReferenceType;
32 import org.jclouds.trmk.vcloud_0_8.domain.VDC;
33 import org.jclouds.trmk.vcloud_0_8.domain.internal.VDCImpl;
34 import org.jclouds.trmk.vcloud_0_8.util.Utils;
35 import org.xml.sax.Attributes;
36 import org.xml.sax.SAXException;
37
38 import com.google.common.collect.Maps;
39
40
41
42
43 public class VDCHandler extends ParseSax.HandlerWithResult<VDC> {
44
45 protected StringBuilder currentText = new StringBuilder();
46
47 protected ReferenceType vDC;
48 protected String description;
49
50 protected Map<String, ReferenceType> resourceEntities = Maps.newLinkedHashMap();
51 protected Map<String, ReferenceType> availableNetworks = Maps.newLinkedHashMap();
52
53 private ReferenceType catalog;
54 private ReferenceType publicIps;
55 private ReferenceType internetServices;
56
57 public VDC getResult() {
58 return new VDCImpl(vDC.getName(), vDC.getType(), vDC.getHref(), description, catalog, publicIps,
59 internetServices, resourceEntities, availableNetworks);
60 }
61
62 @Override
63 public void startElement(String uri, String localName, String qName, Attributes attrs) throws SAXException {
64 Map<String, String> attributes = cleanseAttributes(attrs);
65 super.startElement(uri, localName, qName, attrs);
66 if (qName.endsWith("Vdc")) {
67 vDC = newReferenceType(attributes);
68 } else if (qName.endsWith("Network")) {
69 putReferenceType(availableNetworks, attributes);
70 } else if (qName.endsWith("ResourceEntity")) {
71 putReferenceType(resourceEntities, attributes);
72 } else if (equalsOrSuffix(qName, "Link")) {
73 String name = attributes.get("name");
74 if (name.equals("Internet Services")) {
75 internetServices = Utils.newReferenceType(attributes);
76 } else if (name.equals("Public IPs")) {
77 publicIps = Utils.newReferenceType(attributes);
78 } else {
79 String type = attributes.get("type");
80 if (type.equals(TerremarkVCloudMediaType.CATALOG_XML)) {
81 catalog = Utils.newReferenceType(attributes);
82 }
83 }
84 }
85 }
86
87 public void endElement(String uri, String name, String qName) {
88 if (equalsOrSuffix(qName, "Description")) {
89 description = currentOrNull(currentText);
90 }
91 currentText = new StringBuilder();
92 }
93
94 public void characters(char ch[], int start, int length) {
95 currentText.append(ch, start, length);
96 }
97 }