1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2013 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
4 | *
|
---|
5 | * This file is part of HeuristicLab.
|
---|
6 | *
|
---|
7 | * HeuristicLab is free software: you can redistribute it and/or modify
|
---|
8 | * it under the terms of the GNU General Public License as published by
|
---|
9 | * the Free Software Foundation, either version 3 of the License, or
|
---|
10 | * (at your option) any later version.
|
---|
11 | *
|
---|
12 | * HeuristicLab is distributed in the hope that it will be useful,
|
---|
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
15 | * GNU General Public License for more details.
|
---|
16 | *
|
---|
17 | * You should have received a copy of the GNU General Public License
|
---|
18 | * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
|
---|
19 | */
|
---|
20 | #endregion
|
---|
21 |
|
---|
22 |
|
---|
23 | using System;
|
---|
24 | using System.IO;
|
---|
25 | using HeuristicLab.Common;
|
---|
26 | using HeuristicLab.Core;
|
---|
27 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
28 |
|
---|
29 | namespace HeuristicLab.Data {
|
---|
30 | [Item("PathValue", "Represents a path.")]
|
---|
31 | [StorableClass]
|
---|
32 | public abstract class PathValue : StringValue {
|
---|
33 | [Storable]
|
---|
34 | private bool checkExistence;
|
---|
35 | public bool CheckExistence {
|
---|
36 | get { return checkExistence; }
|
---|
37 | set {
|
---|
38 | if (ReadOnly) throw new NotSupportedException("CheckExistence cannot be set. PathValue is read-only.");
|
---|
39 | if (value != checkExistence) {
|
---|
40 | checkExistence = value;
|
---|
41 | OnCheckExistenceChanged();
|
---|
42 | }
|
---|
43 | }
|
---|
44 | }
|
---|
45 |
|
---|
46 | public override string Value {
|
---|
47 | get { return base.Value; }
|
---|
48 | set {
|
---|
49 | if (value == null) value = string.Empty;
|
---|
50 | if (checkExistence && !string.IsNullOrEmpty(value) && !File.Exists(value) && !Directory.Exists(value))
|
---|
51 | throw new ArgumentException(string.Format("The given file {0} does not exists.", value));
|
---|
52 | if (value[value.Length - 1] == Path.DirectorySeparatorChar)
|
---|
53 | value = value.TrimEnd(Path.DirectorySeparatorChar);
|
---|
54 | if (value[value.Length - 1] == Path.AltDirectorySeparatorChar)
|
---|
55 | value = value.TrimEnd(Path.AltDirectorySeparatorChar);
|
---|
56 | base.Value = value;
|
---|
57 | }
|
---|
58 | }
|
---|
59 |
|
---|
60 | [StorableConstructor]
|
---|
61 | protected PathValue(bool deserializing) : base(deserializing) { }
|
---|
62 | protected PathValue(PathValue original, Cloner cloner)
|
---|
63 | : base(original, cloner) {
|
---|
64 | checkExistence = original.checkExistence;
|
---|
65 | }
|
---|
66 |
|
---|
67 | protected PathValue()
|
---|
68 | : base(string.Empty) {
|
---|
69 | checkExistence = true;
|
---|
70 | }
|
---|
71 |
|
---|
72 | public abstract bool Exists();
|
---|
73 |
|
---|
74 | protected override bool Validate(string value, out string errorMessage) {
|
---|
75 | if (CheckExistence && !File.Exists(value) && !Directory.Exists(value)) {
|
---|
76 | errorMessage = string.Format("The given path {0} does not exist.", value);
|
---|
77 | return false;
|
---|
78 | }
|
---|
79 | return base.Validate(value, out errorMessage);
|
---|
80 | }
|
---|
81 |
|
---|
82 | public event EventHandler CheckExistenceChanged;
|
---|
83 | protected virtual void OnCheckExistenceChanged() {
|
---|
84 | var handler = CheckExistenceChanged;
|
---|
85 | if (handler != null)
|
---|
86 | handler(this, EventArgs.Empty);
|
---|
87 | }
|
---|
88 | }
|
---|
89 | }
|
---|