Source Code Cross Referenced for GenerateJDO.java in  » Database-ORM » XORM » org » xorm » tools » generator » 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 » XORM » org.xorm.tools.generator 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:            $Header: /cvsroot/xorm/xorm/tools/src/org/xorm/tools/generator/GenerateJDO.java,v 1.5 2003/04/15 19:00:51 dcheckoway Exp $
003:
004:            This file is part of XORM.
005:
006:            XORM is free software; you can redistribute it and/or modify
007:            it under the terms of the GNU General Public License as published by
008:            the Free Software Foundation; either version 2 of the License, or
009:            (at your option) any later version.
010:
011:            XORM 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
014:            GNU General Public License for more details.
015:
016:            You should have received a copy of the GNU General Public License
017:            along with Foobar; if not, write to the Free Software
018:            Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
019:         */
020:        package org.xorm.tools.generator;
021:
022:        import org.jdom.*;
023:        import org.jdom.output.XMLOutputter;
024:        import org.xorm.ClassMapping;
025:        import org.xorm.util.FieldDescriptor;
026:        import java.io.*;
027:        import java.lang.reflect.Modifier;
028:        import java.util.ArrayList;
029:        import java.util.Collection;
030:        import java.util.Collections;
031:        import java.util.Comparator;
032:        import java.util.Iterator;
033:        import java.util.Map;
034:        import java.util.StringTokenizer;
035:        import java.util.regex.Pattern;
036:
037:        public class GenerateJDO {
038:            /**
039:             * Usage: GenerateJDO [packageName] [classesDir]
040:             */
041:            public static void main(String[] argv) throws Exception {
042:                String pkgPath = Pattern.compile("\\.").matcher(argv[0])
043:                        .replaceAll("/");
044:
045:                File path = new File(argv[1]);
046:                if (path.exists() && path.isDirectory()) {
047:                    buildDocument(path, pkgPath, argv[0]);
048:                }
049:            }
050:
051:            private static void buildDocument(File path, String dir,
052:                    String pkgName) throws Exception {
053:                Element jdo = new Element("jdo");
054:                Element pkg = new Element("package");
055:                pkg.setAttribute("name", pkgName);
056:                jdo.addContent(pkg);
057:
058:                path = new File(path, dir);
059:                File[] classFiles = path.listFiles(new FileFilter() {
060:                    public boolean accept(File file) {
061:                        return file.getName().endsWith(".class");
062:                    }
063:                });
064:
065:                if (classFiles != null) {
066:                    for (int i = 0; i < classFiles.length; i++) {
067:                        String name = classFiles[i].getName();
068:                        name = name.substring(0, name.indexOf('.'));
069:                        Class clazz = Class.forName(pkgName + "." + name);
070:                        if (clazz.isInterface()
071:                                || Modifier.isAbstract(clazz.getModifiers())) {
072:
073:                            Element classElem = new Element("class");
074:                            pkg.addContent(classElem);
075:                            classElem.setAttribute("name", name);
076:                            Element ext = new Element("extension");
077:                            ext.setAttribute("vendor-name", "XORM");
078:                            ext.setAttribute("key", "table");
079:                            ext.setAttribute("value", makeColumnName(name));
080:                            classElem.addContent(ext);
081:
082:                            // THIS IS THE DEFAULT
083:                            //classElem.setAttribute("identity-type", "datastore");
084:
085:                            ArrayList fds = new ArrayList(FieldDescriptor
086:                                    .getFieldDescriptors(clazz));
087:                            Collections.sort(fds, new Comparator() {
088:                                public int compare(Object obj1, Object obj2) {
089:                                    FieldDescriptor fd1 = (FieldDescriptor) obj1;
090:                                    FieldDescriptor fd2 = (FieldDescriptor) obj1;
091:                                    return fd1.name.compareTo(fd2.name);
092:                                }
093:                            });
094:
095:                            for (Iterator j = fds.iterator(); j.hasNext();) {
096:                                FieldDescriptor fd = (FieldDescriptor) j.next();
097:                                Element field = new Element("field");
098:                                field.setAttribute("name", fd.name);
099:                                Class pdType = fd.type;
100:                                if (pdType.isArray()) {
101:                                    // TODO handling for array
102:                                } else if (Map.class.isAssignableFrom(pdType)) {
103:                                    // TODO handling for map
104:                                } else if (Collection.class
105:                                        .isAssignableFrom(pdType)) {
106:                                    Element collection = new Element(
107:                                            "collection");
108:                                    collection.setAttribute("element-type",
109:                                            makeCollectionElementType(fd.name));
110:                                    field.addContent(collection);
111:                                    ext = new Element("extension");
112:                                    ext.setAttribute("vendor-name", "XORM");
113:                                    ext.setAttribute("key", "table");
114:                                    ext.setAttribute("value", "");
115:                                    collection.addContent(ext);
116:                                    ext = new Element("extension");
117:                                    ext.setAttribute("vendor-name", "XORM");
118:                                    ext.setAttribute("key", "source");
119:                                    ext.setAttribute("value", "");
120:                                    collection.addContent(ext);
121:                                } else {
122:                                    String columnName = makeColumnName(fd.name);
123:                                    ext = new Element("extension");
124:                                    ext.setAttribute("vendor-name", "XORM");
125:                                    ext.setAttribute("key", "column");
126:                                    ext.setAttribute("value", columnName);
127:                                    field.addContent(ext);
128:                                    //field.setAttribute("default-fetch-group", "true");
129:                                }
130:                                classElem.addContent(field);
131:                            }
132:                        } // isInterface
133:                    } // for each file
134:                } // classFiles != null
135:                Document doc = new Document(jdo);
136:                DocType dt = new DocType("jdo");
137:                dt.setSystemID("jdo.dtd");
138:                doc.setDocType(dt);
139:                new XMLOutputter("    ", true).output(doc, System.out);
140:            }
141:
142:            public static String makeCollectionElementType(String fieldName) {
143:                String et = fieldName;
144:                if (et.length() > 1 && et.endsWith("s")) {
145:                    et = et.substring(0, et.length() - 1);
146:                }
147:                return Character.toUpperCase(et.charAt(0)) + et.substring(1);
148:            }
149:
150:            public static String makeColumnName(String fieldName) {
151:                StringBuffer buf = new StringBuffer();
152:                for (int k = 0; k < fieldName.length(); ++k) {
153:                    if (k == 0) {
154:                        buf.append(Character.toLowerCase(fieldName.charAt(k)));
155:                    } else if (fieldName.charAt(k) >= 'A'
156:                            && fieldName.charAt(k) <= 'Z') {
157:                        buf.append('_');
158:                        buf.append(Character.toLowerCase(fieldName.charAt(k)));
159:                    } else {
160:                        buf.append(fieldName.charAt(k));
161:                    }
162:                }
163:                return buf.toString();
164:            }
165:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.