View Javadoc

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 org.jclouds.http.functions.ParseSax;
25  import org.jclouds.savvis.vpdc.domain.FirewallRule;
26  import org.xml.sax.Attributes;
27  import org.xml.sax.SAXException;
28  
29  /**
30   * @author Kedar Dave
31   */
32  public class FirewallRuleHandler extends ParseSax.HandlerWithResult<FirewallRule> {
33     protected StringBuilder currentText = new StringBuilder();
34     private FirewallRule.Builder builder = FirewallRule.builder();
35  
36     public FirewallRule getResult() {
37        try {
38           return builder.build();
39        } finally {
40           builder = FirewallRule.builder();
41        }
42     }
43  
44     public void startElement(String uri, String localName, String qName, Attributes attrs) throws SAXException {
45  
46     }
47  
48     @Override
49     public void endElement(String uri, String localName, String qName) throws SAXException {
50        String current = currentOrNull(currentText);
51        if (current != null) {
52           if (equalsOrSuffix(qName, "Type")) {
53              builder.firewallType(current);
54           } else if (equalsOrSuffix(qName, "IsEnabled")) {
55              builder.isEnabled(Boolean.parseBoolean(current));
56           } else if (equalsOrSuffix(qName, "Source")) {
57              builder.source(current);
58           } else if (equalsOrSuffix(qName, "Destination")) {
59              builder.destination(current);
60           } else if (equalsOrSuffix(qName, "Port")) {
61              builder.port(current);
62           } else if (equalsOrSuffix(qName, "Policy")) {
63              builder.policy(current);
64           } else if (equalsOrSuffix(qName, "Description")) {
65              builder.description(current);
66           } else if (equalsOrSuffix(qName, "Log")) {
67              builder.isLogged(Boolean.parseBoolean(current));
68           } else if (equalsOrSuffix(qName, "Tcp")) {
69              builder.protocol("Tcp");
70           } else if (qName.contains("Udp") || qName.contains("udp")) {
71              builder.protocol("Udp");
72           } else if (qName.contains("Icmp") || qName.contains("icmp") || qName.contains("Ping")
73                 || qName.contains("ping")) {
74              builder.protocol("Icmp-ping");
75           }
76        }
77        currentText = new StringBuilder();
78     }
79  
80     @Override
81     public void characters(char ch[], int start, int length) {
82        currentText.append(ch, start, length);
83     }
84  
85  }