001/*
002 * This library is part of OpenCms -
003 * the Open Source Content Management System
004 *
005 * Copyright (C) Alkacon Software (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.i18n;
029
030import org.opencms.file.CmsFile;
031import org.opencms.file.CmsObject;
032import org.opencms.file.CmsProperty;
033import org.opencms.file.CmsPropertyDefinition;
034import org.opencms.file.CmsResource;
035import org.opencms.main.CmsLog;
036import org.opencms.main.OpenCms;
037
038import java.io.ByteArrayInputStream;
039import java.io.InputStreamReader;
040import java.util.Locale;
041import java.util.Map;
042import java.util.Properties;
043
044import org.apache.commons.logging.Log;
045
046import com.google.common.collect.Maps;
047
048/**
049 * Loads message bundles from .properties files in the VFS.<p>
050 *
051 * The paths of the properties files are formed from the base path in the bundle parameters, the locale, and the .properties suffix.
052 */
053public class CmsVfsBundleLoaderProperties implements CmsVfsResourceBundle.I_Loader {
054
055    /** Logger instance for this class. */
056    private static final Log LOG = CmsLog.getLog(CmsVfsBundleLoaderProperties.class);
057
058    /**
059     * @see org.opencms.i18n.CmsVfsResourceBundle.I_Loader#loadData(org.opencms.file.CmsObject, org.opencms.i18n.CmsVfsBundleParameters)
060     */
061    public Map<Locale, Map<String, String>> loadData(CmsObject cms, CmsVfsBundleParameters params) throws Exception {
062
063        CmsFile file = cms.readFile(params.getBasePath());
064        String encoding = getEncoding(cms, file);
065        Properties props = new Properties();
066        // we do the decoding by ourselves using the encoding set on the resource, so we are not restricted to ISO 8859-1
067        props.load(new InputStreamReader(new ByteArrayInputStream(file.getContents()), encoding));
068        Map<String, String> messages = Maps.newHashMap();
069        for (Map.Entry<Object, Object> entry : props.entrySet()) {
070            messages.put((String)entry.getKey(), (String)entry.getValue());
071        }
072        Map<Locale, Map<String, String>> result = Maps.newHashMap();
073        result.put(params.getLocale(), messages);
074        return result;
075    }
076
077    /**
078     * Gets the encoding which should be used to read the properties file.<p>
079     *
080     * @param cms the CMS context to use
081     * @param res the resource for which we want the encoding
082     *
083     * @return the encoding value
084     */
085    private String getEncoding(CmsObject cms, CmsResource res) {
086
087        String defaultEncoding = OpenCms.getSystemInfo().getDefaultEncoding();
088        try {
089            CmsProperty encProp = cms.readPropertyObject(res, CmsPropertyDefinition.PROPERTY_CONTENT_ENCODING, true);
090            String encoding = encProp.getValue(defaultEncoding);
091            return encoding;
092        } catch (Exception e) {
093            LOG.warn(e.getLocalizedMessage(), e);
094            return defaultEncoding;
095        }
096    }
097
098}