|
1 /* |
|
2 * Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies). |
|
3 * All rights reserved. |
|
4 * This component and the accompanying materials are made available |
|
5 * under the terms of "Eclipse Public License v1.0" |
|
6 * which accompanies this distribution, and is available |
|
7 * at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
8 * |
|
9 * Initial Contributors: |
|
10 * Nokia Corporation - initial contribution. |
|
11 * |
|
12 * Contributors: |
|
13 * |
|
14 * Description: |
|
15 * |
|
16 */ |
|
17 using System; |
|
18 using System.Collections.Generic; |
|
19 using System.Text; |
|
20 using System.IO; |
|
21 |
|
22 namespace SymbianUtils.FileSystem |
|
23 { |
|
24 public abstract class FSEntity |
|
25 { |
|
26 #region Static constructors |
|
27 public static FSEntity New( string aFSEntityName ) |
|
28 { |
|
29 FSEntity ret = null; |
|
30 // |
|
31 try |
|
32 { |
|
33 try |
|
34 { |
|
35 DirectoryInfo dir = new DirectoryInfo( aFSEntityName ); |
|
36 if ( dir.Exists ) |
|
37 { |
|
38 ret = new FSEntityDirectory( dir ); |
|
39 } |
|
40 else |
|
41 { |
|
42 ret = new FSEntityFile( new FileInfo( aFSEntityName ) ); |
|
43 } |
|
44 } |
|
45 catch ( Exception ) |
|
46 { |
|
47 ret = new FSEntityFile( new FileInfo( aFSEntityName ) ); |
|
48 } |
|
49 } |
|
50 catch ( Exception ) |
|
51 { |
|
52 } |
|
53 // |
|
54 if ( ret == null ) |
|
55 { |
|
56 throw new FileNotFoundException( "Could not locate suitable entity", aFSEntityName ); |
|
57 } |
|
58 // |
|
59 return ret; |
|
60 } |
|
61 |
|
62 public static FSEntity New( FSEntity aClone ) |
|
63 { |
|
64 FSEntity ret = null; |
|
65 // |
|
66 if ( aClone is FSEntityFile ) |
|
67 { |
|
68 ret = new FSEntityFile( ( (FSEntityFile) aClone ).File ); |
|
69 } |
|
70 else if ( aClone is FSEntityDirectory ) |
|
71 { |
|
72 ret = new FSEntityDirectory( ( (FSEntityDirectory) aClone ).Directory ); |
|
73 } |
|
74 else |
|
75 { |
|
76 throw new ArgumentException( "Unsupported File System Entity Type" ); |
|
77 } |
|
78 // |
|
79 return ret; |
|
80 } |
|
81 #endregion |
|
82 |
|
83 #region Constructors |
|
84 protected FSEntity() |
|
85 { |
|
86 } |
|
87 #endregion |
|
88 |
|
89 #region API |
|
90 #endregion |
|
91 |
|
92 #region Properties |
|
93 public bool IsFile |
|
94 { |
|
95 get { return this is FSEntityFile; } |
|
96 } |
|
97 |
|
98 public bool IsDirectory |
|
99 { |
|
100 get { return this is FSEntityDirectory; } |
|
101 } |
|
102 |
|
103 public abstract string FullName |
|
104 { |
|
105 get; |
|
106 } |
|
107 |
|
108 public abstract bool IsValid |
|
109 { |
|
110 get; |
|
111 } |
|
112 |
|
113 public abstract bool Exists |
|
114 { |
|
115 get; |
|
116 } |
|
117 #endregion |
|
118 |
|
119 #region Internal API |
|
120 #endregion |
|
121 |
|
122 #region Internal methods |
|
123 #endregion |
|
124 |
|
125 #region Operators |
|
126 public static bool operator ==( FSEntity aLeft, FSEntity aRight ) |
|
127 { |
|
128 // If both are null, or both are same instance, return true. |
|
129 if ( System.Object.ReferenceEquals( aLeft, aRight ) ) |
|
130 { |
|
131 return true; |
|
132 } |
|
133 |
|
134 // If one is null, but not both, return false. |
|
135 if ( ( (object) aLeft == null ) || ( (object) aRight == null ) ) |
|
136 { |
|
137 return false; |
|
138 } |
|
139 |
|
140 // Return true if the fields match: |
|
141 return aLeft.Equals( aRight ); |
|
142 } |
|
143 |
|
144 public static bool operator !=( FSEntity aLeft, FSEntity aRight ) |
|
145 { |
|
146 return !( aLeft == aRight ); |
|
147 } |
|
148 #endregion |
|
149 |
|
150 #region From System.Object |
|
151 public override int GetHashCode() |
|
152 { |
|
153 return FullName.GetHashCode(); |
|
154 } |
|
155 |
|
156 public override bool Equals( object aObject ) |
|
157 { |
|
158 if ( aObject != null ) |
|
159 { |
|
160 if ( aObject is FSEntity ) |
|
161 { |
|
162 FSEntity other = (FSEntity) aObject; |
|
163 // |
|
164 if ( other.IsFile && this.IsFile ) |
|
165 { |
|
166 return other.FullName.Equals( this.FullName ); |
|
167 } |
|
168 else if ( other.IsDirectory && this.IsDirectory ) |
|
169 { |
|
170 return other.FullName.Equals( this.FullName ); |
|
171 } |
|
172 } |
|
173 } |
|
174 // |
|
175 return base.Equals( aObject ); |
|
176 } |
|
177 |
|
178 public override string ToString() |
|
179 { |
|
180 return FullName; |
|
181 } |
|
182 #endregion |
|
183 |
|
184 #region Data members |
|
185 #endregion |
|
186 } |
|
187 } |