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.transformer.cfml.evaluator.impl;
020
021import java.util.ArrayList;
022import java.util.HashMap;
023import java.util.List;
024import java.util.Map;
025
026import lucee.commons.lang.ExceptionUtil;
027import lucee.commons.lang.IDGenerator;
028import lucee.commons.lang.Md5;
029import lucee.runtime.exp.PageRuntimeException;
030import lucee.runtime.op.Caster;
031import lucee.transformer.bytecode.Page;
032import lucee.transformer.bytecode.expression.Expression;
033import lucee.transformer.bytecode.literal.LitString;
034import lucee.transformer.bytecode.op.OpString;
035import lucee.transformer.bytecode.statement.tag.Attribute;
036import lucee.transformer.bytecode.statement.tag.Tag;
037import lucee.transformer.bytecode.util.ASMUtil;
038import lucee.transformer.cfml.evaluator.EvaluatorException;
039import lucee.transformer.cfml.evaluator.EvaluatorSupport;
040import lucee.transformer.library.function.FunctionLib;
041import lucee.transformer.library.tag.TagLibTag;
042
043
044public final class Sprite extends EvaluatorSupport {
045        
046        private static final Expression DELIMITER = LitString.toExprString(",");
047        private static Map<String,Previous> sprites=new HashMap<String,Previous>(); 
048        
049        
050        
051        
052        /**
053         *
054         * @see lucee.transformer.cfml.evaluator.EvaluatorSupport#evaluate(lucee.transformer.bytecode.statement.tag.Tag, lucee.transformer.library.tag.TagLibTag, lucee.transformer.library.function.FunctionLib[])
055         */
056        public void evaluate(Tag tag,TagLibTag tagLibTag,FunctionLib[] flibs) throws EvaluatorException {
057        String id="sprite_"+IDGenerator.intId();
058        try {
059                        Page page = ASMUtil.getAncestorPage(tag);
060                        String key=Md5.getDigestAsString(Thread.currentThread().getId()+":"+page.getPageSource().getDisplayPath());
061                        Expression src = tag.getAttribute("src").getValue();
062                        
063                        
064                        // get data from previous sprites
065                        Previous previous = sprites.get(key);
066                        if(previous!=null) {
067                                previous.tag.removeAttribute("_ids");
068                                previous.tag.removeAttribute("_srcs");
069                                previous.tag=tag;
070                        }
071                        else {
072                                sprites.put(key, previous = new Previous(tag));
073                        }
074                        
075                        previous.ids.add(id);
076                        if(previous.src==null)previous.src=src;
077                        else {
078                                previous.src=OpString.toExprString(previous.src,DELIMITER);
079                                previous.src=OpString.toExprString(previous.src,src);
080                        }
081                        
082                        
083                        
084                        tag.addAttribute(
085                                        new Attribute(
086                                                        false,
087                                                        "_id",
088                                                        LitString.toExprString(id),
089                                                        "string"
090                                        ));
091                        tag.addAttribute(
092                                        new Attribute(
093                                                        false,
094                                                        "_ids",
095                                                        LitString.toExprString(lucee.runtime.type.util.ListUtil.listToList(previous.ids, ",")),
096                                                        "string"
097                                        ));
098
099                        tag.addAttribute(
100                                        new Attribute(
101                                                        false,
102                                                        "_srcs",
103                                                        previous.src,
104                                                        "string"
105                                        ));
106                        
107                } 
108        catch (Throwable e) {// TODO handle Excpetion much more precise
109                        ExceptionUtil.rethrowIfNecessary(e);
110                        throw new PageRuntimeException(Caster.toPageException(e));
111                }
112                   
113        }
114        
115        private static class Previous {
116                public Previous(Tag tag) {
117                        this.tag=tag;
118                }
119                private List<String> ids=new ArrayList<String>();
120                private Expression src=null;
121                private Tag tag;
122                
123        }
124        
125}