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 "". Description: 010 * "" 011 * 012 * The Initial Developer of the Original Code is University Health Network. Copyright (C) 013 * 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 027package ca.uhn.hl7v2.model.primitive; 028 029import ca.uhn.hl7v2.model.AbstractPrimitive; 030import ca.uhn.hl7v2.model.Message; 031 032/** 033 * Base class for a textual primitive datatypes such as FT, TX, ST. 034 * 035 * @author James Agnew 036 */ 037@SuppressWarnings("serial") 038public abstract class AbstractTextPrimitive extends AbstractPrimitive { 039 040 /** 041 * Constructor 042 */ 043 public AbstractTextPrimitive(Message theMessage) { 044 super(theMessage); 045 } 046 047 /** 048 * <p> 049 * Returns the value of this type with HL7 defined formatting codes replaced 050 * by their HTML equivalent. 051 * </p> 052 * <p> 053 * For example, if this type contained the string:<br> 054 * 055 * <pre> 056 * ABC \.ce\MIDDLE\.br\ 057 * </pre> 058 * 059 * <br> 060 * This would be returned as:<br> 061 * 062 * <pre> 063 * ABC <center>MIDDLE<center><br> 064 * </pre> 065 * 066 * </p> 067 * <p> 068 * The following codes are handled (note that contrary to the HL7 069 * specification, codes are interpreted in a case-insensitive manner): 070 * <ul> 071 * <li><b>\.br\</b> (converted to <br>) 072 * <li><b>\.ce\</b> (converted to <center>) 073 * <li><b>\.sk\</b> (converted to &nbsp;) 074 * <li><b>\.sp\</b> (converted to <br> and then multiple &nbsp;) 075 * <li><b>\.sp ###\</b> (converted to multiple <br> and then multiple 076 * &nbsp;) 077 * <li><b>\.fi\</b> (cancels \.nf\) 078 * <li><b>\.nf\</b> (converted to <nobr> ... </nobr> around each line) 079 * <li><b>\.in ###\</b> (converted to <div style="margin-left: ###em;"> ... 080 * </div> around each line) 081 * <li><b>\.ti ###\</b> (treated the same as \.in ###\ but only affects the current 082 * line, and the numeric value is added to the value provided by \.in ###\. 083 * See section 2.7 of the HL7 specification for more details.) 084 * <li><b>\H\</b> (converted to <b>) 085 * <li><b>\N\</b> (converted to </b>) 086 * <li><b>Ampersands (&)</b> are converted to their HTML equivalent (&amp;) 087 * <li><b>Chars over ASCII 160</b> are HTML encoded (e.g. &#199;) 088 * </ul> 089 * </p> 090 * <p> 091 * Note that the returned value from this method is an HTML snippet, not a 092 * complete HTML document. 093 * </p> 094 * 095 * @see FormattedTextEncoder 096 */ 097 public String getValueAsHtml() { 098 return FormattedTextEncoder.getInstanceHtml().encode(getValue()); 099 } 100 101 /** 102 * Returns the value of the datatype as returned by {@link #getValue()} but 103 * returns an empty string ("") if the value is <code>null</code>. This 104 * method is mostly provided as a convenience in code which maps from one 105 * format to another, since it avoids the need for some null checks. 106 */ 107 public String getValueOrEmpty() { 108 String retVal = getValue(); 109 if (retVal == null) { 110 retVal = ""; 111 } 112 return retVal; 113 } 114 115}