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