/**
The contents of this file are subject to the Mozilla Public License Version 1.1
(the "License"); you may not use this file except in compliance with the License.
You may obtain a copy of the License at http://www.mozilla.org/MPL/
Software distributed under the License is distributed on an "AS IS" basis,
WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for the
specific language governing rights and limitations under the License.
The Original Code is "DataTypeGenerator.java". Description:
"Generates skeletal source code for Datatype classes based on the
HL7 database"
The Initial Developer of the Original Code is University Health Network. Copyright (C)
2001. All Rights Reserved.
Contributor(s): James Agnew
Alternatively, the contents of this file may be used under the terms of the
GNU General Public License (the �GPL�), in which case the provisions of the GPL are
applicable instead of those above. If you wish to allow use of your version of this
file only under the terms of the GPL and not to allow others to use your version
of this file under the MPL, indicate your decision by deleting the provisions above
and replace them with the notice and other provisions required by the GPL License.
If you do not delete the provisions above, a recipient may use your version of
this file under either the MPL or the GPL.
*/
package ca.uhn.hl7v2.mvnplugin;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.project.MavenProject;
import org.codehaus.plexus.util.IOUtil;
import ca.uhn.hl7v2.conf.ProfileException;
import ca.uhn.hl7v2.conf.parser.ProfileParser;
import ca.uhn.hl7v2.conf.spec.RuntimeProfile;
import ca.uhn.hl7v2.sourcegen.conf.GenerateDataTypesEnum;
import ca.uhn.hl7v2.sourcegen.conf.ProfileSourceGenerator;
/**
* Maven Plugin Mojo for generating HAPI conformance classes
*
* @author James Agnew
* @goal confgen
* @phase generate-sources
* @requiresDependencyResolution runtime
* @requiresProject
* @inheritedByDefault false
*/
public class ConfGenMojo extends AbstractMojo {
/**
* The maven project.
*
* @parameter expression="${project}"
* @required
* @readonly
*/
private MavenProject project;
/**
* The target directory for the generated source
*
* @parameter
* @required
*/
private String targetDirectory;
/**
* The conformance profile (XML file path) to use
*
* @parameter
*/
private String profile;
/**
* The conformance profile (XML file path) to use
*
* @parameter
*/
private File profileFolder;
/**
* The package for the generated source
*
* @parameter
* @required
*/
private String packageName;
/**
*
* Should data types be generated. Valid options are:
*
*
* - NONE: Do not generate custom data types, use HAPI's normal
* data type classes for the HL7 version that the profile corresponds to
*
- SINGLE: Generate a single instance of each data type. In this
* case, hapi will generate a custom data type for the first instance of
* each type that it finds. So, any customizations that need to be made must
* be made in the very first time that a particular data type is used within
* a profile.
*
*
* @parameter default-value="NONE"
*/
private String generateDataTypes = "NONE";
/**
* The package from which to load the templates
*
* @parameter default="ca.uhn.hl7v2.sourcegen.templates"
*/
private String templatePackage = "ca.uhn.hl7v2.sourcegen.templates";
/**
* Should structures be treated as resources
*
* @parameter default="false"
*/
private boolean structuresAsResources;
/**
* Should structures be treated as resources
*
* @parameter default="java"
*/
private String fileExt = "java";
/**
* {@inheritDoc}
*/
public void execute() throws MojoExecutionException, MojoFailureException {
GenerateDataTypesEnum genDt;
try {
genDt = GenerateDataTypesEnum.valueOf(generateDataTypes);
} catch (IllegalArgumentException e) {
throw new MojoExecutionException("Unknown \"generateDataTypes\" value: " + generateDataTypes);
}
try {
File profileFile = null;
if (profile != null) {
profileFile = new File(profile);
}
if (profileFile == null || !profileFile.exists()) {
if (!profileFolder.exists()) {
throw new MojoExecutionException("No valid profile");
}
File[] listFiles = profileFolder.listFiles();
for (File file : listFiles) {
if (file.isFile() && file.getName().toLowerCase().endsWith(".xml")) {
generate(genDt, file);
}
}
} else {
generate(genDt, profileFile);
}
} catch (Exception e) {
throw new MojoExecutionException(e.getMessage(), e);
}
project.addCompileSourceRoot(targetDirectory);
}
private void generate(GenerateDataTypesEnum genDt, File profileFile)
throws FileNotFoundException, IOException, ProfileException,
Exception {
FileReader reader = new FileReader(profileFile);
String profileString = IOUtil.toString(reader);
ProfileParser profileParser = new ProfileParser(false);
RuntimeProfile runtimeProfile = profileParser.parse(profileString);
ProfileSourceGenerator gen = new ProfileSourceGenerator(runtimeProfile, targetDirectory, packageName, genDt, templatePackage, fileExt);
gen.generate();
if (!structuresAsResources) {
getLog().info("Adding path to compile sources: " + targetDirectory);
project.addCompileSourceRoot(targetDirectory);
}
}
public static void main(String[] args) throws MojoExecutionException, MojoFailureException {
ConfGenMojo tst = new ConfGenMojo();
tst.targetDirectory = "hapi-test/target/generated-sources/confgen";
tst.packageName = "ca.uhn.hl7v2.test.conf.json";
tst.profile = "hapi-test/src/test/resources/ca/uhn/hl7v2/conf/parser/ADT_A01.xml";
tst.structuresAsResources = true;
tst.templatePackage = "ca.uhn.hl7v2.sourcegen.templates.json";
tst.generateDataTypes = "SINGLE";
tst.fileExt = "json";
tst.execute();
}
}