001: package com.avaje.util.codegen;
002:
003: public class CommentGenerator {
004:
005: String getterPrefix = "Return ";
006:
007: String getterSuffix = ".";
008:
009: String setterPrefix = "Set ";
010:
011: String setterSuffix = ".";
012:
013: String commentStart = " /**\r\n * ";
014:
015: String commentEnd = "\r\n */";
016:
017: boolean toLowerCase = true;
018:
019: String embIdPrefix = "Id for ";
020:
021: String embIdSuffix = "";
022:
023: String classPrefix = "";
024:
025: String classSuffix = " entity bean.";
026:
027: public static void main(String[] args) {
028:
029: CommentGenerator c = new CommentGenerator();
030: String s = c.generateMethod("setActiveProductStatus");
031:
032: System.out.println(s);
033: }
034:
035: /**
036: * Generate a property comment.
037: */
038: public String generateProperty(String propertyName) {
039: return null;
040: }
041:
042: /**
043: * Generate a method comment.
044: */
045: public String generateMethod(String methodName) {
046:
047: if (methodName.startsWith("set")) {
048: return generateSetter(methodName.substring(3));
049: } else if (methodName.startsWith("get")) {
050: return generateGetter(methodName.substring(3), null);
051: } else if (methodName.startsWith("is")) {
052: return generateGetter(methodName.substring(0), null);
053: } else {
054: return null;
055: }
056: }
057:
058: /**
059: * Return a EmbeddedId class comment.
060: */
061: public String generateEmbeddedIdClass(String className) {
062:
063: int lastDot = className.lastIndexOf('.');
064: if (lastDot > -1) {
065: className = className.substring(lastDot + 1);
066: }
067: String c = "/**\r\n * " + embIdPrefix
068: + toWords(className, false) + embIdSuffix + "\r\n */";
069: return c;
070: }
071:
072: /**
073: * Return a class comment.
074: */
075: public String generateClass(String className) {
076:
077: int lastDot = className.lastIndexOf('.');
078: if (lastDot > -1) {
079: className = className.substring(lastDot + 1);
080: }
081: String c = "/**\r\n * " + classPrefix
082: + toWords(className, false) + classSuffix + "\r\n */";
083: return c;
084: }
085:
086: public String generateSetter(String methodName) {
087:
088: return commentStart + setterPrefix + toWords(methodName)
089: + setterSuffix + commentEnd;
090: }
091:
092: public String generateGetter(String methodName, String prefix) {
093:
094: StringBuffer sb = new StringBuffer();
095: sb.append(commentStart);
096: sb.append(getterPrefix);
097: if (prefix != null) {
098: sb.append(prefix);
099: }
100: sb.append(toWords(methodName));
101: sb.append(getterSuffix);
102: sb.append(commentEnd);
103: return sb.toString();
104: }
105:
106: public String toWords(String methodName) {
107: return toWords(methodName, toLowerCase);
108: }
109:
110: public String toWords(String methodName, boolean toLower) {
111:
112: int lastUpper = -1;
113: StringBuffer sb = new StringBuffer();
114: for (int i = 0; i < methodName.length(); i++) {
115: char c = methodName.charAt(i);
116: // if Digit or upper case then insert an underscore //
117: if (Character.isDigit(c) || Character.isUpperCase(c)) {
118: if (i > lastUpper + 1) {
119: sb.append(" ");
120: }
121: if (toLower) {
122: c = Character.toLowerCase(c);
123: }
124: sb.append(c);
125: lastUpper = i;
126: } else {
127: sb.append(c);
128: }
129: }
130: return sb.toString();
131: }
132: }
|