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 org.opencms.main.CmsException;
031import org.opencms.main.OpenCms;
032import org.opencms.workplace.CmsWorkplace;
033
034import java.io.PrintStream;
035import java.util.Locale;
036
037/**
038 * Stream report where the output is streamed to the given print stream instance.<p>
039 *
040 * Keep in mind that you are resposible for closing the stream calling the {@link #close()}
041 * method when the report is no longer used.<p>
042 *
043 * @since 6.5.5
044 */
045public class CmsPrintStreamReport extends CmsHtmlReport {
046
047    /** The print stream to write the output to. */
048    private PrintStream m_printStream;
049
050    /** If set to <code>true</code> the report will write html code. */
051    private boolean m_writeHtml;
052
053    /**
054     * Constructs a new report using the provided locale for the output language.<p>
055     *
056     * @param printStream the print stream to write the output to
057     * @param locale the locale to use for the output language
058     * @param writeHtml decides if the report should write clear text or html code
059     */
060    public CmsPrintStreamReport(PrintStream printStream, Locale locale, boolean writeHtml) {
061
062        super(locale, null, true, true);
063        m_printStream = printStream;
064        m_writeHtml = writeHtml;
065    }
066
067    /**
068     * Closes the print stream.<p>
069     *
070     * Has to be called after the report has finished.<p>
071     */
072    public void close() {
073
074        if (m_printStream != null) {
075            m_printStream.close();
076            m_printStream = null;
077        }
078    }
079
080    /**
081     * Finishes the report, closing the stream.<p>
082     */
083    public void finish() {
084
085        if (m_writeHtml) {
086            m_printStream.println(CmsWorkplace.generatePageEndExtended());
087        }
088        close();
089    }
090
091    /**
092     * @see org.opencms.report.I_CmsReport#getReportUpdate()
093     */
094    @Override
095    public synchronized String getReportUpdate() {
096
097        return "";
098    }
099
100    /**
101     * @see org.opencms.report.A_CmsReport#print(java.lang.String, int)
102     */
103    @Override
104    public synchronized void print(String value, int format) {
105
106        if (m_writeHtml) {
107            super.print(value, format);
108            m_printStream.print(super.getReportUpdate());
109            return;
110        }
111        StringBuffer buf;
112        switch (format) {
113            case FORMAT_HEADLINE:
114                buf = new StringBuffer();
115                buf.append("------ ");
116                buf.append(value);
117                m_printStream.print(buf);
118                break;
119            case FORMAT_WARNING:
120                buf = new StringBuffer();
121                buf.append("!!! ");
122                buf.append(value);
123                m_printStream.print(buf);
124                addWarning(value);
125                break;
126            case FORMAT_ERROR:
127                buf = new StringBuffer();
128                buf.append("!!! ");
129                buf.append(value);
130                m_printStream.print(buf);
131                addError(value);
132                break;
133            case FORMAT_NOTE:
134            case FORMAT_OK:
135            case FORMAT_DEFAULT:
136            default:
137                m_printStream.print(value);
138        }
139        setLastEntryTime(System.currentTimeMillis());
140    }
141
142    /**
143     * @see org.opencms.report.I_CmsReport#println()
144     */
145    @Override
146    public synchronized void println() {
147
148        if (m_writeHtml) {
149            super.println();
150            m_printStream.print(super.getReportUpdate());
151            setLastEntryTime(System.currentTimeMillis());
152            return;
153        }
154        m_printStream.println();
155        setLastEntryTime(System.currentTimeMillis());
156    }
157
158    /**
159     * @see org.opencms.report.I_CmsReport#println(java.lang.Throwable)
160     */
161    @Override
162    public synchronized void println(Throwable t) {
163
164        if (m_writeHtml) {
165            super.println(t);
166            m_printStream.print(super.getReportUpdate());
167            setLastEntryTime(System.currentTimeMillis());
168            return;
169        }
170        StringBuffer buf = new StringBuffer();
171        buf.append(getMessages().key(Messages.RPT_EXCEPTION_0));
172        buf.append(t.getMessage());
173        println(new String(buf), FORMAT_ERROR);
174        t.printStackTrace(m_printStream);
175        setLastEntryTime(System.currentTimeMillis());
176    }
177
178    /**
179     * Starts the report.<p>
180     */
181    public void start() {
182
183        if (m_writeHtml) {
184            try {
185                m_printStream.println(
186                    CmsWorkplace.generatePageStartExtended(
187                        OpenCms.initCmsObject(OpenCms.getDefaultUsers().getUserGuest()),
188                        OpenCms.getSystemInfo().getDefaultEncoding()));
189            } catch (CmsException e) {
190                // ignore
191            }
192        }
193    }
194
195    /**
196     * @see org.opencms.report.CmsHtmlReport#getLineBreak()
197     */
198    @Override
199    protected String getLineBreak() {
200
201        return LINEBREAK;
202    }
203}