Coverage Report - ca.uhn.hl7v2.model.AbstractPrimitive
 
Classes in this File Line Coverage Branch Coverage Complexity
AbstractPrimitive
94%
52/55
85%
24/28
2.875
 
 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 "AbstractPrimitive.java".  Description:
 10  
  * "Base class for Primitives.  Performs validation in setValue()."
 11  
  *
 12  
  * The Initial Developer of the Original Code is University Health Network. Copyright (C)
 13  
  * 2001-2005.  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.model;
 29  
 
 30  
 import java.util.Collection;
 31  
 
 32  
 import ca.uhn.hl7v2.HL7Exception;
 33  
 import ca.uhn.hl7v2.parser.EncodingCharacters;
 34  
 import ca.uhn.hl7v2.parser.Escape;
 35  
 import ca.uhn.hl7v2.parser.Parser;
 36  
 import ca.uhn.hl7v2.validation.PrimitiveTypeRule;
 37  
 import ca.uhn.hl7v2.validation.ValidationContext;
 38  
 import ca.uhn.hl7v2.validation.ValidationException;
 39  
 
 40  
 /**
 41  
  * Base class for Primitives.  Performs validation in setValue().
 42  
  *    
 43  
  * @author Bryan Tripp
 44  
  */
 45  
 @SuppressWarnings("serial")
 46  
 public abstract class AbstractPrimitive extends AbstractType implements Primitive {
 47  
     
 48  
     /**
 49  
      * @param message message to which this type belongs
 50  
      */
 51  
     public AbstractPrimitive(Message message) {
 52  103518
         super(message);
 53  103518
     }
 54  
 
 55  
     private String myValue;
 56  
     
 57  
     /** 
 58  
      * Returns the value of getValue() 
 59  
      * @see java.lang.Object#toString 
 60  
      */
 61  
     public String toString() {
 62  1
         return this.getValue();
 63  
     }
 64  
     
 65  
     /**
 66  
      * @see ca.uhn.hl7v2.model.Primitive#getValue()
 67  
      */
 68  
     public String getValue() {
 69  49286
         return myValue;
 70  
     }
 71  
 
 72  
     /**
 73  
      * Sets the value of this Primitive, first performing validation as specified 
 74  
      * by <code>getMessage().getValidationContext()</code>.  No validation is performed 
 75  
      * if getMessage() returns null. 
 76  
      * <p>
 77  
      * Note: as of the next HAPI release, the ValidationContext will be retrieved
 78  
      * from getParser().getValidationContext(), which ultimately is the ValidationContext
 79  
      * of the active HapiContext.
 80  
      * 
 81  
      * @see ca.uhn.hl7v2.model.Primitive#setValue(String)
 82  
      */    
 83  
     public void setValue(String theValue) throws DataTypeException {
 84  22986
         Message message = getMessage();
 85  
 
 86  22986
         if (message != null) {
 87  
                 // Note: this will change in future to reuse the Parser's/HapiContext's
 88  
                 // ValidationContext.
 89  22918
             ValidationContext context = message.getValidationContext();
 90  22918
             String version = message.getVersion();
 91  
 
 92  22918
             if (context != null) {
 93  16744
                 Collection<PrimitiveTypeRule> rules = context.getPrimitiveRules(version, getName(), this); 
 94  
         
 95  16744
                 for (PrimitiveTypeRule rule : rules) {
 96  15491
                     theValue = rule.correct(theValue);
 97  15491
                     ValidationException[] ve = rule.apply(theValue);
 98  15491
                     if (ve.length > 0) {
 99  111
                         throw new DataTypeException(ve[0]);
 100  
                     }
 101  15380
                 }
 102  
             }
 103  
         }
 104  
         
 105  22875
         myValue = theValue;
 106  22875
     }
 107  
 
 108  
     
 109  
     /**
 110  
      * {@inheritDoc }
 111  
      */
 112  
     @Override
 113  
     public String encode() throws HL7Exception {
 114  22
         Parser p = getMessage().getParser();
 115  22
         return p.doEncode(this, EncodingCharacters.getInstance(getMessage()));
 116  
     }
 117  
 
 118  
 
 119  
     /**
 120  
      * {@inheritDoc }
 121  
      */
 122  
     @Override
 123  
     public void parse(String string) throws HL7Exception {
 124  19
         if (string == null) {
 125  1
                 clear();
 126  1
                 return;
 127  
         }
 128  
             
 129  18
             EncodingCharacters encodingCharacters = EncodingCharacters.getInstance(getMessage());
 130  18
         char subc = encodingCharacters.getSubcomponentSeparator();
 131  18
         char cmpc = encodingCharacters.getComponentSeparator();
 132  
 
 133  18
         clear();
 134  
         
 135  
         // If the string contains subcomponent delimiters, parse
 136  
         // these as extra components
 137  18
         int subcIndex = string.indexOf(subc);
 138  18
         int cmpcIndex = string.indexOf(cmpc);
 139  18
         if (subcIndex != -1 || cmpcIndex != -1) {
 140  
             
 141  
             //Object ancestor = AbstractMessage.findAncestorOf(this);
 142  
             
 143  
             int index;
 144  
             char escapeChar;
 145  4
             if (cmpcIndex != -1) {
 146  3
                     index = cmpcIndex;
 147  3
                     escapeChar = cmpc;
 148  
             } else {
 149  1
                             index = subcIndex;
 150  1
                             escapeChar = subc;
 151  
             }
 152  
             
 153  4
                         setValue(string.substring(0, index));
 154  8
             while (index != -1) {
 155  4
                 int prevIndex = index + 1;
 156  4
                 index = string.indexOf(escapeChar, prevIndex);
 157  4
                 if (index != -1) {
 158  0
                     String nextSubComponent = string.substring(prevIndex, index);
 159  0
                     getExtraComponents().getComponent(getExtraComponents().numComponents()).parse(nextSubComponent);
 160  0
                 } else {
 161  4
                     String nextSubComponent = string.substring(prevIndex);
 162  4
                     if (nextSubComponent.length() > 0) {
 163  4
                             getExtraComponents().getComponent(getExtraComponents().numComponents()).parse(nextSubComponent);
 164  
                     }
 165  
                 }
 166  4
             }
 167  
             
 168  4
         } else {
 169  
         
 170  14
                 String escaped = Escape.unescape(string, encodingCharacters);
 171  14
             setValue(escaped);
 172  
         
 173  
         }
 174  18
     }
 175  
 
 176  
 
 177  
     /**
 178  
      * {@inheritDoc }
 179  
      */
 180  
     @Override
 181  
     public void clear() {
 182  126
         super.clear();
 183  126
         myValue = null;
 184  126
     }
 185  
 
 186  
         @Override
 187  
         public boolean isEmpty() {
 188  377
                 return (myValue == null || myValue.length() == 0) && super.isEmpty();
 189  
         }
 190  
 
 191  
     
 192  
 }