Copied remotely
[mono.git] / mcs / class / Microsoft.JScript / Microsoft.JScript / VsaItems.cs
1 //
2 // VsaItems.cs:
3 //
4 // Author:
5 //      Cesar Lopez Nataren (cesar@ciencias.unam.mx)
6 //
7 // (C) 2003, Cesar Lopez Nataren
8 //
9
10 //
11 // Permission is hereby granted, free of charge, to any person obtaining
12 // a copy of this software and associated documentation files (the
13 // "Software"), to deal in the Software without restriction, including
14 // without limitation the rights to use, copy, modify, merge, publish,
15 // distribute, sublicense, and/or sell copies of the Software, and to
16 // permit persons to whom the Software is furnished to do so, subject to
17 // the following conditions:
18 // 
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
21 // 
22 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
25 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
26 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
27 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
28 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29 //
30
31 using System.Collections;
32 using Microsoft.JScript.Vsa;
33 using Microsoft.Vsa;
34 using System;
35
36 namespace Microsoft.JScript {
37
38         public class VsaItems : IVsaItems {
39
40                 private ArrayList items;
41                 private VsaEngine engine;
42                 private ArrayList names;
43
44                 public VsaItems (VsaEngine engine)
45                 {
46                         this.items = new ArrayList ();
47                         this.engine = engine;
48                         this.names = new ArrayList ();
49                 }
50
51                 public IVsaItem this [int index] {
52
53                         get { 
54                                 int size = items.Count;
55
56                                 if (index < 0 || index > size)
57                                         throw new VsaException (VsaError.ItemNotFound);
58                                 return (IVsaItem) items [index];
59                         }
60                 }
61
62                 public IVsaItem this [string name] {
63                         get {
64                                 int i;
65                                 int size = items.Count;
66                                 IVsaItem item;
67
68                                 for (i = 0; i < size; i++) {
69                                         item = (IVsaItem) items [i];
70
71                                         if (item.Name == name)
72                                                 return item;
73                                         continue;
74                                 }
75                                 throw new VsaException (VsaError.ItemNotFound);
76                         }
77                 }
78
79                 public virtual int Count {
80                         get { 
81                                 if (engine.Closed)
82                                         throw new VsaException (VsaError.EngineClosed);
83                                 else if (!engine.InitNewCalled)
84                                         throw new VsaException (VsaError.EngineNotInitialized);
85
86                                 return items.Count;
87                         }
88                 }
89
90                 public virtual void Close ()
91                 {
92                         throw new NotImplementedException ();
93                 }
94
95                 public virtual IVsaItem CreateItem (string name, 
96                                                     VsaItemType itemType,
97                                                     VsaItemFlag itemFlag)
98                 {
99                         if (names.Contains (name))
100                                  throw new VsaException (VsaError.ItemNameInUse);
101
102                         IVsaItem item = null;
103
104                         switch (itemType) {
105                         case VsaItemType.AppGlobal:
106                                 if (itemFlag != VsaItemFlag.None)
107                                         throw new VsaException (VsaError.ItemFlagNotSupported);
108                                 item = new VsaGlobalItem (engine, name, itemFlag);
109                                 break;
110
111                         case VsaItemType.Code:
112                                 item = new VsaCodeItem (engine, name, itemFlag);
113                                 break;
114
115                         case VsaItemType.Reference:
116                                 if (itemFlag != VsaItemFlag.None)
117                                         throw new VsaException (VsaError.ItemFlagNotSupported);
118                                 item = new VsaReferenceItem (engine, name, itemFlag);
119                                         break;
120                         }
121                                 
122                         if (item != null) {
123                                 items.Add (item);
124                                 names.Add (name);
125                         }
126                                 
127                         engine.IsDirty = true;
128
129                         return item;
130                 }
131
132                 public virtual IEnumerator GetEnumerator ()
133                 {
134                         return items.GetEnumerator ();
135                 }
136
137                 public virtual void Remove (string itemName)
138                 {
139                         int i;
140                         int size = items.Count;
141
142                         for (i = 0; i < size; i++)
143                                 if (((IVsaItem) items[i]).Name == itemName) {
144                                         items.RemoveAt (i);
145                                         return;
146                                 }
147                                                 
148                         throw new VsaException (VsaError.ItemNotFound);
149                 }
150
151                 public virtual void Remove (int itemIndex)
152                 {                       
153                         if (itemIndex < 0 || itemIndex > items.Count)
154                                 throw new VsaException (VsaError.ItemNotFound);
155
156                         items.RemoveAt (itemIndex);
157                 } 
158         }
159 }