View Javadoc

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.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  }