File D:\maven-qcr-plugin-testsunitaires\qcr4java\jenkins-master\target\site\qc\core\src\main\java\hudson\console\ConsoleAnnotationOutputStream.java.html

1 /*
2 * The MIT License
3 *
4 * Copyright (c) 2004-2010, Sun Microsystems, Inc.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
23 */
24 package hudson.console;
25
26 import hudson.MarkupText;
27 import org.apache.commons.io.output.ProxyWriter;
28 import org.kohsuke.stapler.framework.io.WriterOutputStream;
29
30 import java.io.ByteArrayInputStream;
31 import java.io.DataInputStream;
32 import java.io.IOException;
33 import java.io.OutputStream;
34 import java.io.StringWriter;
35 import java.io.Writer;
36 import java.nio.charset.Charset;
37 import java.util.ArrayList;
38 import java.util.List;
39 import java.util.logging.Level;
40 import java.util.logging.Logger;
41
42 /**
43 * Used to convert plain text console output (as byte sequence) + embedded annotations into HTML (as char sequence),
44 * by using {@link ConsoleAnnotator}.
45 *
46 * @param <T>
47 * Context type.
48 * @author Kohsuke Kawaguchi
49 * @since 1.349
50 */
51 public class ConsoleAnnotationOutputStream<T> extends LineTransformationOutputStream {
52 private final Writer out;
53 private final T context;
54 private ConsoleAnnotator<T> ann;
55
56 /**
57 * Reused buffer that stores char representation of a single line.
58 */
59 private final LineBuffer line = new LineBuffer(256);
60 /**
61 * {@link OutputStream} that writes to {@link #line}.
62 */
63 private final WriterOutputStream lineOut;
64
65 /**
66 *
67 */
68 public ConsoleAnnotationOutputStream(Writer out, ConsoleAnnotator<? super T> ann, T context, Charset charset) {
69 this.out = out;
70 this.ann = ConsoleAnnotator.cast(ann);
71 this.context = context;
72 this.lineOut = new WriterOutputStream(line,charset);
73 }
74
75 public ConsoleAnnotator getConsoleAnnotator() {
76 return ann;
77 }
78
79 /**
80 * Called after we read the whole line of plain text, which is stored in {@link #buf}.
81 * This method performs annotations and send the result to {@link #out}.
82 */
83 protected void eol(byte[] in, int sz) throws IOException {
84 line.reset();
85 final StringBuffer strBuf = line.getStringBuffer();
86
87 int next = ConsoleNote.findPreamble(in,0,sz);
88
89 List<ConsoleAnnotator<T>> annotators=null;
90
91 {// perform byte[]->char[] while figuring out the char positions of the BLOBs
92 int written = 0;
93 while (next>=0) {
94 if (next>written) {
95 lineOut.write(in,written,next-written);
96 lineOut.flush();
97 written = next;
98 } else {
99 assert next==written;
100 }
101
102 // character position of this annotation in this line
103 final int charPos = strBuf.length();
104
105 int rest = sz - next;
106 ByteArrayInputStream b = new ByteArrayInputStream(in, next, rest);
107
108 try {
109 final ConsoleNote a = ConsoleNote.readFrom(new DataInputStream(b));
110 if (a!=null) {
111 if (annotators==null)
112 annotators = new ArrayList<ConsoleAnnotator<T>>();
113 annotators.add(new ConsoleAnnotator<T>() {
114 public ConsoleAnnotator annotate(T context, MarkupText text) {
115 return a.annotate(context,text,charPos);
116 }
117 });
118 }
119 } catch (IOException e) {
120 // if we failed to resurrect an annotation, ignore it.
121 LOGGER.log(Level.FINE,"Failed to resurrect annotation",e);
122 } catch (ClassNotFoundException e) {
123 LOGGER.log(Level.FINE,"Failed to resurrect annotation",e);
124 }
125
126 int bytesUsed = rest - b.available(); // bytes consumed by annotations
127 written += bytesUsed;
128
129
130 next = ConsoleNote.findPreamble(in,written,sz-written);
131 }
132 // finish the remaining bytes->chars conversion
133 lineOut.write(in,written,sz-written);
134
135 if (annotators!=null) {
136 // aggregate newly retrieved ConsoleAnnotators into the current one.
137 if (ann!=null) annotators.add(ann);
138 ann = ConsoleAnnotator.combine(annotators);
139 }
140 }
141
142 lineOut.flush();
143 MarkupText mt = new MarkupText(strBuf.toString());
144 if (ann!=null)
145 ann = ann.annotate(context,mt);
146 out.write(mt.toString(true)); // this perform escapes
147 }
148
149 @Override
150 public void flush() throws IOException {
151 out.flush();
152 }
153
154 @Override
155 public void close() throws IOException {
156 super.close();
157 out.close();
158 }
159
160 /**
161 * {@link StringWriter} enhancement that's capable of shrinking the buffer size.
162 *
163 * <p>
164 * The problem is that {@link StringBuffer#setLength(int)} doesn't actually release
165 * the underlying buffer, so for us to truncate the buffer, we need to create a new {@link StringWriter} instance.
166 */
167 private static class LineBuffer extends ProxyWriter {
168 private LineBuffer(int initialSize) {
169 super(new StringWriter(initialSize));
170 }
171
172 private void reset() {
173 StringBuffer buf = getStringBuffer();
174 if (buf.length()>4096)
175 out = new StringWriter(256);
176 else
177 buf.setLength(0);
178 }
179
180 private StringBuffer getStringBuffer() {
181 StringWriter w = (StringWriter)out;
182 return w.getBuffer();
183 }
184 }
185
186 private static final Logger LOGGER = Logger.getLogger(ConsoleAnnotationOutputStream.class.getName());
187 }
188