Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* TRANSFER LAYER */
- public class LinksCtrlDto
- {
- /// <summary>
- /// url link
- /// </summary>
- private string _url;
- /// <summary>
- /// name of link
- /// </summary>
- private string _name;
- /// <summary>
- /// description of string
- /// </summary>
- private string _description;
- /// <summary>
- /// constructor for LinksCtrlDto
- /// </summary>
- /// <param name="url"></param>
- /// <param name="name"></param>
- /// <param name="description"></param>
- public LinksCtrlDto( string url, string name, string description ) {
- //only if url si not null/empty, name is not null/empty, and description is not null/empty
- if ( string.IsNullOrEmpty( url ) )
- throw new ArgumentNullException( "url" );
- else
- this._url = url;
- if ( string.IsNullOrEmpty( name ) )
- throw new ArgumentNullException( "name" );
- else
- this._name = name;
- if ( string.IsNullOrEmpty( description ) )
- throw new ArgumentNullException( "description" );
- else
- this._description = description;
- }
- /// <summary>
- /// Url property as getter
- /// </summary>
- public string Url {
- get {
- if ( string.IsNullOrEmpty( Url ) )
- throw new ArgumentNullException( "Url" );
- else
- return this._url;
- }
- }
- /// <summary>
- /// Name property as getter
- /// </summary>
- public string Name {
- get {
- if ( string.IsNullOrEmpty( Name ) )
- throw new ArgumentNullException( "Name" );
- else
- return this._name;
- }
- }
- /// <summary>
- /// Description property as getter
- /// </summary>
- public string Description {
- get {
- if ( string.IsNullOrEmpty( Description ) )
- throw new ArgumentNullException( "Description" );
- else
- return this._description;
- }
- }
- }
- public class CategoriesCtrlDto
- {
- /// <summary>
- /// Name of category
- /// </summary>
- private string _name;
- /// <summary>
- /// List of links
- /// </summary>
- private List<LinksCtrlDto> _ListLink;
- /// <summary>
- /// constructor
- /// </summary>
- /// <param name="name"></param>
- /// <param name="Link"></param>
- public CategoriesCtrlDto( string name, List<LinksCtrlDto> Link) {
- if ( string.IsNullOrEmpty( name ) )
- throw new ArgumentNullException( "name" );
- else
- this._name = name;
- if ( _ListLink == null )
- throw new ArgumentNullException( "_ListLink" );
- else
- this._ListLink = Link;
- }
- /// <summary>
- /// add link in List
- /// </summary>
- /// <param name="url"></param>
- /// <param name="name"></param>
- /// <param name="description"></param>
- public List<LinksCtrlDto> AddLink( string url, string name ,string description ) {
- if ( string.IsNullOrEmpty( url ) )
- throw new ArgumentNullException( "AddLink TL Categories - url" );
- if ( string.IsNullOrEmpty( name ) )
- throw new ArgumentNullException( "Addlink TL Categories - name" );
- if ( string.IsNullOrEmpty( description ) )
- throw new ArgumentNullException( "Addlink TL Categories - description" );
- _ListLink.Add( new LinksCtrlDto( url, name, description ) );
- return _ListLink;
- }
- /// <summary>
- /// delete a link from list if URL name is matched
- /// </summary>
- /// <param name="url"></param>
- /// <param name="name"></param>
- /// <param name="description"></param>
- public void DeleteLink( string url ) {
- if ( string.IsNullOrEmpty( url ) )
- throw new ArgumentNullException( "deletelink TL url" );
- foreach ( LinksCtrlDto lk in _ListLink ) {
- if ( lk.Url == url ) {
- _ListLink.Remove( lk );
- break;
- }
- }
- }
- /// <summary>
- /// Verify that link already exists in specified category
- /// </summary>
- /// <param name="url"></param>
- /// <returns></returns>
- public bool LinkExists(string url){
- bool result = false;
- if ( string.IsNullOrEmpty( url ) )
- throw new ArgumentNullException( "LinkExists name TL Categories" );
- foreach ( LinksCtrlDto lk in Links ) {
- if ( lk.Url == url ) {
- result = true;
- break;
- }
- }
- return result;
- }
- /// <summary>
- /// Name property as getter
- /// </summary>
- public string Name {
- get {
- return this._name;
- }
- }
- /// <summary>
- /// List property as getter
- /// </summary>
- public ReadOnlyCollection<LinksCtrlDto> Links {
- get {
- return this._ListLink.AsReadOnly();
- }
- }
- }
- /* DATA LAYER */
- public class UrlStorage
- {
- /// <summary>
- /// Message string
- /// </summary>
- public static string messages;
- /// <summary>
- /// Path to XML File
- /// </summary>
- public const string FILEXML = @"D:\somewhere\example.xml";
- /// <summary>
- /// private constructor of UrlStorage
- /// useful to avoid creating instance of it
- /// </summary>
- private UrlStorage() {
- }
- /// <summary>
- /// singleton pattern
- /// </summary>
- public static readonly UrlStorage instance = new UrlStorage();
- /// <summary>
- /// read XML file
- /// </summary>
- public List<CategoriesCtrlDto> readXMLtoLIST()
- {
- List<CategoriesCtrlDto> CategoryList = null;
- try {
- if ( File.Exists( FILEXML ) ) {
- XmlDocument document = new XmlDocument();
- document.Load( FILEXML );
- XmlNodeList list_of_categories = document.SelectNodes( "/parent/Categories" );
- CategoryList = new List<CategoriesCtrlDto>();
- foreach ( XmlNode nodecategory in list_of_categories ) {
- if ( nodecategory.HasChildNodes ) {
- List<LinksCtrlDto> List1 = new List<LinksCtrlDto>();
- //if yes then we can create a list of links
- foreach ( XmlNode nodelinks in nodecategory.ChildNodes ) {
- List1.Add( new LinksCtrlDto( nodelinks.Attributes["url"].Value, nodelinks.Attributes["name"].Value, nodelinks.Attributes["descr"].Value ) );
- }
- CategoriesCtrlDto categ = new CategoriesCtrlDto( nodecategory.Attributes["Name"].Value, List1 );
- CategoryList.Add( categ );
- }
- }
- } else {
- //if file not exists, return an empty list of categories
- CategoryList = new List<CategoriesCtrlDto>( 0 );
- }
- //we close the document
- //document.Save();
- } catch ( Exception e ) {
- //show exception message
- messages = e.Message;
- }
- return CategoryList;
- }
- /// <summary>
- /// Find a category name
- /// it is useful if you want to prevent adding duplicate category
- /// or check if the category name exists for remove process
- /// </summary>
- /// <param name="source"></param>
- /// <param name="search"></param>
- /// <param name="categ_name"></param>
- /// <param name="mode"></param>
- /// <returns></returns>
- private bool find_category(string source, string search) {
- bool result = false;
- XmlDocument document = new XmlDocument();
- try {
- if ( File.Exists( source ) ) {
- document.Load( source );
- foreach ( XmlNode category_nodes in document ) {
- if ( ( category_nodes.Attributes["Name"].Value == search ) ) {
- result = true;
- break;
- }
- }
- }
- } catch {
- throw;
- } finally {
- document.Save( source );
- }
- return result;
- }
- /// <summary>
- /// Find a category name
- /// it is useful if you want to prevent adding duplicate category
- /// or check if the category name exists for remove process
- /// </summary>
- /// <param name="source"></param>
- /// <param name="search"></param>
- /// <param name="categ_name"></param>
- /// <param name="mode"></param>
- /// <returns></returns>
- private bool find_link( string source, string search, string categ_name ) {
- bool result = false;
- XmlDocument document = new XmlDocument();
- try {
- if ( File.Exists( source ) ) {
- document.Load( source );
- foreach ( XmlNode categ_node in document ) {
- XmlNodeList xnlist = document.SelectNodes( "/parent/Categories[@Name='" + categ_name + "']" );
- foreach ( XmlNode link_nodes in categ_node ) {
- if ( ( link_nodes.Attributes["url"].Value == search ) )
- result = true;
- break;
- }
- }
- } else
- result = false;
- } catch {
- throw;
- } finally {
- document.Save( source );
- }
- return result;
- }
- /// <summary>
- /// add link to source file
- /// to a specified category
- /// </summary>
- /// <param name="source"></param>
- /// <param name="categoryname"></param>
- /// <param name="links"></param>
- /// <returns></returns>
- public bool add_link( string category_name, string url, string name, string description ) {
- bool result = false;
- XmlDocument document = new XmlDocument();
- XmlTextWriter writer = new XmlTextWriter( FILEXML, Encoding.UTF8 );
- if ( string.IsNullOrEmpty( category_name ) )
- throw new ArgumentNullException( "Add link to category name DL" );
- if ( string.IsNullOrEmpty( url ) )
- throw new ArgumentNullException( "Add link url name DL" );
- if ( string.IsNullOrEmpty( name ) )
- throw new ArgumentNullException( "Add link name DL" );
- if ( string.IsNullOrEmpty( description ) )
- throw new ArgumentNullException( "Add link description DL" );
- try {
- if ( File.Exists( FILEXML ) ) {
- document.Load( FILEXML );
- document.PreserveWhitespace = false;
- XmlNodeList xnlist = document.SelectNodes( "/parent/Categories[@Name='" + category_name + "']" );
- if ( xnlist.Count > 0 ) {
- //if there are no duplicate then we can add link in XML file
- if ( !find_link( FILEXML, url, category_name ) ) {
- XmlElement element = document.CreateElement( "link" );
- element.SetAttribute( "url", url );
- element.SetAttribute( "name", name );
- element.SetAttribute( "descr", description );
- xnlist[0].AppendChild( element );
- result = true;
- } else {
- result = false;
- }
- } else
- throw new ArgumentNullException( "xnlist empty DataLayer" );
- }
- if ( result ) {
- document.WriteTo( writer );
- }
- } catch {
- throw;
- } finally {
- document.Save( FILEXML );
- }
- return result;
- }
- /// <summary>
- /// Add a category into XML File
- /// First, we search it for duplicates
- /// </summary>
- /// <param name="source"></param>
- /// <param name="category"></param>
- /// <returns></returns>
- public bool add_category( string category_name ) {
- bool result = false;
- XmlDocument document = null;
- XmlTextWriter writer = null;
- if ( string.IsNullOrEmpty(category_name) )
- throw new ArgumentNullException( "categories" );
- try {
- if ( File.Exists( FILEXML ) ) {
- document = new XmlDocument();
- writer = new XmlTextWriter( FILEXML, Encoding.UTF8 );
- document.Load( FILEXML );
- if ( !find_category( FILEXML, category_name ) ) {
- XmlElement element = document.CreateElement( "Categories" );
- element.SetAttribute( "Name", category_name );
- result = true;
- }
- } else
- result = false;
- //if is all ok then write data to XML file
- if ( result ) {
- document.WriteTo( writer );
- }
- } catch {
- throw;
- } finally {
- document.Save( FILEXML );
- }
- return result;
- }
- /// <summary>
- /// delete link if it has been found
- /// </summary>
- /// <param name="source"></param>
- /// <param name="category_name"></param>
- /// <param name="url"></param>
- /// <returns></returns>
- public bool delete_link( string category_name, string link_url ) {
- bool result = false;
- XmlDocument document = new XmlDocument();
- if ( string.IsNullOrEmpty(category_name) )
- throw new ArgumentNullException( "delete_link categories" );
- if ( string.IsNullOrEmpty( link_url ) )
- throw new ArgumentNullException( "delete_link DL" );
- try {
- if ( File.Exists( FILEXML ) ) {
- document.Load( FILEXML );
- if ( find_link( FILEXML, link_url, category_name ) ) {
- XmlNode xnlist = document.SelectSingleNode( "/parent/Categories[@Name='" + category_name + "'/link[@url='" + link_url + "']" );
- if ( xnlist != null ) {
- xnlist.ParentNode.RemoveChild( xnlist );
- result = true;
- }
- }
- }
- } catch {
- throw;
- } finally {
- document.Save( FILEXML );
- }
- return result;
- }
- /// <summary>
- /// delete category if it has been found
- /// </summary>
- /// <param name="source"></param>
- /// <param name="category_name"></param>
- /// <param name="name"></param>
- /// <returns></returns>
- public bool delete_category( string category_name ) {
- bool result = false;
- XmlDocument document = new XmlDocument();
- if ( string.IsNullOrEmpty(category_name) )
- throw new ArgumentNullException("delete_category DL");
- try {
- if ( File.Exists( FILEXML ) ) {
- document.Load( FILEXML );
- if ( find_category( FILEXML, category_name ) ) {
- XmlNode xnlist = document.SelectSingleNode( "/parent/Categories[@Name='" + category_name + "']" );
- if ( xnlist != null ) {
- xnlist.ParentNode.RemoveChild( xnlist );
- result = true;
- }
- }
- }
- } catch {
- throw;
- } finally {
- document.Save( FILEXML );
- }
- return result;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement