View Javadoc

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 "DataTypeGenerator.java".  Description: 
10  "Generates skeletal source code for Datatype classes based on the 
11    HL7 database" 
12  
13  The Initial Developer of the Original Code is University Health Network. Copyright (C) 
14  2001.  All Rights Reserved. 
15  
16  Contributor(s):  James Agnew 
17  
18  Alternatively, the contents of this file may be used under the terms of the 
19  GNU General Public License (the  �GPL�), in which case the provisions of the GPL are 
20  applicable instead of those above.  If you wish to allow use of your version of this 
21  file only under the terms of the GPL and not to allow others to use your version 
22  of this file under the MPL, indicate your decision by deleting  the provisions above 
23  and replace  them with the notice and other provisions required by the GPL License.  
24  If you do not delete the provisions above, a recipient may use your version of 
25  this file under either the MPL or the GPL. 
26  
27   */
28  
29  package ca.uhn.hl7v2.mvnplugin;
30  
31  import java.io.FileReader;
32  
33  import org.apache.maven.plugin.AbstractMojo;
34  import org.apache.maven.plugin.MojoExecutionException;
35  import org.apache.maven.plugin.MojoFailureException;
36  import org.apache.maven.project.MavenProject;
37  import org.codehaus.plexus.util.IOUtil;
38  
39  import ca.uhn.hl7v2.conf.parser.ProfileParser;
40  import ca.uhn.hl7v2.conf.spec.RuntimeProfile;
41  import ca.uhn.hl7v2.sourcegen.conf.GenerateDataTypesEnum;
42  import ca.uhn.hl7v2.sourcegen.conf.ProfileSourceGenerator;
43  
44  /**
45   * Maven Plugin for generating HAPI message structure classes which are based on
46   * an HL7 conformance profile file. For more information on using this plugin, see
47   * the <a href="confgen-usage.html">Confgen Usage</a> page.
48   * 
49   * @author <a href="mailto:jamesagnew@sourceforge.net">James Agnew</a>
50   * @goal confgen
51   * @phase generate-sources
52   * @requiresDependencyResolution runtime
53   * @requiresProject
54   * @inheritedByDefault false
55  	 * @since 2.1
56   */
57  public class ConfGenMojo extends AbstractMojo {
58  
59  	/**
60  	 * The maven project.
61  	 * 
62  	 * @parameter property="project"
63  	 * @required
64  	 * @readonly
65  	 * @since 2.1
66  	 */
67  	MavenProject project;
68  
69  	/**
70  	 * The target directory for the generated source
71  	 * 
72  	 * @parameter
73  	 * @required
74  	 * @since 2.1
75  	 */
76  	String targetDirectory;
77  
78  	/**
79  	 * The conformance profile (XML file path) to use
80  	 * 
81  	 * @parameter
82  	 * @required
83  	 * @since 2.1
84  	 */
85  	String profile;
86  
87  	/**
88  	 * The package for the generated source, e.g. "com.acme.hl7structure.adt"
89  	 * 
90  	 * @parameter
91  	 * @required
92  	 * @since 2.1
93  	 */
94  	String packageName;
95  
96  	/**
97  	 * Should data types be generated. Valid options are:
98  	 * <ul>
99  	 * <li><b>NONE</b>: Do not generate custom data types, use HAPI's normal
100 	 * data type classes for the HL7 version that the profile corresponds to
101 	 * <li><b>SINGLE</b>: Generate a single instance of each data type. In this
102 	 * case, hapi will generate a custom data type for the first instance of
103 	 * each type that it finds. So, any customizations that need to be made must
104 	 * be made in the very first time that a particular data type is used within
105 	 * a profile. For example, if you need to customize the "SN" data type, you
106 	 * will need to customize the very first one that appears in the profile, as
107 	 * all SNs within your message type will use the structure generated by the
108 	 * first one.
109 	 * <li><b>ALL</b>: Generate an individual data type object for each and every
110 	 * instance of a datatype within a structure. Note that this is very powerful
111 	 * and flexible,
112 	 * but also has the potential to generate a large number of classes, so use with
113 	 * caution. 
114 	 * </ul>
115 	 * 
116 	 * @parameter default-value="NONE"
117 	 * @since 2.1
118 	 */
119 	String generateDataTypes = "NONE";
120 
121 	/**
122 	 * The package from which to load the templates
123 	 * 
124 	 * @parameter default="ca.uhn.hl7v2.sourcegen.templates"
125 	 * @since 2.1
126 	 */
127 	String templatePackage = "ca.uhn.hl7v2.sourcegen.templates";
128 
129 	/**
130 	 * Should structures be treated as resources
131 	 */
132 	private boolean structuresAsResources;
133 
134 	/**
135 	 * File extension for the generated source files.
136 	 */
137 	private String fileExt = "java";
138 
139 	/**
140 	 * {@inheritDoc}
141 	 */
142 	public void execute() throws MojoExecutionException, MojoFailureException {
143 
144 		GenerateDataTypesEnum genDt;
145 		try {
146 			genDt = GenerateDataTypesEnum.valueOf(generateDataTypes);
147 		} catch (IllegalArgumentException e) {
148 			throw new MojoExecutionException("Unknown \"generateDataTypes\" value: " + generateDataTypes);
149 		}
150 
151 		try {
152 
153 			String profileString;
154 			FileReader reader = new FileReader(profile);
155 			profileString = IOUtil.toString(reader);
156 
157 			ProfileParser profileParser = new ProfileParser(false);
158 			RuntimeProfile runtimeProfile = profileParser.parse(profileString);
159 
160 			ProfileSourceGenerator gen = new ProfileSourceGenerator(runtimeProfile, targetDirectory, packageName, genDt, templatePackage, fileExt);
161 			gen.generate();
162 
163 			if (!structuresAsResources) {
164 				getLog().info("Adding path to compile sources: " + targetDirectory);
165 				project.addCompileSourceRoot(targetDirectory);
166 			}
167 
168 		} catch (Exception e) {
169 			throw new MojoExecutionException(e.getMessage(), e);
170 		}
171 
172 		project.addCompileSourceRoot(targetDirectory);
173 
174 	}
175 
176 	public static void main(String[] args) throws MojoExecutionException, MojoFailureException {
177 
178 		ConfGenMojo tst = new ConfGenMojo();
179 		tst.targetDirectory = "hapi-test/target/generated-sources/confgen";
180 		tst.packageName = "ca.uhn.hl7v2.test.conf.json";
181 		tst.profile = "hapi-test/src/test/resources/ca/uhn/hl7v2/conf/parser/ADT_A01.xml";
182 		tst.structuresAsResources = true;
183 		tst.templatePackage = "ca.uhn.hl7v2.sourcegen.templates.json";
184 		tst.generateDataTypes = "SINGLE";
185 		tst.fileExt = "json";
186 		tst.execute();
187 
188 	}
189 
190 }