Update of /cvsroot/xdoclet/xdoclet/modules/jdo/src/xdoclet/modules/jdo In directory sc8-pr-cvs10.sourceforge.net:/tmp/cvs-serv956/modules/jdo/src/xdoclet/modules/jdo
Modified Files: JdoObjectIdGeneratorTagsHandler.java JdoObjectIdGeneratorSubTask.java Log Message: The generated JDO object IDs encode their numeric IDs now with the radix 36 by default. In order to allow users to still use the previous behaviour, the radix can be passed to the jdodoclet task as seen below: <jdodoclet jdospec="2.0" destdir="${project.dir}/${ejbsrc.dir}"> <fileset dir="${project.dir}/${src.dir}" defaultexcludes="yes"/> <jdometadata project="package" generation="package"/> <jdoobjectidgenerator radix="10" interfaces="org.nightlabs.jdo.ObjectID"/> </jdodoclet> Using the radix 36 (i.e. 0..9, a..z) makes the generated Strings much shorter (e.g. a 10-digit number in decimal system gets shortened to 6 digits). Additionally, the field-order for primary-key fields needs to be either complete or omitted. To allow partial declarations is error-prone. Index: JdoObjectIdGeneratorTagsHandler.java =================================================================== RCS file: /cvsroot/xdoclet/xdoclet/modules/jdo/src/xdoclet/modules/jdo/JdoObjectIdGeneratorTagsHandler.java,v retrieving revision 1.9 retrieving revision 1.10 diff -C2 -r1.9 -r1.10 *** JdoObjectIdGeneratorTagsHandler.java 25 Jul 2007 16:10:51 -0000 1.9 --- JdoObjectIdGeneratorTagsHandler.java 31 Jul 2007 14:37:28 -0000 1.10 *************** *** 8,12 **** import java.io.File; import java.io.FileReader; - import java.io.Serializable; import java.util.ArrayList; import java.util.Collections; --- 8,11 ---- *************** *** 101,111 **** s.add("boolean"); - s.add("byte"); s.add("char"); ! s.add("double"); ! s.add("float"); s.add("int"); s.add("long"); ! s.add("short"); primitiveClassNames = s; } --- 100,110 ---- s.add("boolean"); s.add("char"); ! s.add("byte"); ! s.add("short"); s.add("int"); s.add("long"); ! s.add("float"); ! s.add("double"); primitiveClassNames = s; } *************** *** 144,147 **** --- 143,156 ---- } + public String getRadix(Properties attributes) throws XDocletException + { + int radix = ((JdoObjectIdGeneratorSubTask) getDocletContext().getSubTaskBy("jdoobjectidgenerator")).getRadix(); + + if (radix == -1) + radix = 36; + + return String.valueOf(radix); + } + public void ifNotObjectIdPackageEmpty(String template, Properties attributes) throws XDocletException { *************** *** 448,452 **** while ((bytesRead = r.read(c)) >= 0) { buf.append(c, 0, bytesRead); - // System.out.println("read " + bytesRead + " bytes from file " + includeFile.getAbsolutePath()); } } --- 457,460 ---- *************** *** 463,486 **** } return buf.toString(); - // CharBuffer buf = CharBuffer.allocate((int) includeFile.length()); - // - // try { - // - // Reader r = new FileReader(includeFile); - // - // try { - // r.read(buf); - // } - // finally { - // r.close(); - // } - // - // } - // catch (Exception x) { - // throw new XDocletException(x, "Reading include file failed!"); - // } - // - // buf.flip(); - // return buf.toString(); } --- 471,474 ---- *************** *** 495,498 **** --- 483,487 ---- XClass clazz = getCurrentClass(); + // collect all fields that are tagged as primary-key List pkFields = new ArrayList(); *************** *** 507,520 **** } ! // sort the pk-fields alphabetically by their name ! Collections.sort(pkFields, pkFieldComparator); ! ! // if there is a desired order specified, we respect it String fieldOrderStr = clazz.getDoc().getTagAttributeValue("jdo.create-objectid-class", "field-order"); if (fieldOrderStr != null && !"".equals(fieldOrderStr)) { ! String[] fieldOrder = fieldOrderStr.replaceAll(" ", "").split(","); int newIdx = 0; for (int i = 0; i < fieldOrder.length; ++i) { String field = fieldOrder[i]; --- 496,519 ---- } ! // if there is a desired order specified, we order according to it String fieldOrderStr = clazz.getDoc().getTagAttributeValue("jdo.create-objectid-class", "field-order"); if (fieldOrderStr != null && !"".equals(fieldOrderStr)) { ! String[] fieldOrder = fieldOrderStr.replaceAll("\\s", "").split(","); int newIdx = 0; + // check, whether the specified fieldOrder is complete - i.e. all fields marked as primary key are mentioned. + Set fieldOrderSet = new HashSet(fieldOrder.length); + + for (int i = 0; i < fieldOrder.length; ++i) + fieldOrderSet.add(fieldOrder[i]); + + for (Iterator it = pkFields.iterator(); it.hasNext(); ) { + XField field = (XField) it.next(); + + if (!fieldOrderSet.contains(field.getName())) + throw new XDocletException("Class \"" + clazz.getQualifiedName() + "\" has field \"" + field + "\" tagged as primary-key-field but not specified in '@jdo.create-objectid-class field-order'! The attribute 'field-order' can be omitted (to order the fields alphabetically), but if specified, it must contain all primary key fields!"); + } + for (int i = 0; i < fieldOrder.length; ++i) { String field = fieldOrder[i]; *************** *** 538,542 **** } } ! // if (fieldsOrderStr != null && !"".equals(fieldsOrderStr)) { return pkFields; } --- 537,544 ---- } } ! else { ! // there is no field order specified, hence we sort the pk-fields alphabetically by their name ! Collections.sort(pkFields, pkFieldComparator); ! } return pkFields; } Index: JdoObjectIdGeneratorSubTask.java =================================================================== RCS file: /cvsroot/xdoclet/xdoclet/modules/jdo/src/xdoclet/modules/jdo/JdoObjectIdGeneratorSubTask.java,v retrieving revision 1.3 retrieving revision 1.4 diff -C2 -r1.3 -r1.4 *** JdoObjectIdGeneratorSubTask.java 20 Oct 2006 03:50:55 -0000 1.3 --- JdoObjectIdGeneratorSubTask.java 31 Jul 2007 14:37:28 -0000 1.4 *************** *** 25,29 **** --- 25,31 ---- extends TemplateSubTask { + protected final static String DEFAULT_TEMPLATE_FILE = "resources/java_objectid.xdt"; + private final static long serialVersionUID = 1L; private String interfaces = null; *************** *** 31,34 **** --- 33,41 ---- private String superclass = null; + /** + * @see #getRadix() + */ + private int radix = -1; + public JdoObjectIdGeneratorSubTask() { *************** *** 54,57 **** --- 61,81 ---- } + /** + * The radix that is used for encoding/decoding field values of numeric IDs (byte, short, int, long). <p> + * + * If it is undefined (i.e. not passed to the ant task), this method will return -1 and the radix 36 (0..9, a..z - + * see [EMAIL PROTECTED] Long#parseLong(String, int)}) is returned by [EMAIL PROTECTED] + * JdoObjectIdGeneratorTagsHandler#getRadix(java.util.Properties)}. </p> <p> + * + * The default radix of 36 leads to significantly shorter encoding </p> + * + * @return + * @since 2007-07-31 + */ + public int getRadix() + { + return radix; + } + public void setInterfaces(String interfaces) { *************** *** 64,67 **** --- 88,106 ---- } + /** + * @param radix the new radix. + * @see #getRadix() + */ + public void setRadix(int radix) + { + if (radix < 2) + throw new IllegalArgumentException("radix too small! must be >= 2!"); + + if (radix > 36) + throw new IllegalArgumentException("radix too big! must be <= 36!"); + + this.radix = radix; + } + protected String getGeneratedFileName(XClass clazz) throws XDocletException ------------------------------------------------------------------------- This SF.net email is sponsored by: Splunk Inc. Still grepping through log files to find problems? Stop. Now Search log events and configuration files using AJAX and a browser. Download your FREE copy of Splunk now >> http://get.splunk.com/ _______________________________________________ xdoclet-devel mailing list xdoclet-devel@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/xdoclet-devel