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 "AbstractType.java". Description: 010 * 011 * "An abstract Type that provides a default implementation of getName()" 012 * 013 * The Initial Developer of the Original Code is University Health Network. Copyright (C) 014 * 2001. All Rights Reserved. 015 * 016 * Contributor(s): ______________________________________. 017 * 018 * Alternatively, the contents of this file may be used under the terms of the 019 * GNU General Public License (the ???GPL???), in which case the provisions of the GPL are 020 * applicable instead of those above. If you wish to allow use of your version of this 021 * file only under the terms of the GPL and not to allow others to use your version 022 * of this file under the MPL, indicate your decision by deleting the provisions above 023 * and replace them with the notice and other provisions required by the GPL License. 024 * If you do not delete the provisions above, a recipient may use your version of 025 * this file under either the MPL or the GPL. 026 * 027 */ 028 029 package ca.uhn.hl7v2.model; 030 031 import ca.uhn.hl7v2.HL7Exception; 032 import ca.uhn.hl7v2.parser.EncodingCharacters; 033 import java.util.logging.Level; 034 import java.util.logging.Logger; 035 036 /** 037 * An abstract Type that provides a default implementation of getName(). 038 * 039 * @author Bryan Tripp 040 */ 041 public abstract class AbstractType implements Type { 042 043 private ExtraComponents extra; 044 private Message message; 045 046 /** 047 * Creates a new instance of AbstractType 048 * @param message message to which this type belongs 049 */ 050 public AbstractType(Message message) { 051 extra = new ExtraComponents(message); 052 this.message = message; 053 } 054 055 /** Returns the name of the type (used in XML encoding and profile checking) */ 056 public String getName() { 057 String longClassName = this.getClass().getName(); 058 return longClassName.substring(longClassName.lastIndexOf('.') + 1); 059 } 060 061 /** @see Type#getExtraComponents */ 062 public ExtraComponents getExtraComponents() { 063 return this.extra; 064 } 065 066 067 /** 068 * @return the message to which this Type belongs 069 */ 070 public Message getMessage() { 071 return message; 072 } 073 074 075 /** 076 * {@inheritDoc } 077 */ 078 public void parse(String string) throws HL7Exception { 079 clear(); 080 getMessage().getParser().parse(this, string, EncodingCharacters.getInstance(getMessage())); 081 } 082 083 084 /** 085 * {@inheritDoc } 086 */ 087 public String encode() throws HL7Exception { 088 return getMessage().getParser().doEncode(this, EncodingCharacters.getInstance(getMessage())); 089 } 090 091 092 /** 093 * {@inheritDoc } 094 */ 095 public void clear() { 096 if (this instanceof Composite) { 097 098 Composite composite = (Composite) this; 099 for (Type nextComponent : composite.getComponents()) { 100 nextComponent.clear(); 101 } 102 103 } else if (!(this instanceof AbstractPrimitive) && (this instanceof Primitive)) { 104 105 // Most primitives don't hit this block because they extend AbstractPrimitive, which 106 // has an implementation of clear which clears the value without invoking validation 107 108 Primitive primitive = (Primitive) this; 109 try { 110 primitive.setValue(""); 111 } catch (DataTypeException ex) { 112 throw new Error("Unable to clear the value of a primitive which does not extend AbstractPrimitive. This is likely a software bug."); 113 } 114 } 115 116 extra.clear(); 117 } 118 119 120 121 122 }