| 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.deltacloud.xml; |
| 20 | |
| 21 | import java.net.URI; |
| 22 | import java.util.Map; |
| 23 | import java.util.Set; |
| 24 | |
| 25 | import javax.inject.Inject; |
| 26 | |
| 27 | import org.jclouds.deltacloud.domain.HardwareProfile; |
| 28 | import org.jclouds.deltacloud.domain.HardwareProperty; |
| 29 | import org.jclouds.http.functions.ParseSax; |
| 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.Sets; |
| 35 | |
| 36 | /** |
| 37 | * @author Adrian Cole |
| 38 | */ |
| 39 | public class HardwareProfileHandler extends ParseSax.HandlerWithResult<HardwareProfile> { |
| 40 | private StringBuilder currentText = new StringBuilder(); |
| 41 | private final HardwarePropertyHandler propertyHandler; |
| 42 | |
| 43 | @Inject |
| 44 | HardwareProfileHandler(HardwarePropertyHandler propertyHandler) { |
| 45 | this.propertyHandler = propertyHandler; |
| 46 | } |
| 47 | |
| 48 | private URI href; |
| 49 | private String id; |
| 50 | private String name; |
| 51 | private Set<HardwareProperty> properties = Sets.newLinkedHashSet(); |
| 52 | private boolean inProperty; |
| 53 | |
| 54 | private HardwareProfile profile; |
| 55 | |
| 56 | public HardwareProfile getResult() { |
| 57 | return profile; |
| 58 | } |
| 59 | |
| 60 | @Override |
| 61 | public void startElement(String uri, String localName, String qName, Attributes attrs) throws SAXException { |
| 62 | Map<String, String> attributes = SaxUtils.cleanseAttributes(attrs); |
| 63 | if (qName.equals("property")) { |
| 64 | inProperty = true; |
| 65 | } |
| 66 | |
| 67 | if (inProperty) { |
| 68 | propertyHandler.startElement(uri, localName, qName, attrs); |
| 69 | } else if (qName.equals("hardware_profile")) { |
| 70 | String href = attributes.get("href"); |
| 71 | if (href != null) { |
| 72 | this.href = URI.create(href); |
| 73 | } |
| 74 | this.id = attributes.get("id"); |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | @Override |
| 79 | public void endElement(String uri, String localName, String qName) throws SAXException { |
| 80 | |
| 81 | if (inProperty) |
| 82 | propertyHandler.endElement(uri, localName, qName); |
| 83 | |
| 84 | if (qName.endsWith("property")) { |
| 85 | inProperty = false; |
| 86 | this.properties.add(propertyHandler.getResult()); |
| 87 | } else if (qName.equalsIgnoreCase("name")) { |
| 88 | this.name = currentText.toString().trim(); |
| 89 | } else if (qName.equalsIgnoreCase("hardware_profile")) { |
| 90 | this.profile = new HardwareProfile(href, id, name, properties); |
| 91 | this.href = null; |
| 92 | this.id = null; |
| 93 | this.name = null; |
| 94 | this.properties = Sets.newLinkedHashSet(); |
| 95 | } |
| 96 | currentText = new StringBuilder(); |
| 97 | } |
| 98 | |
| 99 | public void characters(char ch[], int start, int length) { |
| 100 | currentText.append(ch, start, length); |
| 101 | } |
| 102 | } |