#region Disclaimer / License // Copyright (C) 2010, Jackie Ng // http://trac.osgeo.org/mapguide/wiki/maestro, jumpinjackie@gmail.com // // 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 (at your option) 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA // #endregion using System; using System.Collections.Generic; using System.Text; using OSGeo.MapGuide.MaestroAPI.Exceptions; namespace OSGeo.MapGuide.MaestroAPI.Resource.Validation { /// /// A collection of resource validators /// public static class ResourceValidatorSet { private static List m_validators = new List(); /// /// Registers the validator. /// /// The validator. public static void RegisterValidator(IResourceValidator validator) { Check.NotNull(validator, "validator"); if (!m_validators.Contains(validator)) m_validators.Add(validator); } /// /// Validates the specified items. /// /// The items. /// if set to true [recurse]. /// public static ValidationIssue[] Validate(ResourceValidationContext context, IEnumerable items, bool recurse) { Check.NotNull(items, "items"); var issues = new List(); foreach (var item in items) { issues.AddRange(Validate(context, item, true)); } return issues.ToArray(); } /// /// Validates the specified item using an existing validation context to skip over /// items already validated /// /// /// /// /// public static ValidationIssue[] Validate(ResourceValidationContext context, IResource item, bool recurse) { Check.NotNull(item, "item"); var issueSet = new ValidationResultSet(); if (!HasValidator(item.ResourceType, item.ResourceVersion)) { issueSet.AddIssue(new ValidationIssue(item, ValidationStatus.Warning, string.Format(Properties.Resources.ERR_NO_REGISTERED_VALIDATOR, item.ResourceType, item.ResourceVersion))); } else { foreach (IResourceValidator v in m_validators) { if (!v.SupportedResourceAndVersion.Equals(item.GetResourceTypeDescriptor())) continue; try { ValidationIssue[] tmp = v.Validate(context, item, recurse); if (tmp != null) issueSet.AddIssues(tmp); } catch (Exception ex) { string msg = NestedExceptionMessageProcessor.GetFullMessage(ex); issueSet.AddIssue(new ValidationIssue(item, ValidationStatus.Error, "Failed in validator: " + msg)); } } } return issueSet.GetAllIssues(); } /// /// Determines whether the specified resource types has validator. /// /// The resource types. /// The version. /// /// true if the specified resource types has validator; otherwise, false. /// public static bool HasValidator(ResourceTypes resourceTypes, Version version) { bool found = false; var find = new ResourceTypeDescriptor(resourceTypes, version.ToString()); foreach (var v in m_validators) { if (v.SupportedResourceAndVersion.Equals(find)) { found = true; break; } } return found; } } }