1
2
3
4
5
6
7
8
9
10
11
12
13 package org.eclipse.jetty.osgi.equinoxtools.console;
14
15 import java.util.LinkedList;
16 import java.util.Queue;
17
18 import org.eclipse.jetty.osgi.equinoxtools.console.WebConsoleWriterOutputStream.OnFlushListener;
19
20
21
22
23
24 public class EquinoxChattingSupport
25 {
26
27 private WebConsoleSession _consoleSession;
28
29 public EquinoxChattingSupport(WebConsoleSession consoleSession)
30 {
31 _consoleSession = consoleSession;
32 }
33
34
35
36
37
38
39
40 protected Queue<String> processConsoleOutput(boolean escape, OnFlushListener onflush)
41 {
42 Queue<String> result = new LinkedList<String>();
43 String toDisplay = _consoleSession.getOutputAsWriter().getBuffer().toString();
44
45 boolean clearConsole = _consoleSession.getOnFlushListeners().indexOf(onflush) == _consoleSession.getOnFlushListeners().size();
46 if (clearConsole)
47 {
48 _consoleSession.clearOutput();
49 }
50 boolean lastLineIsComplete = toDisplay.endsWith("\n") || toDisplay.endsWith("\r");
51 String[] lines = toDisplay.split("\n");
52 String lastLine = lastLineIsComplete ? null : lines[lines.length-1];
53 if (clearConsole)
54 {
55 _consoleSession.getOutputAsWriter().append(lastLine);
56 }
57 for (int lnNb = 0; lnNb < (lastLineIsComplete ? lines.length : lines.length-1); lnNb++)
58 {
59 String line = lines[lnNb];
60 while (line.trim().startsWith("null"))
61 {
62 line = line.trim().substring("null".length()).trim();
63 }
64 if (line.startsWith("osgi>"))
65 {
66 result.add("osgi>");
67 result.add(escape ? jsonEscapeString(line.substring("osgi>".length())) : line.substring("osgi>".length()));
68 }
69 else
70 {
71 result.add(" ");
72 result.add(escape ? jsonEscapeString(line) : line);
73 }
74 }
75 return result;
76 }
77
78
79
80
81
82
83 public static String jsonEscapeString(String str)
84 {
85 StringBuilder sb = new StringBuilder();
86 char[] asChars = str.toCharArray();
87 for (char ch : asChars)
88 {
89 switch (ch)
90 {
91
92 case '"':
93 sb.append("\\\"");
94 break;
95 case '\\':
96 sb.append("\\\\");
97 break;
98 case '\b':
99 sb.append("\\b");
100 break;
101 case '\f':
102 sb.append("\\f");
103 break;
104 case '\n':
105 sb.append("\\n");
106 break;
107 case '\r':
108 sb.append("\\r");
109 break;
110 case '\t':
111 sb.append("\\t");
112 break;
113 case '/':
114 sb.append("\\/");
115 break;
116 default:
117
118 if (ch >= '\u0000' && ch <= '\u001F')
119 {
120
121 String ss = Integer.toHexString(ch);
122 sb.append("\\u");
123 for (int i = 0; i < 4 - ss.length(); i++)
124 {
125
126 sb.append('0');
127 }
128 sb.append(ss.toUpperCase());
129 }
130 else
131 {
132 sb.append(ch);
133 }
134 }
135 }
136 return sb.toString();
137 }
138
139 public void broadcast(OnFlushListener source)
140 {
141 for (OnFlushListener onflush : _consoleSession.getOnFlushListeners())
142 {
143 if (onflush != source)
144 {
145 onflush.onFlush();
146 }
147 }
148 }
149
150 }