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.ovf.xml; |
20 | |
21 | import static org.jclouds.util.SaxUtils.currentOrNull; |
22 | import static org.jclouds.util.SaxUtils.equalsOrSuffix; |
23 | |
24 | import java.net.URI; |
25 | import java.util.Map; |
26 | |
27 | import javax.annotation.Resource; |
28 | import javax.inject.Inject; |
29 | import javax.inject.Provider; |
30 | |
31 | import org.jclouds.logging.Logger; |
32 | import org.jclouds.ovf.Disk; |
33 | import org.jclouds.ovf.DiskSection; |
34 | import org.jclouds.util.SaxUtils; |
35 | import org.xml.sax.Attributes; |
36 | |
37 | /** |
38 | * @author Adrian Cole |
39 | */ |
40 | public class DiskSectionHandler extends SectionHandler<DiskSection, DiskSection.Builder> { |
41 | |
42 | @Resource |
43 | protected Logger logger = Logger.NULL; |
44 | protected Disk.Builder diskBuilder = Disk.builder(); |
45 | |
46 | @Inject |
47 | public DiskSectionHandler(Provider<DiskSection.Builder> builderProvider) { |
48 | super(builderProvider); |
49 | } |
50 | |
51 | public void startElement(String uri, String localName, String qName, Attributes attrs) { |
52 | Map<String, String> attributes = SaxUtils.cleanseAttributes(attrs); |
53 | if (equalsOrSuffix(qName, "Disk")) { |
54 | diskBuilder.id(attributes.get("diskId")); |
55 | diskBuilder.capacity(attemptToParse(attributes.get("capacity"), "capacity", attributes.get("diskId"))); |
56 | diskBuilder.parentRef(attributes.get("parentRef")); |
57 | diskBuilder.fileRef(attributes.get("fileRef")); |
58 | if (attributes.containsKey("format")) |
59 | diskBuilder.format(URI.create(attributes.get("format"))); |
60 | diskBuilder.populatedSize(attemptToParse(attributes.get("populatedSize"), "populatedSize", attributes |
61 | .get("diskId"))); |
62 | diskBuilder.capacityAllocationUnits(attributes.get("capacityAllocationUnits")); |
63 | } |
64 | } |
65 | |
66 | private Long attemptToParse(String toParse, String key, String diskId) { |
67 | Long val = null; |
68 | if (toParse != null) { |
69 | try { |
70 | val = new Long(toParse); |
71 | } catch (NumberFormatException e) { |
72 | logger.warn("%s for disk %s not a number [%s]", key, diskId, toParse); |
73 | } |
74 | } |
75 | return val; |
76 | } |
77 | |
78 | @Override |
79 | public void endElement(String uri, String localName, String qName) { |
80 | if (equalsOrSuffix(qName, "Info")) { |
81 | builder.info(currentOrNull(currentText)); |
82 | } else if (equalsOrSuffix(qName, "Disk")) { |
83 | try { |
84 | builder.disk(diskBuilder.build()); |
85 | } finally { |
86 | diskBuilder = Disk.builder(); |
87 | } |
88 | } |
89 | super.endElement(uri, localName, qName); |
90 | } |
91 | |
92 | } |