001/** 002The contents of this file are subject to the Mozilla Public License Version 1.1 003(the "License"); you may not use this file except in compliance with the License. 004You may obtain a copy of the License at http://www.mozilla.org/MPL/ 005Software distributed under the License is distributed on an "AS IS" basis, 006WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for the 007specific language governing rights and limitations under the License. 008 009The Original Code is "DataTypeGenerator.java". Description: 010"Creates source code for RIM DataTypes" 011 012The Initial Developer of the Original Code is University Health Network. Copyright (C) 0132001. All Rights Reserved. 014 015Contributor(s): ______________________________________. 016 017Alternatively, the contents of this file may be used under the terms of the 018GNU General Public License (the "GPL"), in which case the provisions of the GPL are 019applicable instead of those above. If you wish to allow use of your version of this 020file only under the terms of the GPL and not to allow others to use your version 021of this file under the MPL, indicate your decision by deleting the provisions above 022and replace them with the notice and other provisions required by the GPL License. 023If you do not delete the provisions above, a recipient may use your version of 024this file under either the MPL or the GPL. 025 026*/ 027 028package ca.uhn.hl7v3.sourcegen; 029 030/** 031 * Creates source code for RIM DataTypes. 032 * @author Bryan Tripp 033 * @deprecated 034 */ 035public class DataTypeGenerator { 036 037 /** Creates a new instance of DataTypeGenerator */ 038 public DataTypeGenerator() { 039 } 040 041 public String makeDataType(DataTypeDefinition type) { 042 String code = null; 043 if (type.getType().equalsIgnoreCase("Primitive")) { 044 code = makePrimitive(type); 045 } else if (type.getType().equalsIgnoreCase("Composite")) { 046 code = makeComposite(type); 047 } else if (type.getType().equalsIgnoreCase("Generic")) { 048 code = makeGeneric(type); 049 } else if (type.getType().equalsIgnoreCase("Instance")) { 050 code = makeInstance(type); 051 } 052 return code; 053 } 054 055 public String makePrimitive(DataTypeDefinition primitiveType) { 056 StringBuffer code = new StringBuffer(); 057 code.append("package "); 058 code.append(SourceGenerator.getRIMDataTypePackage()); 059 code.append("; \r\n\r\n"); 060 code.append(SourceGenerator.makeJavaDocComment(primitiveType.getDescription(), 0)); 061 code.append("public class "); 062 code.append(primitiveType.getName()); 063 code.append(" extends "); 064 if (primitiveType.getSuperClass() != null) { 065 code.append(primitiveType.getSuperClass()); 066 } else { 067 code.append("Primitive"); 068 } 069 code.append(" { \r\n\r\n"); 070 code.append("} \r\n"); 071 return code.toString(); 072 } 073 074 public String makeComposite(DataTypeDefinition compositeType) { 075 StringBuffer code = new StringBuffer(); 076 code.append("package "); 077 code.append(SourceGenerator.getRIMDataTypePackage()); 078 code.append("; \r\n\r\n"); 079 code.append(SourceGenerator.makeJavaDocComment(compositeType.getDescription(), 0)); 080 code.append("public class "); 081 code.append(compositeType.getName()); 082 code.append(" extends "); 083 if (compositeType.getSuperClass() != null) { 084 code.append(compositeType.getSuperClass()); 085 } else { 086 code.append("Composite"); 087 } 088 code.append(" { \r\n\r\n"); 089 ComponentDefinition[] components = compositeType.getComponents(); 090 for (int i = 0; i < components.length; i++) { 091 code.append(makeAttributeDeclaration(components[i])); 092 } 093 code.append("\r\n"); 094 for (int i = 0; i < components.length; i++) { 095 code.append(makeSetter(components[i])); 096 code.append(makeGetter(components[i])); 097 code.append("\r\n"); 098 } 099 code.append("} \r\n"); 100 return code.toString(); 101 } 102 103 public String makeGeneric(DataTypeDefinition genericType) { 104 StringBuffer code = new StringBuffer(); 105 code.append("package "); 106 code.append(SourceGenerator.getRIMDataTypePackage()); 107 code.append("; \r\n\r\n"); 108 code.append(SourceGenerator.makeJavaDocComment(genericType.getDescription(), 0)); 109 code.append("public class "); 110 code.append(genericType.getName()); 111 code.append(" extends "); 112 if (genericType.getSuperClass() != null) { 113 code.append(genericType.getSuperClass()); 114 } else { 115 code.append("Generic"); 116 } 117 code.append(" { \r\n\r\n"); 118 code.append("} \r\n"); 119 return code.toString(); 120 } 121 122 public String makeInstance(DataTypeDefinition instanceType) { 123 StringBuffer code = new StringBuffer(); 124 code.append("package "); 125 code.append(SourceGenerator.getRIMDataTypePackage()); 126 code.append("; \r\n\r\n"); 127 code.append(SourceGenerator.makeJavaDocComment(instanceType.getDescription(), 0)); 128 code.append("public class "); 129 code.append(DataTypeDefinition.mapInstanceName(instanceType.getName())); 130 code.append(" extends "); 131 code.append(DataTypeDefinition.getAssociatedGeneric(instanceType.getName())); 132 code.append(" { \r\n"); 133 code.append("} \r\n"); 134 return code.toString(); 135 } 136 137 public String makeAttributeDeclaration(ComponentDefinition component) { 138 StringBuffer code = new StringBuffer(); 139 code.append(" public "); 140 code.append(component.getDataType()); 141 code.append(" "); 142 code.append(component.getName()); 143 code.append("; \r\n"); 144 return code.toString(); 145 } 146 147 public String makeGetter(ComponentDefinition component) { 148 StringBuffer code = new StringBuffer(); 149 code.append(SourceGenerator.makeJavaDocComment(component.getDescription(), 4)); 150 code.append(" public "); 151 code.append(component.getDataType()); 152 code.append(" get"); 153 code.append(capitalize(component.getName())); 154 code.append("() { \r\n"); 155 code.append(" return this."); 156 code.append(component.getName()); 157 code.append("; \r\n"); 158 code.append(" } \r\n"); 159 return code.toString(); 160 } 161 162 public String makeSetter(ComponentDefinition component) { 163 StringBuffer code = new StringBuffer(); 164 code.append(SourceGenerator.makeJavaDocComment(component.getDescription(), 4)); 165 code.append(" public void set"); 166 code.append(capitalize(component.getName())); 167 code.append("("); 168 code.append(component.getDataType()); 169 code.append(" "); 170 code.append(component.getName()); 171 code.append(") { \r\n"); 172 code.append(" this."); 173 code.append(component.getName()); 174 code.append(" = "); 175 code.append(component.getName()); 176 code.append("; \r\n"); 177 code.append(" } \r\n"); 178 return code.toString(); 179 } 180 181 private static String capitalize(String s) { 182 StringBuffer result = new StringBuffer(); 183 if (s != null && s.length() > 0) { 184 result.append(s.substring(0, 1).toUpperCase()); 185 if (s.length() > 1) result.append(s.substring(1, s.length())); 186 } 187 return result.toString(); 188 } 189}