001/*
002 * This library is part of OpenCms -
003 * the Open Source Content Management System
004 *
005 * Copyright (c) Alkacon Software GmbH & Co. KG (https://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, please see the
018 * company website: https://www.alkacon.com
019 *
020 * For further information about OpenCms, please see the
021 * project website: https://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.i18n.CmsEncoder;
031import org.opencms.main.CmsException;
032import org.opencms.main.CmsLog;
033import org.opencms.util.CmsStringUtil;
034
035import java.util.List;
036import java.util.Locale;
037import java.util.StringTokenizer;
038
039import org.apache.commons.logging.Log;
040
041/**
042 * Report update formatter for the Javascript-based report update format, which was used in the old workplace most of the time.<p>
043 */
044public class CmsClassicJavascriptReportUpdateFormatter implements I_CmsReportUpdateFormatter {
045
046    /** The logger instance for this class. */
047    private static final Log LOG = CmsLog.getLog(CmsClassicJavascriptReportUpdateFormatter.class);
048
049    /** The locale. */
050    private Locale m_locale;
051
052    /**
053     * Creates a new instance.<p>
054     *
055     * @param locale the locale
056     */
057    public CmsClassicJavascriptReportUpdateFormatter(Locale locale) {
058
059        m_locale = locale;
060    }
061
062    /**
063     * @see org.opencms.report.I_CmsReportUpdateFormatter#formatReportUpdate(java.util.List)
064     */
065    public String formatReportUpdate(List<CmsReportUpdateItem> updateItem) {
066
067        StringBuffer buffer = new StringBuffer();
068        for (CmsReportUpdateItem item : updateItem) {
069            try {
070                buffer.append(formatItem(item));
071            } catch (Exception e) {
072                LOG.error(e.getLocalizedMessage(), e);
073            }
074        }
075        return buffer.toString();
076    }
077
078    /**
079     * Generates the formatted output for an exception.<P>
080     *
081     * @param throwable the exception
082     *
083     * @return the formatted output
084     */
085    private String formatException(Throwable throwable) {
086
087        StringBuffer buf = new StringBuffer();
088        buf.append("aT('");
089        buf.append(Messages.get().getBundle(m_locale).key(Messages.RPT_EXCEPTION_0));
090        String exception = CmsEncoder.escapeXml(CmsException.getStackTraceAsString(throwable));
091        StringBuffer excBuffer = new StringBuffer(exception.length() + 50);
092        StringTokenizer tok = new StringTokenizer(exception, "\r\n");
093        while (tok.hasMoreTokens()) {
094            excBuffer.append(tok.nextToken());
095            excBuffer.append(CmsHtmlReport.LINEBREAK);
096        }
097        buf.append(CmsStringUtil.escapeJavaScript(excBuffer.toString()));
098        buf.append("'); ");
099        return buf.toString();
100    }
101
102    /**
103     * Formats a single report item.<p>
104     *
105     * @param updateItem the report item
106     *
107     * @return the formatted output
108     */
109    private String formatItem(CmsReportUpdateItem updateItem) {
110
111        StringBuffer buf = new StringBuffer();
112        CmsReportFormatType format = updateItem.getType();
113        if (format == CmsReportFormatType.fmtException) {
114            return formatException((Throwable)(updateItem.getMessage()));
115        } else {
116            String value = CmsStringUtil.escapeJavaScript((String)(updateItem.getMessage()));
117            switch (format) {
118                case fmtHeadline:
119                    buf = new StringBuffer();
120                    buf.append("aH('");
121                    buf.append(value);
122                    buf.append("'); ");
123                    break;
124                case fmtWarning:
125                    buf = new StringBuffer();
126                    buf.append("aW('");
127                    buf.append(value);
128                    buf.append("'); ");
129                    break;
130                case fmtError:
131                    buf = new StringBuffer();
132                    buf.append("aE('");
133                    buf.append(value);
134                    buf.append("'); ");
135                    break;
136                case fmtNote:
137                    buf = new StringBuffer();
138                    buf.append("aN('");
139                    buf.append(value);
140                    buf.append("'); ");
141                    break;
142                case fmtOk:
143                    buf = new StringBuffer();
144                    buf.append("aO('");
145                    buf.append(value);
146                    buf.append("'); ");
147                    break;
148                case fmtNewline:
149                    buf = new StringBuffer();
150                    buf.append("a('" + CmsHtmlReport.LINEBREAK + "');");
151                    break;
152                case fmtDefault:
153                default:
154                    buf = new StringBuffer();
155                    buf.append("a('");
156                    buf.append(value);
157                    buf.append("'); ");
158            }
159
160            // the output lines get split back into single lines on the client-side.
161            // thus, a separate JavaScript call has to be added here to tell the
162            // client that we want a linebreak here...
163            if (value.trim().endsWith(CmsHtmlReport.LINEBREAK)) {
164                buf.append("aB(); ");
165            }
166        }
167        return buf.toString();
168    }
169}