File: StringAttributeCollection.cs

package info (click to toggle)
mono 6.12.0.199%2Bdfsg-6
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 1,296,836 kB
  • sloc: cs: 11,181,803; xml: 2,850,076; ansic: 699,709; cpp: 123,344; perl: 59,361; javascript: 30,841; asm: 21,853; makefile: 20,405; sh: 15,009; python: 4,839; pascal: 925; sql: 859; sed: 16; php: 1
file content (176 lines) | stat: -rw-r--r-- 5,365 bytes parent folder | download | duplicates (7)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
//------------------------------------------------------------------------------
// <copyright file="CommaDelimitedStringCollection.cs" company="Microsoft">
//     Copyright (c) Microsoft Corporation.  All rights reserved.
// </copyright>
//------------------------------------------------------------------------------

using System;
using System.ComponentModel;
using System.Security.Permissions;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Reflection;
using System.Text;

namespace System.Configuration {

    public sealed class CommaDelimitedStringCollection : StringCollection {
        private bool _Modified;
        private bool _ReadOnly;
        private string _OriginalString;

        //
        // Constructor
        //

        public CommaDelimitedStringCollection() {
            _ReadOnly = false;
            _Modified = false;
            _OriginalString = ToString();
        }

        internal void FromString(string list) {
            char[] _delimiters = { ',' };
            if (list != null) {
                string[] items = list.Split(_delimiters);
                foreach (string item in items) {
                    string trimmedItem = item.Trim();
                    if (trimmedItem.Length != 0) {
                        Add(item.Trim());
                    }
                }
            }
            _OriginalString = ToString();
            _ReadOnly = false;
            _Modified = false;
        }

        public override string ToString() {
            string returnString = null;

            if (Count > 0) {
                StringBuilder sb = new StringBuilder();
                foreach (string str in this) {
                    ThrowIfContainsDelimiter(str); // Since the add methods are not virtual they could still add bad data
                                                   // by casting the collection to a string collection.  This check will catch
                                                   // it before serialization, late is better than never.
                    sb.Append(str.Trim());
                    sb.Append(',');
                }
                returnString = sb.ToString();
                if (returnString.Length > 0) {
                    returnString = returnString.Substring(0, returnString.Length - 1);
                }
                if (returnString.Length == 0) {
                    returnString = null;
                }
            }
            return returnString;
        }

        private void ThrowIfReadOnly() {
            if (IsReadOnly == true) {
                throw new ConfigurationErrorsException(SR.GetString(SR.Config_base_read_only));
            }
        }

        private void ThrowIfContainsDelimiter(string value) {
            if (value.Contains(",")) {
                throw new ConfigurationErrorsException(SR.GetString(SR.Config_base_value_cannot_contain,","));
            }
        }

        public void SetReadOnly() {
            _ReadOnly = true;
        }

        public bool IsModified {
            get {
                return _Modified || (ToString() != _OriginalString);
            }
        }

        public new bool IsReadOnly {
            get {
                return _ReadOnly;
            }
        }

        //
        // Accessors
        //
        public new string this[int index] {
            get {
                return (string)base[index];
            }
            set {
                ThrowIfReadOnly();
                ThrowIfContainsDelimiter(value);
                _Modified = true;
                base[index] = value.Trim();
            }
        }

        //
        // Methods
        //

        public new void Add(string value) {
            ThrowIfReadOnly();
            ThrowIfContainsDelimiter(value);
            _Modified = true;
            base.Add(value.Trim());
        }

        public new void AddRange(string[] range) {
            ThrowIfReadOnly();
            _Modified = true;
            foreach (string str in range) {
                ThrowIfContainsDelimiter(str);
                base.Add(str.Trim());
            }
        }

        public new void Clear() {
            ThrowIfReadOnly();
            _Modified = true;
            base.Clear();
        }
        public new void Insert(int index, string value) {
            ThrowIfReadOnly();
            ThrowIfContainsDelimiter(value);
            _Modified = true;
            base.Insert(index, value.Trim());
        }

        public new void Remove(string value) {
            ThrowIfReadOnly();
            ThrowIfContainsDelimiter(value);
            _Modified = true;
            base.Remove(value.Trim());
        }

        // Clone
        //
        // Clone the object, to get back and object that we can modify
        // without changing the original object
        //
        public CommaDelimitedStringCollection Clone() {
            CommaDelimitedStringCollection copy;

            copy = new CommaDelimitedStringCollection();

            // Copy all values
            foreach (string str in this) {
                copy.Add(str);
            }

            // Copy Attributes
            copy._Modified = false;
            copy._ReadOnly = _ReadOnly;
            copy._OriginalString = _OriginalString;

            return copy;
        }
    }
}