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