Added container accessor to Castle.Core
[castle.git] / Tools / SubversionHooks / Hooks / RepositoryFile.cs
blobe76575f4d2e77561cd1dba95f92283c95638af6f
1 // Copyright 2004-2007 Castle Project - http://www.castleproject.org/
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
15 namespace Castle.SvnHooks
17 using System;
18 using System.IO;
20 /// <summary>
21 /// Summary description for IRepositoryFile.
22 /// </summary>
23 public class RepositoryFile : IDisposable
25 public RepositoryFile(IRepository repository, String path, RepositoryStatus contentsStatus, RepositoryStatus propertiesStatus)
27 if (path == null)
28 throw new ArgumentNullException("path");
29 if (path.Trim().Length == 0)
30 throw new ArgumentException("Path must be set to a valid path", "path");
31 if (path[path.Length-1] == '/')
32 throw new ArgumentException("Path must be set to a file, not a directory", "path");
33 if (propertiesStatus == RepositoryStatus.Added ||
34 propertiesStatus == RepositoryStatus.Deleted)
36 throw new ArgumentException("Properties status cannot be set to Added or Deleted, use Updated", "propertiesStatus");
39 this.contentsStatus = contentsStatus;
40 this.propertiesStatus = propertiesStatus;
41 this.repository = repository;
42 SetPathRelatedFields(path);
44 if (fileName.EndsWith(" "))
45 throw new ArgumentException("Filename cannot end with trailing spaces", "path");
47 if (fileName.StartsWith(" "))
48 throw new ArgumentException("Filename cannot begin with leading spaces", "path");
52 private void SetPathRelatedFields(String path)
54 // Set the path
55 this.path = path;
57 // Extract the file name from the path
58 if (path.IndexOf('/') == -1)
60 fileName = path;
62 else
64 fileName = path.Substring(path.LastIndexOf('/')+1);
67 // Extract the extension from the file name
68 if (fileName.IndexOf('.') == -1)
70 extension = String.Empty;
72 else
74 extension = fileName.Substring(fileName.LastIndexOf('.') + 1);
78 #region IDisposable Members
80 public void Dispose()
82 if(contents != null)
84 contents.Close();
85 contents = null;
90 #endregion
92 public override string ToString()
94 return Path;
97 public Stream GetContents()
99 if (!IsText)
100 throw new InvalidOperationException("Cannot get the contents of a binary file");
102 if (contents == null)
104 this.contents = repository.GetFileContents(Path);
107 contents.Seek(0, SeekOrigin.Begin);
109 return new ReadOnlyStreamWrapper(contents);
112 public String[] GetProperty(string name)
114 if (name == null)
115 throw new ArgumentNullException("name");
116 return repository.GetProperty(Path, name);
120 public RepositoryStatus ContentsStatus
122 get { return contentsStatus; }
125 public RepositoryStatus PropertiesStatus
127 get { return propertiesStatus; }
130 public String Extension
132 get { return extension; }
134 public String FileName
136 get { return fileName; }
138 public String Path
140 get { return path; }
142 /// <value>
143 /// The MIME type of the file it must always
144 /// return a valid string, if the MIME type
145 /// is not known by Subversion it should
146 /// default to "text/plain"
147 /// </value>
148 public String MimeType
150 get
152 if (mimeType == null)
154 String[] props = GetProperty("svn:mime-type");
156 if (props == null)
158 mimeType = "text/plain";
160 else
162 mimeType = props[0];
166 return mimeType;
170 public bool IsText
172 get
174 return MimeType.StartsWith("text/");
179 private IRepository repository;
180 private RepositoryStatus contentsStatus;
181 private RepositoryStatus propertiesStatus;
182 private String path;
183 private String fileName;
184 private String extension;
185 private String mimeType = null;
186 private Stream contents = null;