001/*
002 * (C) Copyright 2006-2015 Nuxeo SA (http://nuxeo.com/) and others.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 *
016 * Contributors:
017 *     Jean-Marc Orliaguet, Chalmers
018 *     Anahide Tchertchian
019 */
020
021package org.nuxeo.theme.styling.service.palettes;
022
023import java.io.ByteArrayInputStream;
024import java.io.IOException;
025import java.util.LinkedHashMap;
026import java.util.Map;
027import java.util.Properties;
028
029import org.nuxeo.runtime.api.Framework;
030
031public class PropertiesPaletteParser extends PaletteParser {
032
033    public static boolean checkSanity(byte[] bytes) {
034        Properties properties = getProperties(bytes);
035        for (Object value : properties.values()) {
036            if (value.equals("")) {
037                return false;
038            }
039        }
040        return true;
041    }
042
043    public static Map<String, String> parse(byte[] bytes) {
044        Map<String, String> entries = new LinkedHashMap<String, String>();
045        Properties properties = getProperties(bytes);
046        for (Object propertyName : properties.keySet()) {
047            String key = (String) propertyName;
048            entries.put(key, Framework.expandVars((String) properties.get(key)));
049        }
050        return entries;
051    }
052
053    private static Properties getProperties(byte[] bytes) {
054        Properties properties = new Properties();
055        try {
056            properties.load(new ByteArrayInputStream(bytes));
057        } catch (IOException e) {
058        }
059        return properties;
060    }
061
062}