001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.apache.tools.ant.module.spi;
043:
044: import java.io.IOException;
045: import java.io.OutputStream;
046: import java.net.URL;
047: import org.openide.filesystems.FileObject;
048:
049: /** OutputStream for wrapping output of Ant task and capable of
050: * parsing Ant output.
051: *
052: * @since 2.15
053: * @deprecated This functionality is not recommended to be used and may result
054: * in loss of some Ant module features as of org.apache.tools.ant.module/3 3.12.
055: */
056: @Deprecated
057: public abstract class AntOutputStream extends OutputStream {
058:
059: /** buffer which will be used for the next line */
060: private StringBuffer buffer = new StringBuffer(1000);
061: /** have we printed any lines yet? used to prevent initial blank line */
062: private boolean hadFirst = false;
063:
064: @Override
065: final public void close() throws IOException {
066: flush();
067: handleClose();
068: }
069:
070: /**
071: * This method is called when the stream is closed and it allows
072: * entensions of this class to do additional tasks.
073: * For example, closing an underlying stream, etc.
074: * The default implementation does nothing.
075: */
076: protected void handleClose() throws IOException {
077: }
078:
079: @Override
080: final public void flush() throws IOException {
081: flushLines(true);
082: }
083:
084: @Override
085: final public void write(byte[] b) throws IOException {
086: write(b, 0, b.length);
087: }
088:
089: @Override
090: final public void write(byte[] b, int offset, int length)
091: throws IOException {
092: buffer.append(new String(b, offset, length));
093: // Will usually contain at least one newline:
094: flushLines(false);
095: }
096:
097: @Override
098: final public void write(int b) throws IOException {
099: buffer.append((char) b);
100: if ((char) b == '\n') {
101: flushLines(false);
102: }
103: }
104:
105: private void flushLines(boolean flushEverything) throws IOException {
106: // Not as efficient as it could be, surely, but keep it simple for now:
107: //System.err.println("flushLines: buffer=" + buffer);
108: MAIN: while (true) {
109: int len = buffer.length();
110: for (int i = 0; i < len; i++) {
111: if (buffer.charAt(i) == '\n') {
112: //System.err.println("flushing; i=" + i);
113: // For Windows:
114: int end = i;
115: if (end > 0 && buffer.charAt(end - 1) == '\r') {
116: end--;
117: }
118: flushLine(buffer.substring(0, end));
119: buffer.delete(0, i + 1);
120: continue MAIN;
121: }
122: }
123: //System.err.println("not found");
124: break MAIN;
125: }
126: if (flushEverything) {
127: flushLine(buffer.substring(0, buffer.length()));
128: buffer.delete(0, buffer.length());
129: }
130: }
131:
132: private void flushLine(String l) throws IOException {
133: //System.err.println("flushing: " + l);
134: if (!hadFirst) {
135: hadFirst = true;
136: // Do not print an initial blank line.
137: if (l.trim().length() == 0) {
138: return;
139: }
140: }
141: writeLine(l);
142: }
143:
144: /**
145: * Write one line of the parsed text (<strong>must be overridden</strong>).
146: * All line and column parameters can be -1 meaning
147: * that the value was not available or parsing was not successful.
148: * @param line original text of the line
149: * @param file file location for which this line was generated
150: * @param line1 starting line of the message
151: * @param col1 starting column of the message
152: * @param line2 ending line of the message
153: * @param col2 ending column of the message
154: * @param message message
155: * @return must always return true
156: * @since org.apache.tools.ant.module/3 3.10
157: * @deprecated No longer called.
158: */
159: @Deprecated
160: protected boolean writeLine(String line, URL file, int line1,
161: int col1, int line2, int col2, String message)
162: throws IOException {
163: return false;
164: }
165:
166: /** Write one line of the parsed text. All line and column parameters can be -1 what means
167: * that value was not available or parsing was not successful.
168: * @param line original text of the line
169: * @param file file object for which this line was generated
170: * @param line1 starting line of the message
171: * @param col1 starting column of the message
172: * @param line2 ending line of the message
173: * @param col2 ending column of the message
174: * @param message message
175: * @deprecated Please override the variant taking URL instead, since org.apache.tools.ant.module/3 3.10.
176: */
177: @Deprecated
178: protected void writeLine(String line, FileObject file, int line1,
179: int col1, int line2, int col2, String message)
180: throws IOException {
181: throw new IllegalStateException(
182: "writeLine(...URL...) must return true if writeLine(...FileObject...) is not implemented"); // NOI18N
183: }
184:
185: /** Write one line of text which was not parsed.
186: */
187: abstract protected void writeLine(String line) throws IOException;
188:
189: /** Create well formated message from the parsed information.
190: * @deprecated No longer used since org.apache.tools.ant.module/3 3.8.
191: */
192: @Deprecated
193: protected String formatMessage(String fileName, String message,
194: int line1, int col1, int line2, int col2) {
195: return message;
196: }
197:
198: }
|