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