| 1 | /** |
| 2 | * |
| 3 | * Copyright (C) 2011 Cloud Conscious, LLC. <info@cloudconscious.com> |
| 4 | * |
| 5 | * ==================================================================== |
| 6 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | * you may not use this file except in compliance with the License. |
| 8 | * 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, software |
| 13 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | * See the License for the specific language governing permissions and |
| 16 | * limitations under the License. |
| 17 | * ==================================================================== |
| 18 | */ |
| 19 | package org.jclouds.vcloud.terremark.xml; |
| 20 | |
| 21 | import java.net.URI; |
| 22 | |
| 23 | import javax.annotation.Resource; |
| 24 | |
| 25 | import org.jclouds.http.functions.ParseSax.HandlerWithResult; |
| 26 | import org.jclouds.logging.Logger; |
| 27 | import org.jclouds.vcloud.terremark.domain.InternetService; |
| 28 | import org.jclouds.vcloud.terremark.domain.Protocol; |
| 29 | import org.jclouds.vcloud.terremark.domain.PublicIpAddress; |
| 30 | import org.xml.sax.Attributes; |
| 31 | import org.xml.sax.SAXException; |
| 32 | |
| 33 | /** |
| 34 | * @author Adrian Cole |
| 35 | */ |
| 36 | public class InternetServiceHandler extends HandlerWithResult<InternetService> { |
| 37 | |
| 38 | @Resource |
| 39 | protected Logger logger = Logger.NULL; |
| 40 | private StringBuilder currentText = new StringBuilder(); |
| 41 | |
| 42 | private boolean inPublicIpAddress; |
| 43 | private URI location; |
| 44 | private URI addressLocation; |
| 45 | private String serviceName; |
| 46 | private String address; |
| 47 | private PublicIpAddress publicIpAddress; |
| 48 | private int port; |
| 49 | private String description; |
| 50 | private int timeout; |
| 51 | private boolean enabled; |
| 52 | private Protocol protocol; |
| 53 | |
| 54 | protected String currentOrNull() { |
| 55 | String returnVal = currentText.toString().trim(); |
| 56 | return returnVal.equals("") ? null : returnVal; |
| 57 | } |
| 58 | |
| 59 | @Override |
| 60 | public InternetService getResult() { |
| 61 | return new InternetService(serviceName, location, publicIpAddress, port, protocol, enabled, timeout, description); |
| 62 | } |
| 63 | |
| 64 | @Override |
| 65 | public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException { |
| 66 | if (qName.equals("PublicIpAddress")) { |
| 67 | inPublicIpAddress = true; |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | public void endElement(String uri, String name, String qName) { |
| 72 | String current = currentOrNull(); |
| 73 | if (qName.equals("PublicIpAddress")) { |
| 74 | publicIpAddress = new PublicIpAddress(address, addressLocation); |
| 75 | address = null; |
| 76 | addressLocation = null; |
| 77 | inPublicIpAddress = false; |
| 78 | } else if (current != null) { |
| 79 | if (qName.equals("Href")) { |
| 80 | if (inPublicIpAddress) |
| 81 | addressLocation = URI.create(current); |
| 82 | else |
| 83 | location = URI.create(current); |
| 84 | } else if (qName.equals("Name")) { |
| 85 | if (inPublicIpAddress) |
| 86 | address = current; |
| 87 | else |
| 88 | serviceName = current; |
| 89 | } else if (qName.equals("Port")) { |
| 90 | port = Integer.parseInt(current); |
| 91 | } else if (qName.equals("Protocol")) { |
| 92 | protocol = Protocol.valueOf(current); |
| 93 | } else if (qName.equals("Enabled")) { |
| 94 | enabled = Boolean.parseBoolean(current); |
| 95 | } else if (qName.equals("Timeout")) { |
| 96 | timeout = Integer.parseInt(current); |
| 97 | } else if (qName.equals("Description")) { |
| 98 | description = current; |
| 99 | } |
| 100 | } |
| 101 | currentText = new StringBuilder(); |
| 102 | } |
| 103 | |
| 104 | public void characters(char ch[], int start, int length) { |
| 105 | currentText.append(ch, start, length); |
| 106 | } |
| 107 | |
| 108 | } |