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.ec2.xml; |
20 | |
21 | import java.util.Date; |
22 | import java.util.Map; |
23 | |
24 | import org.jclouds.date.DateService; |
25 | import org.jclouds.ec2.domain.Attachment; |
26 | import org.jclouds.ec2.domain.BlockDevice; |
27 | import org.jclouds.http.functions.ParseSax; |
28 | |
29 | import com.google.common.collect.Maps; |
30 | import com.google.inject.Inject; |
31 | |
32 | /** |
33 | * |
34 | * @author Adrian Cole |
35 | */ |
36 | public class BlockDeviceMappingHandler extends |
37 | ParseSax.HandlerWithResult<Map<String, BlockDevice>> { |
38 | private StringBuilder currentText = new StringBuilder(); |
39 | |
40 | private Map<String, BlockDevice> ebsBlockDevices = Maps.newHashMap(); |
41 | private String deviceName; |
42 | private String volumeId; |
43 | private boolean deleteOnTermination = true;// correct default is true. |
44 | private Attachment.Status attachmentStatus; |
45 | private Date attachTime; |
46 | |
47 | protected final DateService dateService; |
48 | |
49 | @Inject |
50 | public BlockDeviceMappingHandler(DateService dateService) { |
51 | this.dateService = dateService; |
52 | } |
53 | |
54 | public Map<String, BlockDevice> getResult() { |
55 | return ebsBlockDevices; |
56 | } |
57 | |
58 | public void endElement(String uri, String name, String qName) { |
59 | if (qName.equals("deviceName")) { |
60 | deviceName = currentText.toString().trim(); |
61 | } else if (qName.equals("volumeId")) { |
62 | volumeId = currentText.toString().trim(); |
63 | } else if (qName.equals("deleteOnTermination")) { |
64 | deleteOnTermination = Boolean.parseBoolean(currentText.toString().trim()); |
65 | } else if (qName.equals("status")) { |
66 | attachmentStatus = Attachment.Status.fromValue(currentText.toString().trim()); |
67 | } else if (qName.equals("attachTime")) { |
68 | attachTime = dateService.iso8601DateParse(currentText.toString().trim()); |
69 | } else if (qName.equals("item")) { |
70 | ebsBlockDevices.put(deviceName, new BlockDevice(volumeId, attachmentStatus, attachTime, deleteOnTermination)); |
71 | this.volumeId = null; |
72 | this.deviceName = null; |
73 | this.deleteOnTermination = true; |
74 | this.attachmentStatus = null; |
75 | this.attachTime = null; |
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 | } |