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.equalsOrSuffix; |
22 | |
23 | import java.util.Map; |
24 | |
25 | import javax.inject.Named; |
26 | import javax.inject.Provider; |
27 | |
28 | import org.jclouds.http.functions.ParseSax; |
29 | import org.jclouds.ovf.internal.BaseEnvelope; |
30 | import org.jclouds.ovf.internal.BaseVirtualSystem; |
31 | import org.jclouds.ovf.xml.DiskSectionHandler; |
32 | import org.jclouds.ovf.xml.NetworkSectionHandler; |
33 | import org.jclouds.ovf.xml.SectionHandler; |
34 | import org.jclouds.util.SaxUtils; |
35 | import org.xml.sax.Attributes; |
36 | import org.xml.sax.SAXException; |
37 | |
38 | import com.google.common.collect.ImmutableMap; |
39 | import com.google.inject.Inject; |
40 | |
41 | /** |
42 | * @author Adrian Cole |
43 | */ |
44 | public class BaseEnvelopeHandler<V extends BaseVirtualSystem<V>, B extends BaseVirtualSystem.Builder<V>, H extends BaseVirtualSystemHandler<V, B>, E extends BaseEnvelope<V, E>, T extends BaseEnvelope.Builder<V, E>> |
45 | extends ParseSax.HandlerWithResult<E> { |
46 | |
47 | public E getResult() { |
48 | try { |
49 | return builder.build(); |
50 | } finally { |
51 | builder = envelopeBuilderProvider.get(); |
52 | } |
53 | } |
54 | |
55 | protected final H virtualSystemHandler; |
56 | protected final DiskSectionHandler diskHandler; |
57 | protected final NetworkSectionHandler networkHandler; |
58 | protected final Provider<T> envelopeBuilderProvider; |
59 | protected T builder; |
60 | |
61 | @Inject |
62 | public BaseEnvelopeHandler(DiskSectionHandler diskHandler, NetworkSectionHandler networkHandler, |
63 | H virtualSystemHandler, Provider<T> envelopeBuilderProvider) { |
64 | this.virtualSystemHandler = virtualSystemHandler; |
65 | this.diskHandler = diskHandler; |
66 | this.networkHandler = networkHandler; |
67 | this.envelopeBuilderProvider = envelopeBuilderProvider; |
68 | this.builder = envelopeBuilderProvider.get(); |
69 | } |
70 | |
71 | @SuppressWarnings("unchecked") |
72 | protected SectionHandler defaultSectionHandler = SectionHandler.create(); |
73 | |
74 | @SuppressWarnings("unchecked") |
75 | @Inject(optional = true) |
76 | @Named("Envelope") |
77 | Map<String, Provider<? extends SectionHandler>> extensionHandlers = ImmutableMap |
78 | .<String, Provider<? extends SectionHandler>> of(); |
79 | |
80 | @SuppressWarnings("unchecked") |
81 | protected SectionHandler extensionHandler; |
82 | |
83 | protected boolean inDisk; |
84 | protected boolean inNetwork; |
85 | protected boolean inVirtualSystem; |
86 | protected boolean inSection; |
87 | protected boolean inExtensionSection; |
88 | |
89 | protected int depth = 0; |
90 | |
91 | public void startElement(String uri, String localName, String qName, Attributes attrs) throws SAXException { |
92 | Map<String, String> attributes = SaxUtils.cleanseAttributes(attrs); |
93 | depth++; |
94 | if (depth == 2) { |
95 | if (equalsOrSuffix(qName, "DiskSection")) { |
96 | inDisk = true; |
97 | } else if (equalsOrSuffix(qName, "NetworkSection")) { |
98 | inNetwork = true; |
99 | } else if (equalsOrSuffix(qName, "VirtualSystem")) { |
100 | inVirtualSystem = true; |
101 | } else if (extensionHandlers.containsKey(qName) || attributes.containsKey("type") |
102 | && extensionHandlers.containsKey(attributes.get("type"))) { |
103 | inExtensionSection = true; |
104 | extensionHandler = extensionHandlers.get(qName).get(); |
105 | } else if (qName.endsWith("Section")) { |
106 | inSection = true; |
107 | } |
108 | } |
109 | |
110 | if (inDisk) { |
111 | diskHandler.startElement(uri, localName, qName, attrs); |
112 | } else if (inNetwork) { |
113 | networkHandler.startElement(uri, localName, qName, attrs); |
114 | } else if (inVirtualSystem) { |
115 | virtualSystemHandler.startElement(uri, localName, qName, attrs); |
116 | } else if (inExtensionSection) { |
117 | extensionHandler.startElement(uri, localName, qName, attrs); |
118 | } else if (inSection) { |
119 | defaultSectionHandler.startElement(uri, localName, qName, attrs); |
120 | } |
121 | |
122 | } |
123 | |
124 | @Override |
125 | public void endElement(String uri, String localName, String qName) { |
126 | depth--; |
127 | if (depth == 1) { |
128 | if (equalsOrSuffix(qName, "DiskSection")) { |
129 | inDisk = false; |
130 | builder.diskSection(diskHandler.getResult()); |
131 | } else if (equalsOrSuffix(qName, "NetworkSection")) { |
132 | inNetwork = false; |
133 | builder.networkSection(networkHandler.getResult()); |
134 | } else if (equalsOrSuffix(qName, "VirtualSystem")) { |
135 | inVirtualSystem = false; |
136 | builder.virtualSystem(virtualSystemHandler.getResult()); |
137 | } else if (extensionHandlers.containsKey(qName)) { |
138 | builder.additionalSection(qName, extensionHandler.getResult()); |
139 | inExtensionSection = false; |
140 | } else if (qName.endsWith("Section")) { |
141 | builder.additionalSection(qName, defaultSectionHandler.getResult()); |
142 | inSection = false; |
143 | } |
144 | } |
145 | |
146 | if (inDisk) { |
147 | diskHandler.endElement(uri, localName, qName); |
148 | } else if (inNetwork) { |
149 | networkHandler.endElement(uri, localName, qName); |
150 | } else if (inVirtualSystem) { |
151 | virtualSystemHandler.endElement(uri, localName, qName); |
152 | } else if (inExtensionSection) { |
153 | extensionHandler.endElement(uri, localName, qName); |
154 | } else if (inSection) { |
155 | defaultSectionHandler.endElement(uri, localName, qName); |
156 | } |
157 | } |
158 | |
159 | @Override |
160 | public void characters(char ch[], int start, int length) { |
161 | if (inDisk) { |
162 | diskHandler.characters(ch, start, length); |
163 | } else if (inNetwork) { |
164 | networkHandler.characters(ch, start, length); |
165 | } else if (inVirtualSystem) { |
166 | virtualSystemHandler.characters(ch, start, length); |
167 | } else if (inExtensionSection) { |
168 | extensionHandler.characters(ch, start, length); |
169 | } else if (inSection) { |
170 | defaultSectionHandler.characters(ch, start, length); |
171 | } |
172 | } |
173 | |
174 | } |