/** 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 Initial Developer of the Original Code is University Health Network. Copyright (C) 2001. All Rights Reserved. Contributor(s): ______________________________________. 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.parser; /** * * @author James */ import ca.uhn.hl7v2.HL7Exception; import java.util.Map; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import ca.uhn.hl7v2.model.Type; import ca.uhn.hl7v2.model.Segment; import ca.uhn.hl7v2.model.Message; import ca.uhn.hl7v2.model.Group; import java.util.HashMap; /** * ModelClassFactory which allows custom packages to search to be specified. * These packages will be searched first, and if nothing is found for a particular * structure, DefaultModelClassFactory is used. * * @author Based on implementation by Christian Ohr * @since 1.0 */ public class CustomModelClassFactory implements ModelClassFactory { private static final long serialVersionUID = 1; private ModelClassFactory defaultFactory; private Map customModelClasses; private static Log LOG = LogFactory.getLog(CustomModelClassFactory.class); /** * Constructor which just delegated to {@link DefaultModelClassFactory} */ public CustomModelClassFactory() { this((Map)null); } /** * Constructor * * @param packageName The base package name to use. *

* When searching, package specified here will be appended with .[version].[structure type]. *

*

* So, for instance, when looking for a v2.5 segment object, if "com.foo" is passed in, HAPI will look in "com.foo.v25.segment.*" *

*/ public CustomModelClassFactory(String packageName) { defaultFactory = new DefaultModelClassFactory(); customModelClasses = new HashMap(); if (!packageName.endsWith(".")) { packageName += "."; } for (String nextVersion : Parser.getValidVersions()) { final String packageVersion = "v" + nextVersion.replace(".", ""); customModelClasses.put(nextVersion, new String[] {packageName + packageVersion}); } } /** * Constructor * @param map Map of packages to include. *

* Keys are versions of HL7, e.g. "v25". *

*

* Values are an array of packages to search in for custom model classes. * When searching, the package name here will be appended with ".[structure type]". * So, for example, to specify a custom message type, you could create the class * foo.example.v23.message.ZRM_Z01, and pass in the string "foo.example.v23". *

*/ public CustomModelClassFactory(Map map) { defaultFactory = new DefaultModelClassFactory(); customModelClasses = map; } /** * {@inheritDoc } */ public Class getMessageClass(String name, String version, boolean isExplicit) throws HL7Exception { if (!isExplicit) { name = Parser.getMessageStructureForEvent(name, version); } Class retVal = (Class) findClass("message", name, version); if (retVal == null) { retVal = defaultFactory.getMessageClass(name, version, isExplicit); } return retVal; } /** * {@inheritDoc } */ public Class getGroupClass(String name, String version) throws HL7Exception { Class retVal = (Class) findClass("group", name, version); if (retVal == null) { retVal = defaultFactory.getGroupClass(name, version); } return retVal; } /** * {@inheritDoc } */ public Class getSegmentClass(String name, String version) throws HL7Exception { Class retVal = (Class) findClass("segment", name, version); if (retVal == null) { retVal = defaultFactory.getSegmentClass(name, version); } return retVal; } /** * {@inheritDoc } */ public Class getTypeClass(String name, String version) throws HL7Exception { Class retVal = (Class) findClass("datatype", name, version); if (retVal == null) { retVal = defaultFactory.getTypeClass(name, version); } return retVal; } /** * Finds appropriate classes to be loaded for the given structure/type */ protected Class findClass(String subpackage, String name, String version) throws HL7Exception { if (!Parser.validVersion(version)) { throw new HL7Exception("HL7 version " + version + " is not supported", HL7Exception.UNSUPPORTED_VERSION_ID); } Class classLoaded = null; if (customModelClasses != null) { if (customModelClasses.containsKey(version)) { for (String next : customModelClasses.get(version)) { if (!next.endsWith(".")) { next += "."; } String fullyQualifiedName = next + subpackage + '.' + name; try { classLoaded = Class.forName(fullyQualifiedName); LOG.debug("Found " + fullyQualifiedName + " in custom HL7 model"); } catch (ClassNotFoundException e) { // ignore } } } } return classLoaded; } }