//============================================================================== // tribble/io/DiagnosticOutputDfl.java //------------------------------------------------------------------------------ package tribble.io; // System imports import java.io.OutputStream; import java.io.PrintWriter; import java.io.Writer; import java.lang.String; import java.lang.StringBuffer; import java.lang.Throwable; // Local imports import tribble.io.DiagnosticOutputI; import tribble.io.SuspendablePrintWriter; /******************************************************************************* * Diagnostic message (informational, warning, and error messages) output stream. * *

* This is a default implementation of a diagnostic message output stream (see * {@link tribble.io.DiagnosticOutputI}) that is capable of writing diagnostic * messages as lines of text to an underlying output stream. Such a stream is * used to display informational, warning, and error diagnostic messages by such * classes as lexical analyzers (lexers) and parsers. * See the {@link #printMessage} method. * * @version $Revision: 1.4 $ $Date: 2003/02/26 04:08:35 $ * @since 2001-05-18 * @author * David R. Tribble, * david@tribble.com. *
* Copyright * ©2001-2003 by David R. Tribble all rights reserved. *
* Permission is granted to freely use and distribute this source code * provided that the original copyright and authorship notices remain * intact. * * @see DiagnosticOutputI */ public class DiagnosticOutputDfl extends SuspendablePrintWriter implements DiagnosticOutputI { // Identification /** Revision information. */ static final String REV = "@(#)tribble/io/DiagnosticOutputDfl.java $Revision: 1.4 $ $Date: 2003/02/26 04:08:35 $\n"; // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - // Constants // (None) // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - // Static methods // (None) // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - // Protected variables /** Output message buffer. */ protected StringBuffer m_buf = new StringBuffer(80); // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - // Public constructors /*************************************************************************** * Constructor. * * @param out * An output stream to which diagnostic messages (warning and error messages) * are written. This should not be null. * * @since 1.1, 2001-07-20 */ public DiagnosticOutputDfl(Writer out) { // Establish the output stream super(out); } /*************************************************************************** * Constructor. * * @param out * An output stream to which diagnostic messages (warning and error messages) * are written. This should not be null. * * @since 1.1, 2001-07-20 */ public DiagnosticOutputDfl(OutputStream out) { // Establish the output stream super(new PrintWriter(out, true)); } // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - // Public methods /*************************************************************************** * Writes a diagnostic message to the output stream. * * @param fname * An optional source filename for the diagnostic message. * If it is not null and not empty, it and a colon (":") * immediately following it are written. * * @param line * An optional source line number for the diagnostic message. * If it is not zero, it and a colon (":") immediately following it * are written. * * @param msg * The message text. * This typically contains a prefix indicating the severity of the message, * such as "warning:" or "error:". * Note that the message should not contain a terminating linefeed. * * @since 1.1, 2001-05-18 */ public synchronized void printMessage(String fname, int line, String msg) //implements DiagnosticOutputI { boolean nonblank = false; // Check for an open output stream if (out == null) return; // Format and write the diagnostic message to the output stream m_buf.setLength(0); if (fname != null && fname.length() > 0) { // Append the filename to the output message m_buf.append(fname); m_buf.append(":"); nonblank = true; } if (line > 0) { // Append the line number to the output message m_buf.append(line); m_buf.append(":"); nonblank = true; } if (msg != null) { // Append the client message to the output message if (nonblank) m_buf.append(" "); m_buf.append(msg); } if (m_buf.length() > 0) { synchronized (lock) { // Write the message to the output stream // Note: This is a nearly atomic operation super.println(m_buf); super.flush(); } } if (m_buf.length() >= 1000) m_buf = new StringBuffer(200); } // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - // Protected methods /*************************************************************************** * Finalization. * Flushes the underlying output stream, if any, but does not close it. * * @since 1.1, 2001-05-18 */ protected synchronized void finalize() throws Throwable { // Flush and disassociate (but do not close) the output stream flush(); out = null; m_buf = null; // Cascade super.finalize(); } // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - // Private constructors /*************************************************************************** * Default constructor. * Do not use this constructor. * * @since 1.1, 2001-05-18 */ private DiagnosticOutputDfl() { // Do nothing } } // End DiagnosticOutputDfl.java