| 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 | import java.util.Set; |
| 26 | |
| 27 | import javax.inject.Inject; |
| 28 | |
| 29 | import org.jclouds.http.functions.ParseSax; |
| 30 | import org.jclouds.util.SaxUtils; |
| 31 | import org.jclouds.vcloud.domain.ReferenceType; |
| 32 | import org.jclouds.vcloud.domain.Status; |
| 33 | import org.jclouds.vcloud.domain.Task; |
| 34 | import org.jclouds.vcloud.domain.VAppTemplate; |
| 35 | import org.jclouds.vcloud.domain.Vm; |
| 36 | import org.jclouds.vcloud.domain.internal.VAppTemplateImpl; |
| 37 | import org.jclouds.vcloud.domain.ovf.VCloudNetworkSection; |
| 38 | import org.jclouds.vcloud.xml.ovf.VCloudNetworkSectionHandler; |
| 39 | import org.xml.sax.Attributes; |
| 40 | import org.xml.sax.SAXException; |
| 41 | |
| 42 | import com.google.common.collect.Lists; |
| 43 | import com.google.common.collect.Sets; |
| 44 | |
| 45 | /** |
| 46 | * @author Adrian Cole |
| 47 | */ |
| 48 | public class VAppTemplateHandler extends ParseSax.HandlerWithResult<VAppTemplate> { |
| 49 | |
| 50 | protected final TaskHandler taskHandler; |
| 51 | protected final VmHandler vmHandler; |
| 52 | protected final VCloudNetworkSectionHandler networkSectionHandler; |
| 53 | |
| 54 | @Inject |
| 55 | public VAppTemplateHandler(TaskHandler taskHandler, VmHandler vmHandler, |
| 56 | VCloudNetworkSectionHandler networkSectionHandler) { |
| 57 | this.taskHandler = taskHandler; |
| 58 | this.vmHandler = vmHandler; |
| 59 | this.networkSectionHandler = networkSectionHandler; |
| 60 | } |
| 61 | |
| 62 | protected StringBuilder currentText = new StringBuilder(); |
| 63 | |
| 64 | protected ReferenceType template; |
| 65 | protected Status status; |
| 66 | protected ReferenceType vdc; |
| 67 | protected String description; |
| 68 | protected List<Task> tasks = Lists.newArrayList(); |
| 69 | protected boolean ovfDescriptorUploaded = true; |
| 70 | protected String vAppScopedLocalId; |
| 71 | |
| 72 | private boolean inChildren; |
| 73 | private boolean inTasks; |
| 74 | private boolean inNetworkSection; |
| 75 | protected Set<Vm> children = Sets.newLinkedHashSet(); |
| 76 | private VCloudNetworkSection networkSection; |
| 77 | |
| 78 | public VAppTemplate getResult() { |
| 79 | return new VAppTemplateImpl(template.getName(), template.getType(), template.getHref(), status, vdc, description, |
| 80 | tasks, ovfDescriptorUploaded, vAppScopedLocalId, children, networkSection); |
| 81 | } |
| 82 | |
| 83 | @Override |
| 84 | public void startElement(String uri, String localName, String qName, Attributes attrs) throws SAXException { |
| 85 | Map<String, String> attributes = SaxUtils.cleanseAttributes(attrs); |
| 86 | if (qName.endsWith("Children")) { |
| 87 | inChildren = true; |
| 88 | } else if (qName.endsWith("Tasks")) { |
| 89 | inTasks = true; |
| 90 | } else if (qName.endsWith("NetworkSection")) { |
| 91 | inNetworkSection = true; |
| 92 | } |
| 93 | if (inChildren) { |
| 94 | vmHandler.startElement(uri, localName, qName, attrs); |
| 95 | } else if (inTasks) { |
| 96 | taskHandler.startElement(uri, localName, qName, attrs); |
| 97 | } else if (inNetworkSection) { |
| 98 | networkSectionHandler.startElement(uri, localName, qName, attrs); |
| 99 | } else if (qName.equals("VAppTemplate")) { |
| 100 | template = newReferenceType(attributes); |
| 101 | if (attributes.containsKey("status")) |
| 102 | this.status = Status.fromValue(Integer.parseInt(attributes.get("status"))); |
| 103 | } else if (qName.equals("Link") && "up".equals(attributes.get("rel"))) { |
| 104 | vdc = newReferenceType(attributes); |
| 105 | } |
| 106 | |
| 107 | } |
| 108 | |
| 109 | public void endElement(String uri, String name, String qName) { |
| 110 | if (qName.endsWith("Children")) { |
| 111 | inChildren = false; |
| 112 | Vm vm = vmHandler.getResult(); |
| 113 | if (vm != null) |
| 114 | this.children.add(vmHandler.getResult()); |
| 115 | } else if (qName.endsWith("Tasks")) { |
| 116 | inTasks = false; |
| 117 | this.tasks.add(taskHandler.getResult()); |
| 118 | } else if (qName.endsWith("NetworkSection")) { |
| 119 | inNetworkSection = false; |
| 120 | this.networkSection = networkSectionHandler.getResult(); |
| 121 | } |
| 122 | if (inChildren) { |
| 123 | vmHandler.endElement(uri, name, qName); |
| 124 | } else if (inTasks) { |
| 125 | taskHandler.endElement(uri, name, qName); |
| 126 | } else if (inNetworkSection) { |
| 127 | networkSectionHandler.endElement(uri, name, qName); |
| 128 | } else if (qName.equals("Description")) { |
| 129 | description = currentOrNull(); |
| 130 | } else if (qName.equals("VAppScopedLocalId")) { |
| 131 | vAppScopedLocalId = currentOrNull(); |
| 132 | } else if (qName.equals("ovfDescriptorUploaded")) { |
| 133 | ovfDescriptorUploaded = Boolean.parseBoolean(currentOrNull()); |
| 134 | } |
| 135 | currentText = new StringBuilder(); |
| 136 | } |
| 137 | |
| 138 | public void characters(char ch[], int start, int length) { |
| 139 | if (inTasks) |
| 140 | taskHandler.characters(ch, start, length); |
| 141 | else if (inChildren) |
| 142 | vmHandler.characters(ch, start, length); |
| 143 | else if (inNetworkSection) |
| 144 | networkSectionHandler.characters(ch, start, length); |
| 145 | else |
| 146 | currentText.append(ch, start, length); |
| 147 | } |
| 148 | |
| 149 | protected String currentOrNull() { |
| 150 | String returnVal = currentText.toString().trim(); |
| 151 | return returnVal.equals("") ? null : returnVal; |
| 152 | } |
| 153 | } |