View Javadoc

1   package ca.uhn.hl7v2.examples;
2   
3   import ca.uhn.hl7v2.DefaultHapiContext;
4   import ca.uhn.hl7v2.HL7Exception;
5   import ca.uhn.hl7v2.HapiContext;
6   import ca.uhn.hl7v2.app.Connection;
7   import ca.uhn.hl7v2.app.ConnectionHub;
8   import ca.uhn.hl7v2.app.HL7Service;
9   import ca.uhn.hl7v2.llp.ExtendedMinLowerLayerProtocol;
10  import ca.uhn.hl7v2.llp.MinLLPReader;
11  import ca.uhn.hl7v2.llp.MinLowerLayerProtocol;
12  
13  public class HandlingCharacterEncodings {
14  
15  	@SuppressWarnings("unused")
16  	public static void main(String[] args) throws HL7Exception {
17  
18  		/*
19  		 * When you use ConnectionHub, SimpleServer, or other network related classes within HAPI to
20  		 * transmit and receive messages, you may need to consider character sets if you plan on
21  		 * dealing with messages that contain characters beyond basic ASCII.
22  		 */
23  
24  		/*
25  		 * Character sets are handled by the Lower Layer Protocol implementation, as shown in the
26  		 * two examples below. The most common implementation is MinLowerLayerProtocol.
27  		 * 
28  		 * By default, MinLowerLayerProtocol uses the "US-ASCII" character set, which is not likely
29  		 * what you are looking for if you want to process accents or characters that don't appear
30  		 * in the English alphabet.
31  		 */
32  		HapiContext ctx = new DefaultHapiContext();
33  				
34        /*
35         * Using MinLowerLayerProtocol, it is possible to set the use of a specific character set by
36         * specifying it to the MLLP. For example, if you wanted to receive Central and Eastern
37         * European characters, you probably need the ISO-8859-2 charset.
38         */
39  		MinLowerLayerProtocol mllp = new MinLowerLayerProtocol();
40  		mllp.setCharset("ISO-8859-2");
41  		ctx.setLowerLayerProtocol(mllp);
42  
43  	   /*
44  	    * It is also possible to set the charset globally using a system property. Setting the following
45         * system property before using MinLowerLayerProtocol tells the LLP that incoming messages
46         * should be received using this encoding. Note that you can not change this value after a
47         * connection is established. Also note that this setting does not override individual instances 
48         * where the charset has been explicitly set (such as the lines above)
49         */
50        System.setProperty(MinLLPReader.CHARSET_KEY, "ISO-8859-2");
51  
52        // Create an HL7 Server
53  		HL7Service s = ctx.newServer(123, false);
54  
55  		/*
56  		 * If you want to transmit using the same encoding all of the time, you may use default
57  		 * MinLowerLayerProtocol. In the example below, messages will be sent out using ISO-8859-2
58  		 */
59  		ConnectionHub hub = ctx.getConnectionHub();
60  		Connection c = hub.attach("localhost", 8888, false);
61  
62  		/*
63  		 * If the systems sending you messages are correctly populating MSH-18, as in the following
64  		 * message which specified "8859/1" (aka ISO-8859-1) encoding, you have another option.
65  		 * 
66  MSH|^~\&|4265-ADT|4265|eReferral|eReferral|201004141020||ADT^A45^ADT_A45|102416|T^|2.5^^|||NE|AL|CAN|8859/1
67  EVN|A45|201004141020|
68  PID|1||7010226^^^4265^MR~0000000000^^^CANON^JHN^^^^^^GP~1736465^^^4265^VN||Park^Green^^^MS.^^L||19890812|F|||123 TestingLane^^TORONTO^CA-ON^M5G2C2^CAN^H^~^^^^^^^||^PRN^PH^^1^416^2525252^|^^^^^^^||||||||||||||||N
69  PV1|1|I||||^^^WP^1469^^^^^^^^|||||||||||^Derkach^Peter.^^^Dr.||20913000131|||||||||||||||||||||||||201004011340|201004141018
70  		 * 
71  		 * The other possibility (only available in HAPI 2.0+) is to read the value from MSH-18 and
72  		 * use that to identify the character set. This can be advantageous if you want to be able
73  		 * to receive multiple CharSets. To do this, you need to use the
74  		 * ExtendedMinLowerLayerProtocol.
75  		 */
76  		ctx.setLowerLayerProtocol(new ExtendedMinLowerLayerProtocol());
77  		s = ctx.newServer(123, false);
78  
79  		/*
80  		 * Now the ExtendedMinLowerLayerProtocol is used to send out messages using the value you
81  		 * have placed in MSH-18.
82  		 */
83  		c = hub.attach("localhost", 8888, false);
84  
85  	}
86  
87  }