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