001/*
002 * (C) Copyright 2006-2007 Nuxeo SAS <http://nuxeo.com> and others
003 *
004 * All rights reserved. This program and the accompanying materials
005 * are made available under the terms of the Eclipse Public License v1.0
006 * which accompanies this distribution, and is available at
007 * http://www.eclipse.org/legal/epl-v10.html
008 *
009 * Contributors:
010 *     Jean-Marc Orliaguet, Chalmers
011 *
012 * $Id$
013 */
014
015package org.nuxeo.theme.presets;
016
017import java.io.ByteArrayInputStream;
018import java.io.IOException;
019import java.util.LinkedHashMap;
020import java.util.Map;
021import java.util.Properties;
022
023import org.nuxeo.runtime.api.Framework;
024
025public class PropertiesPaletteParser extends PaletteParser {
026
027    public static boolean checkSanity(byte[] bytes) {
028        Properties properties = getProperties(bytes);
029        for (Object value : properties.values()) {
030            if (value.equals("")) {
031                return false;
032            }
033        }
034        return true;
035    }
036
037    public static Map<String, String> parse(byte[] bytes) {
038        Map<String, String> entries = new LinkedHashMap<String, String>();
039        Properties properties = getProperties(bytes);
040        for (Object propertyName : properties.keySet()) {
041            String key = (String) propertyName;
042            entries.put(key, Framework.expandVars((String) properties.get(key)));
043        }
044        return entries;
045    }
046
047    private static Properties getProperties(byte[] bytes) {
048        Properties properties = new Properties();
049        try {
050            properties.load(new ByteArrayInputStream(bytes));
051        } catch (IOException e) {
052        }
053        return properties;
054    }
055
056}