001/*
002 * (C) Copyright 2006-2015 Nuxeo SA (http://nuxeo.com/) and contributors.
003 *
004 * All rights reserved. This program and the accompanying materials
005 * are made available under the terms of the GNU Lesser General Public License
006 * (LGPL) version 2.1 which accompanies this distribution, and is available at
007 * http://www.gnu.org/licenses/lgpl-2.1.html
008 *
009 * This library is distributed in the hope that it will be useful,
010 * but WITHOUT ANY WARRANTY; without even the implied warranty of
011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012 * Lesser General Public License for more details.
013 *
014 * Contributors:
015 *     Jean-Marc Orliaguet, Chalmers
016 *     Anahide Tchertchian
017 */
018
019package org.nuxeo.theme.styling.service.palettes;
020
021import java.io.ByteArrayInputStream;
022import java.io.IOException;
023import java.util.LinkedHashMap;
024import java.util.Map;
025import java.util.Properties;
026
027import org.nuxeo.runtime.api.Framework;
028
029public class PropertiesPaletteParser extends PaletteParser {
030
031    public static boolean checkSanity(byte[] bytes) {
032        Properties properties = getProperties(bytes);
033        for (Object value : properties.values()) {
034            if (value.equals("")) {
035                return false;
036            }
037        }
038        return true;
039    }
040
041    public static Map<String, String> parse(byte[] bytes) {
042        Map<String, String> entries = new LinkedHashMap<String, String>();
043        Properties properties = getProperties(bytes);
044        for (Object propertyName : properties.keySet()) {
045            String key = (String) propertyName;
046            entries.put(key, Framework.expandVars((String) properties.get(key)));
047        }
048        return entries;
049    }
050
051    private static Properties getProperties(byte[] bytes) {
052        Properties properties = new Properties();
053        try {
054            properties.load(new ByteArrayInputStream(bytes));
055        } catch (IOException e) {
056        }
057        return properties;
058    }
059
060}