EMMA Coverage Report (generated Wed Oct 26 13:47:17 EDT 2011)
[all classes][org.jclouds.ovf.xml.internal]

COVERAGE SUMMARY FOR SOURCE FILE [BaseVirtualSystemHandler.java]

nameclass, %method, %block, %line, %
BaseVirtualSystemHandler.java100% (1/1)83%  (5/6)72%  (310/432)79%  (69.1/88)

COVERAGE BREAKDOWN BY CLASS AND METHOD

nameclass, %method, %block, %line, %
     
class BaseVirtualSystemHandler100% (1/1)83%  (5/6)72%  (310/432)79%  (69.1/88)
setExtensionHandlers (Map): void 0%   (0/1)0%   (0/9)0%   (0/2)
startElement (String, String, String, Attributes): void 100% (1/1)65%  (102/157)75%  (22.4/30)
characters (char [], int, int): void 100% (1/1)75%  (42/56)83%  (10/12)
endElement (String, String, String): void 100% (1/1)76%  (139/183)79%  (27.8/35)
BaseVirtualSystemHandler (Provider, OperatingSystemSectionHandler, VirtualHar... 100% (1/1)100% (19/19)100% (7/7)
addAdditionalSection (String, Section): void 100% (1/1)100% (8/8)100% (2/2)

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 */
19package org.jclouds.ovf.xml.internal;
20 
21import static org.jclouds.util.SaxUtils.cleanseAttributes;
22import static org.jclouds.util.SaxUtils.currentOrNull;
23import static org.jclouds.util.SaxUtils.equalsOrSuffix;
24 
25import java.util.Map;
26 
27import javax.inject.Named;
28import javax.inject.Provider;
29 
30import org.jclouds.ovf.Section;
31import org.jclouds.ovf.internal.BaseVirtualSystem;
32import org.jclouds.ovf.xml.OperatingSystemSectionHandler;
33import org.jclouds.ovf.xml.ProductSectionHandler;
34import org.jclouds.ovf.xml.SectionHandler;
35import org.jclouds.ovf.xml.VirtualHardwareSectionHandler;
36import org.xml.sax.Attributes;
37import org.xml.sax.SAXException;
38 
39import com.google.common.collect.ImmutableMap;
40import com.google.inject.Inject;
41 
42/**
43 * @author Adrian Cole
44 */
45public 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}

[all classes][org.jclouds.ovf.xml.internal]
EMMA 2.0.5312 (C) Vladimir Roubtsov