package de.knipp.registryannouncements;

import java.io.File;
import java.io.FileInputStream;
import java.util.ArrayList;
import java.util.List;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.apache.commons.digester.Digester;


public class Announcement
{
  // member variables

  private String registryName;
  private String domain;
  private String id;
  private String dateFrom;
  private String dateTo;
  private String type;
  private boolean affectedProdSrs;
  private boolean affectedProdWhois;
  private boolean affectedProdAdminWebsite;
  private boolean affectedProdDns;
  private boolean affectedProdOther;
  private boolean affectedOteSrs;
  private boolean affectedOteWhois;
  private boolean affectedOteAdminWebsite;
  private boolean affectedOteDns;
  private boolean affectedOteOther;
  private List<Description> descriptions = new ArrayList<Description> ();

  private Announcement announcement;

  static final String JAXP_SCHEMA_LANGUAGE =
    "http://java.sun.com/xml/jaxp/properties/schemaLanguage";
  static final String W3C_XML_SCHEMA = "http://www.w3.org/2001/XMLSchema";
  static final String JAXP_SCHEMA_SOURCE =
    "http://java.sun.com/xml/jaxp/properties/schemaSource";


  // constructors

  public Announcement () {};

  public Announcement (String xsd, String xml)
  {
    try
    {
      SAXParserFactory factory = SAXParserFactory.newInstance ();
      factory.setValidating (true);

      SAXParser parser = factory.newSAXParser ();
      parser.setProperty (JAXP_SCHEMA_LANGUAGE, W3C_XML_SCHEMA);
      parser.setProperty (JAXP_SCHEMA_SOURCE, new File (xsd));

      Digester digester = new Digester ();
      digester.addObjectCreate ("announcement", Announcement.class);
      digester.addBeanPropertySetter ("announcement/registryName", "registryName");
      digester.addBeanPropertySetter ("announcement/domain", "domain");
      digester.addBeanPropertySetter ("announcement/id", "id");
      digester.addBeanPropertySetter ("announcement/date/from", "dateFrom");
      digester.addBeanPropertySetter ("announcement/date/to", "dateTo");
      digester.addBeanPropertySetter ("announcement/type", "type");
      digester.addBeanPropertySetter ("announcement/affected/prod/srs", "affectedProdSrs");
      digester.addBeanPropertySetter ("announcement/affected/prod/whois", "affectedProdWhois");
      digester.addBeanPropertySetter ("announcement/affected/prod/website", "affectedProdWebsite");
      digester.addBeanPropertySetter ("announcement/affected/prod/dns", "affectedProdDns");
      digester.addBeanPropertySetter ("announcement/affected/prod/other", "affectedProdOther");
      digester.addBeanPropertySetter ("announcement/affected/ote/srs", "affectedOteSrs");
      digester.addBeanPropertySetter ("announcement/affected/ote/whois", "affectedOteWhois");
      digester.addBeanPropertySetter ("announcement/affected/ote/website", "affectedOteWebsite");
      digester.addBeanPropertySetter ("announcement/affected/ote/dns", "affectedOteDns");
      digester.addBeanPropertySetter ("announcement/affected/ote/other", "affectedOteOther");
      digester.addObjectCreate ("announcement/description", Description.class);
      digester.addBeanPropertySetter ("announcement/description", "message");
      digester.addSetProperties ("announcement/description");
      digester.addSetNext ("announcement/description", "addDescription");

      File input = new File (xml);
      parser.parse (new FileInputStream (new File (xml)), digester);
      announcement = (Announcement) digester.parse (input);
   }
   catch (Exception e)
   {
      System.err.println ("something weird happend during parsing the xml file: " + e);
      System.exit (-1);
   }
 }


  // getter and setter

  public void setRegistryName (String s) {registryName = s;}
  public String getRegistryName () {return registryName;}
  
  public void setDomain (String s) {domain = s;} 
  public String getDomain () {return domain;}

  public void setId (String s) {id = s;} 
  public String getId () {return id;}

  public void setDateFrom (String s) {dateFrom = s;}
  public String getDateFrom () {return dateFrom;}
  
  public void setDateTo (String s) {dateTo = s;}
  public String getDateTo () {return dateTo;}
  
  public void setType (String s) {type = s;}
  public String getType () {return type;}
  

  public void setAffectedProdSrs (boolean b) {affectedProdSrs = b;}
  public boolean isAffectedProdSrs () {return affectedProdSrs;}

  public void setAffectedProdWhois (boolean b) {affectedProdWhois = b;}
  public boolean isAffectedProdWhois () {return affectedProdWhois;}

  public void setAffectedProdAdminWebsite (boolean b) {affectedProdAdminWebsite = b;}
  public boolean isAffectedProdAdminWebsite () {return affectedProdAdminWebsite;}

  public void setAffectedProdDns (boolean b) {affectedProdDns = b;}
  public boolean isAffectedProdDns () {return affectedProdDns;}

  public void setAffectedProdOther (boolean b) {affectedProdOther = b;}
  public boolean isAffectedProdOther () {return affectedProdOther;}


  public void setAffectedOteSrs (boolean b) {affectedOteSrs = b;}
  public boolean isAffectedOteSrs () {return affectedOteSrs;}

  public void setAffectedOteWhois (boolean b) {affectedOteWhois = b;}
  public boolean isAffectedOteWhois () {return affectedOteWhois;}

  public void setAffectedOteAdminWebsite (boolean b) {affectedOteAdminWebsite = b;}
  public boolean isAffectedOteAdminWebsite () {return affectedOteAdminWebsite;}

  public void setAffectedOteDns (boolean b) {affectedOteDns = b;}
  public boolean isAffectedOteDns () {return affectedOteDns;}

  public void setAffectedOteOther (boolean b) {affectedOteOther = b;}
  public boolean isAffectedOteOther () {return affectedOteOther;}

  public void addDescription (Description d) {descriptions.add (d);}
  public List<Description> getDescriptions () {return descriptions;}

  public Announcement getAnnouncement () {return announcement;}
}
