Coverage Report - ca.uhn.hl7v2.parser.GroupPointer
 
Classes in this File Line Coverage Branch Coverage Complexity
GroupPointer
0%
0/48
0%
0/34
5.25
 
 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 "GroupPointer.java".  Description: 
 10  
 "A GroupPointer 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  
 package ca.uhn.hl7v2.parser;
 29  
 
 30  
 import ca.uhn.hl7v2.HL7Exception;
 31  
 import ca.uhn.hl7v2.model.Group;
 32  
 
 33  
 /**
 34  
  * A GroupPointer is used when parsing traditionally encoded HL7 messages. 
 35  
  * It acts as a placeholder for a unique group "slot" in a message structure. 
 36  
  * There is one GroupPointer per unique Group path (even if the group 
 37  
  * repeats, and regardless of whether any instances exist).  
 38  
  *
 39  
  * @deprecated MessageIterator is now used 
 40  
  * @author Bryan Tripp (bryan_tripp@sourceforge.net)
 41  
  */
 42  
 public class GroupPointer extends Pointer {
 43  
 
 44  
     private PipeParser parser;
 45  
     private Group parent;
 46  
     private String name;
 47  
     private boolean repeating;
 48  
     private EncodingCharacters encodingChars;
 49  
     private Pointer[] children;
 50  0
     private Group currentGroup = null;
 51  
     private int childAtWhichToStart;
 52  
 
 53  
     /** 
 54  
      * Creates new GroupPointer 
 55  
      */
 56  
     public GroupPointer(PipeParser parser, Group parent, int position, EncodingCharacters encodingChars)
 57  0
         throws HL7Exception {
 58  0
         this.parser = parser;
 59  0
         this.parent = parent;
 60  0
         this.name = parent.getNames()[position];
 61  0
         this.repeating = parent.isRepeating(this.name);
 62  0
         this.encodingChars = encodingChars;
 63  
         //this.createNewInstance();  
 64  0
     }
 65  
 
 66  
     /**
 67  
      * Parses the given String, which must contain a single traditionally encoded 
 68  
      * message segment, into the current repetition of the message Group  
 69  
      * underlying this Pointer by forwarding it to each of it's children until 
 70  
      * one of them succeeds.    
 71  
      */
 72  
     public int setSegment(String segment, boolean orderedCorrectly) throws HL7Exception {
 73  0
         if (this.currentGroup == null)
 74  0
             createNewInstance(); //make first instance of the group
 75  0
         int status = tryToFillChildren(segment, orderedCorrectly);
 76  0
         if ((status == Pointer.FILL_FAILED_FULL || status == Pointer.FILL_FAILED_OUT_OF_ORDER)
 77  
             && this.repeating
 78  
             && orderedCorrectly) {
 79  0
             createNewInstance();
 80  0
             status = tryToFillChildren(segment, orderedCorrectly);
 81  
         }
 82  0
         return status;
 83  
     }
 84  
 
 85  
     private int tryToFillChildren(String segment, boolean orderedCorrectlyInParent) throws HL7Exception {
 86  0
         int status = Pointer.FILL_FAILED_WRONG_SEGMENT;
 87  0
         int c = 0;
 88  0
         boolean fullOneExists = false;
 89  0
         boolean rightSegmentWrongOrder = false;
 90  
 
 91  
         //loop through children ... 
 92  
         //note: can't just stop if FILL_FAILED_FULL b/c have to check all nesting levels 
 93  0
         while (status != Pointer.FILL_OK && c < this.children.length) {
 94  0
             boolean orderedCorrectly = orderedCorrectlyInParent;
 95  0
             if (c < childAtWhichToStart)
 96  0
                 orderedCorrectly = false;
 97  
 
 98  0
             status = children[c++].setSegment(segment, orderedCorrectly);
 99  0
             if (status == Pointer.FILL_FAILED_FULL)
 100  0
                 fullOneExists = true;
 101  0
             if (status == Pointer.FILL_FAILED_OUT_OF_ORDER)
 102  0
                 rightSegmentWrongOrder = true;
 103  0
         }
 104  
 
 105  
         //note: but if there was a FILL_FAILED_FULL at any point, can't return FILL_FAILED_WRONG_SEGMENT
 106  0
         if (fullOneExists && status == Pointer.FILL_FAILED_WRONG_SEGMENT)
 107  0
             status = Pointer.FILL_FAILED_FULL;
 108  0
         if (rightSegmentWrongOrder && status == Pointer.FILL_FAILED_WRONG_SEGMENT)
 109  0
             status = Pointer.FILL_FAILED_OUT_OF_ORDER;
 110  
 
 111  
         //if filled, update start location so that subsequent segments can't be 
 112  
         //parsed into locations prior to this one 
 113  0
         if (status == Pointer.FILL_OK)
 114  0
             childAtWhichToStart = c - 1;
 115  
 
 116  0
         return status;
 117  
     }
 118  
 
 119  
     /** 
 120  
      * Creates a new, empty repetition of the underlying Group and sets up 
 121  
      * Pointers to it's components.  
 122  
      * @thows HL7Exception if there is already 1 rep and group is non-repeating
 123  
      */
 124  
     private void createNewInstance() throws HL7Exception {
 125  
         //find next rep # ... 
 126  0
         int nextRepNum = parent.getAll(this.name).length;
 127  
 
 128  0
         this.currentGroup = (Group) parent.get(this.name, nextRepNum);
 129  0
         this.childAtWhichToStart = 0;
 130  
 
 131  
         //set up pointers for children ... 
 132  0
         String[] childNames = currentGroup.getNames();
 133  0
         this.children = new Pointer[childNames.length];
 134  0
         for (int i = 0; i < childNames.length; i++) {
 135  
             Pointer p;
 136  
             //make new SegmentPointer or GroupPointer depending on whether child is a Group ... 
 137  0
             Class<?> childClass = currentGroup.getClass(childNames[i]);
 138  0
             if (Group.class.isAssignableFrom(childClass)) {
 139  0
                 p = new GroupPointer(this.parser, currentGroup, i, this.encodingChars);
 140  
             }
 141  
             else {
 142  0
                 p = new SegmentPointer(this.parser, currentGroup, i, encodingChars);
 143  
             }
 144  
 
 145  0
             this.children[i] = p;
 146  
         }
 147  
 
 148  0
     }
 149  
 
 150  
 }