Package org.xorm.tools.generator

Source Code of org.xorm.tools.generator.GenerateJDO

/*
    $Header: /cvsroot/xorm/xorm/tools/src/org/xorm/tools/generator/GenerateJDO.java,v 1.5 2003/04/15 19:00:51 dcheckoway Exp $

    This file is part of XORM.

    XORM is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    XORM is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Foobar; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*/
package org.xorm.tools.generator;

import org.jdom.*;
import org.jdom.output.XMLOutputter;
import org.xorm.ClassMapping;
import org.xorm.util.FieldDescriptor;
import java.io.*;
import java.lang.reflect.Modifier;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.Iterator;
import java.util.Map;
import java.util.StringTokenizer;
import java.util.regex.Pattern;

public class GenerateJDO {
    /**
     * Usage: GenerateJDO [packageName] [classesDir]
     */
    public static void main(String[] argv) throws Exception {
  String pkgPath = Pattern.compile("\\.").matcher(argv[0]).replaceAll("/");
 
   File path = new File(argv[1]);
  if (path.exists() && path.isDirectory()) {
      buildDocument(path, pkgPath, argv[0]);
  }
    }

    private static void buildDocument(File path, String dir, String pkgName) throws Exception {
  Element jdo = new Element("jdo");
  Element pkg = new Element("package");
  pkg.setAttribute("name", pkgName);
  jdo.addContent(pkg);

  path = new File(path, dir);
  File[] classFiles = path.listFiles(new FileFilter() {
    public boolean accept(File file) {
        return file.getName().endsWith(".class");
    }
      });

  if (classFiles != null) {
      for (int i = 0; i < classFiles.length; i++) {
    String name = classFiles[i].getName();
    name = name.substring(0, name.indexOf('.'));
    Class clazz = Class.forName(pkgName + "." + name);
    if (clazz.isInterface() || Modifier.isAbstract(clazz.getModifiers())) {
       
        Element classElem = new Element("class");
        pkg.addContent(classElem);
        classElem.setAttribute("name", name);
        Element ext = new Element("extension");
        ext.setAttribute("vendor-name", "XORM");
        ext.setAttribute("key", "table");
        ext.setAttribute("value", makeColumnName(name));
        classElem.addContent(ext);

        // THIS IS THE DEFAULT
        //classElem.setAttribute("identity-type", "datastore");
       
        ArrayList fds = new ArrayList(FieldDescriptor.getFieldDescriptors(clazz));
                    Collections.sort(fds, new Comparator() {
                            public int compare(Object obj1, Object obj2) {
                                FieldDescriptor fd1 = (FieldDescriptor)obj1;
                                FieldDescriptor fd2 = (FieldDescriptor)obj1;
                                return fd1.name.compareTo(fd2.name);
                            }
                        });
                   
        for (Iterator j = fds.iterator(); j.hasNext(); ) {
      FieldDescriptor fd = (FieldDescriptor) j.next();
      Element field = new Element("field");
      field.setAttribute("name", fd.name);
      Class pdType = fd.type;
      if (pdType.isArray()) {
          // TODO handling for array
      } else if (Map.class.isAssignableFrom(pdType)) {
          // TODO handling for map
      } else if (Collection.class.isAssignableFrom(pdType)) {
          Element collection = new Element("collection");
          collection.setAttribute("element-type", makeCollectionElementType(fd.name));
          field.addContent(collection);
          ext = new Element("extension");
          ext.setAttribute("vendor-name", "XORM");
          ext.setAttribute("key", "table");
          ext.setAttribute("value", "");
          collection.addContent(ext);
          ext = new Element("extension");
          ext.setAttribute("vendor-name", "XORM");
          ext.setAttribute("key", "source");
          ext.setAttribute("value", "");
          collection.addContent(ext);
      } else {
                            String columnName = makeColumnName(fd.name);
          ext = new Element("extension");
          ext.setAttribute("vendor-name", "XORM");
          ext.setAttribute("key", "column");
          ext.setAttribute("value", columnName);
          field.addContent(ext);
          //field.setAttribute("default-fetch-group", "true");
      }
      classElem.addContent(field);
        }
    } // isInterface
      } // for each file
  } // classFiles != null
  Document doc = new Document(jdo);
  DocType dt = new DocType("jdo");
  dt.setSystemID("jdo.dtd");
  doc.setDocType(dt);
  new XMLOutputter("    ", true).output(doc, System.out);
    }

    public static String makeCollectionElementType(String fieldName) {
        String et = fieldName;
        if (et.length() > 1 && et.endsWith("s")) {
            et = et.substring(0, et.length() - 1);
        }
        return Character.toUpperCase(et.charAt(0)) +
            et.substring(1);
    }

    public static String makeColumnName(String fieldName) {
        StringBuffer buf = new StringBuffer();
        for (int k = 0; k < fieldName.length(); ++k) {
            if (k == 0) {
                buf.append(Character.toLowerCase(fieldName.charAt(k)));
            }
            else if (fieldName.charAt(k) >= 'A' &&
                     fieldName.charAt(k) <= 'Z') {
                buf.append('_');
                buf.append(Character.toLowerCase(fieldName.charAt(k)));
            }
            else {
                buf.append(fieldName.charAt(k));
            }
        }
        return buf.toString();
    }
}
TOP

Related Classes of org.xorm.tools.generator.GenerateJDO

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.