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
.MonoRail
.Framework
.Helpers
18 using System
.Collections
;
22 /// Provides methods for working with strings and grammar. At the moment,
23 /// it contains the ToSentence overloads.
25 public class TextHelper
: AbstractHelper
28 /// Default word connector
30 public const string DefaultConnector
= "and";
33 /// Converts a camelized text to words. For instance:
34 /// <c>FileWriter</c> is converted to <c>File Writer</c>
36 /// <param name="pascalText">Content in pascal case</param>
37 /// <returns></returns>
38 public static string PascalCaseToWord(string pascalText
)
40 if (pascalText
== null) throw new ArgumentNullException("pascalText");
42 if (pascalText
== string.Empty
) return string.Empty
;
44 StringBuilder sbText
= new StringBuilder(pascalText
.Length
+ 4);
46 char[] chars
= pascalText
.ToCharArray();
48 sbText
.Append(chars
[0]);
50 for(int i
=1; i
< chars
.Length
; i
++)
62 return sbText
.ToString();
66 /// Builds a phrase listing a series of strings with with proper sentence semantics,
67 /// i.e. separating elements with ", " and prefacing the last element with
68 /// the specified <paramref name="connector"/>.
70 /// <param name="elements">Collection with items to use in the sentence.</param>
71 /// <param name="connector">String to preface the last element.</param>
72 /// <returns>String suitable for use in a sentence.</returns>
73 /// <remarks>Calling <c>ToSentence( elements, "y" )</c> results in:
75 /// element1, element2 y element3
77 /// <para>If <paramref name="elements"/> is not an array of strings, each element will be
78 /// converted to string through <see cref="object.ToString"/>.</para>
80 /// <example>This example shows how to use <b>ToSentence</b>:
82 /// $TextHelper.ToSentence( elements, "y" )
85 public string ToSentence(ICollection elements
, string connector
)
87 return ToSentence(elements
, connector
, true);
91 /// Builds a phrase listing a series of strings with with proper sentence semantics,
92 /// i.e. separating elements with ", " and prefacing the last element with
93 /// " and ".
95 /// <param name="elements">Collection with items to use in the sentence.</param>
96 /// <param name="skipLastComma">True to skip the comma before the connector, false to include it.</param>
97 /// <returns>String suitable for use in a sentence.</returns>
98 /// <remarks>Calling <c>ToSentence( elements, false )</c> results in:
100 /// element1, element2, and element3
102 /// <para>If <paramref name="elements"/> is not an array of strings, each element will be
103 /// converted to string through <see cref="object.ToString"/>.</para>
105 /// <example>This example shows how to use <b>ToSentence</b>:
107 /// $TextHelper.ToSentence( elements, false )
110 public string ToSentence(ICollection elements
, bool skipLastComma
)
112 return ToSentence(elements
, DefaultConnector
, skipLastComma
);
116 /// Builds a phrase listing a series of strings with with proper sentence semantics,
117 /// i.e. separating elements with ", " and prefacing the last element with
118 /// " and ".
120 /// <param name="elements">Collection with items to use in the sentence.</param>
121 /// <returns>String suitable for use in a sentence.</returns>
122 /// <remarks>Calling <c>ToSentence( elements )</c> results in:
124 /// element1, element2 and element3
126 /// <para>If <paramref name="elements"/> is not an array of strings, each element will be
127 /// converted to string through <see cref="object.ToString"/>.</para>
129 /// <example>This example shows how to use <b>ToSentence</b>:
131 /// $TextHelper.ToSentence( elements )
134 public string ToSentence(ICollection elements
)
136 if (elements
== null)
138 throw new ArgumentNullException("elements");
141 return ToSentence(elements
, DefaultConnector
, true);
145 /// Builds a phrase listing a series of strings with with proper sentence semantics,
146 /// i.e. separating elements with ", " and prefacing the last element with
147 /// the specified <paramref name="connector"/>.
149 /// <param name="elements">Collection with items to use in the sentence.</param>
150 /// <param name="connector">String to preface the last element.</param>
151 /// <param name="skipLastComma">True to skip the comma before the <paramref name="connector"/>, false to include it.</param>
152 /// <returns>String suitable for use in a sentence.</returns>
153 /// <remarks>Calling <c>ToSentence( elements, "y", false )</c> results in:
155 /// element1, element2, y element3
157 /// <para>If <paramref name="elements"/> is not an array of strings, each element will be
158 /// converted to string through <see cref="Object.ToString"/>.</para>
160 /// <example>This example shows how to use <b>ToSentence</b>:
162 /// $TextHelper.ToSentence( elements, "y", false )
165 public string ToSentence(ICollection elements
, string connector
, bool skipLastComma
)
167 string[] array
= elements
as string[];
170 array
= new string[elements
.Count
];
171 IEnumerator enumerator
= elements
.GetEnumerator();
172 for(int i
= 0; i
< elements
.Count
; i
++)
174 enumerator
.MoveNext();
175 array
[i
] = enumerator
.Current
.ToString();
178 return ToSentence(array
, connector
, skipLastComma
);
182 /// Builds a phrase listing a series of strings with with proper sentence semantics,
183 /// i.e. separating elements with ", " and prefacing the last element with
184 /// the specified <paramref name="connector"/>.
186 /// <param name="elements">Array of strings with items to use in the sentence.</param>
187 /// <param name="connector">String to preface the last element.</param>
188 /// <param name="skipLastComma">True to skip the comma before the <paramref name="connector"/>, false to include it.</param>
189 /// <returns>String suitable for use in a sentence.</returns>
190 /// <remarks>Calling <c>ToSentence( elements, "y", false )</c> results in:
192 /// element1, element2, y element3
195 /// <example>This example shows how to use <b>ToSentence</b>:
197 /// $TextHelper.ToSentence( elements, "y", false )
200 public string ToSentence(string[] elements
, string connector
, bool skipLastComma
)
202 switch(elements
.Length
)
214 return String
.Format("{0} {1} {2}", elements
[0], connector
, elements
[1]);
218 String
[] allButLast
= new String
[elements
.Length
- 1];
220 Array
.Copy(elements
, allButLast
, elements
.Length
- 1);
222 return String
.Format("{0}{1} {2} {3}",
223 String
.Join(", ", allButLast
),
224 skipLastComma
? "" : ",",
226 elements
[elements
.Length
- 1]);
232 /// Shortens a text to the specified length and wraps it into a span-
233 /// element that has the title-property with the full text associated.
234 /// This is convenient for displaying properties in tables that might
235 /// have very much content (desription fields etc.) without destroying
236 /// the table's layout.
237 /// Due to the title-property of the surrounding span-element, the full
238 /// text is displayed in the browser while hovering over the shortened
241 /// <param name="text">The text to display</param>
242 /// <param name="maxLength">The maximum number of character to display</param>
243 /// <returns>The generated HTML</returns>
244 public string Fold(string text
, int maxLength
)
247 if (text
== null) return "";
249 // maxLenght <= 0 switches off folding
250 // Determine whether text must be cut
251 if (maxLength
<= 0 || text
.Length
< maxLength
) return text
;
253 StringBuilder caption
= new StringBuilder();
254 foreach(string word
in text
.Split())
256 if (caption
.Length
+ word
.Length
+ 1 > maxLength
- 1) break;
257 if (caption
.Length
> 0) caption
.Append(" "); // Adding space
258 caption
.Append(word
);
261 caption
.Append("…");
262 return string.Format("<span title=\"{1}\">{0}</span>", caption
, text
.Replace("\"", """));