001    /**
002    The contents of this file are subject to the Mozilla Public License Version 1.1 
003    (the "License"); you may not use this file except in compliance with the License. 
004    You may obtain a copy of the License at http://www.mozilla.org/MPL/ 
005    Software distributed under the License is distributed on an "AS IS" basis, 
006    WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for the 
007    specific language governing rights and limitations under the License. 
008    
009    The Original Code is "MinLLPWriter.java".  Description: 
010    "Title:        MinLLPWriter
011      Description:  Writes HL7 messages to an OutputStream
012      Copyright:    Copyright (c) 2001
013      Company:      University Health Network
014      @author       Damian Horton
015      @version 1.1" 
016    
017    The Initial Developer of the Original Code is University Health Network. Copyright (C) 
018    2001.  All Rights Reserved. 
019    
020    Contributor(s): ______________________________________. 
021    
022    Alternatively, the contents of this file may be used under the terms of the 
023    GNU General Public License (the  ?GPL?), in which case the provisions of the GPL are 
024    applicable instead of those above.  If you wish to allow use of your version of this 
025    file only under the terms of the GPL and not to allow others to use your version 
026    of this file under the MPL, indicate your decision by deleting  the provisions above 
027    and replace  them with the notice and other provisions required by the GPL License.  
028    If you do not delete the provisions above, a recipient may use your version of 
029    this file under either the MPL or the GPL. 
030    
031    */
032    
033    package ca.uhn.hl7v2.llp;
034    
035    import java.io.*;
036    
037    /**
038     * Title:        MinLLPWriter
039     * Description:  Writes HL7 messages to an OutputStream.  The character set defaults to US-ASCII.  
040     * It can be chaged by setting the system property ca.uhn.hl7v2.llp.charset to another value that 
041     * is the name of a valid java.nio.charset.Charset.  If this property is set to "default", then 
042     * the system default is used. 
043     * 
044     * Copyright:    Copyright (c) 2001
045     * Company:      University Health Network
046     * @author       Damian Horton; mods by Bryan Tripp
047     * @version 1.1
048     */
049    
050    public class MinLLPWriter implements HL7Writer
051    {
052        public static final String CHARSET_KEY = "ca.uhn.hl7v2.llp.charset";
053        
054        BufferedWriter myWriter; //reads from the input stream given in the
055                                 //constructor
056        boolean messageStarted = false; //whether or not the necessary characters to
057                                //initialize the message have already been buffered
058    
059        private OutputStream myOutputStream;
060    
061        /**
062         * Creates a MinLLPWriter with no output stream specified - <code>setOutputStream</code>
063         * must be called before attempting to write any messages. 
064         */
065        public MinLLPWriter() {
066        }
067        
068        /** 
069         * Creates a MinLLPWriter, specifying the underlying output stream.
070         */
071        public MinLLPWriter(OutputStream out) throws IOException {
072            setOutputStream(out);
073        }
074        
075        /** 
076         * Sets the underlying output stream to which messages are written. 
077         */
078        public synchronized void setOutputStream(OutputStream out) throws IOException  
079        {
080            myOutputStream = out;
081            myWriter = new BufferedWriter(getWriter(out));
082        }
083    
084        /** 
085         * Sends a complete message to the underlying output stream, delimited 
086         * according to the minimal lower layer protocol.  
087         */
088        public synchronized void writeMessage(String message) throws LLPException, IOException 
089        {
090            myWriter.write('\u000b');
091            myWriter.write(message);
092            myWriter.write('\u001c' + "\r");
093            myWriter.flush();            
094        }
095    
096        /** 
097         * Sends a complete message to the underlying output stream, delimited 
098         * according to the minimal lower layer protocol, using the specified character set. 
099         */
100        public synchronized void writeMessage(String message, String charset) throws LLPException, IOException 
101        {
102            BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(myOutputStream, charset));
103            writer.write('\u000b');
104            writer.write(message);
105            writer.write('\u001c' + "\r");
106            writer.flush();
107        }
108    
109        public synchronized void close() throws java.io.IOException
110        {
111            myWriter.close();
112        }
113        
114        private static OutputStreamWriter getWriter(OutputStream theStream) throws IOException {
115            String charset = System.getProperty(CHARSET_KEY, "US-ASCII");
116            
117            if (charset.equals("default")) {
118                return new OutputStreamWriter(theStream);
119            } else {
120                return new OutputStreamWriter(theStream, charset);
121            }
122        }
123        
124    }