001: package com.avaje.util.codegen;
002:
003: import java.io.File;
004: import java.io.IOException;
005: import java.util.Iterator;
006:
007: import com.avaje.lib.util.Dnode;
008:
009: public class SourceCodeMerger {
010:
011: boolean renameSourceCode;
012: boolean mergeSourceCode;
013:
014: public SourceCodeMerger(GenerateConfiguration config) {
015: String rename = config.getProperty("codegen.source.filerename",
016: "true");
017: renameSourceCode = !rename.equalsIgnoreCase("false");
018:
019: String merge = config.getProperty("codegen.source.merge",
020: "true");
021: mergeSourceCode = !merge.equalsIgnoreCase("false");
022:
023: }
024:
025: public void mergeSource(GenerateInfo info) throws IOException {
026: SourceCode beanSource = getBeanSource(info);
027: if (beanSource != null && mergeSourceCode) {
028: info.setBeanSource(beanSource);
029: addAdditional(info, beanSource);
030: }
031: }
032:
033: private SourceCode getBeanSource(GenerateInfo info)
034: throws IOException {
035:
036: String beanFn = info.getFullFileName();
037:
038: SourceCode beanSource = null;
039: File beanSourceFile = new File(beanFn);
040: if (beanSourceFile.exists()) {
041:
042: SourceCodeParser p = new SourceCodeParser();
043: beanSource = p.parse(beanSourceFile);
044:
045: if (renameSourceCode) {
046: String renameTo = beanFn + "." + info.getNowString()
047: + ".old";
048: File renameFile = new File(renameTo);
049: beanSourceFile.renameTo(renameFile);
050: }
051: beanSourceFile = new File(beanFn);
052: }
053:
054: return beanSource;
055: }
056:
057: private void addAdditional(GenerateInfo info, SourceCode beanSource) {
058:
059: if (beanSource != null) {
060: // set information from the source so that it is kept
061: Dnode beanTree = info.getBeanTree();
062: //context.put("beansource", beanSource);
063:
064: // set package declaration comments
065: if (beanSource.getPackageComment() != null) {
066: beanTree.setAttribute("packagecomment", beanSource
067: .getPackageComment());
068: }
069:
070: // set class declaration from the source code
071: beanTree.setAttribute("classdeclaration", beanSource
072: .getClassDeclaration());
073:
074: if (beanSource.getClassComment() != null) {
075: beanTree.setAttribute("classcomment", beanSource
076: .getClassComment());
077: }
078:
079: // set additional properties
080: String addProps = getAdditionalProperties(info);
081: beanTree.setAttribute("additionalproperties", addProps);
082:
083: // set additional methods
084: String addMethods = getAdditionalMethods(info);
085: beanTree.setAttribute("additionalmethods", addMethods);
086:
087: // set any inner classes...
088: beanTree.setAttribute("innerclasscode", beanSource
089: .innerClassCode());
090: }
091: }
092:
093: private String getAdditionalProperties(GenerateInfo info) {
094:
095: SourceCode beanSource = info.getBeanSource();
096: Dnode beanTree = info.getBeanTree();
097:
098: // find additional properties..
099: StringBuffer sb = new StringBuffer();
100: int addPropCount = 0;
101: Iterator sourcePropNameIt = beanSource.propertyNames();
102: while (sourcePropNameIt.hasNext()) {
103: String propName = (String) sourcePropNameIt.next();
104: Dnode deployProp = beanTree.find("property", "name",
105: propName);
106: if (deployProp == null && !info.isDeleteProperty(propName)) {
107: //not deployed and not in the delete list...
108: String propCode = beanSource.getPropertyCode(propName);
109: if (addPropCount > 0) {
110: sb.append("\r\n");
111: }
112: sb.append(propCode);
113: addPropCount++;
114: }
115: }
116: if (addPropCount == 0) {
117: return null;
118: }
119: return sb.toString();
120: }
121:
122: private String getAdditionalMethods(GenerateInfo info) {
123:
124: SourceCode beanSource = info.getBeanSource();
125:
126: // find additional properties..
127: StringBuffer sb = new StringBuffer();
128: int addCount = 0;
129:
130: Iterator methodNameIt = beanSource.methodNames();
131:
132: while (methodNameIt.hasNext()) {
133: String methodName = (String) methodNameIt.next();
134: if (!info.isDeployedMethod(methodName)) {
135: // it is not a known getter or setter
136:
137: if (!info.isDeleteMethod(methodName)) {
138: // it is not in known delete list..
139: String methodCode = beanSource
140: .getMethod(methodName);
141: if (addCount > 0) {
142: sb.append("\r\n");
143: }
144: sb.append(methodCode);
145: addCount++;
146: }
147: }
148: }
149: if (addCount == 0) {
150: return null;
151: }
152: return sb.toString();
153: }
154: }
|