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.pdftools;
029
030import org.opencms.main.CmsLog;
031
032import java.util.HashMap;
033import java.util.Map;
034import java.util.logging.Level;
035
036import org.apache.commons.logging.Log;
037
038import org.xhtmlrenderer.util.XRLog;
039import org.xhtmlrenderer.util.XRLogger;
040
041/**
042 * Adaspter for sending xhtmlrenderer (flyingsaucer) log messages to the OpenCms log.<p>
043 *
044 * The log channels to which the log messages are sent start with the prefix 'org.opencms.' so that no
045 * change in the log configuration is needed to make error messages appear in the log.<p>
046 */
047public class CmsXRLogAdapter implements XRLogger {
048
049    /** The prefix used for log channels. */
050    public static final String OCMS_PREFIX = "org.opencms.";
051
052    /** Default logger name. */
053    private static final String DEFAULT_LOGGER_NAME = "org.xhtmlrenderer.other";
054
055    /** The logger name map. */
056    private static final Map<Object, String> LOGGER_NAME_MAP;
057
058    static {
059        LOGGER_NAME_MAP = new HashMap<Object, String>();
060        LOGGER_NAME_MAP.put(XRLog.CONFIG, "org.xhtmlrenderer.config");
061        LOGGER_NAME_MAP.put(XRLog.EXCEPTION, "org.xhtmlrenderer.exception");
062        LOGGER_NAME_MAP.put(XRLog.GENERAL, "org.xhtmlrenderer.general");
063        LOGGER_NAME_MAP.put(XRLog.INIT, "org.xhtmlrenderer.init");
064        LOGGER_NAME_MAP.put(XRLog.JUNIT, "org.xhtmlrenderer.junit");
065        LOGGER_NAME_MAP.put(XRLog.LOAD, "org.xhtmlrenderer.load");
066        LOGGER_NAME_MAP.put(XRLog.MATCH, "org.xhtmlrenderer.match");
067        LOGGER_NAME_MAP.put(XRLog.CASCADE, "org.xhtmlrenderer.cascade");
068        LOGGER_NAME_MAP.put(XRLog.XML_ENTITIES, "org.xhtmlrenderer.load.xmlentities");
069        LOGGER_NAME_MAP.put(XRLog.CSS_PARSE, "org.xhtmlrenderer.cssparse");
070        LOGGER_NAME_MAP.put(XRLog.LAYOUT, "org.xhtmlrenderer.layout");
071        LOGGER_NAME_MAP.put(XRLog.RENDER, "org.xhtmlrenderer.render");
072    }
073
074    /**
075     * @see org.xhtmlrenderer.util.XRLogger#log(java.lang.String, java.util.logging.Level, java.lang.String)
076     */
077    public void log(String where, Level level, String msg) {
078
079        Log log = CmsLog.getLog(getLoggerName(where));
080        sendMessageToLogger(level, msg, log, null);
081
082    }
083
084    /**
085     * @see org.xhtmlrenderer.util.XRLogger#log(java.lang.String, java.util.logging.Level, java.lang.String, java.lang.Throwable)
086     */
087    public void log(String where, Level level, String msg, Throwable th) {
088
089        Log log = CmsLog.getLog(getLoggerName(where));
090        sendMessageToLogger(level, msg, log, th);
091    }
092
093    /**
094     * @see org.xhtmlrenderer.util.XRLogger#setLevel(java.lang.String, java.util.logging.Level)
095     */
096    public void setLevel(String logger, Level level) {
097
098        throw new UnsupportedOperationException("setLevel not supported");
099    }
100
101    /**
102     * Gets the real logger name.<p>
103     *
104     * @param xrLoggerName the XR logger name
105     *
106     * @return the real logger name
107     */
108    private String getLoggerName(String xrLoggerName) {
109
110        String result = LOGGER_NAME_MAP.get(xrLoggerName);
111        if (result != null) {
112            return OCMS_PREFIX + result;
113        } else {
114            return OCMS_PREFIX + DEFAULT_LOGGER_NAME;
115        }
116    }
117
118    /**
119     * Sends a log message to the commons-logging interface.<p>
120     *
121     * @param level the log level
122     * @param message the message
123     * @param log the commons logging log object
124     * @param e a throwable or null
125     */
126    private void sendMessageToLogger(Level level, String message, Log log, Throwable e) {
127
128        if (e == null) {
129            if (level == Level.SEVERE) {
130                log.error(message);
131            } else if (level == Level.WARNING) {
132                log.warn(message);
133            } else if (level == Level.INFO) {
134                log.info(message);
135            } else if (level == Level.CONFIG) {
136                log.info(message);
137            } else if ((level == Level.FINE) || (level == Level.FINER) || (level == Level.FINEST)) {
138                log.debug(message);
139            } else {
140                log.info(message);
141            }
142        } else {
143            if (level == Level.SEVERE) {
144                log.error(message, e);
145            } else if (level == Level.WARNING) {
146                log.warn(message, e);
147            } else if (level == Level.INFO) {
148                log.info(message, e);
149            } else if (level == Level.CONFIG) {
150                log.info(message, e);
151            } else if ((level == Level.FINE) || (level == Level.FINER) || (level == Level.FINEST)) {
152                log.debug(message, e);
153            } else {
154                log.info(message, e);
155            }
156        }
157    }
158
159}