Source Code Cross Referenced for DefaultGenerationPolicy.java in  » Database-ORM » sqlc » biz » hammurapi » sql » metadata » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Database ORM » sqlc » biz.hammurapi.sql.metadata 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * sqlc 1
003:         * SQL Compiler 
004:         * Copyright (C) 2003  Hammurapi Group
005:         *
006:         * This program is free software; you can redistribute it and/or
007:         * modify it under the terms of the GNU Lesser General Public
008:         * License as published by the Free Software Foundation; either
009:         * version 2 of the License, or (at your option) any later version.
010:         *
011:         * This program is distributed in the hope that it will be useful,
012:         * but WITHOUT ANY WARRANTY; without even the implied warranty of
013:         * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
014:         * Lesser General Public License for more details.
015:         *
016:         * You should have received a copy of the GNU Lesser General Public
017:         * License along with this library; if not, write to the Free Software
018:         * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
019:         *
020:         * URL: http://www.hammurapi.biz/products/sqlc/index.html
021:         * e-Mail: support@hammurapi.biz 
022:         */
023:        package biz.hammurapi.sql.metadata;
024:
025:        import java.sql.Types;
026:        import java.util.StringTokenizer;
027:
028:        /**
029:         * @author Pavel Vlasov
030:         * @version $Revision: 1.6 $
031:         */
032:        public class DefaultGenerationPolicy implements  GenerationPolicy {
033:
034:            private static final String GENERATE_PREFIX = "_SQLC$";
035:            protected String packageName = "";
036:            protected String wordSeparator = "_";
037:            protected String keyWordSeparator = "$";
038:
039:            /**
040:             * 
041:             */
042:            public DefaultGenerationPolicy() {
043:                super ();
044:            }
045:
046:            public String generateEntityType(final String catalog,
047:                    final String schema, final String table) {
048:                StringBuffer ret = new StringBuffer();
049:                if (catalog != null) {
050:                    ret.append(catalog.toLowerCase());
051:                    ret.append(".");
052:                }
053:                if (schema != null) {
054:                    ret.append(schema.toLowerCase());
055:                    ret.append(".");
056:                }
057:                ret.append(tableName(table));
058:                return ret.toString();
059:            }
060:
061:            /**
062:             * @param table
063:             * @return
064:             */
065:            protected String tableName(final String table) {
066:                StringTokenizer st = new StringTokenizer(table, wordSeparator);
067:                StringBuffer ret = new StringBuffer();
068:                if (packageName != null && packageName.length() > 0) {
069:                    ret.append(packageName);
070:                    ret.append('.');
071:                }
072:                while (st.hasMoreTokens()) {
073:                    String token = st.nextToken();
074:                    ret.append(Character.toUpperCase(token.charAt(0)));
075:                    if (token.length() > 1) {
076:                        ret.append(token.substring(1).toLowerCase());
077:                    }
078:                }
079:                return ret.toString();
080:            }
081:
082:            /**
083:             * Converts name to Java conventions.
084:             * @param name
085:             * @return
086:             */
087:            public static String convert(final String name, String separators) {
088:                StringTokenizer st = new StringTokenizer(name, separators);
089:                StringBuffer ret = new StringBuffer();
090:                while (st.hasMoreTokens()) {
091:                    String token = st.nextToken();
092:                    ret.append(Character.toUpperCase(token.charAt(0)));
093:                    if (token.length() > 1) {
094:                        ret.append(token.substring(1).toLowerCase());
095:                    }
096:                }
097:                return ret.toString();
098:            }
099:
100:            public String generateFactoryType(final String catalog,
101:                    final String schema, final String table) {
102:                return generateEntityType(catalog, schema, table) + "Factory";
103:            }
104:
105:            public String generatePKType(final String catalog,
106:                    final String schema, final String table) {
107:                return generateEntityType(catalog, schema, table) + "PK";
108:            }
109:
110:            public String generateColumnName(String name) {
111:                if (name.trim().length() == 0) {
112:                    return "Column";
113:                }
114:
115:                StringTokenizer st = new StringTokenizer(name, wordSeparator);
116:                StringBuffer ret = new StringBuffer();
117:                while (st.hasMoreTokens()) {
118:                    String token = st.nextToken();
119:                    ret.append(Character.toUpperCase(token.charAt(0)));
120:                    if (token.length() > 1) {
121:                        ret.append(token.substring(1).toLowerCase());
122:                    }
123:                }
124:                return ret.toString();
125:            }
126:
127:            public String getJavaType(int dataType) {
128:                switch (dataType) {
129:                case Types.BIT:
130:                case Types.BOOLEAN:
131:                    return "java.lang.Boolean";
132:
133:                case Types.TINYINT:
134:                    return "java.lang.Byte";
135:
136:                case Types.SMALLINT:
137:                    return "java.lang.Short";
138:
139:                case Types.INTEGER:
140:                    return "java.lang.Integer";
141:
142:                case Types.BIGINT:
143:                    return "java.lang.Long";
144:
145:                case Types.FLOAT:
146:                case Types.REAL:
147:                case Types.DOUBLE:
148:                    return "java.lang.Double";
149:
150:                case Types.NUMERIC:
151:                case Types.DECIMAL:
152:                    return "java.math.BigDecimal";
153:
154:                case Types.DATE:
155:                    return "java.sql.Date";
156:
157:                case Types.TIME:
158:                    return "java.sql.Time";
159:
160:                case Types.TIMESTAMP:
161:                    return "java.sql.Timestamp";
162:
163:                case Types.BINARY:
164:                case Types.VARBINARY:
165:                case Types.LONGVARBINARY:
166:                    return "byte[]";
167:
168:                case Types.OTHER:
169:                case Types.JAVA_OBJECT:
170:                    return "java.lang.Object";
171:
172:                case Types.DISTINCT:
173:                case Types.STRUCT:
174:                case Types.ARRAY:
175:                case Types.BLOB:
176:                case Types.CLOB:
177:                case Types.REF:
178:                case Types.DATALINK:
179:
180:                case Types.NULL:
181:
182:                case Types.CHAR:
183:                case Types.VARCHAR:
184:                case Types.LONGVARCHAR:
185:                default:
186:                    return "java.lang.String";
187:                }
188:            }
189:
190:            public String generateKeyName(String keyName) {
191:                int idx = keyName.indexOf(GENERATE_PREFIX);
192:                if (idx == -1
193:                        || idx == keyName.length() - GENERATE_PREFIX.length()) {
194:                    return null;
195:                }
196:
197:                return tableName(keyName.substring(idx
198:                        + GENERATE_PREFIX.length()));
199:            }
200:
201:            public String getPackageName() {
202:                return packageName;
203:            }
204:
205:            public String generateValueType(String catalog, String schema,
206:                    String table) {
207:                return generateEntityType(catalog, schema, table) + "Value";
208:            }
209:
210:            public String generateEntityImplType(String catalog, String schema,
211:                    String table) {
212:                return generateEntityType(catalog, schema, table) + "Impl";
213:            }
214:
215:            public String generateFactoryImplType(String catalog,
216:                    String schema, String table) {
217:                return generateFactoryType(catalog, schema, table) + "Impl";
218:            }
219:
220:            public String generatePKImplType(String catalog, String schema,
221:                    String table) {
222:                return generatePKType(catalog, schema, table) + "Impl";
223:            }
224:
225:            public String generateValueImplType(String catalog, String schema,
226:                    String table) {
227:                return generateValueType(catalog, schema, table) + "Impl";
228:            }
229:
230:            public String generateEngineType() {
231:                if (packageName == null || packageName.length() == 0) {
232:                    return "Engine";
233:                }
234:
235:                return packageName + ".Engine";
236:            }
237:
238:            public IndexInfo generateIndexInfo(String indexName,
239:                    TableDescriptor descriptor) {
240:                if (indexName == null) {
241:                    return null;
242:                }
243:                int idx = indexName.indexOf(GENERATE_PREFIX);
244:                if (idx == -1
245:                        || idx == indexName.length() - GENERATE_PREFIX.length()) {
246:                    return null;
247:                }
248:
249:                indexName = indexName.substring(idx + GENERATE_PREFIX.length());
250:                idx = indexName.indexOf('_');
251:                if (idx == -1 || idx == 0 || idx == indexName.length() - 1) {
252:                    return null;
253:                }
254:
255:                final String modeString = indexName.substring(0, idx);
256:                final String javaName = tableName(indexName.substring(idx + 1));
257:                return new IndexInfo() {
258:
259:                    public String getJavaName() {
260:                        return javaName;
261:                    }
262:
263:                    public boolean isOrdered() {
264:                        return modeString.indexOf('O') != -1;
265:                    }
266:
267:                    public boolean isEQ() {
268:                        return modeString.indexOf('E') != -1;
269:                    }
270:
271:                    public boolean isNE() {
272:                        return modeString.indexOf('N') != -1;
273:                    }
274:
275:                    public boolean isLT() {
276:                        return modeString.indexOf('L') != -1;
277:                    }
278:
279:                    public boolean isLE() {
280:                        return modeString.indexOf('M') != -1;
281:                    }
282:
283:                    public boolean isGT() {
284:                        return modeString.indexOf('G') != -1;
285:                    }
286:
287:                    public boolean isGE() {
288:                        return modeString.indexOf('H') != -1;
289:                    }
290:
291:                };
292:            }
293:
294:            private static final int LOWER_CASE = 0;
295:            private static final int UPPER_CASE = 1;
296:            private static final int NON_LETTER = 2;
297:
298:            private static int charMode(char ch) {
299:                if (Character.isLetter(ch)) {
300:                    return Character.isUpperCase(ch) ? UPPER_CASE : LOWER_CASE;
301:                }
302:
303:                return NON_LETTER;
304:            }
305:
306:            public String generateLabel(String columnName) {
307:                if (columnName == null) {
308:                    return columnName;
309:                }
310:
311:                StringBuffer ret = new StringBuffer();
312:
313:                char[] ca = columnName.toCharArray();
314:                for (int i = 0; i < ca.length; i++) {
315:                    if (i == 0) {
316:                        ret.append(Character.toUpperCase(ca[i]));
317:                    } else {
318:                        switch (charMode(ca[i])) {
319:                        case LOWER_CASE:
320:                            switch (charMode(ca[i - 1])) {
321:                            case LOWER_CASE:
322:                            case UPPER_CASE:
323:                                break;
324:                            case NON_LETTER:
325:                                ret.append(' ');
326:                                break;
327:                            }
328:                            break;
329:                        case UPPER_CASE:
330:                            switch (charMode(ca[i - 1])) {
331:                            case UPPER_CASE:
332:                                break;
333:                            case LOWER_CASE:
334:                            case NON_LETTER:
335:                                ret.append(' ');
336:                                break;
337:                            }
338:                            break;
339:                        case NON_LETTER:
340:                            switch (charMode(ca[i - 1])) {
341:                            case LOWER_CASE:
342:                            case UPPER_CASE:
343:                                ret.append(' ');
344:                                break;
345:                            case NON_LETTER:
346:                                break;
347:                            }
348:                            break;
349:
350:                        }
351:                        ret.append(Character.toLowerCase(ca[i]));
352:                    }
353:                }
354:
355:                return ret.toString();
356:            }
357:
358:            public static void main(String[] args) {
359:                DefaultGenerationPolicy dgp = new DefaultGenerationPolicy();
360:                System.out.println(dgp.generateLabel("FirstName33"));
361:            }
362:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.