1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.jclouds.aws.ec2.xml;
20
21 import static org.jclouds.util.SaxUtils.currentOrNull;
22 import static org.jclouds.util.SaxUtils.equalsOrSuffix;
23
24 import java.util.Set;
25
26 import javax.inject.Inject;
27
28 import org.jclouds.aws.ec2.domain.AWSRunningInstance;
29 import org.jclouds.date.DateService;
30 import org.jclouds.ec2.domain.Reservation;
31 import org.jclouds.ec2.domain.RunningInstance;
32 import org.jclouds.location.Region;
33 import org.xml.sax.Attributes;
34 import org.xml.sax.SAXException;
35
36 import com.google.common.collect.Sets;
37 import com.google.inject.Provider;
38
39
40
41
42
43
44
45
46
47 public class AWSDescribeInstancesResponseHandler extends
48 BaseAWSReservationHandler<Set<Reservation<? extends RunningInstance>>> {
49 private Set<Reservation<? extends RunningInstance>> reservations = Sets.newLinkedHashSet();
50 private boolean inTagSet;
51 private String key;
52 private String value;
53
54 @Inject
55 AWSDescribeInstancesResponseHandler(DateService dateService, @Region String defaultRegion,
56 Provider<AWSRunningInstance.Builder> builderProvider, TagSetHandler tagSetHandler) {
57 super(dateService, defaultRegion, builderProvider);
58 }
59
60 @Override
61 public void startElement(String uri, String localName, String qName, Attributes attrs) throws SAXException {
62 super.startElement(uri, localName, qName, attrs);
63 if (equalsOrSuffix(qName, "tagSet")) {
64 inTagSet = true;
65 }
66 }
67
68 @Override
69 public void endElement(String uri, String name, String qName) {
70 if (equalsOrSuffix(qName, "tagSet")) {
71 inTagSet = false;
72 } else if (inTagSet) {
73 if (equalsOrSuffix(qName, "key")) {
74 key = currentOrNull(currentText);
75 } else if (equalsOrSuffix(qName, "value")) {
76 value = currentOrNull(currentText);
77 }
78 }
79 super.endElement(uri, name, qName);
80 }
81
82 @Override
83 public Set<Reservation<? extends RunningInstance>> getResult() {
84 return reservations;
85 }
86
87 protected boolean endOfReservationItem() {
88 return itemDepth == 1;
89 }
90
91 @Override
92 protected void inItem() {
93 if (endOfReservationItem()) {
94 reservations.add(super.newReservation());
95 } else if (inTagSet) {
96 builder.tag(key, value);
97 key = null;
98 value = null;
99 } else {
100 super.inItem();
101 }
102 }
103
104 protected boolean endOfInstanceItem() {
105 return itemDepth == 2 && inInstancesSet;
106 }
107 }