| 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 | import java.util.regex.Pattern; |
| 25 | |
| 26 | import org.jclouds.javax.annotation.Nullable; |
| 27 | |
| 28 | import org.jclouds.deltacloud.domain.EnumHardwareProperty; |
| 29 | import org.jclouds.deltacloud.domain.FixedHardwareProperty; |
| 30 | import org.jclouds.deltacloud.domain.HardwareParameter; |
| 31 | import org.jclouds.deltacloud.domain.HardwareProperty; |
| 32 | import org.jclouds.deltacloud.domain.RangeHardwareProperty; |
| 33 | import org.jclouds.deltacloud.domain.HardwareProperty.Kind; |
| 34 | import org.jclouds.http.functions.ParseSax; |
| 35 | import org.jclouds.util.SaxUtils; |
| 36 | import org.xml.sax.Attributes; |
| 37 | import org.xml.sax.SAXException; |
| 38 | |
| 39 | import com.google.common.collect.Sets; |
| 40 | |
| 41 | /** |
| 42 | * @author Adrian Cole |
| 43 | */ |
| 44 | public class HardwarePropertyHandler extends ParseSax.HandlerWithResult<HardwareProperty> { |
| 45 | private Kind kind; |
| 46 | private String name; |
| 47 | private String unit; |
| 48 | private Object value; |
| 49 | private HardwareParameter param; |
| 50 | private Set<Object> availableValues = Sets.newLinkedHashSet(); |
| 51 | private Number first; |
| 52 | private Number last; |
| 53 | |
| 54 | /** |
| 55 | * resets state of the handler when called. |
| 56 | * |
| 57 | * @return property or null |
| 58 | */ |
| 59 | public HardwareProperty getResult() { |
| 60 | try { |
| 61 | switch (kind) { |
| 62 | case FIXED: |
| 63 | return new FixedHardwareProperty(name, unit, value); |
| 64 | case ENUM: |
| 65 | return new EnumHardwareProperty(name, unit, value, param, availableValues); |
| 66 | case RANGE: |
| 67 | return new RangeHardwareProperty(name, unit, (Number) value, param, first, last); |
| 68 | default: |
| 69 | return null; |
| 70 | } |
| 71 | } finally { |
| 72 | this.kind = null; |
| 73 | this.name = null; |
| 74 | this.unit = null; |
| 75 | this.value = null; |
| 76 | this.param = null; |
| 77 | this.availableValues = Sets.newLinkedHashSet(); |
| 78 | this.first = null; |
| 79 | this.last = null; |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | private static final Pattern LONG = Pattern.compile("^[0-9]+$"); |
| 84 | private static final Pattern DOUBLE = Pattern.compile("^[0-9]+\\.[0-9]+$"); |
| 85 | |
| 86 | public static @Nullable |
| 87 | Number parseNumberOrNull(String in) { |
| 88 | if (DOUBLE.matcher(in).matches()) |
| 89 | return new Double(in); |
| 90 | else if (LONG.matcher(in).matches()) |
| 91 | return new Long(in); |
| 92 | return null; |
| 93 | } |
| 94 | |
| 95 | @Override |
| 96 | public void startElement(String uri, String localName, String qName, Attributes attrs) throws SAXException { |
| 97 | Map<String, String> attributes = SaxUtils.cleanseAttributes(attrs); |
| 98 | if (qName.equals("property")) { |
| 99 | this.kind = Kind.fromValue(attributes.get("kind")); |
| 100 | this.name = attributes.get("name"); |
| 101 | this.unit = attributes.get("unit"); |
| 102 | if (attributes.containsKey("value")) { |
| 103 | this.value = stringOrNumber(attributes.get("value")); |
| 104 | } |
| 105 | } else if (qName.equals("param")) { |
| 106 | this.param = new HardwareParameter(URI.create(attributes.get("href")), attributes.get("method"), |
| 107 | attributes.get("name"), attributes.get("operation")); |
| 108 | } else if (qName.equals("range")) { |
| 109 | this.first = parseNumberOrNull(attributes.get("first")); |
| 110 | this.last = parseNumberOrNull(attributes.get("last")); |
| 111 | } else if (qName.equals("entry")) { |
| 112 | this.availableValues.add(stringOrNumber(attributes.get("value"))); |
| 113 | } |
| 114 | } |
| 115 | |
| 116 | public static Object stringOrNumber(String in) { |
| 117 | Number number = parseNumberOrNull(in); |
| 118 | return number != null ? number : in; |
| 119 | } |
| 120 | } |