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 Initial Developer of the Original Code is University Health Network. Copyright (C)
10  2001.  All Rights Reserved.
11  
12  Contributor(s): ______________________________________.
13  
14  Alternatively, the contents of this file may be used under the terms of the
15  GNU General Public License (the  "GPL"), in which case the provisions of the GPL are
16  applicable instead of those above.  If you wish to allow use of your version of this
17  file only under the terms of the GPL and not to allow others to use your version
18  of this file under the MPL, indicate your decision by deleting  the provisions above
19  and replace  them with the notice and other provisions required by the GPL License.
20  If you do not delete the provisions above, a recipient may use your version of
21  this file under either the MPL or the GPL.
22  
23  */
24  package ca.uhn.hl7v2.parser;
25  
26  import ca.uhn.hl7v2.HL7Exception;
27  import ca.uhn.hl7v2.Version;
28  import ca.uhn.hl7v2.model.Group;
29  import ca.uhn.hl7v2.model.Message;
30  import ca.uhn.hl7v2.model.Segment;
31  import ca.uhn.hl7v2.model.SuperStructure;
32  import ca.uhn.hl7v2.model.Type;
33  
34  /**
35   * Specialized version of ModelClassFactory that always returns the same version or even structure. This is useful when designing
36   * applications which are expected to handle multiple versions of HL7. The recommended approach is to
37   * configure this factory to handle the newest version of HL7 you intend to support. Since HL7 is a backwards
38   * compatible protocol, older versions should always be able to parse correctly into a newer message structure.  
39   * 
40   * @version $Revision: 1.2 $ updated on $Date: 2009-10-03 15:25:46 $ by $Author: jamesagnew $
41   * @author This ModelClassFactory implementation is modified by Niranjan.Sharma@med.ge.com on 27-Jul-2009 for CanonicalModel of V2.6
42   */
43  public class CanonicalModelClassFactory extends DefaultModelClassFactory
44  {
45  
46      private static final long serialVersionUID = -1795680089524220526L;
47      
48      private String myVersion;
49  
50  	private Class<? extends Message> myMessageClass;
51  
52      /**
53       * Constructor which selects the newest version of HAPI known to
54       */
55      public CanonicalModelClassFactory() {
56          myVersion = getHighestKnownVersion();
57      }
58  
59      /**
60       * Constructor
61       * 
62       * @param theVersion The version to always return (e.g. "2.6")
63       */
64      public CanonicalModelClassFactory(String theVersion) {
65      	if (theVersion == null || !Version.supportsVersion(theVersion)) {
66      		throw new IllegalArgumentException("Unknown version: " + theVersion);
67      	}
68          myVersion = theVersion;
69      }
70  
71      /**
72       * Constructor for a model class factory which always returns the same
73       * message type.
74       * 
75       * @see SuperStructure
76       */
77      public CanonicalModelClassFactory(Class<? extends Message> theClass) {
78      	if (theClass == null) {
79      		throw new NullPointerException("Class may not be null");
80      	}
81      	myMessageClass = theClass;
82      }
83      
84      
85  	/**
86       * {@inheritDoc}
87       */
88      @Override
89      public Class<? extends Group> getGroupClass(String theName, String theVersion) throws HL7Exception {
90          return super.getGroupClass(theName, myVersion);
91      }
92  
93      /**
94       * {@inheritDoc}
95       */
96      @Override
97      public Class<? extends Message> getMessageClass(String theName, String theVersion, boolean theIsExplicit) throws HL7Exception {
98      	if (myMessageClass != null) {
99      		return myMessageClass;
100     	}
101         return super.getMessageClass(theName, myVersion, theIsExplicit);
102     }
103 
104     /**
105      * {@inheritDoc}
106      */
107     @Override
108     public Class<? extends Segment> getSegmentClass(String theName, String theVersion) throws HL7Exception {
109         return super.getSegmentClass(theName, myVersion);
110     }
111 
112     /**
113      * {@inheritDoc}
114      */
115     @Override
116     public Class<? extends Type> getTypeClass(String theName, String theVersion) throws HL7Exception {
117         return super.getTypeClass(theName, myVersion);
118     }
119     
120 }