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: }
|