| 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.domain.Org; |
| 32 | import org.jclouds.vcloud.domain.ReferenceType; |
| 33 | import org.jclouds.vcloud.domain.Task; |
| 34 | import org.jclouds.vcloud.domain.internal.OrgImpl; |
| 35 | import org.xml.sax.Attributes; |
| 36 | import org.xml.sax.SAXException; |
| 37 | |
| 38 | import com.google.common.collect.Lists; |
| 39 | import com.google.common.collect.Maps; |
| 40 | |
| 41 | /** |
| 42 | * @author Adrian Cole |
| 43 | */ |
| 44 | public class OrgHandler extends ParseSax.HandlerWithResult<Org> { |
| 45 | |
| 46 | protected final TaskHandler taskHandler; |
| 47 | |
| 48 | @Inject |
| 49 | public OrgHandler(TaskHandler taskHandler) { |
| 50 | this.taskHandler = taskHandler; |
| 51 | } |
| 52 | |
| 53 | private StringBuilder currentText = new StringBuilder(); |
| 54 | |
| 55 | protected ReferenceType org; |
| 56 | protected Map<String, ReferenceType> vdcs = Maps.newLinkedHashMap(); |
| 57 | protected ReferenceType tasksList; |
| 58 | protected Map<String, ReferenceType> catalogs = Maps.newLinkedHashMap(); |
| 59 | protected Map<String, ReferenceType> networks = Maps.newLinkedHashMap(); |
| 60 | protected List<Task> tasks = Lists.newArrayList(); |
| 61 | |
| 62 | protected String description; |
| 63 | protected String fullName; |
| 64 | |
| 65 | public Org getResult() { |
| 66 | return new OrgImpl(org.getName(), org.getType(), org.getHref(), fullName != null ? fullName : org.getName(), |
| 67 | description, catalogs, vdcs, networks, tasksList, tasks); |
| 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.endsWith("Org")) { |
| 74 | org = newReferenceType(attributes); |
| 75 | } else if (qName.endsWith("Link")) { |
| 76 | String type = attributes.get("type"); |
| 77 | if (type != null) { |
| 78 | if (type.indexOf("vdc+xml") != -1) { |
| 79 | putReferenceType(vdcs, attributes); |
| 80 | } else if (type.indexOf("catalog+xml") != -1) { |
| 81 | putReferenceType(catalogs, attributes); |
| 82 | } else if (type.indexOf("tasksList+xml") != -1) { |
| 83 | tasksList = newReferenceType(attributes); |
| 84 | } else if (type.indexOf("network+xml") != -1) { |
| 85 | putReferenceType(networks, attributes); |
| 86 | } |
| 87 | } |
| 88 | } else { |
| 89 | taskHandler.startElement(uri, localName, qName, attrs); |
| 90 | } |
| 91 | |
| 92 | } |
| 93 | |
| 94 | public void endElement(String uri, String name, String qName) { |
| 95 | taskHandler.endElement(uri, name, qName); |
| 96 | if (qName.endsWith("Task")) { |
| 97 | this.tasks.add(taskHandler.getResult()); |
| 98 | } else if (qName.endsWith("Description")) { |
| 99 | description = currentOrNull(); |
| 100 | } else if (qName.endsWith("FullName")) { |
| 101 | fullName = currentOrNull(); |
| 102 | } |
| 103 | currentText = new StringBuilder(); |
| 104 | } |
| 105 | |
| 106 | public void characters(char ch[], int start, int length) { |
| 107 | currentText.append(ch, start, length); |
| 108 | } |
| 109 | |
| 110 | protected String currentOrNull() { |
| 111 | String returnVal = currentText.toString().trim(); |
| 112 | return returnVal.equals("") ? null : returnVal; |
| 113 | } |
| 114 | } |