/*
 * The contents of this file are subject to the Mozilla Public License Version 1.1
 * (the "License"); you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at <http://www.mozilla.org/MPL/>.
 * 
 * Software distributed under the License is distributed on an "AS IS" basis, WITHOUT
 * WARRANTY OF ANY KIND, either express or implied. See the License for the specific
 * language governing rights and limitations under the License.
 * 
 * The Original Code is the Venice Web Communities System.
 * 
 * The Initial Developer of the Original Code is Eric J. Bowersox <erbo@silcom.com>,
 * for Silverwrist Design Studios.  Portions created by Eric J. Bowersox are
 * Copyright (C) 2001 Eric J. Bowersox/Silverwrist Design Studios.  All Rights Reserved.
 * 
 * Contributor(s): 
 */
package com.silverwrist.venice.util;

import org.w3c.dom.*;
import com.silverwrist.util.*;
import com.silverwrist.venice.except.*;

public class VCardAddress
{
  /*--------------------------------------------------------------------------------
   * Predicate class to use for searches
   *--------------------------------------------------------------------------------
   */

  static abstract class Predicate
  {
    public abstract boolean test(VCardAddress addr);

  } // end class Predicate

  /*--------------------------------------------------------------------------------
   * Attributes
   *--------------------------------------------------------------------------------
   */

  private boolean home_adr;
  private boolean work_adr;
  private boolean postal_adr;
  private boolean parcel_adr;
  private boolean domestic_adr;
  private boolean intl_adr;
  private boolean preferred;
  private String pobox;
  private String ext_address;
  private String street;
  private String locality;
  private String region;
  private String postal_code;
  private String country;

  /*--------------------------------------------------------------------------------
   * Constructor
   *--------------------------------------------------------------------------------
   */

  VCardAddress(Element adr) throws ValidationException
  {
    DOMElementHelper h = new DOMElementHelper(adr);
    home_adr = h.hasChildElement("HOME");
    work_adr = h.hasChildElement("WORK");
    postal_adr = h.hasChildElement("POSTAL");
    parcel_adr = h.hasChildElement("PARCEL");
    domestic_adr = h.hasChildElement("DOM");
    intl_adr = h.hasChildElement("INTL");
    if (domestic_adr && intl_adr)
      throw new ValidationException("same address can't be both DOM and INTL");
    preferred = h.hasChildElement("PREF");
    pobox = VCard.cleanup(h.getSubElementText("POBOX"));
    ext_address = VCard.cleanup(h.getSubElementText("EXTADR"));
    street = VCard.cleanup(h.getSubElementText("STREET"));
    locality = VCard.cleanup(h.getSubElementText("LOCALITY"));
    region = VCard.cleanup(h.getSubElementText("REGION"));
    postal_code = VCard.cleanup(h.getSubElementText("PCODE"));
    country = VCard.cleanup(h.getSubElementText("CTRY"));

  } // end constructor

  /*--------------------------------------------------------------------------------
   * External getters/setters
   *--------------------------------------------------------------------------------
   */

  public final boolean isHomeAddress()
  {
    return home_adr;

  } // end isHomeAddress

  public final boolean isWorkAddress()
  {
    return work_adr;

  } // end isWorkAddress

  public final boolean isPostalAddress()
  {
    return postal_adr;

  } // end isPostalAddress

  public final boolean isParcelAddress()
  {
    return parcel_adr;

  } // end isParcelAddress

  public final boolean isDomesticAddress()
  {
    return domestic_adr;

  } // end isDomesticAddress

  public final boolean isInternationalAddress()
  {
    return intl_adr;

  } // end isInternationalAddress

  public final boolean isPreferred()
  {
    return preferred;

  } // end isPreferred

  public final String getPOBox()
  {
    return pobox;

  } // end getPOBox

  public final String getExtension()
  {
    return ext_address;

  } // end getExtension

  public final String getStreet()
  {
    return street;

  } // end getStreet

  public final String getLocality()
  {
    return locality;

  } // end getLocality

  public final String getRegion()
  {
    return region;

  } // end getRegion

  public final String getPostalCode()
  {
    return postal_code;

  } // end getPostalCode

  public final String getCountry()
  {
    return country;

  } // end getCountry

} // end class VCardAddress
