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.cleanseAttributes; |
22 | import static org.jclouds.util.SaxUtils.currentOrNull; |
23 | import static org.jclouds.util.SaxUtils.equalsOrSuffix; |
24 | |
25 | import java.util.Map; |
26 | |
27 | import javax.inject.Inject; |
28 | import javax.inject.Provider; |
29 | |
30 | import org.jclouds.cim.xml.ResourceAllocationSettingDataHandler; |
31 | import org.jclouds.cim.xml.VirtualSystemSettingDataHandler; |
32 | import org.jclouds.ovf.VirtualHardwareSection; |
33 | import org.xml.sax.Attributes; |
34 | |
35 | import com.google.common.base.Splitter; |
36 | |
37 | /** |
38 | * @author Adrian Cole |
39 | */ |
40 | public class VirtualHardwareSectionHandler extends |
41 | SectionHandler<VirtualHardwareSection, VirtualHardwareSection.Builder> { |
42 | |
43 | private final VirtualSystemSettingDataHandler systemHandler; |
44 | private final ResourceAllocationSettingDataHandler allocationHandler; |
45 | |
46 | @Inject |
47 | public VirtualHardwareSectionHandler(Provider<VirtualHardwareSection.Builder> builderProvider, |
48 | VirtualSystemSettingDataHandler systemHandler, ResourceAllocationSettingDataHandler allocationHandler) { |
49 | super(builderProvider); |
50 | this.systemHandler = systemHandler; |
51 | this.allocationHandler = allocationHandler; |
52 | } |
53 | |
54 | private boolean inItem; |
55 | private boolean inSystem; |
56 | |
57 | public void startElement(String uri, String localName, String qName, Attributes attrs) { |
58 | Map<String, String> attributes = cleanseAttributes(attrs); |
59 | if (equalsOrSuffix(qName, "VirtualHardwareSection")) { |
60 | if (attributes.containsKey("transport")) |
61 | builder.transports(Splitter.on(' ').split(attributes.get("transport"))); |
62 | } else if (equalsOrSuffix(qName, "System")) { |
63 | inSystem = true; |
64 | } else if (!inSystem && equalsOrSuffix(qName, "Item")) { |
65 | inItem = true; |
66 | } |
67 | if (inSystem) { |
68 | systemHandler.startElement(uri, localName, qName, attrs); |
69 | } else if (inItem) { |
70 | allocationHandler.startElement(uri, localName, qName, attrs); |
71 | } |
72 | } |
73 | |
74 | @Override |
75 | public void endElement(String uri, String localName, String qName) { |
76 | if (equalsOrSuffix(qName, "System")) { |
77 | inSystem = false; |
78 | builder.system(systemHandler.getResult()); |
79 | } else if (equalsOrSuffix(qName, "Item")) { |
80 | inItem = false; |
81 | builder.item(allocationHandler.getResult()); |
82 | } |
83 | if (inSystem) { |
84 | systemHandler.endElement(uri, localName, qName); |
85 | } else if (inItem) { |
86 | allocationHandler.endElement(uri, localName, qName); |
87 | } else { |
88 | if (equalsOrSuffix(qName, "Info")) |
89 | builder.info(currentOrNull(currentText)); |
90 | super.endElement(uri, localName, qName); |
91 | } |
92 | } |
93 | |
94 | @Override |
95 | public void characters(char ch[], int start, int length) { |
96 | if (inSystem) { |
97 | systemHandler.characters(ch, start, length); |
98 | } else if (inItem) { |
99 | allocationHandler.characters(ch, start, length); |
100 | } else { |
101 | super.characters(ch, start, length); |
102 | } |
103 | } |
104 | |
105 | } |