//______________________________________________________________________________
//   |       |
//   | 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: 14936 $ 
//                        $Date: 2010-11-05 15:57:40 +0100 (Fri, 05 Nov 2010) $
//                        $Author: Klaus.Malorny $
//

package de.knipp.rritoolkit.response;

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

import de.knipp.rritoolkit.DomainData;
import de.knipp.rritoolkit.KVMultiList;
import de.knipp.rritoolkit.ResponseParseException;

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

import static de.knipp.rritoolkit.DenicConstants.NS_DOMAIN;



/**
 *  response of a domain info request
 *
 *  @version  $Revision: 14936 $
 *  @author   Klaus Malorny
 */

public class DomainInfoResponse extends Response
{
  /** the domain */
  
  private DomainData domain;
  
  
  //--- implementation of Response ---------------------------------------------
  
  
  /**
   *  subclasses that need to read additional data from the key-value pair
   *  response should override this method. The default implementation is empty 
   *  and need not to be called.
   *
   *  @param list                     the list
   *  @throws ResponseParseException  if the document does not contain the
   *                                  expected content
   */
  
  @Override
  protected void dataFromList (KVMultiList list)
    throws ResponseParseException
  {
    if (isSuccess ())
    {
      domain = new DomainData ();
      domain.fromList (list);
    }
  }

  /**
   *  subclasses that need to read additional data from the XML response should
   *  override this method. The default implementation is empty and need not
   *  to be called.
   *
   *  @param data                     the data element or {@code null}
   *                                  if the data contains no additional data
   *  @throws DOMException            on DOM exceptions
   *  @throws ResponseParseException  if the document does not contain the
   *                                  expected content
   */
  
  @Override
  protected void dataFromXml (Element data)
    throws DOMException, ResponseParseException
  {
    if (data != null && isSuccess ())
    {
      Element el = getFirstElement (data, NS_DOMAIN, "infoData");
      
      if (el != null)
      {
        domain = new DomainData ();
        domain.fromXml (el);
      }
    }
  }
  
  /**
   *  call the suitable method of the given visitor
   *
   *  @param visitor               the visitor
   */
  
  @Override
  public void accept (ResponseVisitor visitor)
  {
    visitor.visit (this);
  }
  
  
  
  
  /**
   *  return a string representation for debugging purposes
   *
   *  @return                      the string representation
   */
  
  @Override
  public String toString ()
  {
    return domain != null ? 
      super.toString () + domain : super.toString ();
  }  
  
  
  
  //--- access methods ---------------------------------------------------------
  
  /**
   *  return the domain data
   *
   *  @return                the domain or {@code null} if the request
   *                         was unsuccessful and no domain has been returned
   */
  
  public DomainData getDomain ()
  {
    return domain;
  }
  
  
  
}

