001: /*
002: * File : $Source: /usr/local/cvs/opencms/src/org/opencms/report/CmsHtmlReport.java,v $
003: * Date : $Date: 2008-02-27 12:05:41 $
004: * Version: $Revision: 1.39 $
005: *
006: * This library is part of OpenCms -
007: * the Open Source Content Management System
008: *
009: * Copyright (c) 2002 - 2008 Alkacon Software GmbH (http://www.alkacon.com)
010: *
011: * This library is free software; you can redistribute it and/or
012: * modify it under the terms of the GNU Lesser General Public
013: * License as published by the Free Software Foundation; either
014: * version 2.1 of the License, or (at your option) any later version.
015: *
016: * This library is distributed in the hope that it will be useful,
017: * but WITHOUT ANY WARRANTY; without even the implied warranty of
018: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
019: * Lesser General Public License for more details.
020: *
021: * For further information about Alkacon Software GmbH, please see the
022: * company website: http://www.alkacon.com
023: *
024: * For further information about OpenCms, please see the
025: * project website: http://www.opencms.org
026: *
027: * You should have received a copy of the GNU Lesser General Public
028: * License along with this library; if not, write to the Free Software
029: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
030: */
031:
032: package org.opencms.report;
033:
034: import org.opencms.i18n.CmsEncoder;
035: import org.opencms.main.CmsException;
036: import org.opencms.util.CmsStringUtil;
037:
038: import java.util.ArrayList;
039: import java.util.List;
040: import java.util.Locale;
041: import java.util.StringTokenizer;
042:
043: /**
044: * HTML report output to be used for import / export / publish operations
045: * in the entire OpenCms system.<p>
046: *
047: * @author Alexander Kandzior
048: * @author Thomas Weckert
049: * @author Jan Baudisch
050: *
051: * @version $Revision: 1.39 $
052: *
053: * @since 6.0.0
054: */
055: public class CmsHtmlReport extends A_CmsReport {
056:
057: /** Constant for a HTML linebreak with added "real" line break. */
058: protected static final String LINEBREAK = "<br>";
059:
060: /**
061: * Constant for a HTML linebreak with added "real" line break-
062: * traditional style for report threads that still use XML templates for their output.
063: */
064: protected static final String LINEBREAK_TRADITIONAL = "<br>\\n";
065:
066: /** The list of report objects e.g. String, CmsPageLink, Exception ... */
067: private List m_content;
068:
069: /**
070: * Counter to remember what is already shown,
071: * indicates the next index of the m_content list that has to be reported.
072: */
073: private int m_indexNext;
074:
075: /** Flag to indicate if an exception should be displayed long or short. */
076: private boolean m_showExceptionStackTrace;
077:
078: /** If set to <code>true</code> nothing is kept in memory. */
079: private boolean m_transient;
080:
081: /** Boolean flag indicating whether this report should generate HTML or JavaScript output. */
082: private boolean m_writeHtml;
083:
084: /**
085: * Constructs a new report using the provided locale for the output language.<p>
086: *
087: * @param locale the locale to use for the output language
088: * @param siteRoot the site root of the user who started this report (may be <code>null</code>)
089: */
090: public CmsHtmlReport(Locale locale, String siteRoot) {
091:
092: this (locale, siteRoot, false, false);
093: }
094:
095: /**
096: * Constructs a new report using the provided locale for the output language.<p>
097: *
098: * @param locale the locale to use for the output language
099: * @param siteRoot the site root of the user who started this report (may be <code>null</code>)
100: * @param writeHtml if <code>true</code>, this report should generate HTML instead of JavaScript output
101: * @param isTransient If set to <code>true</code> nothing is kept in memory
102: */
103: public CmsHtmlReport(Locale locale, String siteRoot,
104: boolean writeHtml, boolean isTransient) {
105:
106: init(locale, siteRoot);
107: m_content = new ArrayList(256);
108: m_showExceptionStackTrace = true;
109: m_writeHtml = writeHtml;
110: m_transient = isTransient;
111: }
112:
113: /**
114: * @see org.opencms.report.I_CmsReport#getReportUpdate()
115: */
116: public synchronized String getReportUpdate() {
117:
118: StringBuffer result = new StringBuffer();
119: int indexEnd = m_content.size();
120: for (int i = m_indexNext; i < indexEnd; i++) {
121: int pos = m_transient ? 0 : i;
122: Object obj = m_content.get(pos);
123: if ((obj instanceof String)
124: || (obj instanceof StringBuffer)) {
125: result.append(obj);
126: } else if (obj instanceof Throwable) {
127: result.append(getExceptionElement((Throwable) obj));
128: }
129: if (m_transient) {
130: m_content.remove(m_indexNext);
131: }
132: }
133: m_indexNext = m_transient ? 0 : indexEnd;
134: return result.toString();
135: }
136:
137: /**
138: * Returns if the report writes html or javascript code.<p>
139: *
140: * @return <code>true</code> if the report writes html, and <code>false</code> if the report writes javascript code
141: */
142: public boolean isWriteHtml() {
143:
144: return m_writeHtml;
145: }
146:
147: /**
148: * @see org.opencms.report.A_CmsReport#print(java.lang.String, int)
149: */
150: public synchronized void print(String value, int format) {
151:
152: StringBuffer buf = null;
153:
154: if (!m_writeHtml) {
155: value = CmsStringUtil.escapeJavaScript(value);
156: switch (format) {
157: case FORMAT_HEADLINE:
158: buf = new StringBuffer();
159: buf.append("aH('");
160: buf.append(value);
161: buf.append("'); ");
162: break;
163: case FORMAT_WARNING:
164: buf = new StringBuffer();
165: buf.append("aW('");
166: buf.append(value);
167: buf.append("'); ");
168: addWarning(value);
169: break;
170: case FORMAT_ERROR:
171: buf = new StringBuffer();
172: buf.append("aE('");
173: buf.append(value);
174: buf.append("'); ");
175: addError(value);
176: break;
177: case FORMAT_NOTE:
178: buf = new StringBuffer();
179: buf.append("aN('");
180: buf.append(value);
181: buf.append("'); ");
182: break;
183: case FORMAT_OK:
184: buf = new StringBuffer();
185: buf.append("aO('");
186: buf.append(value);
187: buf.append("'); ");
188: break;
189: case FORMAT_DEFAULT:
190: default:
191: buf = new StringBuffer();
192: buf.append("a('");
193: buf.append(value);
194: buf.append("'); ");
195: }
196: // the output lines get split back into single lines on the client-side.
197: // thus, a separate JavaScript call has to be added here to tell the
198: // client that we want a linebreak here...
199: if (value.trim().endsWith(getLineBreak())) {
200: buf.append("aB(); ");
201: }
202: m_content.add(buf.toString());
203: } else {
204: switch (format) {
205: case FORMAT_HEADLINE:
206: buf = new StringBuffer();
207: buf.append("<span class='head'>");
208: buf.append(value);
209: buf.append("</span>");
210: break;
211: case FORMAT_WARNING:
212: buf = new StringBuffer();
213: buf.append("<span class='warn'>");
214: buf.append(value);
215: buf.append("</span>");
216: addWarning(value);
217: break;
218: case FORMAT_ERROR:
219: buf = new StringBuffer();
220: buf.append("<span class='err'>");
221: buf.append(value);
222: buf.append("</span>");
223: addError(value);
224: break;
225: case FORMAT_NOTE:
226: buf = new StringBuffer();
227: buf.append("<span class='note'>");
228: buf.append(value);
229: buf.append("</span>");
230: break;
231: case FORMAT_OK:
232: buf = new StringBuffer();
233: buf.append("<span class='ok'>");
234: buf.append(value);
235: buf.append("</span>");
236: break;
237: case FORMAT_DEFAULT:
238: default:
239: buf = new StringBuffer(value);
240: }
241: if (value.trim().endsWith(getLineBreak())) {
242: buf.append("\n");
243: }
244: m_content.add(buf.toString());
245: }
246: }
247:
248: /**
249: * @see org.opencms.report.I_CmsReport#println()
250: */
251: public void println() {
252:
253: print(getLineBreak());
254: }
255:
256: /**
257: * @see org.opencms.report.I_CmsReport#println(java.lang.Throwable)
258: */
259: public synchronized void println(Throwable t) {
260:
261: addError(t.getMessage());
262: m_content.add(t);
263: }
264:
265: /**
266: * Returns the correct line break notation depending on the output style of this report.
267: *
268: * @return the correct line break notation
269: */
270: protected String getLineBreak() {
271:
272: return m_writeHtml ? LINEBREAK_TRADITIONAL : LINEBREAK;
273: }
274:
275: /**
276: * Output helper method to format a reported <code>Throwable</code> element.<p>
277: *
278: * This method ensures that exception stack traces are properly escaped
279: * when they are added to the report.<p>
280: *
281: * There is a member variable {@link #m_showExceptionStackTrace} in this
282: * class that controls if the stack track is shown or not.
283: * In a later version this might be configurable on a per-user basis.<p>
284: *
285: * @param throwable the exception to format
286: * @return the formatted StringBuffer
287: */
288: private StringBuffer getExceptionElement(Throwable throwable) {
289:
290: StringBuffer buf = new StringBuffer(256);
291:
292: if (!m_writeHtml) {
293: if (m_showExceptionStackTrace) {
294: buf.append("aT('");
295: buf.append(getMessages().key(Messages.RPT_EXCEPTION_0));
296: String exception = CmsEncoder.escapeXml(CmsException
297: .getStackTraceAsString(throwable));
298: StringBuffer excBuffer = new StringBuffer(exception
299: .length() + 50);
300: StringTokenizer tok = new StringTokenizer(exception,
301: "\r\n");
302: while (tok.hasMoreTokens()) {
303: excBuffer.append(tok.nextToken());
304: excBuffer.append(getLineBreak());
305: }
306: buf.append(CmsStringUtil.escapeJavaScript(excBuffer
307: .toString()));
308: buf.append("'); ");
309: } else {
310: buf.append("aT('");
311: buf.append(getMessages().key(Messages.RPT_EXCEPTION_0));
312: buf.append(CmsStringUtil.escapeJavaScript(throwable
313: .toString()));
314: buf.append("'); ");
315: }
316: m_content.add(buf);
317: } else {
318: if (m_showExceptionStackTrace) {
319: buf.append("<span class='throw'>");
320: buf.append(getMessages().key(Messages.RPT_EXCEPTION_0));
321: String exception = CmsEncoder.escapeXml(CmsException
322: .getStackTraceAsString(throwable));
323: StringBuffer excBuffer = new StringBuffer(exception
324: .length() + 50);
325: StringTokenizer tok = new StringTokenizer(exception,
326: "\r\n");
327: while (tok.hasMoreTokens()) {
328: excBuffer.append(tok.nextToken());
329: excBuffer.append(getLineBreak());
330: }
331: buf.append(excBuffer.toString());
332: buf.append("</span>");
333: } else {
334: buf.append("<span class='throw'>");
335: buf.append(getMessages().key(Messages.RPT_EXCEPTION_0));
336: buf.append(throwable.toString());
337: buf.append("</span>");
338: buf.append(getLineBreak());
339: }
340: }
341: return buf;
342: }
343: }
|