#region Disclaimer / License // Copyright (C) 2009, Kenneth Skovhede // http://www.hexad.dk, opensource@hexad.dk // // 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; namespace OSGeo.MapGuide.Maestro.ResourceValidators { /// /// The interface for validating an item /// public interface IValidator { ValidationIssue[] Validate(object resource, bool recurse); } /// /// A class holding results of a validation /// public class ValidationIssue { private string m_message; private ValidationStatus m_status; private string m_field; private object m_resource; /// /// Gets the message for the validation issue /// public string Message { get { return m_message; } } /// /// Gets the status of the validation issue /// public ValidationStatus Status { get { return m_status; } } /// /// Gets the field the issue relates to (if any) /// public string Field { get { return m_field; } } /// /// Returns the resource the issue is related to /// public object Resource { get { return m_resource; } } /// /// Returns a textual representation of the issue /// /// A textual representation of the issue public override string ToString() { return string.Format("{0}: {1}", this.Status, this.Message) + (this.Field == null ? "" : "(" + this.Field + ")"); } /// /// Constructs a new validation issue /// /// The issue status /// The issue message public ValidationIssue(object resource, ValidationStatus status, string message) { m_message = message; m_status = status; m_resource = resource; } /// /// Constructs a new validation issue /// /// The issue status /// The issue message /// The field thtat the issue relates to public ValidationIssue(object resource, ValidationStatus status, string message, string field) : this(resource, status, message) { m_field = field; } } /// /// All possible states a validation issue may have /// public enum ValidationStatus { /// /// Indicates that the issue is non-vital, eg. a performance problem /// Information, /// /// Indicates that the issue is likely to cause problems /// Warning, /// /// Indicates that the issue will prevent correct operation of the map /// Error } public static class Validation { private static List m_validators = new List(); public static ValidationIssue[] Validate(object item, bool recurse) { List issues = new List(); foreach (IValidator v in m_validators) { try { ValidationIssue[] tmp = v.Validate(item, recurse); if (tmp != null) issues.AddRange(tmp); } catch (Exception ex) { string msg = NestedExceptionMessageProcessor.GetFullMessage(ex); issues.Add(new ValidationIssue(item, ValidationStatus.Error, "Failed in validator: " + msg)); } } return issues.ToArray(); } public static void RegisterValidator(IValidator v) { if (!m_validators.Contains(v)) m_validators.Add(v); } } }