| 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.trmk.vcloud_0_8.xml; |
| 20 | |
| 21 | import static org.jclouds.util.SaxUtils.equalsOrSuffix; |
| 22 | |
| 23 | import java.util.Set; |
| 24 | |
| 25 | import javax.inject.Inject; |
| 26 | |
| 27 | import org.jclouds.http.functions.ParseSax; |
| 28 | import org.jclouds.trmk.vcloud_0_8.domain.DataCenter; |
| 29 | import org.xml.sax.Attributes; |
| 30 | |
| 31 | import com.google.common.collect.ImmutableSet; |
| 32 | import com.google.common.collect.ImmutableSet.Builder; |
| 33 | |
| 34 | /** |
| 35 | * @author Adrian Cole |
| 36 | */ |
| 37 | public class DataCentersHandler extends ParseSax.HandlerWithResult<Set<DataCenter>> { |
| 38 | protected StringBuilder currentText = new StringBuilder(); |
| 39 | |
| 40 | protected Builder<DataCenter> dataCenters = ImmutableSet.<DataCenter> builder(); |
| 41 | |
| 42 | protected DataCenter.Builder builder = DataCenter.builder(); |
| 43 | |
| 44 | protected final DataCenterHandler handler; |
| 45 | |
| 46 | public Set<DataCenter> getResult() { |
| 47 | try { |
| 48 | return dataCenters.build(); |
| 49 | } finally { |
| 50 | dataCenters = ImmutableSet.<DataCenter> builder(); |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | @Inject |
| 55 | public DataCentersHandler(DataCenterHandler handler) { |
| 56 | this.handler = handler; |
| 57 | } |
| 58 | |
| 59 | @Override |
| 60 | public void startElement(String uri, String localName, String qName, Attributes attrs) { |
| 61 | } |
| 62 | |
| 63 | @Override |
| 64 | public void endElement(String uri, String localName, String qName) { |
| 65 | if (equalsOrSuffix(qName, "DataCenter")) { |
| 66 | dataCenters.add(handler.getResult()); |
| 67 | } else { |
| 68 | handler.endElement(uri, localName, qName); |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | @Override |
| 73 | public void characters(char ch[], int start, int length) { |
| 74 | handler.characters(ch, start, length); |
| 75 | } |
| 76 | |
| 77 | } |