labeled unparsed ovf sections

This commit is contained in:
Adrian Cole 2011-03-22 02:41:08 -07:00
parent da29745ac2
commit 26a3a2f9de
4 changed files with 90 additions and 75 deletions

View File

@ -23,7 +23,10 @@ import static com.google.common.base.Preconditions.checkNotNull;
import java.util.Set;
import com.google.common.collect.ImmutableMultimap;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.LinkedHashMultimap;
import com.google.common.collect.Multimap;
import com.google.common.collect.Sets;
/**
@ -46,7 +49,7 @@ public class Envelope {
protected Set<DiskSection> diskSections = Sets.newLinkedHashSet();
protected Set<NetworkSection> networkSections = Sets.newLinkedHashSet();
@SuppressWarnings("unchecked")
protected Set<Section> additionalSections = Sets.newLinkedHashSet();
protected Multimap<String, Section> additionalSections = LinkedHashMultimap.create();
protected VirtualSystem virtualSystem;
/**
@ -85,8 +88,8 @@ public class Envelope {
* @see Envelope#getAdditionalSections
*/
@SuppressWarnings("unchecked")
public Builder additionalSection(Section additionalSection) {
this.additionalSections.add(checkNotNull(additionalSection, "additionalSection"));
public Builder additionalSection(String name, Section additionalSection) {
this.additionalSections.put(checkNotNull(name, "name"), checkNotNull(additionalSection, "additionalSection"));
return this;
}
@ -94,9 +97,9 @@ public class Envelope {
* @see Envelope#getAdditionalSections
*/
@SuppressWarnings("unchecked")
public Builder additionalSections(Iterable<? extends Section> additionalSections) {
this.additionalSections = ImmutableSet
.<Section> copyOf(checkNotNull(additionalSections, "additionalSections"));
public Builder additionalSections(Multimap<String, Section> additionalSections) {
this.additionalSections = ImmutableMultimap.<String, Section> copyOf(checkNotNull(additionalSections,
"additionalSections"));
return this;
}
@ -116,8 +119,8 @@ public class Envelope {
}
public Builder fromEnvelope(Envelope in) {
return virtualSystem(in.getVirtualSystem()).diskSections(in.getDiskSections()).networkSections(
networkSections).additionalSections(in.getAdditionalSections());
return virtualSystem(in.getVirtualSystem()).diskSections(in.getDiskSections())
.networkSections(networkSections).additionalSections(in.getAdditionalSections());
}
}
@ -125,15 +128,15 @@ public class Envelope {
private final Set<DiskSection> diskSections;
private final Set<NetworkSection> networkSections;
@SuppressWarnings("unchecked")
private final Set<? extends Section> additionalSections;
private final Multimap<String, Section> additionalSections;
private final VirtualSystem virtualSystem;
@SuppressWarnings("unchecked")
public Envelope(Iterable<? extends DiskSection> diskSections, Iterable<? extends NetworkSection> networkSections,
Iterable<? extends Section> additionalSections, VirtualSystem virtualSystem) {
Multimap<String, Section> additionalSections, VirtualSystem virtualSystem) {
this.diskSections = ImmutableSet.copyOf(checkNotNull(diskSections, "diskSections"));
this.networkSections = ImmutableSet.copyOf(checkNotNull(networkSections, "networkSections"));
this.additionalSections = ImmutableSet.copyOf(checkNotNull(additionalSections, "additionalSections"));
this.additionalSections = ImmutableMultimap.copyOf(checkNotNull(additionalSections, "additionalSections"));
this.virtualSystem = checkNotNull(virtualSystem, "virtualSystem");
}
@ -146,7 +149,7 @@ public class Envelope {
}
@SuppressWarnings("unchecked")
public Set<? extends Section> getAdditionalSections() {
public Multimap<String, Section> getAdditionalSections() {
return additionalSections;
}

View File

@ -23,7 +23,10 @@ import static com.google.common.base.Preconditions.checkNotNull;
import java.util.Set;
import com.google.common.collect.ImmutableMultimap;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.LinkedHashMultimap;
import com.google.common.collect.Multimap;
import com.google.common.collect.Sets;
/**
@ -50,7 +53,7 @@ public class VirtualSystem extends Section<VirtualSystem> {
protected OperatingSystemSection operatingSystem;
protected Set<VirtualHardwareSection> hardwareSections = Sets.newLinkedHashSet();
@SuppressWarnings("unchecked")
protected Set<Section> additionalSections = Sets.newLinkedHashSet();
protected Multimap<String, Section> additionalSections = LinkedHashMultimap.create();
/**
* @see VirtualSystem#getName
@ -97,8 +100,8 @@ public class VirtualSystem extends Section<VirtualSystem> {
* @see VirtualSystem#getAdditionalSections
*/
@SuppressWarnings("unchecked")
public Builder additionalSection(Section additionalSection) {
this.additionalSections.add(checkNotNull(additionalSection, "additionalSection"));
public Builder additionalSection(String name, Section additionalSection) {
this.additionalSections.put(checkNotNull(name, "name"), checkNotNull(additionalSection, "additionalSection"));
return this;
}
@ -106,9 +109,9 @@ public class VirtualSystem extends Section<VirtualSystem> {
* @see VirtualSystem#getAdditionalSections
*/
@SuppressWarnings("unchecked")
public Builder additionalSections(Iterable<? extends Section> additionalSections) {
this.additionalSections = ImmutableSet
.<Section> copyOf(checkNotNull(additionalSections, "additionalSections"));
public Builder additionalSections(Multimap<String, Section> additionalSections) {
this.additionalSections = ImmutableMultimap.<String, Section> copyOf(checkNotNull(additionalSections,
"additionalSections"));
return this;
}
@ -149,17 +152,17 @@ public class VirtualSystem extends Section<VirtualSystem> {
private final OperatingSystemSection operatingSystem;
private final Set<VirtualHardwareSection> hardwareSections;
@SuppressWarnings("unchecked")
private final Set<? extends Section> additionalSections;
private final Multimap<String, Section> additionalSections;
@SuppressWarnings("unchecked")
public VirtualSystem(String id, String info, String name, OperatingSystemSection operatingSystem,
Iterable<? extends VirtualHardwareSection> hardwareSections, Iterable<? extends Section> additionalSections) {
Iterable<? extends VirtualHardwareSection> hardwareSections, Multimap<String, Section> additionalSections) {
super(info);
this.id = id;
this.name = name;
this.operatingSystem = checkNotNull(operatingSystem, "operatingSystem");
this.hardwareSections = ImmutableSet.copyOf(checkNotNull(hardwareSections, "hardwareSections"));
this.additionalSections = ImmutableSet.copyOf(checkNotNull(additionalSections, "additionalSections"));
this.additionalSections = ImmutableMultimap.copyOf(checkNotNull(additionalSections, "additionalSections"));
}
public String getId() {
@ -183,7 +186,7 @@ public class VirtualSystem extends Section<VirtualSystem> {
}
@SuppressWarnings("unchecked")
public Set<? extends Section> getAdditionalSections() {
public Multimap<String, Section> getAdditionalSections() {
return additionalSections;
}

View File

@ -39,7 +39,6 @@ import com.google.inject.Inject;
*/
public class EnvelopeHandler extends ParseSax.HandlerWithResult<Envelope> {
protected StringBuilder currentText = new StringBuilder();
protected Envelope.Builder builder = Envelope.builder();
public Envelope getResult() {
@ -80,18 +79,23 @@ public class EnvelopeHandler extends ParseSax.HandlerWithResult<Envelope> {
private boolean inSection;
private boolean inExtensionSection;
private int depth = 0;
public void startElement(String uri, String localName, String qName, Attributes attrs) throws SAXException {
if (equalsOrSuffix(qName, "DiskSection")) {
inDisk = true;
} else if (equalsOrSuffix(qName, "NetworkSection")) {
inNetwork = true;
} else if (equalsOrSuffix(qName, "VirtualSystem")) {
inVirtualSystem = true;
} else if (extensionHandlers.containsKey(qName)) {
inExtensionSection = true;
extensionHandler = extensionHandlers.get(qName).get();
} else if (qName.endsWith("Section")) {
inSection = true;
depth++;
if (depth == 2) {
if (equalsOrSuffix(qName, "DiskSection")) {
inDisk = true;
} else if (equalsOrSuffix(qName, "NetworkSection")) {
inNetwork = true;
} else if (equalsOrSuffix(qName, "VirtualSystem")) {
inVirtualSystem = true;
} else if (extensionHandlers.containsKey(qName)) {
inExtensionSection = true;
extensionHandler = extensionHandlers.get(qName).get();
} else if (qName.endsWith("Section")) {
inSection = true;
}
}
if (inDisk) {
@ -110,21 +114,24 @@ public class EnvelopeHandler extends ParseSax.HandlerWithResult<Envelope> {
@Override
public void endElement(String uri, String localName, String qName) {
if (equalsOrSuffix(qName, "DiskSection")) {
inDisk = false;
builder.diskSection(diskHandler.getResult());
} else if (equalsOrSuffix(qName, "NetworkSection")) {
inNetwork = false;
builder.networkSection(networkHandler.getResult());
} else if (equalsOrSuffix(qName, "VirtualSystem")) {
inVirtualSystem = false;
builder.virtualSystem(virtualSystemHandler.getResult());
} else if (extensionHandlers.containsKey(qName)) {
builder.additionalSection(extensionHandler.getResult());
inExtensionSection = false;
} else if (qName.endsWith("Section")) {
builder.additionalSection(defaultSectionHandler.getResult());
inSection = false;
depth--;
if (depth == 1) {
if (equalsOrSuffix(qName, "DiskSection")) {
inDisk = false;
builder.diskSection(diskHandler.getResult());
} else if (equalsOrSuffix(qName, "NetworkSection")) {
inNetwork = false;
builder.networkSection(networkHandler.getResult());
} else if (equalsOrSuffix(qName, "VirtualSystem")) {
inVirtualSystem = false;
builder.virtualSystem(virtualSystemHandler.getResult());
} else if (extensionHandlers.containsKey(qName)) {
builder.additionalSection(qName, extensionHandler.getResult());
inExtensionSection = false;
} else if (qName.endsWith("Section")) {
builder.additionalSection(qName, defaultSectionHandler.getResult());
inSection = false;
}
}
if (inDisk) {
@ -137,8 +144,6 @@ public class EnvelopeHandler extends ParseSax.HandlerWithResult<Envelope> {
extensionHandler.endElement(uri, localName, qName);
} else if (inSection) {
defaultSectionHandler.endElement(uri, localName, qName);
} else {
currentText = new StringBuilder();
}
}
@ -154,8 +159,6 @@ public class EnvelopeHandler extends ParseSax.HandlerWithResult<Envelope> {
extensionHandler.characters(ch, start, length);
} else if (inSection) {
defaultSectionHandler.characters(ch, start, length);
} else {
currentText.append(ch, start, length);
}
}

View File

@ -67,20 +67,23 @@ public class VirtualSystemHandler extends SectionHandler<VirtualSystem, VirtualS
private boolean inOs;
private boolean inSection;
private boolean inExtensionSection;
private int depth;
public void startElement(String uri, String localName, String qName, Attributes attrs) throws SAXException {
Map<String, String> attributes = cleanseAttributes(attrs);
if (equalsOrSuffix(qName, "VirtualHardwareSection")) {
inHardware = true;
} else if (equalsOrSuffix(qName, "OperatingSystemSection")) {
inOs = true;
} else if (extensionHandlers.containsKey(qName)) {
inExtensionSection = true;
extensionHandler = extensionHandlers.get(qName).get();
} else if (qName.endsWith("Section")) {
inSection = true;
depth++;
if (depth == 2) {
if (equalsOrSuffix(qName, "VirtualHardwareSection")) {
inHardware = true;
} else if (equalsOrSuffix(qName, "OperatingSystemSection")) {
inOs = true;
} else if (extensionHandlers.containsKey(qName)) {
inExtensionSection = true;
extensionHandler = extensionHandlers.get(qName).get();
} else if (qName.endsWith("Section")) {
inSection = true;
}
}
if (inHardware) {
hardwareHandler.startElement(uri, localName, qName, attrs);
} else if (inOs) {
@ -97,18 +100,21 @@ public class VirtualSystemHandler extends SectionHandler<VirtualSystem, VirtualS
@Override
public void endElement(String uri, String localName, String qName) {
if (equalsOrSuffix(qName, "VirtualHardwareSection")) {
inHardware = false;
builder.hardwareSection(hardwareHandler.getResult());
} else if (equalsOrSuffix(qName, "OperatingSystemSection")) {
inOs = false;
builder.operatingSystemSection(osHandler.getResult());
} else if (extensionHandlers.containsKey(qName)) {
builder.additionalSection(extensionHandler.getResult());
inExtensionSection = false;
} else if (qName.endsWith("Section")) {
builder.additionalSection(defaultSectionHandler.getResult());
inSection = false;
depth--;
if (depth == 1) {
if (equalsOrSuffix(qName, "VirtualHardwareSection")) {
inHardware = false;
builder.hardwareSection(hardwareHandler.getResult());
} else if (equalsOrSuffix(qName, "OperatingSystemSection")) {
inOs = false;
builder.operatingSystemSection(osHandler.getResult());
} else if (extensionHandlers.containsKey(qName)) {
builder.additionalSection(qName, extensionHandler.getResult());
inExtensionSection = false;
} else if (qName.endsWith("Section")) {
builder.additionalSection(qName, defaultSectionHandler.getResult());
inSection = false;
}
}
if (inHardware) {