/* XBN Java: Generically useful, non-GUI Java code. http://sourceforge.net/projects/xbnjava Copyright (C) 1997-2003, Jeff Epstein All rights reserved. Modifications: No Redistribution in binary form, with or without modifications, are permitted provided that the following conditions are met: * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. * If modifications are made to source code then this license should indicate that fact in the "Modifications" section above. * Neither the author, nor the contributors may be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. [NOTE: This license contains NO advertising clause.] */ package xbn.output; import xbn.XBNObject; /**

Optionally output text to a variety of destinations, with configuration.

Source code:  Outputter.java

Use this in in any class that implements Debuggable. This Outputter class contains a single OWriter, and a single OConfig.

@version 0.9b @author Jeff Epstein, http://sourceforge.net/projects/xbnjava. **/ public class Outputter extends XBNObject { private OWriter oWriter = null; private OConfig oConfig = null; /**

Create an Outputter that does nothing.

Equal to Outputter((new OWEmpty()), (new OConfig()))

**/ public Outputter() { this((new OWEmpty()), (new OConfig())); } /**

Create an Outputter.

Equal to Outputter(o_writer, (new OConfig(true)))

**/ public Outputter(OWriter o_writer) { this(o_writer, (new OConfig(true))); } /**

Create an Outputter.

@param o_writer Declares the output destination. May not be null. See setOWriter and getOWriter. @param o_config The configuration for this Outputter object. May not be null. See setOConfig and getOConfig. **/ public Outputter(OWriter o_writer, OConfig o_config) { setOWriter(o_writer); setOConfig(o_config); } /**

Get the OWriter object for direct manipulation.

Set with setOWriter.

**/ public final OWriter getOWriter() { return oWriter; } /**

Get the OConfig object for direct manipulation.

Set with setOConfig.

**/ public final OConfig getOConfig() { return oConfig; } /**

Define the destination for output.

Get with getOWriter.

@param o_writer Object declaring the output destination. May not be null. **/ public final void setOWriter(OWriter o_writer) { if(o_writer == null) { throwAX("setOWriter: o_writer is null."); } oWriter = o_writer; } /**

Define the configuration for this Outputter.

Get with getOConfig.

@param o_config The configuration for this Outputter object. May not be null. **/ public final void setOConfig(OConfig o_config) { throwAXIfNull(o_config, "o_config", "setOConfig"); oConfig = o_config; } /**

Is this Outputter actually writing it's output?

Note that this setting is ignored when the OWriter is not active.

@return getOConfig().isOn() **/ public final boolean isOn() { return getOConfig().isOn(); } /**

Write a line of output.

Equal to actuallyWriteLine(s_message, true, true)

**/ public final void write(String s_message) { actuallyWriteLine(s_message, true, true); } /**

Write a line of output to the destination.

Equal to actuallyWriteLine(s_message, false, false)

**/ public final void writeNoln(String s_message) { actuallyWriteLine(s_message, false, false); } /**

Write a newline to the destination.

Equal to actuallyWriteLine(null, false, true)

**/ public final void newln() { actuallyWriteLine(null, false, true); } /**

Write a line of output to the destination.

Equal to actuallyWriteLine(s_message, b_useIndent, true)

**/ public final void write(boolean b_useIndent, String s_message) { actuallyWriteLine(s_message, b_useIndent, true); } /**

Write a line of output to the destination.

Equal to actuallyWriteLine(s_message, b_useIndent, false)

**/ public final void writeNoln(boolean b_useIndent, String s_message) { actuallyWriteLine(s_message, b_useIndent, false); } /**

Write a line of output to the destination.

What is done in this function?

In order:

  1. If either getOWriter.isActive() or getOConfig().isOn() equal false, do nothing. DONE.
  2. If s_message is null and b_newLine is true, then call getOWriter().newln(). DONE.
  3. Format the message (using getOConfig().getFormatted(b_useIndent, s_message)), and then output it via getOWriter().write or getOWriter().writeNoln.

@param s_message The message to output. @param b_useIndent Should this line of output be preceded by an indentation? If true, yes, indent. If false, no indent. @param b_newLine If true, output using OWriter.write. If false, use OWriter.writeNoln. **/ public void actuallyWriteLine(String s_message, boolean b_useIndent, boolean b_newLine) { if(!getOWriter().isActive() || !getOConfig().isOn()) { return; } if(s_message == null && b_newLine) { getOWriter().newln(); return; } String sMessage = getOConfig().getFormatted(b_useIndent, s_message); if(b_newLine) { getOWriter().write(sMessage); } else { getOWriter().writeNoln(sMessage); } } /**

Get some information about this Ouptutter.

**/ public final String toString() { return this.getClass().getName() + ": [" + getOWriter().toString() + "], [" + getOConfig().toString() + "]"; } }