/// <summary> /// Determines whether a category exists. /// </summary> /// <param name="category">The category to check.</param> /// <returns><c>true</c> if the category exists, <c>false</c> otherwise.</returns> private bool CategoryExists(CategoryInfo category) { lock(this) { CategoryInfo[] cats = GetCategories(FindNamespace(NameTools.GetNamespace(category.FullName), GetNamespaces())); CategoryNameComparer comp = new CategoryNameComparer(); for(int i = 0; i < cats.Length; i++) { if(comp.Compare(cats[i], category) == 0) return true; } } return false; }
/// <summary> /// Renames a Category. /// </summary> /// <param name="category">The Category to rename.</param> /// <param name="newName">The new Name.</param> /// <returns>The correct CategoryInfo object.</returns> /// <exception cref="ArgumentNullException">If <paramref name="category"/> or <paramref name="newName"/> are <c>null</c>.</exception> /// <exception cref="ArgumentException">If <paramref name="newName"/> is empty.</exception> public CategoryInfo RenameCategory(CategoryInfo category, string newName) { if(category == null) throw new ArgumentNullException("category"); if(newName == null) throw new ArgumentNullException("newName"); if(newName.Length == 0) throw new ArgumentException("New Name cannot be empty", "newName"); lock(this) { CategoryInfo result = new CategoryInfo(NameTools.GetFullName(NameTools.GetNamespace(category.FullName), newName), this); if(CategoryExists(result)) return null; CategoryInfo[] cats = GetAllCategories(); CategoryNameComparer comp = new CategoryNameComparer(); for(int i = 0; i < cats.Length; i++) { if(comp.Compare(cats[i], category) == 0) { result.Pages = cats[i].Pages; cats[i] = result; DumpCategories(cats); categoriesCache = null; return result; } } } return null; }
/// <summary> /// Binds a Page with one or more Categories. /// </summary> /// <param name="page">The Page to bind.</param> /// <param name="categories">The Categories to bind the Page with (full name).</param> /// <returns>True if the binding succeeded.</returns> /// <remarks>After a successful operation, the Page is bound with all and only the categories passed as argument.</remarks> /// <exception cref="ArgumentNullException">If <paramref name="page"/> or <paramref name="categories"/> are <c>null</c>.</exception> public bool RebindPage(PageInfo page, string[] categories) { if(page == null) throw new ArgumentNullException("page"); if(categories == null) throw new ArgumentNullException("categories"); lock(this) { if(!PageExists(page)) return false; CategoryInfo[] cats = GetAllCategories(); // Check all categories (they all must exist and be valid) foreach(string cat in categories) { if(cat == null) throw new ArgumentNullException("categories", "A category name cannot be null"); if(cat.Length == 0) throw new ArgumentException("A category name cannot be empty", "categories"); CategoryNameComparer comp = new CategoryNameComparer(); if(Array.Find<CategoryInfo>(cats, delegate(CategoryInfo x) { return comp.Compare(x, new CategoryInfo(cat, this)) == 0; }) == null) return false; } // Operations: // - Remove the Page from every Category // - For each specified category, add (if needed) the Page List<string> pages; CategoryNameComparer catComp = new CategoryNameComparer(); for(int i = 0; i < cats.Length; i++) { pages = new List<string>(cats[i].Pages); int idx = GetIndex(pages, page.FullName); if(idx != -1) pages.Remove(pages[idx]); cats[i].Pages = pages.ToArray(); } for(int i = 0; i < cats.Length; i++) { for(int k = 0; k < categories.Length; k++) { if(catComp.Compare(cats[i], new CategoryInfo(categories[k], this)) == 0) { pages = new List<string>(cats[i].Pages); pages.Add(page.FullName); cats[i].Pages = pages.ToArray(); } } } DumpCategories(cats); pagesCache = null; categoriesCache = null; } return true; }
/// <summary> /// Removes a Category. /// </summary> /// <param name="category">The Category to remove.</param> /// <returns>True if the Category has been removed successfully.</returns> /// <exception cref="ArgumentNullException">If <paramref name="category"/> is <c>null</c>.</exception> public bool RemoveCategory(CategoryInfo category) { if(category == null) throw new ArgumentNullException("category"); lock(this) { CategoryInfo[] cats = GetAllCategories(); CategoryNameComparer comp = new CategoryNameComparer(); for(int i = 0; i < cats.Length; i++) { if(comp.Compare(cats[i], category) == 0) { List<CategoryInfo> tmp = new List<CategoryInfo>(cats); tmp.Remove(tmp[i]); DumpCategories(tmp.ToArray()); categoriesCache = null; return true; } } } return false; }
/// <summary> /// Merges two Categories. /// </summary> /// <param name="source">The source Category.</param> /// <param name="destination">The destination Category.</param> /// <returns>True if the Categories have been merged successfully.</returns> /// <remarks>The destination Category remains, while the source Category is deleted, and all its Pages re-bound in the destination Category.</remarks> /// <exception cref="ArgumentNullException">If <paramref name="source"/> or <paramref name="destination"/> are <c>null</c>.</exception> public CategoryInfo MergeCategories(CategoryInfo source, CategoryInfo destination) { if(source == null) throw new ArgumentNullException("source"); if(destination == null) throw new ArgumentNullException("destination"); lock(this) { NamespaceInfo[] allNamespaces = GetNamespaces(); NamespaceInfo sourceNs = FindNamespace(NameTools.GetNamespace(source.FullName), allNamespaces); NamespaceInfo destinationNs = FindNamespace(NameTools.GetNamespace(destination.FullName), allNamespaces); NamespaceComparer nsComp = new NamespaceComparer(); if(!(sourceNs == null && destinationNs == null) && nsComp.Compare(sourceNs, destinationNs) != 0) { // Different namespaces return null; } CategoryInfo[] cats = GetAllCategories(); int idxSource = -1, idxDest = -1; CategoryNameComparer comp = new CategoryNameComparer(); for(int i = 0; i < cats.Length; i++) { if(comp.Compare(cats[i], source) == 0) idxSource = i; if(comp.Compare(cats[i], destination) == 0) idxDest = i; if(idxSource != -1 && idxDest != -1) break; } if(idxSource == -1 || idxDest == -1) return null; List<CategoryInfo> tmp = new List<CategoryInfo>(cats); List<string> newPages = new List<string>(cats[idxDest].Pages); for(int i = 0; i < cats[idxSource].Pages.Length; i++) { bool found = false; for(int k = 0; k < newPages.Count; k++) { if(StringComparer.OrdinalIgnoreCase.Compare(newPages[k], cats[idxSource].Pages[i]) == 0) { found = true; break; } } if(!found) { newPages.Add(cats[idxSource].Pages[i]); } } tmp[idxDest].Pages = newPages.ToArray(); tmp.Remove(tmp[idxSource]); DumpCategories(tmp.ToArray()); CategoryInfo newCat = new CategoryInfo(destination.FullName, this); newCat.Pages = newPages.ToArray(); categoriesCache = null; return newCat; } }