private void writeRule(XmlWriter xml, Rule rule) {
    xml.start("Rule");
    if (rule.getId() != null) {
      xml.start("ID").value(rule.getId()).end();
    }
    xml.start("Prefix").value(rule.getPrefix()).end();
    xml.start("Status").value(rule.getStatus()).end();

    Transition transition = rule.getTransition();
    if (transition != null) {
      xml.start("Transition");
      if (transition.getDate() != null) {
        xml.start("Date");
        xml.value(ServiceUtils.formatIso8601Date(transition.getDate()));
        xml.end();
      }
      if (transition.getDays() != -1) {
        xml.start("Days");
        xml.value(Integer.toString(transition.getDays()));
        xml.end();
      }

      xml.start("StorageClass");
      xml.value(transition.getStorageClass().toString());
      xml.end(); // <StorageClass>
      xml.end(); // </Transition>
    }

    if (rule.getExpirationInDays() != -1) {
      xml.start("Expiration");
      xml.start("Days").value("" + rule.getExpirationInDays()).end();
      xml.end(); // </Expiration>
    }

    if (rule.getExpirationDate() != null) {
      xml.start("Expiration");
      xml.start("Date").value(ServiceUtils.formatIso8601Date(rule.getExpirationDate())).end();
      xml.end(); // </Expiration>
    }

    xml.end(); // </Rule>
  }
  private void writeRule(XmlWriter xml, RoutingRule rule) {
    xml.start("RoutingRule");
    if (rule.getCondition() != null) {
      xml.start("Condition");
      xml.start("KeyPrefixEquals");
      if (rule.getCondition().getKeyPrefixEquals() != null) {
        xml.value(rule.getCondition().getKeyPrefixEquals());
      }
      xml.end(); // </KeyPrefixEquals">

      if (rule.getCondition().getHttpErrorCodeReturnedEquals() != null) {
        xml.start("HttpErrorCodeReturnedEquals ")
            .value(rule.getCondition().getHttpErrorCodeReturnedEquals())
            .end();
      }

      xml.end(); // </Condition>
    }

    xml.start("Redirect");
    if (rule.getRedirect() != null) {
      if (rule.getRedirect().getprotocol() != null) {
        xml.start("Protocol").value(rule.getRedirect().getprotocol()).end();
      }

      if (rule.getRedirect().getHostName() != null) {
        xml.start("HostName").value(rule.getRedirect().getHostName()).end();
      }

      if (rule.getRedirect().getReplaceKeyPrefixWith() != null) {
        xml.start("ReplaceKeyPrefixWith").value(rule.getRedirect().getReplaceKeyPrefixWith()).end();
      }

      if (rule.getRedirect().getReplaceKeyWith() != null) {
        xml.start("ReplaceKeyWith").value(rule.getRedirect().getReplaceKeyWith()).end();
      }
    }
    xml.end(); // </Redirect>
    xml.end(); // </CORSRule>
  }