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