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.library.function;
020
021import java.io.IOException;
022import java.net.URI;
023import java.net.URISyntaxException;
024import java.util.HashMap;
025import java.util.Iterator;
026import java.util.Map;
027import java.util.Map.Entry;
028
029import lucee.commons.lang.Md5;
030import lucee.runtime.exp.ExpressionException;
031import lucee.runtime.exp.PageRuntimeException;
032
033
034/**
035 * Eine FunctionLib repraesentiert eine FLD, 
036 * sie stellt Methoden zur Verfuegung um auf alle Informationen 
037 * die eine FLD bietet zuzugreifen.
038 */
039public final class FunctionLib {
040
041        
042        // all functions of the lib
043        private HashMap<String,FunctionLibFunction> functions=new HashMap<String,FunctionLibFunction>();
044        private String version="";
045        private String shortName="";
046        private URI uri;
047        private String displayName="";
048        private String description="";
049        private String source;
050        
051        /**
052         * Geschuetzer Konstruktor ohne Argumente.
053         */
054        protected FunctionLib() {}
055        
056        /**
057         * Gibt eine einzelne Funktion der FLD zurueck mit dem passenden Namen. 
058         * Gibt null zurueck falls die Funktion nicht existiert.
059         * @param name Name der Funktion.
060         * @return FunctionLibFunction 
061         */
062        public FunctionLibFunction getFunction(String name)     {
063                return functions.get(name.toLowerCase());
064        }
065
066        /**
067         * Gibt die Beschreibung der FLD zurueck.
068         * @return Beschreibung der FLD.
069         */
070        public String getDescription() {
071                return description;
072        }
073
074        /**
075         * Gibt den Namen zur Ausgabe (Praesentation) der FLD zurueck.
076         * @return Ausgabename.
077         */
078        public String getDisplayName() {
079                return displayName;
080        }
081
082        /**
083         * Gibt den Kurzname der FLD zurueck.
084         * @return Kurzname.
085         */
086        public String getShortName() {
087                return shortName;
088        }
089
090        /**
091         * Gibt die eindeutige URI der FLD zurueck.
092         * @return URI.
093         */
094        public URI getUri() {
095                return uri;
096        }
097
098        /**
099         * Gibt die Version der FLD zurueck.
100         * @return String
101         */
102        public String getVersion() {
103                return version;
104        }
105
106        /**
107         * Fuegt der FunctionLib eine Funktion (FunctionLibFunction) zu.
108         * @param function
109         */
110        public void setFunction(FunctionLibFunction function) {
111                function.setFunctionLib(this);
112                functions.put(function.getName(),function);
113        }
114        
115        /**
116         * Setzt die Beschreibung der FLD.
117         * @param description Beschreibung der FLD.
118         */
119        protected void setDescription(String description) {
120                this.description = description;
121        }
122
123        /**
124         * Setzt den Ausgabename der FLD.
125         * @param displayName Ausgabename
126         */
127        protected void setDisplayName(String displayName) {
128                this.displayName = displayName;
129        }
130
131        /**
132         * Setzt den Kurznamen der FLD.
133         * @param shortName Kurznamen der FLD.
134         */
135        protected void setShortName(String shortName) {
136                this.shortName = shortName;
137        }
138
139        /**
140         * Setzt den eindeutigen URI der FLD.
141         * @param uriString URI.
142         * @throws URISyntaxException
143         */
144        protected void setUri(String uriString) throws URISyntaxException {
145                setUri(new URI(uriString));
146        }
147        
148        protected void setUri(URI uri)  {
149                this.uri = uri;
150        }
151
152        /**
153         * Setzt die Version der FLD.
154         * @param version FLD der Version.
155         */
156        protected void setVersion(String version) {
157                this.version = version;
158        }
159
160        /**
161         * @return Returns the functions.
162         */
163        public Map<String,FunctionLibFunction> getFunctions() {
164                return functions;
165        }
166
167    /**
168     * @see java.lang.Object#toString()
169     */
170    public String toString() {
171        return getDisplayName()+":"+getShortName()+":"+super.toString();
172    }
173    
174    public String getHash() {
175        StringBuffer sb=new StringBuffer();
176        Iterator<String> it = functions.keySet().iterator();
177        while(it.hasNext()) {
178                sb.append((functions.get(it.next())).getHash()+"\n");
179        }
180        try {
181                        return Md5.getDigestAsString(sb.toString());
182                } catch (IOException e) {
183                        return "";
184                }
185    }
186
187        /**
188         * duplicate this FunctionLib
189         * @param deepCopy
190         * @return
191         */
192        public FunctionLib duplicate(boolean deepCopy) {
193                FunctionLib fl = new FunctionLib();
194                
195                fl.description=this.description;
196                fl.displayName=this.displayName;
197                fl.functions=duplicate(this.functions,deepCopy);
198                fl.shortName=this.shortName;
199                fl.uri=this.uri;
200                fl.version=this.version;
201                                
202                return fl;
203        }
204
205        /**
206         * @param source the source to set
207         */
208        public void setSource(String source) {
209                this.source = source;
210        }
211
212        /**
213         * @return the source
214         */
215        public String getSource() {
216                return source;
217        }
218
219        /**
220         * duplcate a hashmap with FunctionLibFunction's
221         * @param funcs
222         * @param deepCopy
223         * @return cloned map
224         */
225        private HashMap duplicate(HashMap funcs, boolean deepCopy) {
226                if(deepCopy) throw new PageRuntimeException(new ExpressionException("deep copy not supported"));
227                
228                Iterator it = funcs.entrySet().iterator();
229                Map.Entry entry;
230                HashMap cm = new HashMap();
231                while(it.hasNext()){
232                        entry=(Entry) it.next();
233                        cm.put(
234                                        entry.getKey(), 
235                                        deepCopy?
236                                                        entry.getValue(): // TODO add support for deepcopy ((FunctionLibFunction)entry.getValue()).duplicate(deepCopy):
237                                                        entry.getValue());
238                }
239                return cm;
240        }
241}