| 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 java.util.Set; |
| 22 | |
| 23 | import org.jclouds.javax.annotation.Nullable; |
| 24 | import javax.annotation.Resource; |
| 25 | |
| 26 | import org.jclouds.http.functions.ParseSax; |
| 27 | import org.jclouds.logging.Logger; |
| 28 | import org.jclouds.trmk.vcloud_0_8.domain.IpAddress; |
| 29 | import org.jclouds.trmk.vcloud_0_8.domain.IpAddress.Status; |
| 30 | import org.xml.sax.Attributes; |
| 31 | import org.xml.sax.SAXException; |
| 32 | |
| 33 | import com.google.common.collect.Sets; |
| 34 | |
| 35 | /** |
| 36 | * @author Adrian Cole |
| 37 | */ |
| 38 | public class IpAddressesHandler extends ParseSax.HandlerWithResult<Set<IpAddress>> { |
| 39 | protected StringBuilder currentText = new StringBuilder(); |
| 40 | |
| 41 | @Resource |
| 42 | protected Logger logger = Logger.NULL; |
| 43 | |
| 44 | private Set<IpAddress> addresses = Sets.newLinkedHashSet(); |
| 45 | private String address; |
| 46 | private Status status; |
| 47 | @Nullable |
| 48 | private String server; |
| 49 | private boolean skip; |
| 50 | |
| 51 | public Set<IpAddress> getResult() { |
| 52 | return addresses; |
| 53 | } |
| 54 | |
| 55 | public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException { |
| 56 | if (attributes.getIndex("xsi:nil") != -1) { |
| 57 | skip = true; |
| 58 | return; |
| 59 | } else { |
| 60 | skip = false; |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | @Override |
| 65 | public void endElement(String uri, String localName, String qName) throws SAXException { |
| 66 | String current = currentOrNull(); |
| 67 | if (current != null) { |
| 68 | if (qName.equals("Name")) { |
| 69 | address = current; |
| 70 | } else if (qName.equals("Status")) { |
| 71 | status = IpAddress.Status.fromValue(current); |
| 72 | } else if (!skip && qName.equals("Server")) { |
| 73 | server = current; |
| 74 | } |
| 75 | } else if (qName.equals("IpAddress")) { |
| 76 | addresses.add(new IpAddress(address, status, server)); |
| 77 | address = null; |
| 78 | status = null; |
| 79 | server = null; |
| 80 | } |
| 81 | currentText = new StringBuilder(); |
| 82 | } |
| 83 | |
| 84 | public void characters(char ch[], int start, int length) { |
| 85 | currentText.append(ch, start, length); |
| 86 | } |
| 87 | |
| 88 | protected String currentOrNull() { |
| 89 | String returnVal = currentText.toString().trim(); |
| 90 | return returnVal.equals("") ? null : returnVal; |
| 91 | } |
| 92 | |
| 93 | } |