1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.jclouds.savvis.vpdc.xml;
20
21 import static org.jclouds.savvis.vpdc.util.Utils.cleanseAttributes;
22 import static org.jclouds.savvis.vpdc.util.Utils.currentOrNull;
23 import static org.jclouds.savvis.vpdc.util.Utils.newResource;
24 import static org.jclouds.util.SaxUtils.equalsOrSuffix;
25
26 import java.util.Map;
27
28 import org.jclouds.http.functions.ParseSax;
29 import org.jclouds.savvis.vpdc.domain.Link;
30 import org.jclouds.savvis.vpdc.domain.Org;
31 import org.jclouds.savvis.vpdc.domain.Resource;
32 import org.xml.sax.Attributes;
33 import org.xml.sax.SAXException;
34
35 import com.google.common.collect.ImmutableMap;
36
37
38
39
40 public class OrgHandler extends ParseSax.HandlerWithResult<Org> {
41
42 protected StringBuilder currentText = new StringBuilder();
43
44 protected Org.Builder builder = Org.builder();
45
46 public Org getResult() {
47 try {
48 return builder.build();
49 } finally {
50 builder = Org.builder();
51 }
52 }
53
54 @Override
55 public void startElement(String uri, String localName, String qName, Attributes attrs) throws SAXException {
56 Map<String, String> attributes = cleanseAttributes(attrs);
57 if (equalsOrSuffix(qName, "Org")) {
58
59 if (!attributes.containsKey("href") && getRequest() != null)
60 attributes = ImmutableMap.<String, String> builder().putAll(attributes)
61 .put("href", getRequest().getEndpoint().toASCIIString()).build();
62 Resource org = newResource(attributes);
63 builder.name(org.getName()).type(org.getType()).id(org.getId()).href(org.getHref());
64 } else if (equalsOrSuffix(qName, "Link")) {
65 Link link = Link.class.cast(newResource(attributes));
66 if ("down".equals(link.getRel()))
67 builder.vDC(link);
68 else
69 builder.image(link);
70 }
71 }
72
73 public void endElement(String uri, String name, String qName) {
74 if (equalsOrSuffix(qName, "Description")) {
75 builder.description(currentOrNull(currentText));
76 }
77 currentText = new StringBuilder();
78 }
79
80 public void characters(char ch[], int start, int length) {
81 currentText.append(ch, start, length);
82 }
83 }