| 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.vcloud.xml; |
| 20 | |
| 21 | import static org.jclouds.vcloud.util.Utils.newReferenceType; |
| 22 | |
| 23 | import java.util.List; |
| 24 | import java.util.Map; |
| 25 | |
| 26 | import javax.inject.Inject; |
| 27 | |
| 28 | import org.jclouds.http.functions.ParseSax; |
| 29 | import org.jclouds.util.SaxUtils; |
| 30 | import org.jclouds.vcloud.domain.GuestCustomizationSection; |
| 31 | import org.jclouds.vcloud.domain.NetworkConnectionSection; |
| 32 | import org.jclouds.vcloud.domain.ReferenceType; |
| 33 | import org.jclouds.vcloud.domain.Status; |
| 34 | import org.jclouds.vcloud.domain.Task; |
| 35 | import org.jclouds.vcloud.domain.Vm; |
| 36 | import org.jclouds.vcloud.domain.internal.VmImpl; |
| 37 | import org.jclouds.vcloud.domain.ovf.VCloudOperatingSystemSection; |
| 38 | import org.jclouds.vcloud.domain.ovf.VCloudVirtualHardwareSection; |
| 39 | import org.jclouds.vcloud.xml.ovf.VCloudOperatingSystemHandler; |
| 40 | import org.xml.sax.Attributes; |
| 41 | import org.xml.sax.SAXException; |
| 42 | |
| 43 | import com.google.common.collect.Lists; |
| 44 | |
| 45 | /** |
| 46 | * @author Adrian Cole |
| 47 | */ |
| 48 | public class VmHandler extends ParseSax.HandlerWithResult<Vm> { |
| 49 | |
| 50 | protected final TaskHandler taskHandler; |
| 51 | protected final VCloudVirtualHardwareHandler virtualHardwareHandler; |
| 52 | protected final VCloudOperatingSystemHandler operatingSystemHandler; |
| 53 | protected final GuestCustomizationSectionHandler guestCustomizationHandler; |
| 54 | protected final NetworkConnectionSectionHandler networkConnectionSectionHandler; |
| 55 | |
| 56 | @Inject |
| 57 | public VmHandler(TaskHandler taskHandler, VCloudVirtualHardwareHandler virtualHardwareHandler, |
| 58 | VCloudOperatingSystemHandler operatingSystemHandler, |
| 59 | NetworkConnectionSectionHandler networkConnectionSectionHandler, |
| 60 | GuestCustomizationSectionHandler guestCustomizationHandler) { |
| 61 | this.taskHandler = taskHandler; |
| 62 | this.virtualHardwareHandler = virtualHardwareHandler; |
| 63 | this.operatingSystemHandler = operatingSystemHandler; |
| 64 | this.networkConnectionSectionHandler = networkConnectionSectionHandler; |
| 65 | this.guestCustomizationHandler = guestCustomizationHandler; |
| 66 | } |
| 67 | |
| 68 | protected StringBuilder currentText = new StringBuilder(); |
| 69 | |
| 70 | protected ReferenceType vm; |
| 71 | protected Status status; |
| 72 | protected ReferenceType vdc; |
| 73 | protected String description; |
| 74 | protected List<Task> tasks = Lists.newArrayList(); |
| 75 | protected VCloudVirtualHardwareSection hardware; |
| 76 | protected VCloudOperatingSystemSection os; |
| 77 | protected NetworkConnectionSection networkConnectionSection; |
| 78 | protected GuestCustomizationSection guestCustomization; |
| 79 | protected String vAppScopedLocalId; |
| 80 | |
| 81 | private boolean inTasks; |
| 82 | private boolean inHardware; |
| 83 | private boolean inOs; |
| 84 | private boolean inNetworkConnectionSection; |
| 85 | private boolean inGuestCustomization; |
| 86 | |
| 87 | public Vm getResult() { |
| 88 | return vm == null ? null : new VmImpl(vm.getName(), vm.getType(), vm.getHref(), status, vdc, description, tasks, |
| 89 | hardware, os, networkConnectionSection, guestCustomization, vAppScopedLocalId); |
| 90 | } |
| 91 | |
| 92 | @Override |
| 93 | public void startElement(String uri, String localName, String qName, Attributes attrs) throws SAXException { |
| 94 | Map<String, String> attributes = SaxUtils.cleanseAttributes(attrs); |
| 95 | if (qName.endsWith("VirtualHardwareSection")) { |
| 96 | inHardware = true; |
| 97 | } else if (qName.endsWith("OperatingSystemSection")) { |
| 98 | inOs = true; |
| 99 | } else if (qName.endsWith("GuestCustomizationSection")) { |
| 100 | inGuestCustomization = true; |
| 101 | } else if (qName.endsWith("NetworkConnectionSection")) { |
| 102 | inNetworkConnectionSection = true; |
| 103 | } else if (qName.endsWith("Tasks")) { |
| 104 | inTasks = true; |
| 105 | } |
| 106 | if (inHardware) { |
| 107 | virtualHardwareHandler.startElement(uri, localName, qName, attrs); |
| 108 | } else if (inOs) { |
| 109 | operatingSystemHandler.startElement(uri, localName, qName, attrs); |
| 110 | } else if (inNetworkConnectionSection) { |
| 111 | networkConnectionSectionHandler.startElement(uri, localName, qName, attrs); |
| 112 | } else if (inGuestCustomization) { |
| 113 | guestCustomizationHandler.startElement(uri, localName, qName, attrs); |
| 114 | } else if (inTasks) { |
| 115 | taskHandler.startElement(uri, localName, qName, attrs); |
| 116 | } else if (qName.equals("Vm")) { |
| 117 | vm = newReferenceType(attributes); |
| 118 | String status = attributes.get("status"); |
| 119 | if (status != null) |
| 120 | this.status = Status.fromValue(Integer.parseInt(status)); |
| 121 | } else if (qName.equals("Link") && "up".equals(attributes.get("rel"))) { |
| 122 | vdc = newReferenceType(attributes); |
| 123 | } |
| 124 | } |
| 125 | |
| 126 | public void endElement(String uri, String name, String qName) { |
| 127 | if (qName.endsWith("VirtualHardwareSection")) { |
| 128 | inHardware = false; |
| 129 | this.hardware = virtualHardwareHandler.getResult(); |
| 130 | } else if (qName.endsWith("OperatingSystemSection")) { |
| 131 | inOs = false; |
| 132 | os = operatingSystemHandler.getResult(); |
| 133 | } else if (qName.endsWith("NetworkConnectionSection")) { |
| 134 | inNetworkConnectionSection = false; |
| 135 | networkConnectionSection = networkConnectionSectionHandler.getResult(); |
| 136 | } else if (qName.endsWith("GuestCustomizationSection")) { |
| 137 | inGuestCustomization = false; |
| 138 | guestCustomization = guestCustomizationHandler.getResult(); |
| 139 | } else if (qName.endsWith("Tasks")) { |
| 140 | inTasks = false; |
| 141 | this.tasks.add(taskHandler.getResult()); |
| 142 | } |
| 143 | if (inHardware) { |
| 144 | virtualHardwareHandler.endElement(uri, name, qName); |
| 145 | } else if (inOs) { |
| 146 | operatingSystemHandler.endElement(uri, name, qName); |
| 147 | } else if (inGuestCustomization) { |
| 148 | guestCustomizationHandler.endElement(uri, name, qName); |
| 149 | } else if (inNetworkConnectionSection) { |
| 150 | networkConnectionSectionHandler.endElement(uri, name, qName); |
| 151 | } else if (inTasks) { |
| 152 | taskHandler.endElement(uri, name, qName); |
| 153 | } else if (qName.equals("Description")) { |
| 154 | description = currentOrNull(); |
| 155 | } else if (qName.equals("VAppScopedLocalId")) { |
| 156 | vAppScopedLocalId = currentOrNull(); |
| 157 | } |
| 158 | currentText = new StringBuilder(); |
| 159 | } |
| 160 | |
| 161 | public void characters(char ch[], int start, int length) { |
| 162 | if (inHardware) |
| 163 | virtualHardwareHandler.characters(ch, start, length); |
| 164 | else if (inOs) |
| 165 | operatingSystemHandler.characters(ch, start, length); |
| 166 | else if (inGuestCustomization) |
| 167 | guestCustomizationHandler.characters(ch, start, length); |
| 168 | else if (inNetworkConnectionSection) |
| 169 | networkConnectionSectionHandler.characters(ch, start, length); |
| 170 | else if (inTasks) |
| 171 | taskHandler.characters(ch, start, length); |
| 172 | else |
| 173 | currentText.append(ch, start, length); |
| 174 | } |
| 175 | |
| 176 | protected String currentOrNull() { |
| 177 | String returnVal = currentText.toString().trim(); |
| 178 | return returnVal.equals("") ? null : returnVal; |
| 179 | } |
| 180 | } |