View Javadoc

1   /**
2   The contents of this file are subject to the Mozilla Public License Version 1.1 
3   (the "License"); you may not use this file except in compliance with the License. 
4   You may obtain a copy of the License at http://www.mozilla.org/MPL/ 
5   Software distributed under the License is distributed on an "AS IS" basis, 
6   WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for the 
7   specific language governing rights and limitations under the License. 
8   
9   The Original Code is "SegmentPointer.java".  Description: 
10  "A SegmentPointer is used when parsing traditionally encoded HL7 messages" 
11  
12  The Initial Developer of the Original Code is University Health Network. Copyright (C) 
13  2001.  All Rights Reserved. 
14  
15  Contributor(s): ______________________________________. 
16  
17  Alternatively, the contents of this file may be used under the terms of the 
18  GNU General Public License (the "GPL"), in which case the provisions of the GPL are 
19  applicable instead of those above.  If you wish to allow use of your version of this 
20  file only under the terms of the GPL and not to allow others to use your version 
21  of this file under the MPL, indicate your decision by deleting  the provisions above 
22  and replace  them with the notice and other provisions required by the GPL License.  
23  If you do not delete the provisions above, a recipient may use your version of 
24  this file under either the MPL or the GPL. 
25  
26  */
27  
28  /*
29   * Created on October 15, 2001, 3:19 PM
30   */
31  
32  package ca.uhn.hl7v2.parser;
33  
34  import org.slf4j.Logger;
35  import org.slf4j.LoggerFactory;
36  
37  import ca.uhn.hl7v2.HL7Exception;
38  import ca.uhn.hl7v2.model.Group;
39  import ca.uhn.hl7v2.model.Segment;
40  
41  /**
42   * A SegmentPointer is used when parsing traditionally encoded HL7 messages. 
43   * It acts as a placeholder for a unique segment "slot" in a message structure. 
44   * There is one SegmentPointer per unique segment path (even if the segment 
45   * repeats, and regardless of whether any instances exist).  
46   *
47   * @deprecated PipeParser now uses MessageIterator
48   * @author Bryan Tripp (bryan_tripp@sourceforge.net)
49   */
50  public class SegmentPointer extends Pointer {
51  
52      private static final Logger log = LoggerFactory.getLogger(SegmentPointer.class);
53      private PipeParser parser;
54      private Group parent;
55      private String name;
56      private EncodingCharacters encodingChars;
57      private boolean repeating;
58      private Segment currSegment = null;
59      private boolean currSegmentFull = true;
60  
61      /** 
62       * Creates new SegmentPointer 
63       * @param parser the PipeParser used to parse segments 
64       * @param parent the Group object that would be the parent of any instances of the 
65       *      Segment underlying this SegmentPointer
66       * @param position the position (among siblings; from 0) of the underlying Segment 
67       * @param encodingChars array of encoding characters, starting w/ field delim
68       */
69      public SegmentPointer(PipeParser parser, Group parent, int position, EncodingCharacters encodingChars)
70          throws HL7Exception {
71          this.parser = parser;
72          this.parent = parent;
73          this.encodingChars = encodingChars;
74          this.name = parent.getNames()[position];
75          this.repeating = parent.isRepeating(this.name);
76  
77      }
78  
79      /**
80       * Parses the given String, which must contain a single traditionally encoded 
81       * message segment, into the current repetition of the message Structure 
82       * underlying this Pointer.  See Pointer static fields for return values. 
83       * @throws HL7Exception if there is an error during parsing 
84       */
85      public int setSegment(String segment, boolean correctOrder) throws HL7Exception {
86          int status = Pointer.FILL_FAILED_WRONG_SEGMENT;
87  
88          //make sure segment is right kind
89          if (segment.substring(0, 3).equals(this.name)) {
90              if (correctOrder) {
91                  //make sure empty rep exists
92                  if (prepEmptyInstance()) {
93                      try {
94                          this.parser.parse(this.currSegment, segment, this.encodingChars);
95                      }
96                      catch (HL7Exception e) {
97                          //add segment name info and re-throw
98                          e.setSegmentName(this.name);
99                          throw e;
100                     }
101                     this.currSegmentFull = true;
102                     status = Pointer.FILL_OK;
103                 }
104                 else {
105                     status = Pointer.FILL_FAILED_FULL;
106                 }
107             }
108             else {
109                 status = Pointer.FILL_FAILED_OUT_OF_ORDER;
110             }
111         }
112         log.debug(
113             "Attempt to put segment " + segment.substring(0, 3) + " in " + this.name + ": code = " + status);
114         return status;
115     }
116 
117     /** 
118      * Ensures that there is an empty repetition of the underlying message Structure.  
119      * @returns true if successful, false if structure is non-repeating and full. 
120      */
121     private boolean prepEmptyInstance() throws HL7Exception {
122         boolean success = false;
123 
124         if (this.currSegment == null || (this.repeating && this.currSegmentFull)) {
125             int numExisting = parent.getAll(this.name).length;
126             this.currSegment = (Segment) parent.get(this.name, numExisting); //get next rep            
127             this.currSegmentFull = false;
128             success = true;
129         }
130         else if (!this.currSegmentFull) {
131             success = true;
132         }
133 
134         return success;
135     }
136 
137 }