001/**
002 *
003 * Copyright (c) 2014, the Railo Company Ltd. All rights reserved.
004 *
005 * This library is free software; you can redistribute it and/or
006 * modify it under the terms of the GNU Lesser General Public
007 * License as published by the Free Software Foundation; either 
008 * version 2.1 of the License, or (at your option) any later version.
009 * 
010 * This library is distributed in the hope that it will be useful,
011 * but WITHOUT ANY WARRANTY; without even the implied warranty of
012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
013 * Lesser General Public License for more details.
014 * 
015 * You should have received a copy of the GNU Lesser General Public 
016 * License along with this library.  If not, see <http://www.gnu.org/licenses/>.
017 * 
018 **/
019package lucee.runtime.functions.image;
020
021import java.util.Iterator;
022import java.util.List;
023import java.util.Map;
024import java.util.Map.Entry;
025
026import lucee.runtime.PageContext;
027import lucee.runtime.exp.PageException;
028import lucee.runtime.img.Image;
029import lucee.runtime.type.Struct;
030import lucee.runtime.type.StructImpl;
031
032public class ImageGetEXIFMetadata {
033
034        public static Struct call(PageContext pc, Object name) throws PageException {
035                //if(name instanceof String) name=pc.getVariable(Caster.toString(name));
036                Image img = Image.toImage(pc,name);
037                return getData(img);
038        }
039
040        public static Struct getData(Image img) throws PageException {
041                Struct sct = img.info(),data=new StructImpl();
042                Iterator it = sct.entrySet().iterator();
043                Map.Entry entry;
044                while(it.hasNext()){
045                        entry=(Entry) it.next();
046                        if(entry.getValue() instanceof Map) 
047                                fill(data,(Map)entry.getValue());
048                        else if(entry.getValue() instanceof List) 
049                                fill(data,entry.getKey(),(List)entry.getValue());
050                        else
051                                data.put(entry.getKey(),entry.getValue());
052                }
053                
054                return data;
055        }
056
057        private static void fill(Struct data, Map map) throws PageException {
058                Iterator it = map.entrySet().iterator();
059                Map.Entry entry;
060                while(it.hasNext()){
061                        entry=(Entry) it.next();
062                        if(entry.getValue() instanceof Map) 
063                                fill(data,(Map)entry.getValue());
064                        else if(entry.getValue() instanceof List) 
065                                fill(data,entry.getKey(),(List)entry.getValue());
066                        else
067                                data.put(entry.getKey(),entry.getValue());
068                }
069        }
070
071        private static void fill(Struct data, Object key, List list) throws PageException {
072                data.put(
073                                key,
074                                lucee.runtime.type.util.ListUtil.listToList(list, ","));
075        }
076}