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, 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.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 m_locale = locale; 059 } 060 061 /** 062 * @see org.opencms.report.I_CmsReportUpdateFormatter#formatReportUpdate(java.util.List) 063 */ 064 public String formatReportUpdate(List<CmsReportUpdateItem> updateItem) { 065 066 StringBuffer buffer = new StringBuffer(); 067 for (CmsReportUpdateItem item : updateItem) { 068 try { 069 buffer.append(formatItem(item)); 070 } catch (Exception e) { 071 LOG.error(e.getLocalizedMessage(), e); 072 } 073 } 074 return buffer.toString(); 075 } 076 077 /** 078 * Generates the formatted output for an exception.<P> 079 * 080 * @param throwable the exception 081 * 082 * @return the formatted output 083 */ 084 private String formatException(Throwable throwable) { 085 086 StringBuffer buf = new StringBuffer(); 087 buf.append("aT('"); 088 buf.append(Messages.get().getBundle(m_locale).key(Messages.RPT_EXCEPTION_0)); 089 String exception = CmsEncoder.escapeXml(CmsException.getStackTraceAsString(throwable)); 090 StringBuffer excBuffer = new StringBuffer(exception.length() + 50); 091 StringTokenizer tok = new StringTokenizer(exception, "\r\n"); 092 while (tok.hasMoreTokens()) { 093 excBuffer.append(tok.nextToken()); 094 excBuffer.append(CmsHtmlReport.LINEBREAK); 095 } 096 buf.append(CmsStringUtil.escapeJavaScript(excBuffer.toString())); 097 buf.append("'); "); 098 return buf.toString(); 099 } 100 101 /** 102 * Formats a single report item.<p> 103 * 104 * @param updateItem the report item 105 * 106 * @return the formatted output 107 */ 108 private String formatItem(CmsReportUpdateItem updateItem) { 109 110 StringBuffer buf = new StringBuffer(); 111 CmsReportFormatType format = updateItem.getType(); 112 if (format == CmsReportFormatType.fmtException) { 113 return formatException((Throwable)(updateItem.getMessage())); 114 } else { 115 String value = CmsStringUtil.escapeJavaScript((String)(updateItem.getMessage())); 116 switch (format) { 117 case fmtHeadline: 118 buf = new StringBuffer(); 119 buf.append("aH('"); 120 buf.append(value); 121 buf.append("'); "); 122 break; 123 case fmtWarning: 124 buf = new StringBuffer(); 125 buf.append("aW('"); 126 buf.append(value); 127 buf.append("'); "); 128 break; 129 case fmtError: 130 buf = new StringBuffer(); 131 buf.append("aE('"); 132 buf.append(value); 133 buf.append("'); "); 134 break; 135 case fmtNote: 136 buf = new StringBuffer(); 137 buf.append("aN('"); 138 buf.append(value); 139 buf.append("'); "); 140 break; 141 case fmtOk: 142 buf = new StringBuffer(); 143 buf.append("aO('"); 144 buf.append(value); 145 buf.append("'); "); 146 break; 147 case fmtNewline: 148 buf = new StringBuffer(); 149 buf.append("a('" + CmsHtmlReport.LINEBREAK + "');"); 150 break; 151 case fmtDefault: 152 default: 153 buf = new StringBuffer(); 154 buf.append("a('"); 155 buf.append(value); 156 buf.append("'); "); 157 } 158 159 // the output lines get split back into single lines on the client-side. 160 // thus, a separate JavaScript call has to be added here to tell the 161 // client that we want a linebreak here... 162 if (value.trim().endsWith(CmsHtmlReport.LINEBREAK)) { 163 buf.append("aB(); "); 164 } 165 } 166 return buf.toString(); 167 } 168}