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    
034    /**
035     * An abstract Type that provides a default implementation of getName(). 
036     * 
037     * @author Bryan Tripp
038     */
039    public abstract class AbstractType implements Type {
040    
041            private static final long serialVersionUID = -6976260024197429201L;
042            
043            private final ExtraComponents extra;
044        private final 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                    extra.clear();
097            }
098    
099            
100            /**
101             * Returns the datatype and attempts to pipe-encode it. For example, a string implementation
102             * might return "ST[Value^Value2]". This is only intended for logging/debugging purposes.
103             */
104            @Override
105            public String toString() {
106                    return toString(this);
107            }
108    
109            
110            /**
111             * Returns the datatype and attempts to pipe-encode it. For example, a string implementation
112             * might return "ST[Value^Value2]". This is only intended for logging/debugging purposes.
113             */
114            static String toString(Type theType) {
115                    StringBuilder b = new StringBuilder();
116                    b.append(theType.getClass().getSimpleName());
117                    b.append("[");
118                    try {
119                            b.append(theType.encode());
120                    } catch (HL7Exception e) {
121                            b.append("Unable to encode");
122                    }
123                    b.append("]");
124                    return b.toString();
125            }
126    
127    
128            
129    
130    }