| 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.cim.xml; |
| 20 | |
| 21 | import static org.jclouds.util.SaxUtils.currentOrNull; |
| 22 | import static org.jclouds.util.SaxUtils.equalsOrSuffix; |
| 23 | |
| 24 | import org.jclouds.cim.ResourceAllocationSettingData; |
| 25 | import org.jclouds.cim.ResourceAllocationSettingData.ConsumerVisibility; |
| 26 | import org.jclouds.cim.ResourceAllocationSettingData.MappingBehavior; |
| 27 | import org.jclouds.cim.ResourceAllocationSettingData.ResourceType; |
| 28 | import org.jclouds.http.functions.ParseSax; |
| 29 | import org.xml.sax.Attributes; |
| 30 | |
| 31 | /** |
| 32 | * @author Adrian Cole |
| 33 | */ |
| 34 | public class ResourceAllocationSettingDataHandler extends ParseSax.HandlerWithResult<ResourceAllocationSettingData> { |
| 35 | protected StringBuilder currentText = new StringBuilder(); |
| 36 | |
| 37 | protected ResourceAllocationSettingData.Builder builder = ResourceAllocationSettingData.builder(); |
| 38 | |
| 39 | public ResourceAllocationSettingData getResult() { |
| 40 | try { |
| 41 | return builder.build(); |
| 42 | } finally { |
| 43 | builder = ResourceAllocationSettingData.builder(); |
| 44 | } |
| 45 | } |
| 46 | |
| 47 | @Override |
| 48 | public void startElement(String uri, String localName, String qName, Attributes attrs) { |
| 49 | } |
| 50 | |
| 51 | @Override |
| 52 | public void endElement(String uri, String localName, String qName) { |
| 53 | String current = currentOrNull(currentText); |
| 54 | if (current != null) { |
| 55 | if (equalsOrSuffix(qName, "ElementName")) { |
| 56 | builder.elementName(current); |
| 57 | } else if (equalsOrSuffix(qName, "InstanceID")) { |
| 58 | builder.instanceID(current); |
| 59 | } else if (equalsOrSuffix(qName, "Caption")) { |
| 60 | builder.caption(current); |
| 61 | } else if (equalsOrSuffix(qName, "Description")) { |
| 62 | builder.description(current); |
| 63 | } else if (equalsOrSuffix(qName, "Address")) { |
| 64 | builder.address(current); |
| 65 | } else if (equalsOrSuffix(qName, "AddressOnParent")) { |
| 66 | builder.addressOnParent(current); |
| 67 | } else if (equalsOrSuffix(qName, "AllocationUnits")) { |
| 68 | builder.allocationUnits(current); |
| 69 | } else if (equalsOrSuffix(qName, "AutomaticAllocation")) { |
| 70 | builder.automaticAllocation(new Boolean(current)); |
| 71 | } else if (equalsOrSuffix(qName, "AutomaticDeallocation")) { |
| 72 | builder.automaticDeallocation(new Boolean(current)); |
| 73 | } else if (equalsOrSuffix(qName, "ConsumerVisibility")) { |
| 74 | builder.consumerVisibility(ConsumerVisibility.fromValue(current)); |
| 75 | } else if (equalsOrSuffix(qName, "Limit")) { |
| 76 | builder.limit(new Long(current)); |
| 77 | } else if (equalsOrSuffix(qName, "MappingBehavior")) { |
| 78 | builder.mappingBehavior(MappingBehavior.fromValue(current)); |
| 79 | } else if (equalsOrSuffix(qName, "OtherResourceType")) { |
| 80 | builder.otherResourceType(current); |
| 81 | } else if (equalsOrSuffix(qName, "Parent")) { |
| 82 | builder.parent(current); |
| 83 | } else if (equalsOrSuffix(qName, "PoolID")) { |
| 84 | builder.poolID(current); |
| 85 | } else if (equalsOrSuffix(qName, "Reservation")) { |
| 86 | builder.reservation(new Long(current)); |
| 87 | } else if (equalsOrSuffix(qName, "ResourceSubType")) { |
| 88 | builder.resourceSubType(current); |
| 89 | } else if (equalsOrSuffix(qName, "ResourceType")) { |
| 90 | builder.resourceType(ResourceType.fromValue(current)); |
| 91 | } else if (equalsOrSuffix(qName, "VirtualQuantity")) { |
| 92 | builder.virtualQuantity(new Long(current)); |
| 93 | } else if (equalsOrSuffix(qName, "VirtualQuantityUnits")) { |
| 94 | builder.virtualQuantityUnits(current); |
| 95 | } else if (equalsOrSuffix(qName, "Weight")) { |
| 96 | builder.weight(new Integer(current)); |
| 97 | } else if (equalsOrSuffix(qName, "Connection")) { |
| 98 | builder.connection(current); |
| 99 | } else if (equalsOrSuffix(qName, "HostResource")) { |
| 100 | builder.hostResource(current); |
| 101 | } |
| 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 | } |