public void test() {
   InputStream is = getClass().getResourceAsStream("/firewallService.xml");
   Injector injector = Guice.createInjector(new SaxParserModule());
   Factory factory = injector.getInstance(ParseSax.Factory.class);
   FirewallService result =
       factory.create(injector.getInstance(FirewallServiceHandler.class)).parse(is);
   assertEquals(result.isEnabled(), false);
   assertEquals(
       result.getFirewallRules(),
       ImmutableSet.<FirewallRule>of(
           FirewallRule.builder()
               .firewallType("SERVER_TIER_FIREWALL")
               .isEnabled(false)
               .source("internet")
               .destination("VM Tier01")
               .port("22")
               .protocol("Tcp")
               .policy("allow")
               .description("Server Tier Firewall Rule")
               .isLogged(false)
               .build(),
           FirewallRule.builder()
               .firewallType("SERVER_TIER_FIREWALL")
               .isEnabled(true)
               .source("VM Tier03")
               .destination("VM Tier03")
               .protocol("Icmp-ping")
               .policy("allow")
               .description("Server Tier Firewall Rule")
               .isLogged(false)
               .build()));
 }
 public FirewallService getResult() {
   try {
     return builder.build();
   } finally {
     builder = FirewallService.builder();
   }
 }
/** @author Kedar Dave */
public class FirewallServiceHandler extends ParseSax.HandlerWithResult<FirewallService> {
  protected StringBuilder currentText = new StringBuilder();
  private FirewallRuleHandler firewallRuleHandler;
  private FirewallService.Builder builder = FirewallService.builder();
  boolean inFirewallService;
  boolean inFirewallRule;

  @Inject
  public FirewallServiceHandler(FirewallRuleHandler firewallRuleHandler) {
    this.firewallRuleHandler = firewallRuleHandler;
  }

  public FirewallService getResult() {
    try {
      return builder.build();
    } finally {
      builder = FirewallService.builder();
    }
  }

  public void startElement(String uri, String localName, String qName, Attributes attrs)
      throws SAXException {
    if (equalsOrSuffix(qName, "FirewallService")) {
      inFirewallService = true;
    } else if (equalsOrSuffix(qName, "FirewallRule")) {
      inFirewallRule = true;
      firewallRuleHandler.startElement(uri, localName, qName, attrs);
    }
  }

  @Override
  public void endElement(String uri, String localName, String qName) throws SAXException {
    if (equalsOrSuffix(qName, "FirewallService")) {
      inFirewallService = false;
    } else if (equalsOrSuffix(qName, "FirewallRule")) {
      builder.firewallRule(firewallRuleHandler.getResult());
      inFirewallRule = false;
    } else if (equalsOrSuffix(qName, "isEnabled")) {
      if (inFirewallService) {
        String current = currentOrNull(currentText);
        if (current != null) {
          builder.isEnabled(Boolean.parseBoolean(current));
        }
      }
    }

    if (inFirewallRule) {
      firewallRuleHandler.endElement(uri, localName, qName);
    }
    currentText = new StringBuilder();
  }

  @Override
  public void characters(char ch[], int start, int length) {
    currentText.append(ch, start, length);
    firewallRuleHandler.characters(ch, start, length);
  }
}
Exemple #4
0
 public static Builder fromFirewallService(FirewallService in) {
   return new Builder()
       .id(in.getId())
       .name(in.getName())
       .type(in.getType())
       .href(in.getHref())
       .isEnabled(in.isEnabled())
       .firewallRules(in.getFirewallRules());
 }