| 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.trmk.vcloud_0_8.util.Utils.newReferenceType; |
| 22 | import static org.jclouds.util.SaxUtils.cleanseAttributes; |
| 23 | import static org.jclouds.util.SaxUtils.currentOrNull; |
| 24 | |
| 25 | import java.util.Map; |
| 26 | |
| 27 | import javax.annotation.Resource; |
| 28 | |
| 29 | import org.jclouds.http.functions.ParseSax; |
| 30 | import org.jclouds.logging.Logger; |
| 31 | import org.jclouds.trmk.vcloud_0_8.domain.FenceMode; |
| 32 | import org.jclouds.trmk.vcloud_0_8.domain.Network; |
| 33 | import org.jclouds.trmk.vcloud_0_8.domain.ReferenceType; |
| 34 | import org.jclouds.trmk.vcloud_0_8.domain.internal.NetworkImpl; |
| 35 | import org.xml.sax.Attributes; |
| 36 | import org.xml.sax.SAXException; |
| 37 | |
| 38 | /** |
| 39 | * @author Adrian Cole |
| 40 | */ |
| 41 | public class NetworkHandler extends ParseSax.HandlerWithResult<Network> { |
| 42 | |
| 43 | @Resource |
| 44 | protected Logger logger = Logger.NULL; |
| 45 | |
| 46 | protected StringBuilder currentText = new StringBuilder(); |
| 47 | |
| 48 | protected ReferenceType network; |
| 49 | |
| 50 | protected String description; |
| 51 | |
| 52 | protected String gateway; |
| 53 | protected String netmask; |
| 54 | protected FenceMode fenceMode; |
| 55 | |
| 56 | private ReferenceType ips; |
| 57 | private ReferenceType extension; |
| 58 | |
| 59 | public Network getResult() { |
| 60 | return new NetworkImpl(network.getName(), network.getType(), network.getHref(), description, gateway, netmask, |
| 61 | fenceMode, extension, ips); |
| 62 | } |
| 63 | |
| 64 | @Override |
| 65 | public void startElement(String uri, String localName, String qName, Attributes attrs) throws SAXException { |
| 66 | Map<String, String> attributes = cleanseAttributes(attrs); |
| 67 | if (qName.equals("Network")) { |
| 68 | network = newReferenceType(attributes); |
| 69 | } else if (qName.equals("Link")) { |
| 70 | if ("IP Addresses".equals(attributes.get("name"))) { |
| 71 | ips = newReferenceType(attributes); |
| 72 | } else if ("down".equals(attributes.get("rel"))) { |
| 73 | extension = newReferenceType(attributes); |
| 74 | } |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | public void endElement(String uri, String name, String qName) { |
| 79 | if (qName.equals("Description")) { |
| 80 | description = currentOrNull(currentText); |
| 81 | } else if (qName.equals("Gateway")) { |
| 82 | gateway = currentOrNull(currentText); |
| 83 | } else if (qName.equals("Netmask")) { |
| 84 | netmask = currentOrNull(currentText); |
| 85 | } else if (qName.equals("FenceMode")) { |
| 86 | fenceMode = (FenceMode.fromValue(currentOrNull(currentText))); |
| 87 | } |
| 88 | currentText = new StringBuilder(); |
| 89 | } |
| 90 | |
| 91 | public void characters(char ch[], int start, int length) { |
| 92 | currentText.append(ch, start, length); |
| 93 | } |
| 94 | |
| 95 | } |