001/*
002 * This library is part of OpenCms -
003 * the Open Source Content Management System
004 *
005 * Copyright (c) Alkacon Software GmbH & Co. KG (http://www.alkacon.com)
006 *
007 * This library is free software; you can redistribute it and/or
008 * modify it under the terms of the GNU Lesser General Public
009 * License as published by the Free Software Foundation; either
010 * version 2.1 of the License, or (at your option) any later version.
011 *
012 * This library is distributed in the hope that it will be useful,
013 * but WITHOUT ANY WARRANTY; without even the implied warranty of
014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015 * Lesser General Public License for more details.
016 *
017 * For further information about Alkacon Software GmbH & Co. KG, please see the
018 * company website: http://www.alkacon.com
019 *
020 * For further information about OpenCms, please see the
021 * project website: http://www.opencms.org
022 *
023 * You should have received a copy of the GNU Lesser General Public
024 * License along with this library; if not, write to the Free Software
025 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
026 */
027
028package org.opencms.report;
029
030import java.util.Locale;
031
032/**
033 * Report class used to write the output of a report to a StringBuffer.<p>
034 *
035 * It stores everything and generates no output.
036 * After the report is finished, you can access to result of the
037 * report using the {@link #toString()} method.<p>
038 *
039 * @since 6.0.0
040 */
041public class CmsStringBufferReport extends A_CmsReport {
042
043    /** The StringBuffer to write to. */
044    private StringBuffer m_strBuf;
045
046    /**
047     * Constructs a new report using the provided locale for the output language.<p>
048     *
049     * @param locale the locale to use for the output language
050     */
051    public CmsStringBufferReport(Locale locale) {
052
053        init(locale, null);
054
055        m_strBuf = new StringBuffer();
056    }
057
058    /**
059     * @see org.opencms.report.I_CmsReport#getReportUpdate()
060     */
061    public String getReportUpdate() {
062
063        return "";
064    }
065
066    /**
067     * @see org.opencms.report.A_CmsReport#print(java.lang.String, int)
068     */
069    @Override
070    public void print(String value, int format) {
071
072        switch (format) {
073            case FORMAT_HEADLINE:
074            case FORMAT_WARNING:
075                addWarning(value);
076                m_strBuf.append(value);
077                break;
078            case FORMAT_ERROR:
079                addError(value);
080                m_strBuf.append(value);
081                break;
082            case FORMAT_NOTE:
083            case FORMAT_OK:
084            case FORMAT_DEFAULT:
085            default:
086                m_strBuf.append(value);
087        }
088        setLastEntryTime(System.currentTimeMillis());
089    }
090
091    /**
092     * @see org.opencms.report.I_CmsReport#println()
093     */
094    public void println() {
095
096        m_strBuf.append("\n");
097    }
098
099    /**
100     * @see org.opencms.report.I_CmsReport#println(java.lang.Throwable)
101     */
102    public void println(Throwable t) {
103
104        print(getMessages().key(Messages.RPT_EXCEPTION_0), FORMAT_WARNING);
105        println(t.getMessage(), FORMAT_ERROR);
106
107        StackTraceElement[] stackTrace = t.getStackTrace();
108        for (int i = 0; i < stackTrace.length; i++) {
109            StackTraceElement element = stackTrace[i];
110            println(element.toString());
111        }
112    }
113
114    /**
115     * @see java.lang.Object#toString()
116     */
117    @Override
118    public String toString() {
119
120        return m_strBuf.toString();
121    }
122}