| 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.currentOrNull; |
| 22 | import static org.jclouds.util.SaxUtils.equalsOrSuffix; |
| 23 | |
| 24 | import java.net.URI; |
| 25 | |
| 26 | import org.jclouds.http.functions.ParseSax.HandlerWithResult; |
| 27 | import org.jclouds.trmk.vcloud_0_8.domain.InternetService; |
| 28 | import org.jclouds.trmk.vcloud_0_8.domain.Protocol; |
| 29 | import org.jclouds.trmk.vcloud_0_8.domain.PublicIpAddress; |
| 30 | import org.xml.sax.Attributes; |
| 31 | |
| 32 | /** |
| 33 | * @author Adrian Cole |
| 34 | */ |
| 35 | public class InternetServiceHandler extends HandlerWithResult<InternetService> { |
| 36 | |
| 37 | private StringBuilder currentText = new StringBuilder(); |
| 38 | |
| 39 | private boolean inPublicIpAddress; |
| 40 | private URI location; |
| 41 | private URI addressLocation; |
| 42 | private String serviceName; |
| 43 | private String address; |
| 44 | private PublicIpAddress publicIpAddress; |
| 45 | private int port; |
| 46 | private String description; |
| 47 | private int timeout; |
| 48 | private boolean enabled; |
| 49 | private Protocol protocol; |
| 50 | |
| 51 | protected int depth = 0; |
| 52 | |
| 53 | private int thisDepth; |
| 54 | |
| 55 | @Override |
| 56 | public InternetService getResult() { |
| 57 | return new InternetService(serviceName, location, publicIpAddress, port, protocol, enabled, timeout, description); |
| 58 | } |
| 59 | |
| 60 | @Override |
| 61 | public void startElement(String uri, String localName, String qName, Attributes attrs) { |
| 62 | depth++; |
| 63 | if (equalsOrSuffix(qName, "InternetService")) { |
| 64 | thisDepth = depth; |
| 65 | } else if (equalsOrSuffix(qName, "PublicIpAddress")) { |
| 66 | inPublicIpAddress = true; |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | public void endElement(String uri, String name, String qName) { |
| 71 | depth--; |
| 72 | if (equalsOrSuffix(qName, "PublicIpAddress")) { |
| 73 | inPublicIpAddress = false; |
| 74 | publicIpAddress = new PublicIpAddress(address, addressLocation); |
| 75 | address = null; |
| 76 | addressLocation = null; |
| 77 | } else { |
| 78 | String value = currentOrNull(currentText); |
| 79 | if (value != null && !value.equals("")) { |
| 80 | if (depth == thisDepth) { |
| 81 | if (equalsOrSuffix(qName, "Href")) { |
| 82 | location = URI.create(value); |
| 83 | } else if (equalsOrSuffix(qName, "Name")) { |
| 84 | serviceName = value; |
| 85 | } else if (equalsOrSuffix(qName, "Port")) { |
| 86 | port = Integer.parseInt(value); |
| 87 | } else if (equalsOrSuffix(qName, "Protocol")) { |
| 88 | protocol = Protocol.valueOf(value); |
| 89 | } else if (equalsOrSuffix(qName, "Enabled")) { |
| 90 | enabled = Boolean.parseBoolean(value); |
| 91 | } else if (equalsOrSuffix(qName, "Timeout")) { |
| 92 | timeout = Integer.parseInt(value); |
| 93 | } else if (equalsOrSuffix(qName, "Description")) { |
| 94 | description = currentOrNull(currentText); |
| 95 | } |
| 96 | } else if (inPublicIpAddress) { |
| 97 | if (equalsOrSuffix(qName, "Href")) { |
| 98 | addressLocation = URI.create(value); |
| 99 | } else if (equalsOrSuffix(qName, "Name")) { |
| 100 | address = value; |
| 101 | } |
| 102 | } |
| 103 | } |
| 104 | } |
| 105 | currentText = new StringBuilder(); |
| 106 | } |
| 107 | |
| 108 | public void characters(char ch[], int start, int length) { |
| 109 | currentText.append(ch, start, length); |
| 110 | } |
| 111 | |
| 112 | } |