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.publish;
029
030import org.opencms.i18n.CmsMessageContainer;
031import org.opencms.report.CmsHtmlReport;
032import org.opencms.report.CmsPrintStreamReport;
033import org.opencms.report.I_CmsReport;
034import org.opencms.report.I_CmsReportUpdateFormatter;
035import org.opencms.util.CmsStringUtil;
036
037import java.io.ByteArrayOutputStream;
038import java.io.IOException;
039import java.io.PrintStream;
040import java.util.List;
041import java.util.Locale;
042
043/**
044 * Report class used for the publish operations.<p>
045 *
046 * It stores nothing. It just prints everything to a temporary file.<p>
047 *
048 * @since 6.5.5
049 */
050public class CmsPublishReport extends CmsPrintStreamReport {
051
052    /** The output stream. */
053    protected ByteArrayOutputStream m_outputStream;
054
055    /** The busy flag to prevent duplicated output. */
056    private boolean m_busy;
057
058    /** The original report. */
059    private I_CmsReport m_report;
060
061    /**
062     * Constructs a new publish report using the provided locale for the output language.<p>
063     *
064     * @param locale the locale to use for the output language
065     *
066     */
067    protected CmsPublishReport(Locale locale) {
068
069        this(new ByteArrayOutputStream(), locale);
070    }
071
072    /**
073     * Constructs a new publish report using the provided locale for the output language.<p>
074     *
075     * @param outputStream the underlying byte array output stream
076     * @param locale the locale to use for the output language
077     *
078     */
079    private CmsPublishReport(ByteArrayOutputStream outputStream, Locale locale) {
080
081        super(new PrintStream(outputStream), locale, true);
082        init(locale, null);
083        m_outputStream = outputStream;
084    }
085
086    /**
087     * Constructs a new publish report decorating the provided report.<p>
088     *
089     * @param report the report to decorate
090     */
091    private CmsPublishReport(I_CmsReport report) {
092
093        this(new ByteArrayOutputStream(), report.getLocale());
094        m_report = report;
095        if (report instanceof CmsHtmlReport) {
096            if (((CmsHtmlReport)report).isWriteHtml()) {
097                try {
098                    m_outputStream.write(CmsStringUtil.substitute(getReportUpdate(), "\\n", "").getBytes());
099                } catch (IOException e) {
100                    // ignore
101                }
102            }
103        }
104    }
105
106    /**
107     * Returns a publish report instance that writes to the given report as well as
108     * to the given temporary file.<p>
109     *
110     * @param report the report to decorate
111     *
112     * @return the publish report
113     */
114    protected static CmsPrintStreamReport decorate(final I_CmsReport report) {
115
116        return new CmsPublishReport(report);
117    }
118
119    /**
120     * @see org.opencms.report.A_CmsReport#addError(java.lang.Object)
121     */
122    @Override
123    public void addError(Object obj) {
124
125        if (!m_busy && (m_report != null)) {
126            m_report.addError(obj);
127        }
128        m_busy = true;
129        super.addError(obj);
130        m_busy = false;
131    }
132
133    /**
134     * @see org.opencms.report.A_CmsReport#addWarning(java.lang.Object)
135     */
136    @Override
137    public void addWarning(Object obj) {
138
139        if (!m_busy && (m_report != null)) {
140            m_report.addWarning(obj);
141        }
142        m_busy = true;
143        super.addWarning(obj);
144        m_busy = false;
145    }
146
147    /**
148     * Returns the contents of the publish report as byte array.<p>
149     *
150     * @return the contents of the publish report
151     */
152    public byte[] getContents() {
153
154        return m_outputStream.toByteArray();
155    }
156
157    /**
158     * @see org.opencms.report.A_CmsReport#getErrors()
159     */
160    @Override
161    public List<Object> getErrors() {
162
163        if (m_report != null) {
164            return m_report.getErrors();
165        }
166        return super.getErrors();
167    }
168
169    /**
170     * @see org.opencms.report.CmsPrintStreamReport#getReportUpdate()
171     */
172    @Override
173    public synchronized String getReportUpdate() {
174
175        if (m_report != null) {
176            return m_report.getReportUpdate();
177        }
178        return super.getReportUpdate();
179    }
180
181    /**
182     * @see org.opencms.report.A_CmsReport#getReportUpdate(org.opencms.report.I_CmsReportUpdateFormatter)
183     */
184    @Override
185    public synchronized String getReportUpdate(I_CmsReportUpdateFormatter formatter) {
186
187        if (m_report != null) {
188            return m_report.getReportUpdate(formatter);
189        }
190        return super.getReportUpdate(formatter);
191    }
192
193    /**
194     * @see org.opencms.report.A_CmsReport#getWarnings()
195     */
196    @Override
197    public List<Object> getWarnings() {
198
199        if (m_report != null) {
200            return m_report.getWarnings();
201        }
202        return super.getWarnings();
203    }
204
205    /**
206     * @see org.opencms.report.A_CmsReport#print(org.opencms.i18n.CmsMessageContainer)
207     */
208    @Override
209    public void print(CmsMessageContainer container) {
210
211        if (!m_busy && (m_report != null)) {
212            m_report.print(container);
213        }
214        m_busy = true;
215        super.print(container);
216        m_busy = false;
217    }
218
219    /**
220     * @see org.opencms.report.A_CmsReport#print(org.opencms.i18n.CmsMessageContainer, int)
221     */
222    @Override
223    public void print(CmsMessageContainer container, int format) {
224
225        if (!m_busy && (m_report != null)) {
226            m_report.print(container, format);
227        }
228        m_busy = true;
229        super.print(container, format);
230        m_busy = false;
231    }
232
233    /**
234     * @see org.opencms.report.CmsPrintStreamReport#println()
235     */
236    @Override
237    public synchronized void println() {
238
239        if (!m_busy && (m_report != null)) {
240            m_report.println();
241        }
242        m_busy = true;
243        super.println();
244        m_busy = false;
245    }
246
247    /**
248     * @see org.opencms.report.A_CmsReport#println(org.opencms.i18n.CmsMessageContainer)
249     */
250    @Override
251    public void println(CmsMessageContainer container) {
252
253        if (!m_busy && (m_report != null)) {
254            m_report.println(container);
255        }
256        m_busy = true;
257        super.println(container);
258        m_busy = false;
259    }
260
261    /**
262     * @see org.opencms.report.A_CmsReport#println(org.opencms.i18n.CmsMessageContainer, int)
263     */
264    @Override
265    public void println(CmsMessageContainer container, int format) {
266
267        if (!m_busy && (m_report != null)) {
268            m_report.println(container, format);
269        }
270        m_busy = true;
271        super.println(container, format);
272        m_busy = false;
273    }
274
275    /**
276     * @see org.opencms.report.CmsPrintStreamReport#println(java.lang.Throwable)
277     */
278    @Override
279    public synchronized void println(Throwable t) {
280
281        if (!m_busy && (m_report != null)) {
282            m_report.println(t);
283        }
284        m_busy = true;
285        super.println(t);
286        m_busy = false;
287    }
288
289    /**
290     * @see org.opencms.report.A_CmsReport#printMessageWithParam(org.opencms.i18n.CmsMessageContainer, java.lang.Object)
291     */
292    @Override
293    public void printMessageWithParam(CmsMessageContainer container, Object param) {
294
295        if (!m_busy && (m_report != null)) {
296            m_report.printMessageWithParam(container, param);
297        }
298        m_busy = true;
299        super.printMessageWithParam(container, param);
300        m_busy = false;
301    }
302
303    /**
304     * @see org.opencms.report.A_CmsReport#printMessageWithParam(int, int, org.opencms.i18n.CmsMessageContainer, java.lang.Object)
305     */
306    @Override
307    public void printMessageWithParam(int m, int n, CmsMessageContainer container, Object param) {
308
309        if (!m_busy && (m_report != null)) {
310            m_report.printMessageWithParam(m, n, container, param);
311        }
312        m_busy = true;
313        super.printMessageWithParam(m, n, container, param);
314        m_busy = false;
315    }
316
317    /**
318     * @see org.opencms.report.A_CmsReport#resetRuntime()
319     */
320    @Override
321    public void resetRuntime() {
322
323        if (!m_busy && (m_report != null)) {
324            m_report.resetRuntime();
325        }
326        m_busy = true;
327        super.resetRuntime();
328        m_busy = false;
329    }
330}