View Javadoc

1   /**
2    * Licensed to jclouds, Inc. (jclouds) under one or more
3    * contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  jclouds licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
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   * Parses the following XML document:
41   * <p/>
42   * DescribeImagesResponse xmlns="http:
43   * 
44   * @author Adrian Cole
45   * @see <a href="http: />
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 }