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 "MessagePointer.java". Description:
010 "A MessagePointer is used when parsing traditionally encoded HL7 messages"
011
012 The Initial Developer of the Original Code is University Health Network. Copyright (C)
013 2001. All Rights Reserved.
014
015 Contributor(s): ______________________________________.
016
017 Alternatively, the contents of this file may be used under the terms of the
018 GNU General Public License (the "GPL"), in which case the provisions of the GPL are
019 applicable instead of those above. If you wish to allow use of your version of this
020 file only under the terms of the GPL and not to allow others to use your version
021 of this file under the MPL, indicate your decision by deleting the provisions above
022 and replace them with the notice and other provisions required by the GPL License.
023 If you do not delete the provisions above, a recipient may use your version of
024 this file under either the MPL or the GPL.
025
026 */
027
028 /*
029 * Created on October 15, 2001, 4:17 PM
030 */
031
032 package ca.uhn.hl7v2.parser;
033
034 import ca.uhn.hl7v2.HL7Exception;
035 import ca.uhn.hl7v2.model.Group;
036 import ca.uhn.hl7v2.model.Message;
037
038 /**
039 * A MessagePointer is used when parsing traditionally encoded HL7 messages.
040 * This is similar to a GroupPointer except that there is only one underlying
041 * instance of the Message, and it has no parent.
042 *
043 * @deprecated PipeParser now uses MessageIterator
044 * @see GroupPointer
045 * @author Bryan Tripp (bryan_tripp@sourceforge.net)
046 */
047 public class MessagePointer {
048
049 private PipeParser parser;
050 private EncodingCharacters encodingChars;
051 private Pointer[] children;
052 private Message message;
053 private int childAtWhichToStart = 0;
054
055 /**
056 * Creates new GroupPointer
057 */
058 public MessagePointer(PipeParser parser, Message message, EncodingCharacters encodingChars) throws HL7Exception {
059 this.parser = parser;
060 this.message = message;
061 this.encodingChars = encodingChars;
062 makeChildren();
063 }
064
065 /**
066 * Parses the given String, which must contain a single traditionally encoded
067 * message segment, by forwarding it to child GroupPointers and SegmentPointers
068 * until one of them succeeds.
069 */
070 public int setSegment(String segment) throws HL7Exception {
071 int status = tryToFillChildren(segment);
072 return status;
073 }
074
075 private int tryToFillChildren(String segment) throws HL7Exception {
076 int status = Pointer.FILL_FAILED_WRONG_SEGMENT;
077 int c = 0;
078 //loop through children ...
079 //note: can't just stop if FILL_FAILED_FULL b/c have to check all nesting levels
080 while (status != Pointer.FILL_OK && c < this.children.length) {
081 boolean correctOrder = (c >= childAtWhichToStart ? true : false);
082 status = children[c++].setSegment(segment, correctOrder);
083 }
084 if (status == Pointer.FILL_OK)
085 childAtWhichToStart = c - 1;
086 return status;
087 }
088
089 /**
090 * Creates a new, empty repetition of the underlying Group and sets up
091 * Pointers to it's components.
092 * @thows HL7Exception if there is already 1 rep and group is non-repeating
093 */
094 private void makeChildren() throws HL7Exception {
095
096 //set up pointers for children ...
097 String[] childNames = message.getNames();
098 this.children = new Pointer[childNames.length];
099 for (int i = 0; i < childNames.length; i++) {
100 Pointer p;
101 //make new SegmentPointer or GroupPointer depending on whether child is a Group ...
102 Class childClass = message.getClass(childNames[i]);
103 if (Group.class.isAssignableFrom(childClass)) {
104 p = new GroupPointer(this.parser, message, i, this.encodingChars);
105 }
106 else {
107 p = new SegmentPointer(this.parser, message, i, encodingChars);
108 }
109
110 this.children[i] = p;
111 }
112
113 }
114
115 }