KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tools > ant > module > spi > AntOutputStream


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.apache.tools.ant.module.spi;
21
22 import java.io.IOException JavaDoc;
23 import java.io.OutputStream JavaDoc;
24 import java.net.URL JavaDoc;
25 import org.openide.filesystems.FileObject;
26
27 /** OutputStream for wrapping output of Ant task and capable of
28  * parsing Ant output.
29  *
30  * @since 2.15
31  * @deprecated This functionality is not recommended to be used and may result
32  * in loss of some Ant module features as of org.apache.tools.ant.module/3 3.12.
33  */

34 @Deprecated JavaDoc
35 public abstract class AntOutputStream extends OutputStream JavaDoc {
36
37     /** buffer which will be used for the next line */
38     private StringBuffer JavaDoc buffer = new StringBuffer JavaDoc (1000);
39     /** have we printed any lines yet? used to prevent initial blank line */
40     private boolean hadFirst = false;
41
42     @Override JavaDoc
43     final public void close() throws IOException JavaDoc {
44         flush ();
45         handleClose();
46     }
47
48     /**
49      * This method is called when the stream is closed and it allows
50      * entensions of this class to do additional tasks.
51      * For example, closing an underlying stream, etc.
52      * The default implementation does nothing.
53      */

54     protected void handleClose() throws IOException JavaDoc {
55     }
56
57     @Override JavaDoc
58     final public void flush() throws IOException JavaDoc {
59         flushLines (true);
60     }
61
62     @Override JavaDoc
63     final public void write(byte[] b) throws IOException JavaDoc {
64         write (b, 0, b.length);
65     }
66
67     @Override JavaDoc
68     final public void write(byte[] b, int offset, int length) throws IOException JavaDoc {
69         buffer.append (new String JavaDoc (b, offset, length));
70         // Will usually contain at least one newline:
71
flushLines (false);
72     }
73
74     @Override JavaDoc
75     final public void write(int b) throws IOException JavaDoc {
76         buffer.append ((char) b);
77         if ((char) b == '\n') {
78             flushLines (false);
79         }
80     }
81
82     private void flushLines (boolean flushEverything) throws IOException JavaDoc {
83         // Not as efficient as it could be, surely, but keep it simple for now:
84
//System.err.println("flushLines: buffer=" + buffer);
85
MAIN:
86         while (true) {
87             int len = buffer.length ();
88             for (int i = 0; i < len; i++) {
89                 if (buffer.charAt (i) == '\n') {
90                     //System.err.println("flushing; i=" + i);
91
// For Windows:
92
int end = i;
93                     if (end > 0 && buffer.charAt (end - 1) == '\r') {
94                         end--;
95                     }
96                     flushLine (buffer.substring (0, end));
97                     buffer.delete (0, i + 1);
98                     continue MAIN;
99                 }
100             }
101             //System.err.println("not found");
102
break MAIN;
103         }
104         if (flushEverything) {
105             flushLine(buffer.substring (0, buffer.length()));
106             buffer.delete(0, buffer.length());
107         }
108     }
109     
110     private void flushLine (String JavaDoc l) throws IOException JavaDoc {
111         //System.err.println("flushing: " + l);
112
if (! hadFirst) {
113             hadFirst = true;
114             // Do not print an initial blank line.
115
if (l.trim ().length () == 0) {
116                 return;
117             }
118         }
119         writeLine(l);
120     }
121
122     /**
123      * Write one line of the parsed text (<strong>must be overridden</strong>).
124      * All line and column parameters can be -1 meaning
125      * that the value was not available or parsing was not successful.
126      * @param line original text of the line
127      * @param file file location for which this line was generated
128      * @param line1 starting line of the message
129      * @param col1 starting column of the message
130      * @param line2 ending line of the message
131      * @param col2 ending column of the message
132      * @param message message
133      * @return must always return true
134      * @since org.apache.tools.ant.module/3 3.10
135      * @deprecated No longer called.
136      */

137     @Deprecated JavaDoc
138     protected boolean writeLine(String JavaDoc line, URL JavaDoc file, int line1, int col1, int line2, int col2, String JavaDoc message) throws IOException JavaDoc {
139         return false;
140     }
141
142     /** Write one line of the parsed text. All line and column parameters can be -1 what means
143     * that value was not available or parsing was not successful.
144     * @param line original text of the line
145     * @param file file object for which this line was generated
146     * @param line1 starting line of the message
147     * @param col1 starting column of the message
148     * @param line2 ending line of the message
149     * @param col2 ending column of the message
150     * @param message message
151      * @deprecated Please override the variant taking URL instead, since org.apache.tools.ant.module/3 3.10.
152     */

153     @Deprecated JavaDoc
154     protected void writeLine(String JavaDoc line, FileObject file, int line1, int col1, int line2, int col2, String JavaDoc message) throws IOException JavaDoc {
155         throw new IllegalStateException JavaDoc("writeLine(...URL...) must return true if writeLine(...FileObject...) is not implemented"); // NOI18N
156
}
157
158     /** Write one line of text which was not parsed.
159      */

160     abstract protected void writeLine(String JavaDoc line) throws IOException JavaDoc;
161     
162     /** Create well formated message from the parsed information.
163      * @deprecated No longer used since org.apache.tools.ant.module/3 3.8.
164      */

165     @Deprecated JavaDoc
166     protected String JavaDoc formatMessage(String JavaDoc fileName, String JavaDoc message, int line1, int col1, int line2, int col2) {
167         return message;
168     }
169     
170 }
171
Popular Tags