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 "AbstractPrimitive.java". Description:
010 * "Base class for Primitives. Performs validation in setValue()."
011 *
012 * The Initial Developer of the Original Code is University Health Network. Copyright (C)
013 * 2001-2005. 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 package ca.uhn.hl7v2.model;
029
030 import ca.uhn.hl7v2.HL7Exception;
031 import ca.uhn.hl7v2.parser.EncodingCharacters;
032 import ca.uhn.hl7v2.parser.Escape;
033 import ca.uhn.hl7v2.parser.Parser;
034 import ca.uhn.hl7v2.validation.PrimitiveTypeRule;
035 import ca.uhn.hl7v2.validation.ValidationContext;
036
037 /**
038 * Base class for Primitives. Performs validation in setValue().
039 *
040 * @author Bryan Tripp
041 */
042 @SuppressWarnings("serial")
043 public abstract class AbstractPrimitive extends AbstractType implements Primitive {
044
045 /**
046 * @param message message to which this type belongs
047 */
048 public AbstractPrimitive(Message message) {
049 super(message);
050 }
051
052 private String myValue;
053
054 /**
055 * Returns the value of getValue()
056 * @see java.lang.Object#toString
057 */
058 public String toString() {
059 return this.getValue();
060 }
061
062 /**
063 * @see ca.uhn.hl7v2.model.Primitive#getValue()
064 */
065 public String getValue() {
066 return myValue;
067 }
068
069 /**
070 * Sets the value of this Primitive, first performing validation as specified
071 * by <code>getMessage().getValidationContext()</code>. No validation is performed
072 * if getMessage() returns null.
073 *
074 * @see ca.uhn.hl7v2.model.Primitive#setValue(String)
075 */
076 public void setValue(String theValue) throws DataTypeException {
077 Message message = getMessage();
078
079 if (message != null) {
080 ValidationContext context = message.getValidationContext();
081 String version = message.getVersion();
082
083 if (context != null) {
084 PrimitiveTypeRule[] rules = context.getPrimitiveRules(version, getName(), this);
085
086 for (int i = 0; i < rules.length; i++) {
087 theValue = rules[i].correct(theValue);
088 if (!rules[i].test(theValue)) {
089 throw new DataTypeException("Failed validation rule for value \"" + theValue + "\": " + rules[i].getDescription());
090 }
091 }
092 }
093 }
094
095 myValue = theValue;
096 }
097
098
099 /**
100 * {@inheritDoc }
101 */
102 @Override
103 public String encode() throws HL7Exception {
104 Parser p = getMessage().getParser();
105 return p.doEncode(this, EncodingCharacters.getInstance(getMessage()));
106 }
107
108
109 /**
110 * {@inheritDoc }
111 */
112 @Override
113 public void parse(String string) throws HL7Exception {
114 if (string == null) {
115 clear();
116 return;
117 }
118
119 EncodingCharacters encodingCharacters = EncodingCharacters.getInstance(getMessage());
120 char subc = encodingCharacters.getSubcomponentSeparator();
121 char cmpc = encodingCharacters.getComponentSeparator();
122
123 clear();
124
125 // If the string contains subcomponent delimiters, parse
126 // these as extra components
127 int subcIndex = string.indexOf(subc);
128 int cmpcIndex = string.indexOf(cmpc);
129 if (subcIndex != -1 || cmpcIndex != -1) {
130
131 //Object ancestor = AbstractMessage.findAncestorOf(this);
132
133 int index;
134 char escapeChar;
135 if (cmpcIndex != -1) {
136 index = cmpcIndex;
137 escapeChar = cmpc;
138 } else {
139 index = subcIndex;
140 escapeChar = subc;
141 }
142
143 setValue(string.substring(0, index));
144 while (index != -1) {
145 int prevIndex = index + 1;
146 index = string.indexOf(escapeChar, prevIndex);
147 if (index != -1) {
148 String nextSubComponent = string.substring(prevIndex, index);
149 getExtraComponents().getComponent(getExtraComponents().numComponents()).parse(nextSubComponent);
150 } else {
151 String nextSubComponent = string.substring(prevIndex);
152 if (nextSubComponent.length() > 0) {
153 getExtraComponents().getComponent(getExtraComponents().numComponents()).parse(nextSubComponent);
154 }
155 }
156 }
157
158 } else {
159
160 String escaped = Escape.unescape(string, encodingCharacters);
161 setValue(escaped);
162
163 }
164 }
165
166
167 /**
168 * {@inheritDoc }
169 */
170 @Override
171 public void clear() {
172 super.clear();
173 myValue = null;
174 }
175
176 }