~ifolder-dev/simias/trunk-packaging

« back to all changes in this revision

Viewing changes to src/core/CollectionStore/.svn/text-base/BaseFileNode.cs.svn-base

  • Committer: Jorge O. Castro
  • Date: 2007-12-03 06:56:46 UTC
  • Revision ID: jorge@ubuntu.com-20071203065646-mupcnjcwgm5mnhyt
* Remove a bunch of .svn directories we no longer need.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/****************************************************************************
2
 
 |
3
 
 | Copyright (c) 2007 Novell, Inc.
4
 
 | All Rights Reserved.
5
 
 |
6
 
 | This program is free software; you can redistribute it and/or
7
 
 | modify it under the terms of version 2 of the GNU General Public License as
8
 
 | published by the Free Software Foundation.
9
 
 |
10
 
 | This program is distributed in the hope that it will be useful,
11
 
 | but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 
 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 
 | GNU General Public License for more details.
14
 
 |
15
 
 | You should have received a copy of the GNU General Public License
16
 
 | along with this program; if not, contact Novell, Inc.
17
 
 |
18
 
 | To contact Novell about this file by physical or electronic mail,
19
 
 | you may find current contact information at www.novell.com 
20
 
 |
21
 
 |   Author: Mike Lasky <mlasky@novell.com>
22
 
 |***************************************************************************/
23
 
 
24
 
using System;
25
 
using System.Collections;
26
 
using System.Collections.Specialized;
27
 
using System.IO;
28
 
using System.Reflection;
29
 
using System.Xml;
30
 
 
31
 
using Simias.Client;
32
 
 
33
 
namespace Simias.Storage
34
 
{
35
 
        /// <summary>
36
 
        /// Represents a generic file object.
37
 
        /// </summary>
38
 
        [ Serializable ]
39
 
        public abstract class BaseFileNode : Node
40
 
        {
41
 
                #region Class Members
42
 
                /// <summary>
43
 
                /// Xml file where metadata file types are registered.
44
 
                /// </summary>
45
 
                private const string metadataExtensionFile = "csmetadata.xml";
46
 
 
47
 
                /// <summary>
48
 
                /// Well known xml attribute tags.
49
 
                /// </summary>
50
 
                private const string associatedtypeString = "associatedtype";
51
 
                private const string extensionString = "extension";
52
 
                private const string assemblyString = "assembly";
53
 
 
54
 
                /// <summary>
55
 
                /// Name of the method to invoke to gather the file's metadata.
56
 
                /// </summary>
57
 
                private const string metadataMethodName = "GetMetaData";
58
 
                #endregion
59
 
 
60
 
                #region Properties
61
 
                /// <summary>
62
 
                /// Gets or sets the file creation time in the metadata.
63
 
                /// </summary>
64
 
                new public DateTime CreationTime
65
 
                {
66
 
                        get 
67
 
                        { 
68
 
                                Property p = properties.FindSingleValue( PropertyTags.CreationTime );
69
 
                                return ( p != null ) ? ( DateTime )p.Value : DateTime.MinValue;
70
 
                        }
71
 
 
72
 
                        set     { properties.ModifyNodeProperty( PropertyTags.CreationTime, value ); }
73
 
                }
74
 
 
75
 
                /// <summary>
76
 
                /// Gets or sets the file last access time in the metadata.
77
 
                /// </summary>
78
 
                public DateTime LastAccessTime
79
 
                {
80
 
                        get 
81
 
                        { 
82
 
                                Property p = properties.FindSingleValue( PropertyTags.LastAccessTime );
83
 
                                return ( p != null ) ? ( DateTime )p.Value : DateTime.MinValue;
84
 
                        }
85
 
 
86
 
                        set { properties.ModifyNodeProperty( PropertyTags.LastAccessTime, value ); }
87
 
                }
88
 
 
89
 
                /// <summary>
90
 
                /// Gets or sets the file last write time in the metadata.
91
 
                /// </summary>
92
 
                public DateTime LastWriteTime
93
 
                {
94
 
                        get 
95
 
                        { 
96
 
                                Property p = properties.FindSingleValue( PropertyTags.LastWriteTime );
97
 
                                return ( p != null ) ? ( DateTime )p.Value : DateTime.MinValue;
98
 
                        }
99
 
 
100
 
                        set { properties.ModifyNodeProperty( PropertyTags.LastWriteTime, value ); }
101
 
                }
102
 
 
103
 
                /// <summary>
104
 
                /// Gets or sets the file length in the metadata.
105
 
                /// </summary>
106
 
                public long Length
107
 
                {
108
 
                        get 
109
 
                        { 
110
 
                                Property p = properties.FindSingleValue( PropertyTags.FileLength );
111
 
                                return ( p != null ) ? ( long )p.Value : 0;
112
 
                        }
113
 
 
114
 
                        set { properties.ModifyNodeProperty( PropertyTags.FileLength, value ); }
115
 
                }
116
 
                #endregion
117
 
 
118
 
                #region Constructors
119
 
                /// <summary>
120
 
                /// Constructor used to create a new BaseFileNode object.
121
 
                /// </summary>
122
 
                /// <param name="collection">Collection that this file entry will be associated with.</param>
123
 
                /// <param name="parentPath">Fully qualified path to the parent directory.</param>
124
 
                /// <param name="fileName">Friendly name of the file entry.</param>
125
 
                /// <param name="fileID">Globally unique identifier for the file entry.</param>
126
 
                /// <param name="fileType">Class type to deserialize file entry as.</param>
127
 
                internal protected BaseFileNode( Collection collection, string parentPath, string fileName, string fileID, string fileType ) :
128
 
                        this ( fileName, fileID, fileType )
129
 
                {
130
 
                        // Add the disk file attributes if the file exists.
131
 
                        if ( UpdateFileInfo( collection, Path.Combine( parentPath, fileName ) ) )
132
 
                        {
133
 
                                // If there are metadata collectors registered for this file type, add the extra metadata.
134
 
                                AddFileMetadata( collection, GetFullPath( collection ) );
135
 
                        }
136
 
                }
137
 
 
138
 
                /// <summary>
139
 
                /// Constructor used to create a new BaseFileNode object.
140
 
                /// </summary>
141
 
                /// <param name="stream">Stream object that contains the file data.</param>
142
 
                /// <param name="fileName">Friendly name of the file entry.</param>
143
 
                /// <param name="fileID">Globally unique identifier for the file entry.</param>
144
 
                /// <param name="fileType">Class type to deserialize file entry as.</param>
145
 
                internal protected BaseFileNode( Stream stream, string fileName, string fileID, string fileType ) :
146
 
                        this ( fileName, fileID, fileType )
147
 
                {
148
 
                        // Add the length of the file.
149
 
                        Length = stream.Length;
150
 
                }
151
 
 
152
 
                /// <summary>
153
 
                /// Constructor used to create a new BaseFileNode object.
154
 
                /// </summary>
155
 
                /// <param name="fileName">Friendly name of the file entry.</param>
156
 
                /// <param name="fileID">Globally unique identifier for the file entry.</param>
157
 
                /// <param name="fileType">Class type to deserialize file entry as.</param>
158
 
                internal protected BaseFileNode( string fileName, string fileID, string fileType ) :
159
 
                        base ( fileName, fileID, fileType )
160
 
                {
161
 
                        // Add to the Types list.
162
 
                        properties.AddNodeProperty( PropertyTags.Types, NodeTypes.BaseFileNodeType );
163
 
                }
164
 
 
165
 
                /// <summary>
166
 
                /// Constructor for creating an existing BaseFileNode object.
167
 
                /// </summary>
168
 
                /// <param name="node">Node object to create BaseFileNode object from.</param>
169
 
                internal protected BaseFileNode( Node node ) :
170
 
                        base ( node )
171
 
                {
172
 
                }
173
 
 
174
 
                /// <summary>
175
 
                /// Constructor for create an existing BaseFileNode object from a ShallowNode object.
176
 
                /// </summary>
177
 
                /// <param name="collection">Collection that the ShallowNode belongs to.</param>
178
 
                /// <param name="shallowNode">ShallowNode object to create new BaseFileNode object from.</param>
179
 
                internal protected BaseFileNode( Collection collection, ShallowNode shallowNode ) :
180
 
                        base( collection, shallowNode )
181
 
                {
182
 
                }
183
 
 
184
 
                /// <summary>
185
 
                /// Constructor for creating an existing BaseFileNode object from an Xml document object.
186
 
                /// </summary>
187
 
                /// <param name="document">Xml document object to create BaseFileNode object from.</param>
188
 
                internal protected BaseFileNode( XmlDocument document ) :
189
 
                        base ( document )
190
 
                {
191
 
                }
192
 
                #endregion
193
 
 
194
 
                #region Private Methods
195
 
                /// <summary>
196
 
                /// If file type is a registered metadata type, then the file metadata is collected and automatically
197
 
                /// added to this node.
198
 
                /// </summary>
199
 
                /// <param name="collection">Collection that this file entry will be associated with.</param>
200
 
                /// <param name="filePath">Fully qualified path to the file.</param>
201
 
                private void AddFileMetadata( Collection collection, string filePath )
202
 
                {
203
 
                        // Get the directory path that this assembly was loaded from.
204
 
                        string loadDir = Path.GetDirectoryName( Assembly.GetExecutingAssembly().CodeBase );
205
 
 
206
 
                        // Build a path to the extension mapping file.
207
 
                        string xmlMetaFile = Path.Combine( loadDir, metadataExtensionFile );
208
 
                        if ( File.Exists( xmlMetaFile ) )
209
 
                        {
210
 
                                // Check if this file is a registered metadata type.
211
 
                                XmlDocument regExtDoc = new XmlDocument();
212
 
                                regExtDoc.Load( xmlMetaFile );
213
 
 
214
 
                                // See if this file has an extension.
215
 
                                string extension = Path.GetExtension( filePath );
216
 
                                if (extension != String.Empty )
217
 
                                {
218
 
                                        // Find out if there is a registered entry for this extension.
219
 
                                        XmlNode metaNode = regExtDoc.DocumentElement.SelectSingleNode( associatedtypeString + "[@" + extensionString + "='" + extension.ToLower() + "']" );
220
 
                                        if ( metaNode != null )
221
 
                                        {
222
 
                                                // Load the assembly that knows how to parse the file metadata.
223
 
                                                Assembly metaAssembly = Assembly.LoadFrom( Path.Combine( loadDir, metaNode.Attributes[ assemblyString ].Value ) );
224
 
                                                Type[] types = metaAssembly.GetTypes();
225
 
                                                foreach ( Type type in types )
226
 
                                                {
227
 
                                                        // Find the method to invoke to gather the metadata.
228
 
                                                        MethodInfo method = type.GetMethod( metadataMethodName, BindingFlags.Static | BindingFlags.Public );
229
 
                                                        if ( method != null )
230
 
                                                        {
231
 
                                                                try
232
 
                                                                {
233
 
                                                                        // Found the method.  Invoke it to return a name-value object that will contain the metadata.
234
 
                                                                        StringDictionary sd = ( StringDictionary )method.Invoke( null, new object[] { filePath } );
235
 
 
236
 
                                                                        // Walk through each name-value and add it to the parent node.
237
 
                                                                        foreach ( string s in sd.Keys )
238
 
                                                                        {
239
 
                                                                                properties.ModifyProperty( s, sd[ s ] );
240
 
                                                                        }
241
 
                                                                }
242
 
                                                                catch
243
 
                                                                {
244
 
                                                                        // Do nothing.  Just don't let it blow up.
245
 
                                                                }
246
 
 
247
 
                                                                break;
248
 
                                                        }
249
 
                                                }
250
 
                                        }
251
 
                                }
252
 
                        }
253
 
                }
254
 
                #endregion
255
 
 
256
 
                #region Public Methods
257
 
                /// <summary>
258
 
                /// Gets the file entry name with its extension.
259
 
                /// </summary>
260
 
                /// <returns>The name of the file including the extension.</returns>
261
 
                public abstract string GetFileName();
262
 
 
263
 
                /// <summary>
264
 
                /// Gets the full path of the file entry.
265
 
                /// </summary>
266
 
                /// <param name="collection">Collection that this file entry is associated with.</param>
267
 
                /// <returns>The absolute path to the file.</returns>
268
 
                public abstract string GetFullPath( Collection collection );
269
 
 
270
 
                /// <summary>
271
 
                /// Updates the file node with file information from the disk file.
272
 
                /// </summary>
273
 
                /// <param name="collection">Collection that this file entry is associated with.</param>
274
 
                /// <returns>True if the file information has changed, otherwise false is returned.</returns>
275
 
                public bool UpdateFileInfo( Collection collection )
276
 
                {
277
 
                        return UpdateFileInfo( collection, GetFullPath( collection ) );
278
 
                }
279
 
 
280
 
                /// <summary>
281
 
                /// Updates the file node with file information from the disk file.
282
 
                /// </summary>
283
 
                /// <param name="collection">Collection that this file entry is associated with.</param>
284
 
                /// <param name="fullPath">Absolute path to the disk file.</param>
285
 
                /// <returns>True if the file information has changed, otherwise false is returned.</returns>
286
 
                public bool UpdateFileInfo( Collection collection, string fullPath )
287
 
                {
288
 
                        bool infoChanged = false;
289
 
                        if ( File.Exists( fullPath ) )
290
 
                        {
291
 
                                // Get the file information for this file and set it as properties in the object.
292
 
                                FileInfo fi = new FileInfo( fullPath );
293
 
                                Length = fi.Length;
294
 
 
295
 
                                // Don't update the creation time if it already exists.
296
 
                                if ( CreationTime == DateTime.MinValue )
297
 
                                {
298
 
                                        CreationTime = fi.CreationTime;
299
 
                                }
300
 
 
301
 
                                LastAccessTime = fi.LastAccessTime;
302
 
                                LastWriteTime = fi.LastWriteTime;
303
 
                                infoChanged = true;
304
 
                        }
305
 
 
306
 
                        return infoChanged;
307
 
                }
308
 
                public bool UpdateWebFileInfo( Collection collection, long length)
309
 
                {
310
 
                        bool infoChanged = false;
311
 
                        if ( File.Exists( GetFullPath(collection) ) )
312
 
                        {
313
 
                                // Get the file information for this file and set it as properties in the object.
314
 
                                FileInfo fi = new FileInfo( GetFullPath(collection) );
315
 
                                Length = length;
316
 
 
317
 
                                // Don't update the creation time if it already exists.
318
 
                                if ( CreationTime == DateTime.MinValue )
319
 
                                {
320
 
                                        CreationTime = fi.CreationTime;
321
 
                                }
322
 
 
323
 
                                LastAccessTime = fi.LastAccessTime;
324
 
                                LastWriteTime = fi.LastWriteTime;
325
 
                                infoChanged = true;
326
 
                        }
327
 
 
328
 
                        return infoChanged;
329
 
                }
330
 
                #endregion
331
 
        }
332
 
}