Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.Data Path DataTypes/HeuristicLab.Data/3.3/Path Types/PathValue.cs @ 9680

Last change on this file since 9680 was 9680, checked in by mkommend, 11 years ago

#2081: Corrected namespaces of new path value types and added build command.

File size: 3.1 KB
Line 
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
23using System;
24using System.IO;
25using HeuristicLab.Common;
26using HeuristicLab.Core;
27using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
28
29namespace 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}
Note: See TracBrowser for help on using the repository browser.