Merge sources from S-Core's RSA git (release)
[sdk/ide/common-eplugin.git] / org.tizen.common / src / org / tizen / common / ui / view / console / AnsicodeAdapter.java
1 /*
2 *  Common
3 *
4 * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
5 *
6 * Contact: 
7 * Yoonki Park <yoonki.park@samsung.com>
8 * Kangho Kim <kh5325.kim@samsung.com>
9
10 * Licensed under the Apache License, Version 2.0 (the "License");
11 * you may not use this file except in compliance with the License.
12 * You may obtain a copy of the License at
13 *
14 * http://www.apache.org/licenses/LICENSE-2.0
15 *
16 * Unless required by applicable law or agreed to in writing, software
17 * distributed under the License is distributed on an "AS IS" BASIS,
18 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19 * See the License for the specific language governing permissions and
20 * limitations under the License.
21 *
22 * Contributors:
23 * - S-Core Co., Ltd
24 *
25 */
26 package org.tizen.common.ui.view.console;
27
28 import static org.tizen.common.util.SWTUtil.getDisplay;
29
30 import java.util.ArrayList;
31 import java.util.Collections;
32 import java.util.List;
33 import java.util.Map;
34
35 import org.eclipse.swt.graphics.Color;
36 import org.tizen.common.util.MapUtil;
37
38 public class AnsicodeAdapter {
39         public static final char ESCAPE = '\033'; // ANSI Escape Character that starts commands
40         
41         public static final Color BLACK = new Color(getDisplay(), 0, 0, 0);                             //Display.getCurrent().getSystemColor(SWT.COLOR_BLACK);
42         public static final Color RED = new Color(getDisplay(), 255, 0, 0);                             //Display.getCurrent().getSystemColor(SWT.COLOR_RED);
43         public static final Color GREEN = new Color(getDisplay(), 0, 255, 0);                   //Display.getCurrent().getSystemColor(SWT.COLOR_GREEN);
44         public static final Color YELLOW = new Color(getDisplay(), 255, 255, 0);                //Display.getCurrent().getSystemColor(SWT.COLOR_YELLOW);
45         public static final Color BLUE = new Color(getDisplay(), 0,0,255);                              //Display.getCurrent().getSystemColor(SWT.COLOR_BLUE);
46         public static final Color MAGENTA = new Color(getDisplay(), 255, 0, 255);       //Display.getCurrent().getSystemColor(SWT.COLOR_MAGENTA);
47         public static final Color CYAN = new Color(getDisplay(), 0,255, 255);                   //Display.getCurrent().getSystemColor(SWT.COLOR_CYAN);
48         public static final Color WHITE = new Color(getDisplay(), 255, 255, 255);       //Display.getCurrent().getSystemColor(SWT.COLOR_WHITE);
49         
50         public static final Color INTENSE_BLACK = new Color(getDisplay(), 0, 0, 0);     //Display.getCurrent().getSystemColor(SWT.COLOR_DARK_GRAY);
51         public static final Color INTENSE_RED = new Color(getDisplay(), 139, 0, 0);     //Display.getCurrent().getSystemColor(SWT.COLOR_DARK_RED);
52         public static final Color INTENSE_GREEN = new Color(getDisplay(), 0, 100, 0);//Display.getCurrent().getSystemColor(SWT.COLOR_DARK_GREEN);
53         public static final Color INTENSE_YELLOW = new Color(getDisplay(), 250,250,210);//Display.getCurrent().getSystemColor(SWT.COLOR_DARK_YELLOW);
54         public static final Color INTENSE_BLUE = new Color(getDisplay(), 0,0,139);//Display.getCurrent().getSystemColor(SWT.COLOR_DARK_BLUE);
55         public static final Color INTENSE_MAGENTA = new Color(getDisplay(), 139,0,139);//Display.getCurrent().getSystemColor(SWT.COLOR_DARK_MAGENTA);
56         public static final Color INTENSE_CYAN = new Color(getDisplay(), 0, 139, 139);//Display.getCurrent().getSystemColor(SWT.COLOR_DARK_CYAN);
57         public static final Color INTENSE_WHITE = new Color(getDisplay(), 245, 245, 245);//Display.getCurrent().getSystemColor(SWT.COLOR_WHITE);
58
59     public static final Map<Integer, Color[]> CODE2COLOR = Collections.unmodifiableMap( MapUtil.<Integer, Color[]>asMap( new Object[][] {
60         new Object[] { 30, new Color[] { INTENSE_BLACK, BLACK } },
61         new Object[] { 31, new Color[] { INTENSE_RED, RED } },
62         new Object[] { 32, new Color[] { INTENSE_GREEN, GREEN } },
63         new Object[] { 33, new Color[] { INTENSE_YELLOW, YELLOW } },
64         new Object[] { 34, new Color[] { INTENSE_BLUE, BLUE } },
65         new Object[] { 35, new Color[] { INTENSE_MAGENTA, MAGENTA } },
66         new Object[] { 36, new Color[] { INTENSE_CYAN, CYAN } },
67         new Object[] { 37, new Color[] { INTENSE_WHITE, WHITE } },
68         new Object[] { 40, new Color[] { INTENSE_BLACK, BLACK } },
69         new Object[] { 41, new Color[] { INTENSE_RED, RED } },
70         new Object[] { 42, new Color[] { INTENSE_GREEN, GREEN } },
71         new Object[] { 43, new Color[] { INTENSE_YELLOW, YELLOW } },
72         new Object[] { 44, new Color[] { INTENSE_BLUE, BLUE } },
73         new Object[] { 45, new Color[] { INTENSE_MAGENTA, MAGENTA } },
74         new Object[] { 46, new Color[] { INTENSE_CYAN, CYAN } },
75         new Object[] { 47, new Color[] { INTENSE_WHITE, WHITE } },
76     } ) );
77
78         private static int[] commands = {'m','n'}; //SGR , DSR 
79         
80         private static List<TextStyle> parseAnsiString(String ansiString) {
81                 char command = 0;
82                 List<TextStyle> styles = new ArrayList<TextStyle>();
83                 int arrayIndex = 0;
84                 StringBuilder sb = new StringBuilder();
85                 
86                 for (int i=0; i<ansiString.length(); i++) {                     
87                 if (ansiString.charAt(i) == ESCAPE) {
88                         int min = ansiString.length() ;
89                         int startCommand = 0;
90                         
91                         for (int j=0 ; j<commands.length ; j++) {
92                                 startCommand = ansiString.indexOf(commands[j], i);
93                                 if (min > startCommand && startCommand > -1) {
94                                         min = startCommand;
95                                 }
96                         }
97                                 command=ansiString.charAt(min);
98                         startCommand=min;
99                         
100                         if ( startCommand < ansiString.length()) {
101                                 
102                                 String colorCodes = ansiString.substring(i+2,startCommand); 
103                                 String codes[] = colorCodes.split(";");
104                                 Color[] colors = getColorAttribute(codes);
105                                 int styleStart = startCommand+1;
106                                 int styleEnd = 0;
107                                 int endEscape = ansiString.indexOf(ESCAPE,styleStart);
108                                 if (endEscape < 0 ) {
109                                         styleEnd = ansiString.length();
110                                         i = ansiString.length();
111                                 } else {
112                                         int endCommand = ansiString.indexOf(command,endEscape); 
113                                         if (endCommand < 0 ) {
114                                                 styleEnd = ansiString.length();
115                                                 i = ansiString.length();
116                                         } else {
117                                                 styleEnd = endEscape;
118                                                 i = endCommand;
119                                         }       
120                                 }
121                                 String styleString = null;
122                                 styleString = ansiString.substring(styleStart, styleEnd);
123                                 
124                                 TextStyle styleTs = new TextStyle(colors[0],colors[1],styleString);         
125
126                                 if (sb.length() > 0) {
127                                         TextStyle defaultTs = new TextStyle(BLACK,WHITE, sb.toString());                                
128                                         styles.add(arrayIndex++,defaultTs);
129                                         sb.setLength(0);
130                                 }
131                                 styles.add(arrayIndex++,styleTs);                                       
132
133                     }                   
134                 }else { //nothing found! 
135                         
136                         sb.append(ansiString.charAt(i));
137                 }               
138         }
139                 if (sb.length() > 0) {
140                         TextStyle defaultTs = new TextStyle(BLACK,WHITE, sb.toString());                                
141                         styles.add(arrayIndex,defaultTs);
142                 }
143                 return styles;
144         }
145         private static Color[] getColorAttribute(String codes[]) {
146                 boolean brighter = false;
147                 Color[] color = {BLACK,WHITE}; //foreground&background
148         
149         for (int j = 0; j < codes.length; j++) {
150                 
151             if (codes[j].length() > 0 && codes[j].matches("[\\d]*")) {
152                 int code = Integer.parseInt(codes[j]);
153                 if (code == 0)
154                     brighter = false;
155                 else if (code == 1)
156                     brighter = true;
157                 else if (code >= 30 && code <= 39) {
158                     if (code == 39)
159                         color[0] = BLACK; //default foreground color
160                     else if (code >= 30 && code <= 37) {
161                         color[0] = getColorFromANSICode(code, brighter);
162                         brighter = false;
163                     }
164                 } else if (code >= 40 && code <= 49) {
165                     if (code == 49)
166                         color[1] = WHITE; //default background color
167                     else if (code >= 40 && code <= 47)
168                         color[1] = getColorFromANSICode(code, brighter);
169                 }
170             }
171         }
172         return color;
173         }
174         
175             
176
177         private static Color getColorFromANSICode(int code, boolean brighter) {
178             final Color[] colorSet = CODE2COLOR.get( code );
179             if ( null == colorSet )
180             {
181                 return null;
182             }
183             return colorSet[brighter?0:1];
184     }
185         public static String getStripAnsiString(String newLineStr) {
186                 List<TextStyle> styles = parseAnsiString(newLineStr);           
187                 if (styles != null) {
188                         StringBuilder sb = new StringBuilder();
189                         for (int i=0 ; i<styles.size(); i++) {
190                                 sb.append(styles.get(i).getStripString());
191                         }
192                         return sb.toString();
193                 }else {
194                         return newLineStr;
195                 }
196         }
197         public static List<TextStyle> getStringStyles(String newLineStr) {
198                 List<TextStyle> styles = parseAnsiString(newLineStr);
199                 if (styles != null) {
200                         return styles;
201                 }else {
202                         return null;
203                 }
204         }
205 }