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.internal; |
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.Named; |
28 | import javax.inject.Provider; |
29 | |
30 | import org.jclouds.ovf.Section; |
31 | import org.jclouds.ovf.internal.BaseVirtualSystem; |
32 | import org.jclouds.ovf.xml.OperatingSystemSectionHandler; |
33 | import org.jclouds.ovf.xml.ProductSectionHandler; |
34 | import org.jclouds.ovf.xml.SectionHandler; |
35 | import org.jclouds.ovf.xml.VirtualHardwareSectionHandler; |
36 | import org.xml.sax.Attributes; |
37 | import org.xml.sax.SAXException; |
38 | |
39 | import com.google.common.collect.ImmutableMap; |
40 | import com.google.inject.Inject; |
41 | |
42 | /** |
43 | * @author Adrian Cole |
44 | */ |
45 | public class BaseVirtualSystemHandler<T extends BaseVirtualSystem<T>, B extends BaseVirtualSystem.Builder<T>> extends |
46 | SectionHandler<T, B> { |
47 | |
48 | private final OperatingSystemSectionHandler osHandler; |
49 | private final VirtualHardwareSectionHandler hardwareHandler; |
50 | private final ProductSectionHandler productHandler; |
51 | |
52 | @Inject |
53 | public BaseVirtualSystemHandler(Provider<B> builderProvider, OperatingSystemSectionHandler osHandler, |
54 | VirtualHardwareSectionHandler hardwareHandler, ProductSectionHandler productHandler) { |
55 | super(builderProvider); |
56 | this.osHandler = osHandler; |
57 | this.hardwareHandler = hardwareHandler; |
58 | this.productHandler = productHandler; |
59 | } |
60 | |
61 | @SuppressWarnings("unchecked") |
62 | protected SectionHandler defaultSectionHandler = SectionHandler.create(); |
63 | |
64 | @SuppressWarnings("unchecked") |
65 | protected Map<String, Provider<? extends SectionHandler>> extensionHandlers = ImmutableMap |
66 | .<String, Provider<? extends SectionHandler>> of(); |
67 | |
68 | @SuppressWarnings("unchecked") |
69 | @Inject(optional = true) |
70 | protected void setExtensionHandlers( |
71 | @Named("VirtualSystem") Map<String, Provider<? extends SectionHandler>> extensionHandlers) { |
72 | extensionHandlers = ImmutableMap.<String, Provider<? extends SectionHandler>> builder().putAll( |
73 | this.extensionHandlers).putAll(extensionHandlers).build(); |
74 | } |
75 | |
76 | @SuppressWarnings("unchecked") |
77 | protected SectionHandler extensionHandler; |
78 | |
79 | private boolean inHardware; |
80 | private boolean inOs; |
81 | private boolean inProduct; |
82 | private boolean inSection; |
83 | private boolean inExtensionSection; |
84 | private int depth; |
85 | |
86 | public void startElement(String uri, String localName, String qName, Attributes attrs) throws SAXException { |
87 | Map<String, String> attributes = cleanseAttributes(attrs); |
88 | depth++; |
89 | if (depth == 2) { |
90 | if (equalsOrSuffix(qName, "VirtualHardwareSection")) { |
91 | inHardware = true; |
92 | } else if (equalsOrSuffix(qName, "OperatingSystemSection")) { |
93 | inOs = true; |
94 | } else if (equalsOrSuffix(qName, "ProductSection")) { |
95 | inProduct = true; |
96 | } else if (extensionHandlers.containsKey(qName)) { |
97 | inExtensionSection = true; |
98 | extensionHandler = extensionHandlers.get(qName).get(); |
99 | } else if (attributes.containsKey("type") && extensionHandlers.containsKey(attributes.get("type"))) { |
100 | inExtensionSection = true; |
101 | extensionHandler = extensionHandlers.get(attributes.get("type")).get(); |
102 | } else if (qName.endsWith("Section")) { |
103 | inSection = true; |
104 | } |
105 | } |
106 | if (inHardware) { |
107 | hardwareHandler.startElement(uri, localName, qName, attrs); |
108 | } else if (inOs) { |
109 | osHandler.startElement(uri, localName, qName, attrs); |
110 | } else if (inProduct) { |
111 | productHandler.startElement(uri, localName, qName, attrs); |
112 | } else if (inExtensionSection) { |
113 | extensionHandler.startElement(uri, localName, qName, attrs); |
114 | } else if (inSection) { |
115 | defaultSectionHandler.startElement(uri, localName, qName, attrs); |
116 | } else if (equalsOrSuffix(qName, "VirtualSystem")) { |
117 | builder.id(attributes.get("id")); |
118 | } |
119 | |
120 | } |
121 | |
122 | @Override |
123 | public void endElement(String uri, String localName, String qName) { |
124 | depth--; |
125 | if (depth == 1) { |
126 | if (equalsOrSuffix(qName, "VirtualHardwareSection")) { |
127 | inHardware = false; |
128 | builder.virtualHardwareSection(hardwareHandler.getResult()); |
129 | } else if (equalsOrSuffix(qName, "OperatingSystemSection")) { |
130 | inOs = false; |
131 | builder.operatingSystemSection(osHandler.getResult()); |
132 | } else if (equalsOrSuffix(qName, "ProductSection")) { |
133 | inProduct = false; |
134 | builder.productSection(productHandler.getResult()); |
135 | } else if (extensionHandlers.containsKey(qName)) { |
136 | addAdditionalSection(qName, extensionHandler.getResult()); |
137 | inSection = false; |
138 | inExtensionSection = false; |
139 | } else if (qName.endsWith("Section")) { |
140 | addAdditionalSection(qName, inExtensionSection ? extensionHandler.getResult() : defaultSectionHandler |
141 | .getResult()); |
142 | inSection = false; |
143 | inExtensionSection = false; |
144 | } |
145 | } |
146 | |
147 | if (inHardware) { |
148 | hardwareHandler.endElement(uri, localName, qName); |
149 | } else if (inOs) { |
150 | osHandler.endElement(uri, localName, qName); |
151 | } else if (inProduct) { |
152 | productHandler.endElement(uri, localName, qName); |
153 | } else if (inExtensionSection) { |
154 | extensionHandler.endElement(uri, localName, qName); |
155 | } else if (inSection) { |
156 | defaultSectionHandler.endElement(uri, localName, qName); |
157 | } else { |
158 | if (equalsOrSuffix(qName, "Info")) { |
159 | builder.info(currentOrNull(currentText)); |
160 | } else if (equalsOrSuffix(qName, "Name")) { |
161 | builder.name(currentOrNull(currentText)); |
162 | } |
163 | super.endElement(uri, localName, qName); |
164 | } |
165 | } |
166 | |
167 | @SuppressWarnings("unchecked") |
168 | protected void addAdditionalSection(String qName, Section additionalSection) { |
169 | builder.additionalSection(qName, additionalSection); |
170 | } |
171 | |
172 | @Override |
173 | public void characters(char ch[], int start, int length) { |
174 | if (inHardware) { |
175 | hardwareHandler.characters(ch, start, length); |
176 | } else if (inOs) { |
177 | osHandler.characters(ch, start, length); |
178 | } else if (inProduct) { |
179 | productHandler.characters(ch, start, length); |
180 | } else if (inExtensionSection) { |
181 | extensionHandler.characters(ch, start, length); |
182 | } else if (inSection) { |
183 | defaultSectionHandler.characters(ch, start, length); |
184 | } else { |
185 | super.characters(ch, start, length); |
186 | } |
187 | } |
188 | |
189 | } |