1 /*
2 * Copyright 2006 Outsource Cafe, Inc.
3 *
4 * Licensed under the Apache License, Version 2.0 (the 'License')
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an 'AS IS' BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16 package org.javagen.agile.oo.naming;
17
18 import org.javagen.agile.core.model.Model;
19 import org.javagen.agile.core.util.NanoTemplate;
20 import org.javagen.agile.oo.context.Keys;
21 import org.javagen.agile.oo.model.Class;
22 import org.javagen.agile.oo.util.OOUtil;
23
24 /***
25 * Common Java naming services.
26 *
27 * @author Richard Easterling
28 */
29 public class JavaNamingService implements OONamingService {
30
31 private Collections collections;
32 private Pluralisation pluralisation;
33
34 public JavaNamingService() { }
35
36 ///////////////////////////////////////////////////////////////////////////
37 // convenience methods:
38 ///////////////////////////////////////////////////////////////////////////
39
40 public String varFromClassName(String className) {
41 String name = OOUtil.varFromClassName(className);
42 return name;
43 }
44
45 public String packageFromPath(String filePath) {
46 return OOUtil.packageFromPath(filePath);
47 }
48
49 public String camelBackName(String anyString) {
50 return OOUtil.camelBackJavaClass(anyString);
51 }
52
53 ///////////////////////////////////////////////////////////////////////////
54 // context-based methods:
55 ///////////////////////////////////////////////////////////////////////////
56
57
58 public String bindName(String templateKey, Model model) {
59 String name = NanoTemplate.bindKey(templateKey, model);
60 return name;
61 }
62
63 public String className(String classNameTemplateKey, Class _class) {
64 String className = _class.getName();
65 if (classNameTemplateKey!=null) {
66 if (_class.get(Keys.MODEL_NAME.toString())==null)
67 _class.put(Keys.MODEL_NAME.toString(), _class.getName());
68 className = bindName(classNameTemplateKey, _class);
69 }
70 return className;
71 }
72
73 public String className(Class _class) {
74 return _class.getName();
75 }
76
77 public String packageName(String packageNameTemplateKey, Model _class) {
78 String path = bindName(packageNameTemplateKey, _class);
79 return packageFromPath(path);
80 }
81
82 // public String packageName(Model _class) {
83 // return packageName(Keys.PACKAGE_NAME_TEMPLATE.toString(), _class);
84 // }
85 //
86 public String fullClassName(String packageNameTemplateKey, String classNameTemplateKey, Class _class) {
87 String packageName = packageName(packageNameTemplateKey, _class);
88 String className = className(classNameTemplateKey, _class);
89 return packageName.length()==0 ? className : packageName+"."+className;
90 }
91
92 // public String fullClassName(Class _class) {
93 // return fullClassName(Keys.PACKAGE_NAME_TEMPLATE.toString(), Keys.CLASS_NAME_TEMPLATE.toString(), _class);
94 // }
95 //
96 // public String templateSubstituion(String templateKey, Context context, String defaultValue) {
97 // String template = context==null ? null : (String)context.get(templateKey);
98 // if (template==null) {
99 // return defaultValue==null ? NanoTemplate.getDefaultMark()+"{"+templateKey+"}" : defaultValue;
100 // } else {
101 // String result = NanoTemplate.bindVariables(template, context);
102 // return result;
103 // }
104 //}
105 //
106 //public String templateSubstituion(String templateKey, Context context) {
107 // return templateSubstituion(templateKey, context, null);
108 //}
109 ///////////////////////////////////////////////////////////////////////////
110 // Pluralisation interface:
111 ///////////////////////////////////////////////////////////////////////////
112
113 public String collectionInterface(String collectionKey) {
114 return getCollections().getInterface(collectionKey);
115 }
116
117 public String collectionImplementation(String collectionKey) {
118 return getCollections().getImplementation(collectionKey);
119 }
120
121 ///////////////////////////////////////////////////////////////////////////
122 // Pluralisation interface:
123 ///////////////////////////////////////////////////////////////////////////
124
125 public String toPlural(String singular) {
126 return getPluralisation().toPlural(singular);
127 }
128
129 public String toSingular(String plural) {
130 return getPluralisation().toSingular(plural);
131 }
132
133 ///////////////////////////////////////////////////////////////////////////
134 // getters and setters:
135 ///////////////////////////////////////////////////////////////////////////
136
137 public Collections getCollections() {
138 if (collections==null)
139 collections = new Collections();
140 return collections;
141 }
142
143 public void setCollections(Collections collections) {
144 this.collections = collections;
145 }
146
147 public Pluralisation getPluralisation() {
148 if (pluralisation==null) {
149 pluralisation = new RegularPlurals();
150 }
151 return pluralisation;
152 }
153
154 public void setPluralisation(Pluralisation pluralisation) {
155 this.pluralisation = pluralisation;
156 }
157
158 // public boolean isPluralInput() {
159 // return pluralInput;
160 // }
161 //
162 // public void setPluralInput(boolean pluralInput) {
163 // this.pluralInput = pluralInput;
164 // }
165 //
166 }