alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

Groovy example source code file (IndentPrinter.java)

This example Groovy source code file (IndentPrinter.java) is included in the DevDaily.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Java - Groovy tags/keywords

groovyruntimeexception, groovyruntimeexception, illegalargumentexception, indentprinter, indentprinter, io, must, string, string, writer, writer

The Groovy IndentPrinter.java source code

/*
 * Copyright 2003-2011 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package groovy.util;

import groovy.lang.GroovyRuntimeException;

import java.io.IOException;
import java.io.PrintWriter;
import java.io.Writer;

/**
 * <p>A helper class for printing indented text. This can be used stand-alone or, more commonly, from Builders.

* * <p>By default, a PrintWriter to System.out is used as the Writer, but it is possible * to change the Writer by passing a new one as a constructor argument.</p> * * <p>Indention by default is 2 characters but can be changed by passing a * different value as a constructor argument.</p> * * <p>The following is an example usage. Note that within a "with" block you need to * specify a parameter name so that this.println is not called instead of IndentPrinter.println: </p> * <pre> * new IndentPrinter(new PrintWriter(out)).with { p -> * p.printIndent() * p.println('parent1') * p.incrementIndent() * p.printIndent() * p.println('child 1') * p.printIndent() * p.println('child 2') * p.decrementIndent() * p.printIndent() * p.println('parent2') * p.flush() * } * </pre> * <p>The above example prints this to standard output:

* <pre> * parent1 * child 1 * child 2 * parent2 * </pre> * @author <a href="mailto:james@coredevelopers.net">James Strachan */ public class IndentPrinter { private int indentLevel; private String indent; private Writer out; private final boolean addNewlines; /** * Creates an IndentPrinter backed by a PrintWriter pointing to System.out, with an indent of two spaces. * * @see #IndentPrinter(Writer, String) */ public IndentPrinter() { this(new PrintWriter(System.out), " "); } /** * Creates an IndentPrinter backed by the supplied Writer, with an indent of two spaces. * * @param out Writer to output to * @see #IndentPrinter(Writer, String) */ public IndentPrinter(Writer out) { this(out, " "); } /** * Creates an IndentPrinter backed by the supplied Writer, * with a user-supplied String to be used for indenting. * * @param out Writer to output to * @param indent character(s) used to indent each line */ public IndentPrinter(Writer out, String indent) { this(out, indent, true); } /** * Creates an IndentPrinter backed by the supplied Writer, * with a user-supplied String to be used for indenting * and the ability to override newline handling. * * @param out Writer to output to * @param indent character(s) used to indent each line * @param addNewlines set to false to gobble all new lines (default true) */ public IndentPrinter(Writer out, String indent, boolean addNewlines) { this.addNewlines = addNewlines; if (out == null) { throw new IllegalArgumentException("Must specify a Writer"); } this.out = out; this.indent = indent; } /** * Prints a string followed by an end of line character. * * @param text String to be written */ public void println(String text) { try { out.write(text); println(); } catch(IOException ioe) { throw new GroovyRuntimeException(ioe); } } /** * Prints a string. * * @param text String to be written */ public void print(String text) { try { out.write(text); } catch(IOException ioe) { throw new GroovyRuntimeException(ioe); } } /** * Prints a character. * * @param c char to be written */ public void print(char c) { try { out.write(c); } catch(IOException ioe) { throw new GroovyRuntimeException(ioe); } } /** * Prints the current indent level. */ public void printIndent() { for (int i = 0; i < indentLevel; i++) { try { out.write(indent); } catch(IOException ioe) { throw new GroovyRuntimeException(ioe); } } } /** * Prints an end-of-line character (if enabled via addNewLines property). * Defaults to outputting a single '\n' character but by using a custom * Writer, e.g. PlatformLineWriter, you can get platform-specific * end-of-line characters. * * @see #IndentPrinter(Writer, String, boolean) */ public void println() { if (addNewlines) { try { out.write("\n"); } catch(IOException ioe) { throw new GroovyRuntimeException(ioe); } } } public void incrementIndent() { ++indentLevel; } public void decrementIndent() { --indentLevel; } public int getIndentLevel() { return indentLevel; } public void setIndentLevel(int indentLevel) { this.indentLevel = indentLevel; } public void flush() { try { out.flush(); } catch(IOException ioe) { throw new GroovyRuntimeException(ioe); } } }

Other Groovy examples (source code examples)

Here is a short list of links related to this Groovy IndentPrinter.java source code file:

... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.