KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > opencms > report > CmsHtmlReport


1 /*
2  * File : $Source: /usr/local/cvs/opencms/src/org/opencms/report/CmsHtmlReport.java,v $
3  * Date : $Date: 2006/10/04 07:35:21 $
4  * Version: $Revision: 1.34 $
5  *
6  * This library is part of OpenCms -
7  * the Open Source Content Mananagement System
8  *
9  * Copyright (c) 2005 Alkacon Software GmbH (http://www.alkacon.com)
10  *
11  * This library is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU Lesser General Public
13  * License as published by the Free Software Foundation; either
14  * version 2.1 of the License, or (at your option) any later version.
15  *
16  * This library is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19  * Lesser General Public License for more details.
20  *
21  * For further information about Alkacon Software GmbH, please see the
22  * company website: http://www.alkacon.com
23  *
24  * For further information about OpenCms, please see the
25  * project website: http://www.opencms.org
26  *
27  * You should have received a copy of the GNU Lesser General Public
28  * License along with this library; if not, write to the Free Software
29  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
30  */

31
32 package org.opencms.report;
33
34 import org.opencms.i18n.CmsEncoder;
35 import org.opencms.main.CmsException;
36 import org.opencms.util.CmsStringUtil;
37
38 import java.util.ArrayList JavaDoc;
39 import java.util.List JavaDoc;
40 import java.util.Locale JavaDoc;
41 import java.util.StringTokenizer JavaDoc;
42
43 /**
44  * HTML report output to be used for import / export / publish operations
45  * in the entire OpenCms system.<p>
46  *
47  * @author Alexander Kandzior
48  * @author Thomas Weckert
49  * @author Jan Baudisch
50  *
51  * @version $Revision: 1.34 $
52  *
53  * @since 6.0.0
54  */

55 public class CmsHtmlReport extends A_CmsReport {
56
57     /** Constant for a HTML linebreak with added "real" line break. */
58     private static final String JavaDoc LINEBREAK = "<br>";
59
60     /**
61      * Constant for a HTML linebreak with added "real" line break-
62      * traditional style for report threads that still use XML templates for their output.
63      */

64     private static final String JavaDoc LINEBREAK_TRADITIONAL = "<br>\\n";
65
66     /** The list of report objects e.g. String, CmsPageLink, Exception ... */
67     private List JavaDoc m_content;
68
69     /**
70      * Counter to remember what is already shown,
71      * indicates the next index of the m_content list that has to be reported.
72      */

73     private int m_indexNext;
74
75     /** Flag to indicate if an exception should be displayed long or short. */
76     private boolean m_showExceptionStackTracke;
77
78     /** Boolean flag indicating whether this report should generate HTML or JavaScript output. */
79     private boolean m_writeHtml;
80
81     /**
82      * Constructs a new report using the provided locale for the output language.<p>
83      *
84      * @param locale the locale to use for the output language
85      * @param siteRoot the site root of the user who started this report (may be <code>null</code>)
86      */

87     public CmsHtmlReport(Locale JavaDoc locale, String JavaDoc siteRoot) {
88
89         this(locale, siteRoot, false);
90     }
91
92     /**
93      * Constructs a new report using the provided locale for the output language.<p>
94      *
95      * @param locale the locale to use for the output language
96      * @param siteRoot the site root of the user who started this report (may be <code>null</code>)
97      * @param writeHtml if <code>true</code>, this report should generate HTML instead of JavaScript output
98      */

99     protected CmsHtmlReport(Locale JavaDoc locale, String JavaDoc siteRoot, boolean writeHtml) {
100
101         init(locale, siteRoot);
102         m_content = new ArrayList JavaDoc(256);
103         m_showExceptionStackTracke = true;
104         m_writeHtml = writeHtml;
105     }
106
107     /**
108      * @see org.opencms.report.I_CmsReport#getReportUpdate()
109      */

110     public synchronized String JavaDoc getReportUpdate() {
111
112         StringBuffer JavaDoc result = new StringBuffer JavaDoc();
113         int indexEnd = m_content.size();
114         for (int i = m_indexNext; i < indexEnd; i++) {
115             Object JavaDoc obj = m_content.get(i);
116             if (obj instanceof String JavaDoc || obj instanceof StringBuffer JavaDoc) {
117                 result.append(obj);
118             } else if (obj instanceof Throwable JavaDoc) {
119                 result.append(getExceptionElement((Throwable JavaDoc)obj));
120             }
121         }
122         m_indexNext = indexEnd;
123
124         return result.toString();
125     }
126
127     /**
128      * @see org.opencms.report.A_CmsReport#print(java.lang.String, int)
129      */

130     public synchronized void print(String JavaDoc value, int format) {
131
132         value = CmsStringUtil.escapeJavaScript(value);
133         StringBuffer JavaDoc buf;
134
135         if (!m_writeHtml) {
136             switch (format) {
137                 case FORMAT_HEADLINE:
138                     buf = new StringBuffer JavaDoc();
139                     buf.append("aH('");
140                     buf.append(value);
141                     buf.append("'); ");
142                     m_content.add(buf);
143                     break;
144                 case FORMAT_WARNING:
145                     buf = new StringBuffer JavaDoc();
146                     buf.append("aW('");
147                     buf.append(value);
148                     buf.append("'); ");
149                     m_content.add(buf);
150                     addWarning(value);
151                     break;
152                 case FORMAT_ERROR:
153                     buf = new StringBuffer JavaDoc();
154                     buf.append("aE('");
155                     buf.append(value);
156                     buf.append("'); ");
157                     m_content.add(buf);
158                     addError(value);
159                     break;
160                 case FORMAT_NOTE:
161                     buf = new StringBuffer JavaDoc();
162                     buf.append("aN('");
163                     buf.append(value);
164                     buf.append("'); ");
165                     m_content.add(buf);
166                     break;
167                 case FORMAT_OK:
168                     buf = new StringBuffer JavaDoc();
169                     buf.append("aO('");
170                     buf.append(value);
171                     buf.append("'); ");
172                     m_content.add(buf);
173                     break;
174                 case FORMAT_DEFAULT:
175                 default:
176                     buf = new StringBuffer JavaDoc();
177                     buf.append("a('");
178                     buf.append(value);
179                     buf.append("'); ");
180                     m_content.add(buf);
181             }
182
183             // the output lines get split back into single lines on the client-side.
184
// thus, a separate JavaScript call has to be added here to tell the
185
// client that we want a linebreak here...
186
if (value.trim().endsWith(getLineBreak())) {
187                 buf.append("aB(); ");
188             }
189         } else {
190             // TODO remove this code when all reports are switched from XML templates to JSP pages
191
switch (format) {
192                 case FORMAT_HEADLINE:
193                     buf = new StringBuffer JavaDoc();
194                     buf.append("<span class='head'>");
195                     buf.append(value);
196                     buf.append("</span>");
197                     m_content.add(buf);
198                     break;
199                 case FORMAT_WARNING:
200                     buf = new StringBuffer JavaDoc();
201                     buf.append("<span class='warn'>");
202                     buf.append(value);
203                     buf.append("</span>");
204                     m_content.add(buf);
205                     addWarning(value);
206                     break;
207                 case FORMAT_ERROR:
208                     buf = new StringBuffer JavaDoc();
209                     buf.append("<span class='err'>");
210                     buf.append(value);
211                     buf.append("</span>");
212                     m_content.add(buf);
213                     addError(value);
214                     break;
215                 case FORMAT_NOTE:
216                     buf = new StringBuffer JavaDoc();
217                     buf.append("<span class='note'>");
218                     buf.append(value);
219                     buf.append("</span>");
220                     m_content.add(buf);
221                     break;
222                 case FORMAT_OK:
223                     buf = new StringBuffer JavaDoc();
224                     buf.append("<span class='ok'>");
225                     buf.append(value);
226                     buf.append("</span>");
227                     m_content.add(buf);
228                     break;
229                 case FORMAT_DEFAULT:
230                 default:
231                     m_content.add(value);
232             }
233         }
234     }
235
236     /**
237      * @see org.opencms.report.I_CmsReport#println()
238      */

239     public void println() {
240
241         print(getLineBreak());
242     }
243
244     /**
245      * @see org.opencms.report.I_CmsReport#println(java.lang.Throwable)
246      */

247     public synchronized void println(Throwable JavaDoc t) {
248
249         addError(t.getMessage());
250         m_content.add(t);
251     }
252
253     /**
254      * Returns the corrent linebreak notation depending on the output style of this report.
255      *
256      * @return the corrent linebreak notation
257      */

258     protected String JavaDoc getLineBreak() {
259
260         return m_writeHtml ? LINEBREAK_TRADITIONAL : LINEBREAK;
261     }
262
263     /**
264      * Output helper method to format a reported <code>Throwable</code> element.<p>
265      *
266      * This method ensures that exception stack traces are properly escaped
267      * when they are added to the report.<p>
268      *
269      * There is a member variable {@link #m_showExceptionStackTracke} in this
270      * class that controls if the stack track is shown or not.
271      * In a later version this might be configurable on a per-user basis.<p>
272      *
273      * @param throwable the exception to format
274      * @return the formatted StringBuffer
275      */

276     private StringBuffer JavaDoc getExceptionElement(Throwable JavaDoc throwable) {
277
278         StringBuffer JavaDoc buf = new StringBuffer JavaDoc(256);
279
280         if (!m_writeHtml) {
281             if (m_showExceptionStackTracke) {
282                 buf.append("aT('");
283                 buf.append(getMessages().key(Messages.RPT_EXCEPTION_0));
284                 String JavaDoc exception = CmsEncoder.escapeXml(CmsException.getStackTraceAsString(throwable));
285                 StringBuffer JavaDoc excBuffer = new StringBuffer JavaDoc(exception.length() + 50);
286                 StringTokenizer JavaDoc tok = new StringTokenizer JavaDoc(exception, "\r\n");
287                 while (tok.hasMoreTokens()) {
288                     excBuffer.append(tok.nextToken());
289                     excBuffer.append(getLineBreak());
290                 }
291                 buf.append(CmsStringUtil.escapeJavaScript(excBuffer.toString()));
292                 buf.append("'); ");
293                 m_content.add(buf);
294             } else {
295                 buf.append("aT('");
296                 buf.append(getMessages().key(Messages.RPT_EXCEPTION_0));
297                 buf.append(CmsStringUtil.escapeJavaScript(throwable.toString()));
298                 buf.append("'); ");
299                 m_content.add(buf);
300             }
301         } else {
302             if (m_showExceptionStackTracke) {
303                 buf.append("<span class='throw'>");
304                 buf.append(getMessages().key(Messages.RPT_EXCEPTION_0));
305                 String JavaDoc exception = CmsEncoder.escapeXml(CmsException.getStackTraceAsString(throwable));
306                 StringBuffer JavaDoc excBuffer = new StringBuffer JavaDoc(exception.length() + 50);
307                 StringTokenizer JavaDoc tok = new StringTokenizer JavaDoc(exception, "\r\n");
308                 while (tok.hasMoreTokens()) {
309                     excBuffer.append(tok.nextToken());
310                     excBuffer.append(getLineBreak());
311                 }
312                 buf.append(CmsStringUtil.escapeJavaScript(excBuffer.toString()));
313                 buf.append("</span>");
314             } else {
315                 buf.append("<span class='throw'>");
316                 buf.append(getMessages().key(Messages.RPT_EXCEPTION_0));
317                 buf.append(CmsStringUtil.escapeJavaScript(throwable.toString()));
318                 buf.append("</span>");
319                 buf.append(getLineBreak());
320             }
321         }
322
323         return buf;
324     }
325 }
Popular Tags