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
.ActiveRecord
.Generator
.Components
18 using System
.Text
.RegularExpressions
;
20 using Castle
.ActiveRecord
.Generator
.Components
.Utils
;
23 public class NamingService
: INamingService
25 private Pair
[] pluralRules
= new Pair
[]
27 new Pair("(x|ch|ss|sh)$", "$1es"),
28 new Pair("series$", "$1series"),
29 new Pair("([^aeiouy]|qu)y$", "$1ies"),
30 new Pair("(?:([^f])fe|([lr])f)$", "$1$2ves"),
31 new Pair("sis$", "ses"),
32 new Pair("([ti])um$", "$1a"),
33 new Pair("Person$", "People"),
34 new Pair("Man$", "Men"),
35 new Pair("Child$", "Children"),
40 private Pair
[] singularRules
= new Pair
[]
42 new Pair("(x|ch|ss)es$", @"$1"),
43 new Pair("Movies$", "Movie"),
44 new Pair("([^aeiouy]|qu)ies$", "$1y"),
45 new Pair("([lr])ves$", @"$1f"),
46 new Pair("([^f])ves$", @"$1fe"),
47 new Pair("(analy|ba|diagno|parenthe|progno|synop|the)ses$", @"$1sis"),
48 new Pair("([ti])a$", @"$1um"),
49 new Pair("People$", @"Person"),
50 new Pair("Men$", @"Man"),
51 new Pair("Status$", @"Status"),
52 new Pair("Children$", @"Child"),
56 public String
CreateRelationName(String tableName
)
58 MatchEvaluator UpCaser
= new MatchEvaluator(UpCaserDef
);
60 tableName
= Regex
.Replace(tableName
, "(tb_|_)", "");
61 tableName
= Regex
.Replace(tableName
, "^[a-z]", UpCaser
);
63 return Pluralize(tableName
);
66 public String
CreateClassName(String tableName
)
68 MatchEvaluator UpCaser
= new MatchEvaluator(UpCaserDef
);
70 tableName
= Regex
.Replace(tableName
, "(tb_|_)", "");
71 tableName
= Regex
.Replace(tableName
, "^[a-z]", UpCaser
);
73 return Singularize(tableName
);
76 public String
CreatePropertyName(String columnName
)
78 MatchEvaluator UpCaser
= new MatchEvaluator(UpCaserDef
);
80 columnName
= Regex
.Replace(columnName
, "([a-z])_([a-z])", UpCaser
);
81 columnName
= Regex
.Replace(columnName
, "(tb_|_)", "");
82 columnName
= Regex
.Replace(columnName
, "^[a-z]", UpCaser
);
83 columnName
= Regex
.Replace(columnName
, "[^1-9a-zA-Z]", "");
88 public String
CreateFieldName(String columnName
)
90 MatchEvaluator UpCaser
= new MatchEvaluator(UpCaserDef
);
91 MatchEvaluator DownCaser
= new MatchEvaluator(DownCaserDef
);
93 columnName
= Regex
.Replace(columnName
, "([a-z])_([a-z])", UpCaser
);
94 columnName
= Regex
.Replace(columnName
, "(tb_|_)", "");
95 columnName
= Regex
.Replace(columnName
, "^[A-Z]", DownCaser
);
96 columnName
= Regex
.Replace(columnName
, "[^1-9a-zA-Z]", "");
98 return String
.Format("_{0}", columnName
);
101 private String
UpCaserDef(Match match
)
103 if (match
.Value
.Length
== 1)
105 return match
.Value
.ToUpper();
108 return String
.Format( "{0}_{1}", match
.Value
[0], match
.Value
.ToUpper()[2] );
111 private String
DownCaserDef(Match match
)
113 return match
.Value
.ToLower();
116 private String
Singularize(String name
)
118 foreach(Pair pair
in singularRules
)
120 String result
= Regex
.Replace(name
, pair
.First
, pair
.Second
);
121 if (!result
.Equals(name
))
130 private String
Pluralize(String name
)
132 String result
= name
;
134 foreach(Pair pair
in pluralRules
)
136 result
= Regex
.Replace(name
, pair
.First
, pair
.Second
, RegexOptions
.IgnoreCase
|RegexOptions
.Singleline
);
137 if (!result
.Equals(name
))
143 if (result
.EndsWith("ss"))
145 result
= result
.Substring(0, result
.Length
- 1);