Example #1
0
        internal static IFilter ReadFilter(XmlReader xml)
        {
            var elementName = xml.Name;
            var attributes  = new AttributeParser(xml);

            IFilter filter = null;

            switch (elementName.ToLower())
            {
            case "biggerthan":
                filter = ReadBiggerThanFilter(xml, attributes);
                break;

            case "regex":
                filter = ReadRegexFilter(xml, attributes);
                break;

            case "olderthan":
                filter = ReadOlderThanFilter(xml, attributes);
                break;

            case "wildcards":
                filter = ReadWildcardsFilter(xml, attributes);
                break;

            case "matchingnone":
                filter = ReadMatchingNoneFilter(xml, attributes);
                break;

            case "matchingall":
                filter = ReadMatchingAllFilter(xml, attributes);
                break;

            case "matchingany":
                filter = ReadMatchingAnyFilter(xml, attributes);
                break;

            case "smallerthan":
                filter = ReadSmallerThanFilter(xml, attributes);
                break;

            case "empty":
                filter = ReadEmptyFilter(xml, attributes);
                break;

            default:
                xml.Skip();
                throw new IgnoredElementException(elementName);
            }

            attributes.AssertNoUnused();

            return(filter);
        }
        internal static IFilter ReadFilter(XmlReader xml)
        {
            var elementName = xml.Name;
            var attributes = new AttributeParser(xml);

            IFilter filter = null;

            switch (elementName.ToLower())
            {
                case "biggerthan":
                    filter = ReadBiggerThanFilter(xml, attributes);
                    break;
                case "regex":
                    filter = ReadRegexFilter(xml, attributes);
                    break;
                case "olderthan":
                    filter = ReadOlderThanFilter(xml, attributes);
                    break;
                case "wildcards":
                    filter = ReadWildcardsFilter(xml, attributes);
                    break;
                case "matchingnone":
                    filter = ReadMatchingNoneFilter(xml, attributes);
                    break;
                case "matchingall":
                    filter = ReadMatchingAllFilter(xml, attributes);
                    break;
                case "matchingany":
                    filter = ReadMatchingAnyFilter(xml, attributes);
                    break;
                case "smallerthan":
                    filter = ReadSmallerThanFilter(xml, attributes);
                    break;
                case "empty":
                    filter = ReadEmptyFilter(xml, attributes);
                    break;
                default:
                    xml.Skip();
                    throw new IgnoredElementException(elementName);
            }

            attributes.AssertNoUnused();

            return filter;
        }
        internal static IRule ReadRule(XmlReader xml, bool includeFilter=true)
        {
            IRule rule;

            var elementName = xml.Name;

            switch (elementName.ToLower())
            {
                case "recycle":
                    rule = new RecycleRule();
                    break;
                case "delete":
                    rule = new DeleteRule();
                    break;
                case "ignore":
                    rule = new IgnoreRule();
                    break;
                case "move":
                    rule = new MoveRule();
                    break;
                case "rename":
                    rule = new RenameRule();
                    break;
                default:
                    xml.Skip();
                    throw new IgnoredElementException(elementName);
            }

            var attributes = new AttributeParser(xml);

            rule.Target              = attributes.GetOptional("Target").AsEnum(RuleTarget.FilesAndFolders);
            rule.AppliesToSubfolders = attributes.GetOptional("ApplyToSubfolders").AsBool(true);

            if (rule is MoveRuleBase)
            {
                ((MoveRuleBase)rule).IfExists =
                    attributes.GetOptional("ifexists").AsEnum(MoveRuleBase.IfExistsMode.Cancel);
            }

            if( rule is MoveRule)
            {
                ((MoveRule)rule).Destination = attributes.GetMandatory("destination").AsString();
                ((MoveRule)rule).CreateFolder = attributes.GetOptional("createfolder").AsBool(true);
            }

            if (rule is RenameRule)
            {
                ((RenameRule)rule).Name = attributes.GetMandatory("name").AsString();
            }

            attributes.AssertNoUnused();

            if (includeFilter)
            {
                var filters = ReadFilters(xml).ToArray();

                if (filters.Length == 0)
                    throw new Exception("You must specificy a filter for this rule");

                if (filters.Length > 1)
                    throw new Exception("You can only specify one filter at rule's root. Please use <MatchingAll>, <MatchingAny> or <MatchingNone>.");

                rule.Filter = filters.First();
            }

            return rule;
        }
Example #4
0
        internal static IRule ReadRule(XmlReader xml, bool includeFilter = true)
        {
            IRule rule;

            var elementName = xml.Name;

            switch (elementName.ToLower())
            {
            case "recycle":
                rule = new RecycleRule();
                break;

            case "delete":
                rule = new DeleteRule();
                break;

            case "ignore":
                rule = new IgnoreRule();
                break;

            case "move":
                rule = new MoveRule();
                break;

            case "rename":
                rule = new RenameRule();
                break;

            default:
                xml.Skip();
                throw new IgnoredElementException(elementName);
            }

            var attributes = new AttributeParser(xml);

            rule.Target = attributes.GetOptional("Target").AsEnum(RuleTarget.FilesAndFolders);
            rule.AppliesToSubfolders = attributes.GetOptional("ApplyToSubfolders").AsBool(true);

            if (rule is MoveRuleBase)
            {
                ((MoveRuleBase)rule).IfExists =
                    attributes.GetOptional("ifexists").AsEnum(MoveRuleBase.IfExistsMode.Cancel);
            }

            if (rule is MoveRule)
            {
                ((MoveRule)rule).Destination  = attributes.GetMandatory("destination").AsString();
                ((MoveRule)rule).CreateFolder = attributes.GetOptional("createfolder").AsBool(true);
            }

            if (rule is RenameRule)
            {
                ((RenameRule)rule).Name = attributes.GetMandatory("name").AsString();
            }

            attributes.AssertNoUnused();

            if (includeFilter)
            {
                var filters = ReadFilters(xml).ToArray();

                if (filters.Length == 0)
                {
                    throw new Exception("You must specificy a filter for this rule");
                }

                if (filters.Length > 1)
                {
                    throw new Exception("You can only specify one filter at rule's root. Please use <MatchingAll>, <MatchingAny> or <MatchingNone>.");
                }

                rule.Filter = filters.First();
            }

            return(rule);
        }