1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29 package ca.uhn.hl7v2.sourcegen;
30
31 import java.util.StringTokenizer;
32 import java.io.File;
33 import java.io.IOException;
34
35 import ca.uhn.hl7v2.HL7Exception;
36 import org.apache.commons.lang.StringUtils;
37
38
39
40
41
42
43
44 public class SourceGenerator {
45
46 public static final String ENCODING = "UTF-8";
47
48
49 public SourceGenerator() {
50 }
51
52
53
54
55
56
57 public static void makeAll(String baseDirectory, String version, boolean failOnError, String theTemplatePackage, String theFileExt) throws HL7Exception {
58
59
60
61
62
63 try {
64 Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
65 MessageGenerator.makeAll(baseDirectory, version, failOnError, theTemplatePackage, theFileExt);
66 SegmentGenerator.makeAll(baseDirectory, version, theTemplatePackage, theFileExt);
67 DataTypeGenerator.makeAll(baseDirectory, version, theTemplatePackage, theFileExt);
68
69 } catch (Exception e) {
70 throw new HL7Exception(e);
71 }
72
73 }
74
75
76
77
78
79
80
81
82
83 public static String makeAccessorName(String fieldDesc, String parentName) {
84 StringBuilder aName = new StringBuilder();
85 char[] chars = fieldDesc.toCharArray();
86 boolean lastCharWasNotLetter = true;
87 int inBrackets = 0;
88 StringBuffer bracketContents = new StringBuffer();
89 for (char aChar : chars) {
90 if (aChar == '(') inBrackets++;
91 if (aChar == ')') inBrackets--;
92
93 if (Character.isLetterOrDigit(aChar)) {
94 if (inBrackets > 0) {
95
96 bracketContents.append(aChar);
97 } else {
98
99 if (bracketContents.length() > 0) {
100 aName.append(capitalize(filterBracketedText(bracketContents.toString())));
101 bracketContents = new StringBuffer();
102 }
103 if (lastCharWasNotLetter) {
104
105 aName.append(Character.toUpperCase(aChar));
106 } else {
107 aName.append(aChar);
108 }
109 lastCharWasNotLetter = false;
110 }
111 } else {
112 lastCharWasNotLetter = true;
113 }
114 }
115 aName.append(capitalize(filterBracketedText(bracketContents.toString())));
116 String retVal = aName.toString();
117
118
119 if (retVal.equals("Parent")) {
120 retVal = parentName + "Parent";
121 }
122 if (retVal.equals("Name")) {
123 retVal = parentName + "Name";
124 }
125
126 return retVal;
127 }
128
129
130
131
132
133
134
135
136
137
138 private static String filterBracketedText(String text) {
139 String filtered = "";
140 boolean isDataType = true;
141 if (!text.equals(text.toUpperCase())) isDataType = false;
142 if (text.length() < 2 || text.length() > 3) isDataType = false;
143
144 if (!isDataType) filtered = text;
145 return filtered;
146 }
147
148
149 private static String capitalize(String text) {
150 StringBuilder cap = new StringBuilder();
151 if (text.length() > 0) {
152 cap.append(Character.toUpperCase(text.charAt(0)));
153 cap.append(text.substring(1));
154 }
155 return cap.toString();
156 }
157
158
159
160
161 public static File makeDirectory(String directory) throws IOException {
162 StringTokenizer tok = new StringTokenizer(directory, "\\/", false);
163
164 File currDir = directory.startsWith("/") ? new File("/") : null;
165 while (tok.hasMoreTokens()) {
166 String thisDirName = tok.nextToken();
167
168 currDir = new File(currDir, thisDirName);
169
170 if (!currDir.exists()) {
171
172 currDir.mkdir();
173 } else if (currDir.isFile()) {
174 throw new IOException("Can't create directory " + thisDirName +
175 " - file with same name exists.");
176 }
177 }
178
179 return currDir;
180 }
181
182
183
184
185
186
187
188
189
190
191
192
193 public static String getAlternateType(String dataTypeName, String version) {
194 String ret = dataTypeName;
195
196
197 if (ret.equals("varies")) ret = "Varies";
198
199
200 if (ret.equals("NUL")) ret = "NULLDT";
201 if (ret.equals("-")) ret = "NULLDT";
202
203
204
205
206
207
208
209
210
211
212
213 return ret;
214 }
215
216 public static void main(String[] args) throws ClassNotFoundException, HL7Exception {
217 Class.forName("com.mysql.jdbc.Driver");
218 System.setProperty("ca.on.uhn.hl7.database.url", "jdbc:mysql://localhost:3306/hl7v65");
219 System.setProperty("ca.on.uhn.hl7.database.user", "hl7");
220 System.setProperty("ca.on.uhn.hl7.database.password", "hl7");
221 makeAll("tmp", "2.5.1", true, "", "java");
222 }
223
224 public static String makeAlternateAccessorName(String fieldDesc, String parentName, int index) {
225 StringBuilder aName = new StringBuilder();
226
227 aName.append(StringUtils.capitalize(parentName.toLowerCase())).append(index).append("_");
228
229 char[] chars = fieldDesc.toCharArray();
230 boolean lastCharWasNotLetter = true;
231 int inBrackets = 0;
232 StringBuffer bracketContents = new StringBuffer();
233 for (char aChar : chars) {
234 if (aChar == '(') inBrackets++;
235 if (aChar == ')') inBrackets--;
236
237 if (Character.isLetterOrDigit(aChar)) {
238 if (inBrackets > 0) {
239
240 bracketContents.append(aChar);
241 } else {
242
243 if (bracketContents.length() > 0) {
244 aName.append(capitalize(filterBracketedText(bracketContents.toString())));
245 bracketContents = new StringBuffer();
246 }
247 if (lastCharWasNotLetter) {
248
249 aName.append(Character.toUpperCase(aChar));
250 } else {
251 aName.append(aChar);
252 }
253 lastCharWasNotLetter = false;
254 }
255 } else {
256 lastCharWasNotLetter = true;
257 }
258 }
259 aName.append(capitalize(filterBracketedText(bracketContents.toString())));
260
261
262 return aName.toString();
263 }
264
265 }