001: /*
002: * File : $Source: /usr/local/cvs/opencms/src/org/opencms/report/A_CmsReport.java,v $
003: * Date : $Date: 2008-02-27 12:05:41 $
004: * Version: $Revision: 1.25 $
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.file.CmsRequestContext;
035: import org.opencms.i18n.CmsMessageContainer;
036: import org.opencms.i18n.CmsMessages;
037: import org.opencms.util.CmsStringUtil;
038:
039: import java.util.ArrayList;
040: import java.util.List;
041: import java.util.Locale;
042:
043: /**
044: * Base report class.<p>
045: *
046: * @author Alexander Kandzior
047: * @author Thomas Weckert
048: * @author Jan Baudisch
049: * @author Peter Bonrad
050: *
051: * @version $Revision: 1.25 $
052: *
053: * @since 6.0.0
054: */
055: public abstract class A_CmsReport implements I_CmsReport {
056:
057: /** Contains all error messages generated by the report. */
058: private List m_errors = new ArrayList();
059:
060: /** The locale this report is written in. */
061: private Locale m_locale;
062:
063: /** The default report message bundle. */
064: private CmsMessages m_messages;
065:
066: /** The original site root of the user who started this report. */
067: private String m_siteRoot;
068:
069: /** Runtime of the report. */
070: private long m_starttime;
071:
072: /** Contains all warning messages generated by the report. */
073: private List m_warnings = new ArrayList();
074:
075: /**
076: * @see org.opencms.report.I_CmsReport#addError(java.lang.Object)
077: */
078: public void addError(Object obj) {
079:
080: m_errors.add(obj);
081: }
082:
083: /**
084: * @see org.opencms.report.I_CmsReport#addWarning(java.lang.Object)
085: */
086: public void addWarning(Object obj) {
087:
088: m_warnings.add(obj);
089: }
090:
091: /**
092: * @see org.opencms.report.I_CmsReport#formatRuntime()
093: */
094: public String formatRuntime() {
095:
096: return CmsStringUtil.formatRuntime(getRuntime());
097: }
098:
099: /**
100: * @see org.opencms.report.I_CmsReport#getErrors()
101: */
102: public List getErrors() {
103:
104: return m_errors;
105: }
106:
107: /**
108: * @see org.opencms.report.I_CmsReport#getLocale()
109: */
110: public Locale getLocale() {
111:
112: return m_locale;
113: }
114:
115: /**
116: * @see org.opencms.report.I_CmsReport#getRuntime()
117: */
118: public long getRuntime() {
119:
120: return System.currentTimeMillis() - m_starttime;
121: }
122:
123: /**
124: * Returns the original site root of the user who started this report,
125: * or <code>null</code> if the original site root has not been set.<p>
126: *
127: * @return the original site root of the user who started this report
128: */
129: public String getSiteRoot() {
130:
131: return m_siteRoot;
132: }
133:
134: /**
135: * @see org.opencms.report.I_CmsReport#getWarnings()
136: */
137: public List getWarnings() {
138:
139: return m_warnings;
140: }
141:
142: /**
143: * @see org.opencms.report.I_CmsReport#hasError()
144: */
145: public boolean hasError() {
146:
147: return (m_errors.size() > 0);
148: }
149:
150: /**
151: * @see org.opencms.report.I_CmsReport#hasWarning()
152: */
153: public boolean hasWarning() {
154:
155: return (m_warnings.size() > 0);
156: }
157:
158: /**
159: * @see org.opencms.report.I_CmsReport#print(org.opencms.i18n.CmsMessageContainer)
160: */
161: public void print(CmsMessageContainer container) {
162:
163: print(container.key(getLocale()), FORMAT_DEFAULT);
164: }
165:
166: /**
167: * @see org.opencms.report.I_CmsReport#print(org.opencms.i18n.CmsMessageContainer, int)
168: */
169: public void print(CmsMessageContainer container, int format) {
170:
171: print(container.key(getLocale()), format);
172: }
173:
174: /**
175: * @see org.opencms.report.I_CmsReport#println(org.opencms.i18n.CmsMessageContainer)
176: */
177: public void println(CmsMessageContainer container) {
178:
179: println(container.key(getLocale()), FORMAT_DEFAULT);
180: }
181:
182: /**
183: * @see org.opencms.report.I_CmsReport#println(org.opencms.i18n.CmsMessageContainer, int)
184: */
185: public void println(CmsMessageContainer container, int format) {
186:
187: println(container.key(getLocale()), format);
188: }
189:
190: /**
191: * @see org.opencms.report.I_CmsReport#printMessageWithParam(org.opencms.i18n.CmsMessageContainer,Object)
192: */
193: public void printMessageWithParam(CmsMessageContainer container,
194: Object param) {
195:
196: print(container, I_CmsReport.FORMAT_NOTE);
197: print(Messages.get().container(Messages.RPT_ARGUMENT_1, param));
198: print(Messages.get().container(Messages.RPT_DOTS_0));
199: }
200:
201: /**
202: * @see org.opencms.report.I_CmsReport#printMessageWithParam(int,int,org.opencms.i18n.CmsMessageContainer,Object)
203: */
204: public void printMessageWithParam(int m, int n,
205: CmsMessageContainer container, Object param) {
206:
207: print(Messages.get().container(Messages.RPT_SUCCESSION_2,
208: String.valueOf(m), String.valueOf(n)),
209: I_CmsReport.FORMAT_NOTE);
210: printMessageWithParam(container, param);
211: }
212:
213: /**
214: * Removes the report site root prefix from the absolute path in the resource name,
215: * that is adjusts the resource name for the report site root.<p>
216: *
217: * If the site root for this report has not been set,
218: * or the resource name does not start with the report site root,
219: * the name it is left untouched.<p>
220: *
221: * @param resourcename the resource name (full path)
222: *
223: * @return the resource name adjusted for the report site root
224: *
225: * @see CmsRequestContext#removeSiteRoot(String)
226: */
227: public String removeSiteRoot(String resourcename) {
228:
229: if (m_siteRoot == null) {
230: // site root has not been set
231: return resourcename;
232: }
233:
234: String siteRoot = CmsRequestContext.getAdjustedSiteRoot(
235: m_siteRoot, resourcename);
236: if ((siteRoot.equals(m_siteRoot))
237: && resourcename.startsWith(siteRoot)) {
238: resourcename = resourcename.substring(siteRoot.length());
239: }
240: return resourcename;
241: }
242:
243: /**
244: * @see org.opencms.report.I_CmsReport#resetRuntime()
245: */
246: public void resetRuntime() {
247:
248: m_starttime = System.currentTimeMillis();
249: }
250:
251: /**
252: * Returns the default report message bundle.<p>
253: *
254: * @return the default report message bundle
255: */
256: protected CmsMessages getMessages() {
257:
258: return m_messages;
259: }
260:
261: /**
262: * Initializes some member variables for this report.<p>
263: *
264: * @param locale the locale for this report
265: * @param siteRoot the site root of the user who started this report (may be <code>null</code>)
266: */
267: protected void init(Locale locale, String siteRoot) {
268:
269: m_starttime = System.currentTimeMillis();
270: m_locale = locale;
271: m_siteRoot = siteRoot;
272: m_messages = Messages.get().getBundle(locale);
273: }
274:
275: /**
276: * Prints a String to the report.<p>
277: *
278: * @param value the String to add
279: */
280: protected void print(String value) {
281:
282: print(value, FORMAT_DEFAULT);
283: }
284:
285: /**
286: * Prints a String to the report, using the indicated formatting.<p>
287: *
288: * Use the contants starting with <code>FORMAT</code> from this interface
289: * to indicate which formatting to use.<p>
290: *
291: * @param value the message container to add
292: * @param format the formatting to use for the output
293: */
294: protected abstract void print(String value, int format);
295:
296: /**
297: * Prints a String with line break to the report.<p>
298: *
299: * @param value the message container to add
300: */
301: protected void println(String value) {
302:
303: println(value, FORMAT_DEFAULT);
304: }
305:
306: /**
307: * Prints a String with line break to the report, using the indicated formatting.<p>
308: *
309: * Use the contants starting with <code>C_FORMAT</code> from this interface
310: * to indicate which formatting to use.<p>
311: *
312: * @param value the String to add
313: * @param format the formatting to use for the output
314: */
315: protected void println(String value, int format) {
316:
317: print(value, format);
318: println();
319: }
320: }
|