| 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.cloudwatch.xml; |
| 20 | |
| 21 | import java.util.Date; |
| 22 | |
| 23 | import javax.inject.Inject; |
| 24 | |
| 25 | import org.jclouds.cloudwatch.domain.Datapoint; |
| 26 | import org.jclouds.cloudwatch.domain.StandardUnit; |
| 27 | import org.jclouds.date.DateService; |
| 28 | import org.jclouds.http.functions.ParseSax; |
| 29 | |
| 30 | /** |
| 31 | * |
| 32 | * @author Adrian Cole |
| 33 | */ |
| 34 | public class DatapointHandler extends ParseSax.HandlerForGeneratedRequestWithResult<Datapoint> { |
| 35 | private StringBuilder currentText = new StringBuilder(); |
| 36 | |
| 37 | protected final DateService dateService; |
| 38 | private Double average; |
| 39 | private Double maximum; |
| 40 | private Double minimum; |
| 41 | private Date timestamp; |
| 42 | private Double samples; |
| 43 | private Double sum; |
| 44 | private StandardUnit unit; |
| 45 | private String customUnit; |
| 46 | |
| 47 | @Inject |
| 48 | public DatapointHandler(DateService dateService) { |
| 49 | this.dateService = dateService; |
| 50 | } |
| 51 | |
| 52 | public Datapoint getResult() { |
| 53 | Datapoint datapoint = new Datapoint(average, maximum, minimum, timestamp, samples, sum, unit, customUnit); |
| 54 | this.average = null; |
| 55 | this.maximum = null; |
| 56 | this.minimum = null; |
| 57 | this.timestamp = null; |
| 58 | this.samples = null; |
| 59 | this.sum = null; |
| 60 | this.unit = null; |
| 61 | this.customUnit = null; |
| 62 | return datapoint; |
| 63 | } |
| 64 | |
| 65 | public void endElement(String uri, String name, String qName) { |
| 66 | if (qName.equals("Average")) { |
| 67 | average = doubleOrNull(); |
| 68 | } else if (qName.equals("Maximum")) { |
| 69 | maximum = doubleOrNull(); |
| 70 | } else if (qName.equals("Minimum")) { |
| 71 | minimum = doubleOrNull(); |
| 72 | } else if (qName.equals("Timestamp")) { |
| 73 | timestamp = dateService.iso8601SecondsDateParse(currentText.toString().trim()); |
| 74 | } else if (qName.equals("Samples")) { |
| 75 | samples = doubleOrNull(); |
| 76 | } else if (qName.equals("Sum")) { |
| 77 | sum = doubleOrNull(); |
| 78 | } else if (qName.equals("Unit")) { |
| 79 | unit = StandardUnit.fromValue(currentText.toString().trim()); |
| 80 | } else if (qName.equals("CustomUnit")) { |
| 81 | customUnit = currentText.toString().trim(); |
| 82 | } |
| 83 | currentText = new StringBuilder(); |
| 84 | } |
| 85 | |
| 86 | private Double doubleOrNull() { |
| 87 | String string = currentText.toString().trim(); |
| 88 | if (!string.equals("")) { |
| 89 | return new Double(string); |
| 90 | } |
| 91 | return null; |
| 92 | } |
| 93 | |
| 94 | public void characters(char ch[], int start, int length) { |
| 95 | currentText.append(ch, start, length); |
| 96 | } |
| 97 | } |