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