Hi Ryan,

> It appears as if there is no batch support in hapi. Or am I missing this?
> If this is the case, what are people commonly doing to handle batch files?

> You mean if there's more than one message in a file?
> I have a ReadMessage() function that pulls one message at a time out of =
> the file and returns an array of strings. I just read segments until I =
> reach the end of the file or I hit an MSH header.
> Tony

I do use the HL7Reader.getMessage method in a standard way. This HL7Reader interface is provided by HAPI with only one implementation (at least in my version): MinLLPReader, to be used with IP sockets. Based on this, I've added two others: - NullLLPReader: to be used when no LLP is needed for example with files or JMS messages; - BatchLLPReader: to be used with batch files, where messages are separated by a linefeed (\n) character.
Of course, there is similar classes for writers (see attached).

I hope this could help.
Fred.

/**
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 Original Code is "NullLowerLayerProtocol.java".  Description:
"A dummy LowerLayerProtocol implementation"

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.llp;

import java.io.*;

/**
 * A dummy LowerLayerProtocol implementation, to be used when no LLP is needed,
 * for example with files or JMS messages.
 *
 * @author Frédéric Dubru
 * @version $Id: NullLowerLayerProtocol.java,v 1.2 2006/09/13 09:35:06 df2673 Exp $
 */
public class NullLowerLayerProtocol
    extends LowerLayerProtocol {
    /**
     * Creates new NullLowerLayerProtocol
     */
    public NullLowerLayerProtocol() {
    }

    /**
     * Creates an HL7Reader that implements message reading according to
     * this protocol.
     */
    public HL7Reader getReader(InputStream in) throws LLPException {
        try {
            return new NullLLPReader(in);
        } catch (IOException e) {
            throw new LLPException("Can't create NullLLPReader with the given input stream: " +
                e.getMessage(), e);
        }
    }

    /**
     * Creates an HL7Writer that implements message writing according to
     * this protocol.
     */
    public HL7Writer getWriter(OutputStream out) throws LLPException {
        try {
            return new NullLLPWriter(out);
        } catch (IOException e) {
            throw new LLPException("Can't create NullLLPWriter with the given output stream: " +
                e.getMessage(), e);
        }
    }
}
/**
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 Original Code is "BatchLLPWriter.java".  Description:
"A simple HL7Writer implementation for batches"

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.llp;

import java.io.*;

/**
 * A simple HL7Writer implementation for batches. Messages are separated by a linefeed (\n) character.
 *
 * @author Frédéric Dubru
 * @version $Id$
 */
public class BatchLLPWriter extends NullLLPWriter {
  
    public BatchLLPWriter() {
        super();
    }

    public BatchLLPWriter(OutputStream out) throws IOException {
        super(out);
    }

    /**
     * Sends a complete message to the underlying output stream.
     */
    public synchronized void writeMessage(String message) throws LLPException, IOException {
        myWriter.write(message);
        myWriter.write(BatchLowerLayerProtocol.MESSAGE_SEPARATOR);
        myWriter.flush();
    }
}
/**
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 Original Code is "NullLowerLayerProtocol.java".  Description:
"A simple LowerLayerProtocol implementation for batches"

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.llp;

import java.io.*;

/**
 * A simple LowerLayerProtocol implementation for batches. Messages are separated by a linefeed (\n) character.
 *
 * @author Frédéric Dubru
 * @version $Id$
 */
public class BatchLowerLayerProtocol
    extends LowerLayerProtocol {
    static final char MESSAGE_SEPARATOR = '\n'; 
        
    /**
     * Creates new MinLowerLayerProtocol
     */
    public BatchLowerLayerProtocol() {
    }

    /**
     * Creates an HL7Reader that implements message reading according to
     * this protocol.
     */
    public HL7Reader getReader(InputStream in) throws LLPException {
        try {
            return new BatchLLPReader(in);
        } catch (IOException e) {
            throw new LLPException("Can't create BatchLLPReader with the given input stream: " +
                e.getMessage(), e);
        }
    }

    /**
     * Creates an HL7Writer that implements message writing according to
     * this protocol.
     */
    public HL7Writer getWriter(OutputStream out) throws LLPException {
        try {
            return new BatchLLPWriter(out);
        } catch (IOException e) {
            throw new LLPException("Can't create BatchLLPWriter with the given output stream: " +
                e.getMessage(), e);
        }
    }
}
/**
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 Original Code is "NullLLPReader.java".  Description:
"A dummy HL7Reader implementation"

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.llp;

import java.io.*;

/**
 * A dummy HL7Reader implementation, to be used when no LLP is needed,
 * for example with files or JMS messages.
 *
 * @author Frédéric Dubru
 * @version $Id: NullLLPReader.java,v 1.3 2006/09/13 09:33:37 df2673 Exp $
 */
public class NullLLPReader implements HL7Reader {
    protected BufferedReader myReader;

    /**
     * Creates a NullLLPReader with no setup.
     */
    public NullLLPReader() {
    }

    /**
     * Creates a NullLLPReader which reads from the given InputStream. The stream
     * is assumed to be an ASCII bit stream.
     */
    public NullLLPReader(InputStream in) throws IOException {
        setInputStream(in);
    }

    /**
     * Sets the InputStream from which to read messages.  The InputStream must be set
     * before any calls to <code>getMessage()</code>.
     */
    public synchronized void setInputStream(InputStream in) throws IOException {
        myReader = new BufferedReader(new InputStreamReader(in, "ASCII"));
    }

    /**
     * Reads an HL7 encoded message from this Reader's InputStream.
     * @return The message, in string format, without the lower level
     * protocol delimeters. Returns null if a -1 is received on the initial
     * read.
     */
    public synchronized String getMessage() throws LLPException, IOException {
        StringBuffer s_buffer = new StringBuffer();

        boolean end_of_message = false;

        int c = myReader.read();

        //trying to read when there is no data
        if (c == -1) {
            throw new IOException("read() from InputStream returns -1");
        }
        LowerLayerProtocol.logCharacterReceived(c);

        while (!end_of_message) {
            if (c == -1) {
                end_of_message = true;
            } else {
                LowerLayerProtocol.logCharacterReceived(c);
                //the character wasn't the end of message, append it to the message
                s_buffer.append((char) c);
                c = myReader.read();
            }
        } //end while

        return s_buffer.toString();
    }

    /**
     * Closes the underlying BufferedReader.
     */
    public synchronized void close() throws java.io.IOException {
        myReader.close();
    }
}
/**
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 Original Code is "NullLLPWriter.java".  Description:
"A dummy HL7Writer implementation"

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.llp;

import java.io.*;

/**
 * A dummy HL7Writer implementation, to be used when no LLP is needed,
 * for example with files or JMS messages.
 *
 * @author Frédéric Dubru
 * @version $Id: NullLLPWriter.java,v 1.2 2006/09/13 09:34:32 df2673 Exp $
 */
public class NullLLPWriter implements HL7Writer {
    protected BufferedWriter myWriter;

    /**
     * Creates a NullLLPWriter with no output stream or writer specified.
     */
    public NullLLPWriter() {
    }

    /**
     * Creates a NullLLPWriter, specifying the underlying output stream.
     */
    public NullLLPWriter(OutputStream out) throws IOException {
        setOutputStream(out);
    }

    /**
     * Sets the underlying output stream to which messages are written.
     */
    public synchronized void setOutputStream(OutputStream out) throws IOException {
        myWriter = new BufferedWriter(new OutputStreamWriter(out, "ASCII"));
    }

    /**
     * Sends a complete message to the underlying output stream.
     */
    public synchronized void writeMessage(String message) throws LLPException, IOException {
        myWriter.write(message);
        myWriter.flush();
    }

    public synchronized void close() throws java.io.IOException {
        myWriter.close();
    }
}
/**
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 Original Code is "BatchLLPReader.java".  Description:
"A simple HL7Reader implementation for batches"

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.llp;

import java.io.*;

/**
 * A simple HL7Reader implementation for batches. Messages are separated by a linefeed (\n) character.
 *
 * @author Frédéric Dubru
 * @version $Id$
 */
public class BatchLLPReader extends NullLLPReader {

    public BatchLLPReader() {
        super();
    }

    public BatchLLPReader(InputStream in) throws IOException {
        super(in);
    }

    /**
     * Reads an HL7 encoded message from this Reader's InputStream.
     * @return The message, in string format, without the lower level
     * protocol delimeters. Returns null if a -1 is received on the initial
     * read.
     */
    public synchronized String getMessage() throws LLPException, IOException {
        StringBuffer s_buffer = new StringBuffer();

        boolean end_of_message = false;

        int c = myReader.read();

        //trying to read when there is no data
        if (c == -1) {
            throw new IOException("read() from InputStream returns -1");
        }
        LowerLayerProtocol.logCharacterReceived(c);

        while (!end_of_message) {
            if (c == -1 || c == BatchLowerLayerProtocol.MESSAGE_SEPARATOR) {
                end_of_message = true;
            } else {
                LowerLayerProtocol.logCharacterReceived(c);
                //the character wasn't the end of message, append it to the message
                s_buffer.append((char) c);
                c = myReader.read();
            }
        } //end while

        return s_buffer.toString();
    }
}
-------------------------------------------------------------------------
SF.Net email is sponsored by: The Future of Linux Business White Paper
from Novell.  From the desktop to the data center, Linux is going
mainstream.  Let it simplify your IT future.
http://altfarm.mediaplex.com/ad/ck/8857-50307-18918-4
_______________________________________________
Hl7api-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/hl7api-devel

Reply via email to