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.xml.content;
029
030import org.opencms.file.CmsFile;
031import org.opencms.file.CmsObject;
032import org.opencms.i18n.CmsVfsBundleParameters;
033import org.opencms.i18n.CmsVfsResourceBundle;
034import org.opencms.xml.CmsXmlUtils;
035import org.opencms.xml.types.I_CmsXmlContentValue;
036
037import java.util.HashMap;
038import java.util.List;
039import java.util.Locale;
040import java.util.Map;
041
042import com.google.common.collect.Maps;
043
044/**
045 * Loads message bundles from the different locales of a single XML content.<p>
046 */
047public class CmsVfsBundleLoaderXml implements CmsVfsResourceBundle.I_Loader {
048
049    /** Node name. */
050    public static final String N_KEY = "Key";
051
052    /** Node name. */
053    public static final String N_MESSAGE = "Message";
054
055    /** Node name. */
056    public static final String N_VALUE = "Value";
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        CmsXmlContent content = CmsXmlContentFactory.unmarshal(cms, file);
065        Map<Locale, Map<String, String>> result = Maps.newHashMap();
066        for (Locale locale : content.getLocales()) {
067            List<I_CmsXmlContentValue> messages = content.getValues(N_MESSAGE, locale);
068            Map<String, String> currentLocale = new HashMap<String, String>();
069            for (I_CmsXmlContentValue messageValue : messages) {
070                String path = messageValue.getPath();
071                I_CmsXmlContentValue keyValue = content.getValue(CmsXmlUtils.concatXpath(path, N_KEY), locale);
072                String keyStr = keyValue.getStringValue(cms);
073                // Ignore leading/trailing spaces in the key to protect from user error
074                keyStr = keyStr.trim();
075                I_CmsXmlContentValue valueValue = content.getValue(CmsXmlUtils.concatXpath(path, N_VALUE), locale);
076                String valueStr = valueValue.getStringValue(cms);
077                currentLocale.put(keyStr, valueStr);
078            }
079            result.put(locale, currentLocale);
080        }
081        return result;
082    }
083}