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