1 // Copyright 2004-2007 Castle Project - http://www.castleproject.org/
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
7 // http://www.apache.org/licenses/LICENSE-2.0
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
21 /// Summary description for IRepositoryFile.
23 public class RepositoryFile
: IDisposable
25 public RepositoryFile(IRepository repository
, String path
, RepositoryStatus contentsStatus
, RepositoryStatus propertiesStatus
)
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
)
57 // Extract the file name from the path
58 if (path
.IndexOf('/') == -1)
64 fileName
= path
.Substring(path
.LastIndexOf('/')+1);
67 // Extract the extension from the file name
68 if (fileName
.IndexOf('.') == -1)
70 extension
= String
.Empty
;
74 extension
= fileName
.Substring(fileName
.LastIndexOf('.') + 1);
78 #region IDisposable Members
92 public override string ToString()
97 public Stream
GetContents()
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
)
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; }
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"
148 public String MimeType
152 if (mimeType
== null)
154 String
[] props
= GetProperty("svn:mime-type");
158 mimeType
= "text/plain";
174 return MimeType
.StartsWith("text/");
179 private IRepository repository
;
180 private RepositoryStatus contentsStatus
;
181 private RepositoryStatus propertiesStatus
;
183 private String fileName
;
184 private String extension
;
185 private String mimeType
= null;
186 private Stream contents
= null;