| 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.trmk.vcloud_0_8.xml; |
| 20 | |
| 21 | import static org.jclouds.trmk.vcloud_0_8.util.Utils.newReferenceType; |
| 22 | import static org.jclouds.trmk.vcloud_0_8.util.Utils.putReferenceType; |
| 23 | import static org.jclouds.util.SaxUtils.cleanseAttributes; |
| 24 | import static org.jclouds.util.SaxUtils.currentOrNull; |
| 25 | import static org.jclouds.util.SaxUtils.equalsOrSuffix; |
| 26 | |
| 27 | import java.util.Map; |
| 28 | |
| 29 | import org.jclouds.http.functions.ParseSax; |
| 30 | import org.jclouds.trmk.vcloud_0_8.TerremarkVCloudMediaType; |
| 31 | import org.jclouds.trmk.vcloud_0_8.domain.ReferenceType; |
| 32 | import org.jclouds.trmk.vcloud_0_8.domain.VDC; |
| 33 | import org.jclouds.trmk.vcloud_0_8.domain.internal.VDCImpl; |
| 34 | import org.jclouds.trmk.vcloud_0_8.util.Utils; |
| 35 | import org.xml.sax.Attributes; |
| 36 | import org.xml.sax.SAXException; |
| 37 | |
| 38 | import com.google.common.collect.Maps; |
| 39 | |
| 40 | /** |
| 41 | * @author Adrian Cole |
| 42 | */ |
| 43 | public class VDCHandler extends ParseSax.HandlerWithResult<VDC> { |
| 44 | |
| 45 | protected StringBuilder currentText = new StringBuilder(); |
| 46 | |
| 47 | protected ReferenceType vDC; |
| 48 | protected String description; |
| 49 | |
| 50 | protected Map<String, ReferenceType> resourceEntities = Maps.newLinkedHashMap(); |
| 51 | protected Map<String, ReferenceType> availableNetworks = Maps.newLinkedHashMap(); |
| 52 | |
| 53 | private ReferenceType catalog; |
| 54 | private ReferenceType publicIps; |
| 55 | private ReferenceType internetServices; |
| 56 | |
| 57 | public VDC getResult() { |
| 58 | return new VDCImpl(vDC.getName(), vDC.getType(), vDC.getHref(), description, catalog, publicIps, |
| 59 | internetServices, resourceEntities, availableNetworks); |
| 60 | } |
| 61 | |
| 62 | @Override |
| 63 | public void startElement(String uri, String localName, String qName, Attributes attrs) throws SAXException { |
| 64 | Map<String, String> attributes = cleanseAttributes(attrs); |
| 65 | super.startElement(uri, localName, qName, attrs); |
| 66 | if (qName.endsWith("Vdc")) { |
| 67 | vDC = newReferenceType(attributes); |
| 68 | } else if (qName.endsWith("Network")) { |
| 69 | putReferenceType(availableNetworks, attributes); |
| 70 | } else if (qName.endsWith("ResourceEntity")) { |
| 71 | putReferenceType(resourceEntities, attributes); |
| 72 | } else if (equalsOrSuffix(qName, "Link")) { |
| 73 | String name = attributes.get("name"); |
| 74 | if (name.equals("Internet Services")) { |
| 75 | internetServices = Utils.newReferenceType(attributes); |
| 76 | } else if (name.equals("Public IPs")) { |
| 77 | publicIps = Utils.newReferenceType(attributes); |
| 78 | } else { |
| 79 | String type = attributes.get("type"); |
| 80 | if (type.equals(TerremarkVCloudMediaType.CATALOG_XML)) { |
| 81 | catalog = Utils.newReferenceType(attributes); |
| 82 | } |
| 83 | } |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | public void endElement(String uri, String name, String qName) { |
| 88 | if (equalsOrSuffix(qName, "Description")) { |
| 89 | description = currentOrNull(currentText); |
| 90 | } |
| 91 | currentText = new StringBuilder(); |
| 92 | } |
| 93 | |
| 94 | public void characters(char ch[], int start, int length) { |
| 95 | currentText.append(ch, start, length); |
| 96 | } |
| 97 | } |