View Javadoc

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 ""  Description:
10   * ""
11   *
12   * The Initial Developer of the Original Code is University Health Network. Copyright (C)
13   * 2001.  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  package ca.uhn.hl7v2.testpanel.util;
27  
28  import java.io.IOException;
29  import java.io.StringReader;
30  
31  import jsyntaxpane.Token;
32  
33  import org.slf4j.Logger;
34  import org.slf4j.LoggerFactory;
35  
36  import testpanel.Er7Lexer;
37  
38  public class FormatUtil {
39  
40  	private static final Logger ourLog = LoggerFactory.getLogger(FormatUtil.class);
41  	
42  	public static String formatEr7(String theEr7, boolean theIsType) {
43  		StringBuilder b = new StringBuilder();
44  		b.append("<html>");
45  
46  		Er7Lexer lexer = new Er7Lexer(new StringReader(theEr7));
47  
48  		if (theIsType) {
49  			lexer.yybegin(Er7Lexer.AT_START_OF_VALUE);
50  		}
51  		
52  		Token next;
53  		do {
54  			try {
55  				next = lexer.yylex();
56  			} catch (IOException e) {
57  				ourLog.error("Failed to parse", e);
58  				break;
59  			}
60  
61  			if (next != null) {
62  				switch (next.type) {
63  				case KEYWORD:
64  				case KEYWORD2:
65  					b.append("<span style=\"color: #3333EE;\">");
66  					b.append(theEr7, next.start, next.end());
67  					b.append("</span>");
68  					break;
69  				case TYPE:
70  					b.append("<span style=\"color: #A0A0A0;\">");
71  					b.append(theEr7, next.start, next.end());
72  					b.append("</span>");
73  					break;
74  				case TYPE2:
75  					b.append("<span style=\"color: #990033;\">");
76  					b.append(theEr7, next.start, next.end());
77  					b.append("</span>");
78  					break;
79  				case TYPE3:
80  					b.append("<span style=\"color: #00A000;\">");
81  					b.append(theEr7, next.start, next.end());
82  					b.append("</span>");
83  					break;
84  				default:
85  					b.append(theEr7, next.start, next.end());
86  					break;
87  				}
88  			}
89  
90  		} while (next != null);
91  
92  		b.append("</html>");
93  		String html = b.toString().replace("\r", "<br>");
94  		
95  		return html;
96  	}
97  	
98  }