| 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 | |
| 23 | import java.util.Map; |
| 24 | import java.util.SortedMap; |
| 25 | |
| 26 | import org.jclouds.http.functions.ParseSax; |
| 27 | import org.jclouds.trmk.vcloud_0_8.domain.CatalogItem; |
| 28 | import org.jclouds.trmk.vcloud_0_8.domain.ReferenceType; |
| 29 | import org.jclouds.trmk.vcloud_0_8.domain.internal.CatalogItemImpl; |
| 30 | import org.jclouds.util.SaxUtils; |
| 31 | import org.xml.sax.Attributes; |
| 32 | import org.xml.sax.SAXException; |
| 33 | |
| 34 | import com.google.common.collect.Maps; |
| 35 | |
| 36 | /** |
| 37 | * @author Adrian Cole |
| 38 | */ |
| 39 | public class CatalogItemHandler extends ParseSax.HandlerWithResult<CatalogItem> { |
| 40 | private StringBuilder currentText = new StringBuilder(); |
| 41 | |
| 42 | protected ReferenceType catalogItem; |
| 43 | protected ReferenceType entity; |
| 44 | |
| 45 | protected String description; |
| 46 | protected String key; |
| 47 | protected SortedMap<String, String> properties = Maps.newTreeMap(); |
| 48 | private ReferenceType customizationOptions; |
| 49 | private ReferenceType computeOptions; |
| 50 | |
| 51 | public CatalogItem getResult() { |
| 52 | return new CatalogItemImpl(catalogItem.getName(), catalogItem.getHref(), description, computeOptions, |
| 53 | customizationOptions, entity, properties); |
| 54 | } |
| 55 | |
| 56 | @Override |
| 57 | public void startElement(String uri, String localName, String qName, Attributes attrs) throws SAXException { |
| 58 | Map<String, String> attributes = SaxUtils.cleanseAttributes(attrs); |
| 59 | if (qName.equals("CatalogItem")) { |
| 60 | catalogItem = newReferenceType(attributes); |
| 61 | } else if (qName.equals("Entity")) { |
| 62 | entity = newReferenceType(attributes); |
| 63 | } else if (qName.equals("Property")) { |
| 64 | key = attributes.get("key"); |
| 65 | } else if (qName.equals("Link")) { |
| 66 | if (attributes.containsKey("name")) { |
| 67 | if (attributes.get("name").equals("Customization Options")) { |
| 68 | customizationOptions = newReferenceType(attributes); |
| 69 | } else if (attributes.get("name").equals("Compute Options")) { |
| 70 | computeOptions = newReferenceType(attributes); |
| 71 | } |
| 72 | } |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | public void endElement(String uri, String name, String qName) { |
| 77 | if (qName.equals("Description")) { |
| 78 | description = currentOrNull(); |
| 79 | } else if (qName.equals("Property")) { |
| 80 | properties.put(key, currentOrNull()); |
| 81 | key = null; |
| 82 | } |
| 83 | currentText = new StringBuilder(); |
| 84 | } |
| 85 | |
| 86 | public void characters(char ch[], int start, int length) { |
| 87 | currentText.append(ch, start, length); |
| 88 | } |
| 89 | |
| 90 | protected String currentOrNull() { |
| 91 | String returnVal = currentText.toString().trim(); |
| 92 | return returnVal.equals("") ? null : returnVal; |
| 93 | } |
| 94 | } |