//______________________________________________________________________________
//   |       |
//   | knipp |           Knipp Medien und Kommunikation GmbH
//    -------
//
//
//     Knipp RRI Toolkit
//
//
//     Copyright (C) 2005 by Knipp Medien und Kommunikation GmbH
//
//     This library is free software; you can redistribute it and/or
//     modify it under the terms of the GNU Lesser General Public License
//     as published by the Free Software Foundation; either version 2.1
//     of the License, or any later version.
//
//     This library is distributed in the hope that it will be useful,
//     but WITHOUT ANY WARRANTY; without even the implied warranty of
//     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
//     GNU Lesser General Public License for more details.
//
//     You should have received a copy of the GNU Lesser General
//     Public License along with this library; if not, write to the
//     Free Software Foundation, Inc., 59 Temple Place, Suite 330,
//     Boston, MA 02111-1307 USA
//
//
//     initial author:    Klaus Malorny
//
//     version:           1.3 (15112), 2011/05/27 16:00:14
//                        $Revision: 15112 $
//                        $Date: 2011-05-27 15:48:22 +0200 (Fri, 27 May 2011) $
//                        $Author: Klaus.Malorny $
//

package de.knipp.rritoolkit.dns;

import org.w3c.dom.Node;
import org.w3c.dom.Element;
import org.w3c.dom.DOMException;

import de.knipp.rritoolkit.KVList;
import de.knipp.rritoolkit.XmlBuilder;
import de.knipp.rritoolkit.ResponseParseException;
import de.knipp.rritoolkit.EncodeException;

import static de.knipp.rritoolkit.DenicConstants.NS_DNS_ENTRY;
import static de.knipp.rritoolkit.DenicConstants.NS_XSI;
import static de.knipp.rritoolkit.DenicConstants.PREFIX_DNS_ENTRY;

import static de.knipp.rritoolkit.XmlHelper.getFirstElement;
import static de.knipp.rritoolkit.XmlHelper.getFirstElementText;


/**
 *  base class for A or AAAA records in the domain
 *
 *  @version  $Revision: 15112 $
 *  @author   Klaus Malorny
 */

public abstract class DnsEntryAddress extends DnsEntry
{
  /** the IP address */

  private String ipAddress;


  /**
   *  constructor
   *
   *  @param owner                       the owner
   *  @param address                     the address
   */

  protected DnsEntryAddress (String owner, String address)
  {
    setOwner (owner);
    ipAddress = address;
  }



  /**
   *  constructor
   *
   *  @param el                       the element
   *  @throws DOMException            on DOM exceptions
   *  @throws ResponseParseException  on wrong content
   */

  protected DnsEntryAddress (Element el)
    throws DOMException, ResponseParseException
  {
    String owner = getFirstElementText (el, NS_DNS_ENTRY, "owner");

    if (owner == null)
      throw new ResponseParseException ("missing <owner> element", el);

    Element rdata = getFirstElement (el, NS_DNS_ENTRY, "rdata");

    if (rdata == null)
      throw new ResponseParseException ("missing <rdata> element", el);

    String address = getFirstElementText (rdata, NS_DNS_ENTRY, "address");

    if (address == null)
      throw new ResponseParseException ("missing <address> element", rdata);

    setOwner (owner);
    ipAddress = address;
  }



  /**
   *  convert the instance to an entry in the given list
   *
   *  @param list                     the list
   *  @throws EncodeException         if the encoding fails
   */

  @Override
  public void toList (KVList list)
    throws EncodeException
  {
    list.add ("nsentry", getOwner () + " IN " + getRecordName () +
      " " + ipAddress);
  }



  /**
   *  convert the instance to XML
   *
   *  @param node                     the node to append to
   *  @param encodedDomainName        the encoded domain name from the domain
   *                                  info data
   *  @throws DOMException            on DOM exceptions
   *  @throws EncodeException         if the encoding fails
   */

  @Override
  public void toXml (Node node, String encodedDomainName)
    throws DOMException, EncodeException
  {
    XmlBuilder xb = new XmlBuilder (node);

    xb.es (NS_DNS_ENTRY, PREFIX_DNS_ENTRY, "dnsentry");

    Node current = xb.getCurrent ();
    String prefix = current.getPrefix ();

    xb.attr (NS_XSI, PREFIX_DNS_ENTRY, "type", prefix.length () > 0 ? 
      prefix + ":" + getElementName () : getElementName ());

    xb.
      elt (NS_DNS_ENTRY, PREFIX_DNS_ENTRY, "owner", getOwner () == null ?
        encodedDomainName : getOwner ()).
      es (NS_DNS_ENTRY, PREFIX_DNS_ENTRY, "rdata").
      elt (NS_DNS_ENTRY, PREFIX_DNS_ENTRY, "address", ipAddress).
      ee ().
      ee ();
  }


  /**
   *  create a string representation (for debugging purposes)
   *
   *  @return                    the string representation
   */

  @Override
  public String toString ()
  {
    return getOwner () + " " + getRecordName () + " " + ipAddress;
  }


  /**
   *  return the record name
   *
   *  @return               the record name
   */

  protected abstract String getRecordName ();


  /**
   *  return the (local) element name
   *
   *  @return               the element name
   */

  protected abstract String getElementName ();



  /**
   *  create either an A or an AAAA record, depending on the given address
   *
   *  @param owner          the owner
   *  @param address        the address
   *  @return               the created address entry
   */

  public static DnsEntryAddress createAddressEntry (String owner,
    String address)
  {
    return isIPv6 (address) ?
      new DnsEntryAAAA (owner, address) : new DnsEntryA (owner, address);
  }



  //--- access methods ---------------------------------------------------------

  /**
   *  return the IP address
   *
   *  @return                            the IP address
   */

  public String getAddress ()
  {
    return ipAddress;
  }

}

