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.DataInputStream;
022import java.io.FileNotFoundException;
023import java.io.IOException;
024import java.io.InputStream;
025import java.net.URL;
026import java.util.HashMap;
027import java.util.Map;
028
029import org.apache.commons.logging.Log;
030import org.apache.commons.logging.LogFactory;
031
032public class PaletteParser {
033
034    private static final Log log = LogFactory.getLog(PaletteParser.class);
035
036    public static Map<String, String> parse(URL url) {
037        Map<String, String> entries = new HashMap<>();
038        InputStream in = null;
039        try {
040            in = url.openStream();
041            entries = parse(in, url.getFile());
042        } catch (FileNotFoundException e) {
043            log.error("File not found: " + url);
044        } catch (IOException e) {
045            log.error("Could not open file: " + url);
046        } catch (PaletteParseException e) {
047            log.error("Could not parse palette: " + url);
048        } finally {
049            if (in != null) {
050                try {
051                    in.close();
052                } catch (IOException e) {
053                    log.error(e, e);
054                } finally {
055                    in = null;
056                }
057            }
058        }
059        return entries;
060    }
061
062    public static boolean checkSanity(byte[] bytes) {
063        return false;
064    }
065
066    public static String rgbToHex(int r, int g, int b) {
067        final StringBuffer hexcolor = new StringBuffer("#");
068        final int[] rgb = { r, g, b };
069        for (final int val : rgb) {
070            if (val < 16) {
071                hexcolor.append("0");
072            }
073            hexcolor.append(Integer.toHexString(val));
074        }
075        // optimize #aabbcc to #abc
076        if (hexcolor.charAt(1) == hexcolor.charAt(2) && hexcolor.charAt(3) == hexcolor.charAt(4)
077                && hexcolor.charAt(5) == hexcolor.charAt(6)) {
078            return String.format("#%s%s%s", hexcolor.charAt(1), hexcolor.charAt(4), hexcolor.charAt(6));
079        }
080        return hexcolor.toString();
081    }
082
083    public static Map<String, String> parse(InputStream in, String filename) throws IOException, PaletteParseException {
084        DataInputStream dis = new DataInputStream(in);
085        byte[] bytes = new byte[dis.available()];
086        dis.read(bytes);
087        return parse(bytes, filename);
088    }
089
090    public static Map<String, String> parse(byte[] bytes, String filename) throws PaletteParseException {
091        return PropertiesPaletteParser.parse(bytes);
092    }
093
094}