PageRenderTime 17ms CodeModel.GetById 9ms app.highlight 5ms RepoModel.GetById 1ms app.codeStats 1ms

/hudson-core/src/main/java/hudson/tasks/_ant/AntConsoleAnnotator.java

http://github.com/hudson/hudson
Java | 79 lines | 35 code | 13 blank | 31 comment | 6 complexity | ccd982f4148bacb81b6ae4168926eba2 MD5 | raw file
 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 */
24package hudson.tasks._ant;
25
26import hudson.console.LineTransformationOutputStream;
27
28import java.io.IOException;
29import java.io.OutputStream;
30import java.nio.ByteBuffer;
31import java.nio.charset.Charset;
32
33/**
34 * Filter {@link OutputStream} that places an annotation that marks Ant target execution.
35 * 
36 * @author Kohsuke Kawaguchi
37 * @sine 1.349
38 */
39public class AntConsoleAnnotator extends LineTransformationOutputStream {
40    private final OutputStream out;
41    private final Charset charset;
42
43    private boolean seenEmptyLine;
44
45    public AntConsoleAnnotator(OutputStream out, Charset charset) {
46        this.out = out;
47        this.charset = charset;
48    }
49
50    @Override
51    protected void eol(byte[] b, int len) throws IOException {
52        String line = charset.decode(ByteBuffer.wrap(b, 0, len)).toString();
53
54        // trim off CR/LF from the end
55        line = trimEOL(line);
56
57        if (seenEmptyLine && endsWith(line,':') && line.indexOf(' ')<0)
58            // put the annotation
59            new AntTargetNote().encodeTo(out);
60
61        if (line.equals("BUILD SUCCESSFUL") || line.equals("BUILD FAILED"))
62            new AntOutcomeNote().encodeTo(out);
63
64        seenEmptyLine = line.length()==0;
65        out.write(b,0,len);
66    }
67
68    private boolean endsWith(String line, char c) {
69        int len = line.length();
70        return len>0 && line.charAt(len-1)==c;
71    }
72
73    @Override
74    public void close() throws IOException {
75        super.close();
76        out.close();
77    }
78
79}