2ronwalf//The MIT License
2ronwalf//
2ronwalf//Copyright (c) 2003 Ron Alford, Mike Grove, Bijan Parsia, Evren Sirin
2ronwalf//
2ronwalf//Permission is hereby granted, free of charge, to any person obtaining a copy
2ronwalf//of this software and associated documentation files (the "Software"), to
2ronwalf//deal in the Software without restriction, including without limitation the
2ronwalf//rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
2ronwalf//sell copies of the Software, and to permit persons to whom the Software is
2ronwalf//furnished to do so, subject to the following conditions:
2ronwalf//
2ronwalf//The above copyright notice and this permission notice shall be included in
2ronwalf//all copies or substantial portions of the Software.
2ronwalf//
2ronwalf//THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
2ronwalf//IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
2ronwalf//FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
2ronwalf//AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
2ronwalf//LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
2ronwalf//FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
2ronwalf//IN THE SOFTWARE.
2ronwalf
2ronwalfpackage org.mindswap.utils;
2ronwalf
2ronwalf
2ronwalfimport java.io.OutputStream;
2ronwalfimport java.io.PrintWriter;
2ronwalfimport java.io.Writer;
2ronwalf
2ronwalf
2ronwalf/*
2ronwalf * Created on Sep 3, 2003
2ronwalf *
2ronwalf */
2ronwalf
2ronwalf/**
2ronwalf * A simple class to ease the the process of printing on console and printing an HTML output. By
2ronwalf * setting one variable the format of the output printed is changed, e.g. HTML tags will not
2ronwalf * be printed when the output is being printed to console.
2ronwalf *
2ronwalf * @author Evren Sirin
2ronwalf *
2ronwalf */
2ronwalfpublic class OutputFormatter {
2ronwalf PrintWriter out = null;
2ronwalf QNameProvider qnames;
2ronwalf boolean formatHTML;
2ronwalf
2ronwalf public OutputFormatter() {
2ronwalf this(false);
2ronwalf }
2ronwalf
2ronwalf public OutputFormatter(boolean formatHTML) {
2ronwalf this(System.out, formatHTML);
2ronwalf }
2ronwalf
2ronwalf public OutputFormatter(OutputStream out, boolean formatHTML) {
2ronwalf this(new PrintWriter(out), formatHTML);
2ronwalf }
2ronwalf
2ronwalf public OutputFormatter(Writer out, boolean formatHTML) {
2ronwalf this.out = (out instanceof PrintWriter) ? (PrintWriter) out : new PrintWriter(out);
2ronwalf this.formatHTML = formatHTML;
2ronwalf this.qnames = new QNameProvider();
2ronwalf }
2ronwalf
2ronwalf public boolean isFormatHTML() {
2ronwalf return formatHTML;
2ronwalf }
2ronwalf
2ronwalf public void setFormatHTML(boolean formatHTML) {
2ronwalf this.formatHTML = formatHTML;
2ronwalf }
2ronwalf
2ronwalf public PrintWriter getWriter() {
2ronwalf return out;
2ronwalf }
2ronwalf public void flush() {
2ronwalf out.flush();
2ronwalf }
2ronwalf public OutputFormatter printTab() {
2ronwalf if(formatHTML)
2ronwalf out.print("   ");
2ronwalf else
2ronwalf out.print(" ");
2ronwalf return this;
2ronwalf }
2ronwalf public OutputFormatter print(String s) {
2ronwalf out.print(s);
2ronwalf return this;
2ronwalf }
2ronwalf public OutputFormatter print(Object o) {
2ronwalf out.print(o);
2ronwalf return this;
2ronwalf }
2ronwalf public OutputFormatter print(boolean b) {
2ronwalf out.print(b);
2ronwalf return this;
2ronwalf }
2ronwalf public OutputFormatter println(Object o) {
2ronwalf print(o);
2ronwalf println();
2ronwalf return this;
2ronwalf }
2ronwalf public OutputFormatter println(String s) {
2ronwalf out.print(s);
2ronwalf println();
2ronwalf return this;
2ronwalf }
2ronwalf public OutputFormatter println() {
2ronwalf printHTML("<br>");
2ronwalf out.println();
2ronwalf return this;
2ronwalf }
2ronwalf public OutputFormatter printParagraph() {
2ronwalf if(formatHTML)
2ronwalf out.println("<p>");
2ronwalf else
2ronwalf out.println();
2ronwalf return this;
2ronwalf }
2ronwalf public OutputFormatter printURI(String uri) {
2ronwalf String label = (qnames != null)
2ronwalf ? qnames.shortForm(uri)
2ronwalf : URIUtils.getLocalName(uri);
2ronwalf
2ronwalf if(formatHTML)
2ronwalf printLink(uri, label);
2ronwalf else
2ronwalf print(label);
2ronwalf
2ronwalf return this;
2ronwalf }
2ronwalf public OutputFormatter printLink(String uri) {
2ronwalf if(formatHTML)
2ronwalf printLink(uri, uri);
2ronwalf else
2ronwalf out.print(uri);
2ronwalf
2ronwalf return this;
2ronwalf }
2ronwalf public OutputFormatter printLink(String uri, String label) {
2ronwalf if(formatHTML) {
2ronwalf out.print("<a href=\"");
2ronwalf out.print(uri);
2ronwalf out.print("\">");
2ronwalf out.print(label);
2ronwalf out.print("</a>");
2ronwalf }
2ronwalf else
2ronwalf out.print(label + " (" + uri + ")");
2ronwalf
2ronwalf return this;
2ronwalf }
2ronwalf public OutputFormatter printBold(String s) {
2ronwalf return printInsideTag(s, "b");
2ronwalf }
2ronwalf public OutputFormatter printItalic(String s) {
2ronwalf return printInsideTag(s, "i");
2ronwalf }
2ronwalf public OutputFormatter printInsideTag(String s, String tag) {
2ronwalf if(formatHTML) {
2ronwalf out.print("<"); out.print(tag); out.print(">");
2ronwalf out.print(s);
2ronwalf out.print("</"); out.print(tag); out.print(">");
2ronwalf }
2ronwalf else
2ronwalf out.print(s);
2ronwalf return this;
2ronwalf }
2ronwalf /**
2ronwalf * Print an HTML tag that will be ignored if the output format is not
2ronwalf * HTML.
2ronwalf *
2ronwalf * @param tag
2ronwalf * @return
2ronwalf */
2ronwalf public OutputFormatter printHTML(String tag) {
2ronwalf if(formatHTML)
2ronwalf out.print(tag);
2ronwalf return this;
2ronwalf }
2ronwalf /**
2ronwalf * @return Returns the qnames.
2ronwalf */
2ronwalf public QNameProvider getQNames() {
2ronwalf return qnames;
2ronwalf }
2ronwalf /**
2ronwalf * @param qnames The qnames to set.
2ronwalf */
2ronwalf public void setQNames(QNameProvider qnames) {
2ronwalf this.qnames = qnames;
2ronwalf }
2ronwalf}