1 /**
2 The contents of this file are subject to the Mozilla Public License Version 1.1
3 (the "License"); you may not use this file except in compliance with the License.
4 You may obtain a copy of the License at http://www.mozilla.org/MPL/
5 Software distributed under the License is distributed on an "AS IS" basis,
6 WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for the
7 specific language governing rights and limitations under the License.
8
9 The Original Code is "GeneratedClass.java". Description:
10 "This Class generates a Class File which is used by the Conformance Builder Classes"
11
12 The Initial Developer of the Original Code is University Health Network. Copyright (C)
13 2001. All Rights Reserved.
14
15 Contributor(s): James Agnew
16 Paul Brohman
17 Mitch Delachevrotiere
18 Shawn Dyck
19 Cory Metcalf
20
21 Alternatively, the contents of this file may be used under the terms of the
22 GNU General Public License (the ?GPL?), in which case the provisions of the GPL are
23 applicable instead of those above. If you wish to allow use of your version of this
24 file only under the terms of the GPL and not to allow others to use your version
25 of this file under the MPL, indicate your decision by deleting the provisions above
26 and replace them with the notice and other provisions required by the GPL License.
27 If you do not delete the provisions above, a recipient may use your version of
28 this file under either the MPL or the GPL.
29
30 */
31 package ca.uhn.hl7v2.conf.classes.generator.genclasses;
32 import java.util.*;
33 import ca.uhn.hl7v2.conf.classes.generator.builders.*;
34
35 /** This Class generates a Class File which is used by the Conformance Builder Classes
36 * @author <table><tr>James Agnew</tr>
37 * <tr>Paul Brohman</tr>
38 * <tr>Mitch Delachevrotiere</tr>
39 * <tr>Shawn Dyck</tr>
40 * <tr>Cory Metcalf</tr></table>
41 */
42 public class GeneratedClass {
43
44 private static final String INDENT = " ";
45 private final Vector<String> classComments;
46 private final Vector<String> classImports;
47 private String classPackage;
48 private GeneratedMethod constructor;
49 private final Vector<String> license;
50 private final Vector<String> memberVariables;
51 private final Vector<GeneratedMethod> methods;
52 private String name;
53 private String properties;
54
55 /** Creates a new instance of GeneratedClass,
56 * creates a new instance of the all the memberVariables
57 */
58 public GeneratedClass() {
59 classImports = new Vector<>();
60 memberVariables = new Vector<>();
61 methods = new Vector<>();
62 classComments = new Vector<>();
63 license = new Vector<>();
64 constructor = new GeneratedMethod();
65 constructor.setVisibility("public");
66 classPackage = "";
67 properties = "";
68 }
69
70 /** This method adds a new comment to the class contained within the generated Java source file
71 * @param classComment adds a comment to the class
72 */
73 public void addClassComment(String classComment) {
74 this.classComments.add(classComment);
75 }
76
77 /** This method adds a new import statement to the class contained within the generated Java source file
78 * @param classImport the import path to import
79 */
80 public void addClassImport(String classImport) {
81 this.classImports.add("import " + classImport + ";");
82 }
83
84 /** This method adds a new member variable to the class contained within the generated Java source file
85 * @param memberVariable the member variable to add
86 */
87 public void addMemberVariable(String memberVariable) {
88 memberVariables.add(memberVariable);
89 }
90
91 /** This method adds a new method to the class contained within the generated Java source file
92 * @param method the generated method to add
93 */
94 public void addMethod(GeneratedMethod method) {
95 methods.add(method);
96 }
97
98 /** This method sets the class comments for the class contained within the generated Java source file
99 * @param comments the comments to add to the class comments
100 */
101 public void addToClassComments(String comments) {
102 this.classComments.add(comments);
103 }
104
105 /** This method returns the constructor for the class contained within the generated Java source file
106 * @return the generated methods constructor
107 */
108 public GeneratedMethod getConstructor() {
109 return this.constructor;
110 }
111
112 /** This method returns the class name of the class contained within the generated Java source file
113 * @return the class name
114 */
115 public String getName() {
116 return this.name;
117 }
118
119 /** This method sets the classPackage for the class contained within the generated Java source file
120 * @param classPackage the classPackage to set
121 */
122 public void setClassPackage(String classPackage) {
123 if (classPackage != null)
124 this.classPackage = classPackage;
125 else
126 this.classPackage = "";
127 }
128
129 /** This method sets the constructor for the class contained within the generated Java source file
130 * @return the generated methods constructor
131 */
132 public void setConstructor(GeneratedMethod constructor) {
133 this.constructor = constructor;
134 }
135
136 /** This method sets the class name for the class contained within the generated Java source file
137 * @param name the class name
138 */
139 public void setName(String name) {
140 this.name = name + " ";
141 this.constructor.setName(name);
142 }
143
144 /** This method sets the class properties for the class contained within the generated Java source file
145 * @param properties properties to set
146 */
147 public void setProperties(String properties) {
148 if (properties != null)
149 this.properties = properties + " ";
150 else
151 this.properties = "";
152 }
153
154 /** This method will return a String representation of the class contained within the generated
155 * Java source file
156 * @return a String representation of the class
157 */
158 public String toString() {
159 String theClass = "";
160
161 theClass += DocumentationBuilder.getDocumentationBuilder().getGeneratedClassHeader();
162 theClass += vectorToString(0, license, "").concat("\n\n"); // License block
163 theClass += "package " + classPackage + ";\n\n"; // Package statement
164 theClass += vectorToString(0, classImports, "").concat("\n\n"); // Package and import statements
165 theClass += "/**\n" + vectorToString(0, classComments, " * ").concat(" */\n"); // Class comments
166
167 theClass += "public class " + name + properties + "{\n\n"; // Class statement
168 theClass += vectorToString(1, memberVariables, "").concat("\n\n"); // member variables
169 theClass += constructor.toString() + "\n\n"; // Constructor
170 theClass += vectorToString(0, methods, "").concat("\n"); // methods (no indentation because methods indent themselves)
171 theClass += "}\n";
172 return theClass;
173 }
174
175 /** Creates a string based on a vector representing the individual lines of a string
176 * @param indentLevel the indentation level to use. For example, a level of 2 would use two "tabs" before each line.
177 * @param vec the vector to be converted
178 * @param prefix a string to prepend to each line (ie <example>" * "</example> for a comment)
179 * @return the generated string
180 */
181 private String vectorToString(int indentLevel, Vector<?> vec, String prefix) {
182 StringBuilder pString = new StringBuilder();
183 StringBuilder indent = new StringBuilder("".concat(prefix));
184
185 // Create indentation string
186 for (int i = 0; i < indentLevel; i++)
187 indent.insert(0, INDENT);
188
189 // Convert the vector to a string
190 for (Object o : vec) pString.append(indent).append(o.toString()).append("\n");
191 return pString.toString();
192 }
193
194 }