| 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.savvis.vpdc.xml; |
| 20 | |
| 21 | import static org.jclouds.savvis.vpdc.util.Utils.cleanseAttributes; |
| 22 | import static org.jclouds.savvis.vpdc.util.Utils.currentOrNull; |
| 23 | import static org.jclouds.savvis.vpdc.util.Utils.newResource; |
| 24 | import static org.jclouds.util.SaxUtils.equalsOrSuffix; |
| 25 | |
| 26 | import java.util.Map; |
| 27 | |
| 28 | import org.jclouds.http.functions.ParseSax; |
| 29 | import org.jclouds.savvis.vpdc.domain.Resource; |
| 30 | import org.jclouds.savvis.vpdc.domain.VDC; |
| 31 | import org.jclouds.savvis.vpdc.domain.VDC.Status; |
| 32 | import org.xml.sax.Attributes; |
| 33 | import org.xml.sax.SAXException; |
| 34 | |
| 35 | import com.google.common.collect.ImmutableMap; |
| 36 | |
| 37 | /** |
| 38 | * @author Adrian Cole |
| 39 | */ |
| 40 | public class VDCHandler extends ParseSax.HandlerWithResult<VDC> { |
| 41 | |
| 42 | protected StringBuilder currentText = new StringBuilder(); |
| 43 | |
| 44 | protected VDC.Builder builder = VDC.builder(); |
| 45 | |
| 46 | public VDC getResult() { |
| 47 | try { |
| 48 | return builder.build(); |
| 49 | } finally { |
| 50 | builder = VDC.builder(); |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | @Override |
| 55 | public void startElement(String uri, String localName, String qName, Attributes attrs) throws SAXException { |
| 56 | Map<String, String> attributes = cleanseAttributes(attrs); |
| 57 | if (equalsOrSuffix(qName, "Vdc")) { |
| 58 | // savvis doesn't add href in the header for some reason |
| 59 | if (!attributes.containsKey("href") && getRequest() != null) |
| 60 | attributes = ImmutableMap.<String, String> builder().putAll(attributes) |
| 61 | .put("href", getRequest().getEndpoint().toASCIIString()).build(); |
| 62 | Resource vDC = newResource(attributes); |
| 63 | builder.name(vDC.getName()).type(vDC.getType()).id(vDC.getId()).href(vDC.getHref()); |
| 64 | } else if (equalsOrSuffix(qName, "Network")) { |
| 65 | builder.availableNetwork(newResource(attributes)); |
| 66 | } else if (equalsOrSuffix(qName, "ResourceEntity")) { |
| 67 | builder.resourceEntity(newResource(attributes)); |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | public void endElement(String uri, String name, String qName) { |
| 72 | if (equalsOrSuffix(qName, "Description")) { |
| 73 | builder.description(currentOrNull(currentText)); |
| 74 | } else if (equalsOrSuffix(qName, "OfferingTag")) { |
| 75 | builder.status(Status.fromValue(currentOrNull(currentText))); |
| 76 | } |
| 77 | currentText = new StringBuilder(); |
| 78 | } |
| 79 | |
| 80 | public void characters(char ch[], int start, int length) { |
| 81 | currentText.append(ch, start, length); |
| 82 | } |
| 83 | } |