| 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 | import static org.jclouds.vcloud.util.Utils.putReferenceType; |
| 23 | |
| 24 | import java.util.List; |
| 25 | import java.util.Map; |
| 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.VCloudMediaType; |
| 32 | import org.jclouds.vcloud.domain.Catalog; |
| 33 | import org.jclouds.vcloud.domain.ReferenceType; |
| 34 | import org.jclouds.vcloud.domain.Task; |
| 35 | import org.jclouds.vcloud.domain.internal.CatalogImpl; |
| 36 | import org.xml.sax.Attributes; |
| 37 | import org.xml.sax.SAXException; |
| 38 | |
| 39 | import com.google.common.collect.Lists; |
| 40 | import com.google.common.collect.Maps; |
| 41 | |
| 42 | /** |
| 43 | * @author Adrian Cole |
| 44 | */ |
| 45 | public class CatalogHandler extends ParseSax.HandlerWithResult<Catalog> { |
| 46 | |
| 47 | protected final TaskHandler taskHandler; |
| 48 | |
| 49 | @Inject |
| 50 | public CatalogHandler(TaskHandler taskHandler) { |
| 51 | this.taskHandler = taskHandler; |
| 52 | } |
| 53 | |
| 54 | private StringBuilder currentText = new StringBuilder(); |
| 55 | |
| 56 | private ReferenceType catalog; |
| 57 | private Map<String, ReferenceType> contents = Maps.newLinkedHashMap(); |
| 58 | protected List<Task> tasks = Lists.newArrayList(); |
| 59 | private String description; |
| 60 | private ReferenceType org; |
| 61 | |
| 62 | private boolean published = true; |
| 63 | private boolean readOnly = true; |
| 64 | |
| 65 | public Catalog getResult() { |
| 66 | return new CatalogImpl(catalog.getName(), catalog.getType(), catalog.getHref(), org, description, contents, |
| 67 | tasks, published, readOnly); |
| 68 | } |
| 69 | |
| 70 | @Override |
| 71 | public void startElement(String uri, String localName, String qName, Attributes attrs) throws SAXException { |
| 72 | Map<String, String> attributes = SaxUtils.cleanseAttributes(attrs); |
| 73 | if (qName.equals("Catalog")) { |
| 74 | catalog = newReferenceType(attributes, VCloudMediaType.CATALOG_XML); |
| 75 | } else if (qName.equals("CatalogItem")) { |
| 76 | putReferenceType(contents, attributes); |
| 77 | } else if (qName.equals("Link") && "up".equals(attributes.get("rel"))) { |
| 78 | org = newReferenceType(attributes); |
| 79 | } else if (qName.equals("Link") && "add".equals(attributes.get("rel"))) { |
| 80 | readOnly = false; |
| 81 | } else { |
| 82 | taskHandler.startElement(uri, localName, qName, attrs); |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | public void endElement(String uri, String name, String qName) { |
| 87 | taskHandler.endElement(uri, name, qName); |
| 88 | if (qName.equals("Task")) { |
| 89 | this.tasks.add(taskHandler.getResult()); |
| 90 | } else if (qName.equals("Description")) { |
| 91 | description = currentOrNull(); |
| 92 | } else if (qName.equals("IsPublished")) { |
| 93 | published = Boolean.parseBoolean(currentOrNull()); |
| 94 | } |
| 95 | currentText = new StringBuilder(); |
| 96 | } |
| 97 | |
| 98 | public void characters(char ch[], int start, int length) { |
| 99 | currentText.append(ch, start, length); |
| 100 | } |
| 101 | |
| 102 | protected String currentOrNull() { |
| 103 | String returnVal = currentText.toString().trim(); |
| 104 | return returnVal.equals("") ? null : returnVal; |
| 105 | } |
| 106 | } |