//\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
// CommonHelpers satellite library | Author: Sergei Y.Kitáev
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
using System;
using System.Xml;

namespace CommonHelpers {

//**************************************************************************************************
partial class HXmlConfigurationBase {
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

/// <exception cref="ArgumentNullException" />
/// <exception cref="InvalidOperationException" />
/// <exception cref="HXmlConfigurationException" />
protected XmlElement CreateElement(string name)
{
   HArgChecking.VerifyNotNull( name );
   VerifyDocumentLoaded();
   try
      { return Document.CreateElement( name, TargetNamespace ); }
   catch (Exception e)
      { throw CreateExceptionWithFile( e.Message ); }
}

//--------------------------------------------------------------------------------------------------

/// <exception cref="ArgumentNullException" />
/// <exception cref="InvalidOperationException" />
/// <exception cref="HXmlConfigurationException" />
protected XmlElement CreateAndAppendElement(string name)
{
   return (XmlElement)CurrentElement.AppendChild( CreateElement( name ) );
}

//==================================================================================================

/// <exception cref="ArgumentNullException" />
/// <exception cref="ArgumentException" />
/// <exception cref="InvalidOperationException" />
/// <exception cref="HObjectNotFoundException" />
/// <exception cref="HXmlConfigurationException" />
protected void RemoveAttribute(string name)
{
   RemoveAttribute( name, false );
}

/// <exception cref="ArgumentNullException" />
/// <exception cref="ArgumentException" />
/// <exception cref="InvalidOperationException" />
/// <exception cref="HObjectNotFoundException" />
/// <exception cref="HXmlConfigurationException" />
bool RemoveAttribute(string name,bool ignoreNotFoundError)
{
   HArgChecking.VerifyNotNull( name );
   VerifyCurrentElementSelected();
   try
   {
      var attribute = CurrentElement.GetAttributeNode( name );
      if ( attribute == null )
         if ( ignoreNotFoundError )
            return false;
         else
            throw new HObjectNotFoundException(ProvideErrorMessageWithFile(
               Verbose.ATTRIBUTE_NOT_FOUND_0, name ));
      CurrentElement.RemoveAttributeNode( attribute );
      UpdateAttributeValueCollection();
   }
   catch (Exception e)
   {
      var configurationException = CreateExceptionWithPath( e.Message );
      if ( e is HObjectNotFoundException )
         throw new HObjectNotFoundException( configurationException.Message );
      throw configurationException;
   }
   return true;
}

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
} // HXmlConfigurationBase
//**************************************************************************************************

} // CommonHelpers
