| 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.savvis.vpdc.xml; |
| 20 | |
| 21 | import static org.jclouds.util.SaxUtils.currentOrNull; |
| 22 | import static org.jclouds.util.SaxUtils.equalsOrSuffix; |
| 23 | |
| 24 | import javax.inject.Inject; |
| 25 | |
| 26 | import org.jclouds.http.functions.ParseSax; |
| 27 | import org.jclouds.savvis.vpdc.domain.FirewallService; |
| 28 | import org.xml.sax.Attributes; |
| 29 | import org.xml.sax.SAXException; |
| 30 | |
| 31 | /** |
| 32 | * @author Kedar Dave |
| 33 | */ |
| 34 | public class FirewallServiceHandler extends ParseSax.HandlerWithResult<FirewallService> { |
| 35 | protected StringBuilder currentText = new StringBuilder(); |
| 36 | private FirewallRuleHandler firewallRuleHandler; |
| 37 | private FirewallService.Builder builder = FirewallService.builder(); |
| 38 | boolean inFirewallService; |
| 39 | boolean inFirewallRule; |
| 40 | |
| 41 | @Inject |
| 42 | public FirewallServiceHandler(FirewallRuleHandler firewallRuleHandler) { |
| 43 | this.firewallRuleHandler = firewallRuleHandler; |
| 44 | } |
| 45 | |
| 46 | public FirewallService getResult() { |
| 47 | try { |
| 48 | return builder.build(); |
| 49 | } finally { |
| 50 | builder = FirewallService.builder(); |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | public void startElement(String uri, String localName, String qName, Attributes attrs) throws SAXException { |
| 55 | if (equalsOrSuffix(qName, "FirewallService")) { |
| 56 | inFirewallService = true; |
| 57 | } else if (equalsOrSuffix(qName, "FirewallRule")) { |
| 58 | inFirewallRule = true; |
| 59 | firewallRuleHandler.startElement(uri, localName, qName, attrs); |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | @Override |
| 64 | public void endElement(String uri, String localName, String qName) throws SAXException { |
| 65 | if (equalsOrSuffix(qName, "FirewallService")) { |
| 66 | inFirewallService = false; |
| 67 | } else if (equalsOrSuffix(qName, "FirewallRule")) { |
| 68 | builder.firewallRule(firewallRuleHandler.getResult()); |
| 69 | inFirewallRule = false; |
| 70 | } else if (equalsOrSuffix(qName, "isEnabled")) { |
| 71 | if (inFirewallService) { |
| 72 | String current = currentOrNull(currentText); |
| 73 | if (current != null) { |
| 74 | builder.isEnabled(Boolean.parseBoolean(current)); |
| 75 | } |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | if (inFirewallRule) { |
| 80 | firewallRuleHandler.endElement(uri, localName, qName); |
| 81 | } |
| 82 | currentText = new StringBuilder(); |
| 83 | } |
| 84 | |
| 85 | @Override |
| 86 | public void characters(char ch[], int start, int length) { |
| 87 | currentText.append(ch, start, length); |
| 88 | firewallRuleHandler.characters(ch, start, length); |
| 89 | } |
| 90 | |
| 91 | } |