001    package railo.runtime.schedule;
002    
003    import java.io.File;
004    import java.io.IOException;
005    import java.io.InputStream;
006    import java.io.OutputStream;
007    
008    import org.apache.commons.httpclient.Credentials;
009    import org.apache.commons.httpclient.UsernamePasswordCredentials;
010    import org.apache.xerces.parsers.DOMParser;
011    import org.apache.xml.serialize.OutputFormat;
012    import org.apache.xml.serialize.XMLSerializer;
013    import org.w3c.dom.Document;
014    import org.w3c.dom.Element;
015    import org.w3c.dom.Node;
016    import org.w3c.dom.NodeList;
017    import org.xml.sax.InputSource;
018    import org.xml.sax.SAXException;
019    
020    import railo.commons.io.IOUtil;
021    import railo.commons.io.res.Resource;
022    import railo.commons.io.res.util.ResourceUtil;
023    import railo.runtime.Info;
024    import railo.runtime.config.Config;
025    import railo.runtime.engine.ThreadLocalPageContext;
026    import railo.runtime.op.Caster;
027    import railo.runtime.op.date.DateCaster;
028    import railo.runtime.type.dt.Date;
029    import railo.runtime.type.dt.DateImpl;
030    import railo.runtime.type.dt.DateTime;
031    import railo.runtime.type.dt.Time;
032    import railo.runtime.type.dt.TimeImpl;
033    
034    
035    /**
036     * 
037     */
038    public final class StorageUtil {
039    
040        /**
041             * create xml file from a resource definition
042             * @param file 
043         * @param resourcePath 
044             * @throws IOException
045             */
046            public void loadFile(File file,String resourcePath) throws IOException {
047                    loadFile(ResourceUtil.toResource(file), resourcePath);
048            }
049        /**
050             * create xml file from a resource definition
051             * @param res 
052         * @param resourcePath 
053             * @throws IOException
054             */
055            public void loadFile(Resource res,String resourcePath) throws IOException {
056                    res.createFile(true);
057                    InputStream is = new Info().getClass().getResourceAsStream(resourcePath);
058            IOUtil.copy(is,res,true);
059            }
060    
061        /**
062         * load a XML Document as DOM representation
063         * @param file XML File to load
064         * @return DOM Object
065         * @throws SAXException
066         * @throws IOException
067         */
068        public Document loadDocument(Resource file) throws SAXException, IOException {
069            DOMParser parser = new DOMParser();
070                
071                    InputStream in = null;
072                    try {
073                            in = file.getInputStream();
074                            InputSource source = new InputSource(in);
075                            parser.parse(source);
076                    }
077                    finally {
078                            IOUtil.closeEL(in);
079                    }
080            
081            return parser.getDocument();
082        }
083        
084        public Document loadDocument(String content) throws SAXException, IOException {
085            DOMParser parser = new DOMParser();
086                
087                    InputSource source = new InputSource(content);
088                    parser.parse(source);
089                    
090            return parser.getDocument();
091        }
092    
093        /**
094         * return XML Element matching name
095         * @param list source node list
096         * @param key key to compare
097         * @param value value to compare
098         * @return matching XML Element
099         */
100        public Element getElement(NodeList list,String key, String value) {
101            int len=list.getLength();
102            for(int i=0;i<len;i++) {
103                Node n=list.item(i);
104                if(n instanceof Element) {
105                    Element el = (Element)n;
106                    if(el.getAttribute(key).equalsIgnoreCase(value)) return el;
107                }
108            }
109            return null;
110        }
111        
112            /**
113         * store loaded data to xml file
114             * @param doc 
115             * @param file 
116             * @throws IOException
117         */
118        public synchronized void store(Document doc,File file) throws IOException {
119            store(doc, ResourceUtil.toResource(file));
120        }
121        
122            /**
123         * store loaded data to xml file
124             * @param doc 
125             * @param res 
126             * @throws IOException
127         */
128        public synchronized void store(Document doc,Resource res) throws IOException {
129            OutputFormat format = new OutputFormat(doc, null, true);
130                    format.setLineSeparator("\r\n");
131                    format.setLineWidth(72);
132                    
133                    OutputStream os=null;
134                    try {
135                            XMLSerializer serializer = new XMLSerializer(os=res.getOutputStream(), format);
136                            serializer.serialize(doc.getDocumentElement());
137                    }
138                    finally {
139                            IOUtil.closeEL(os);
140                    }
141        }
142    
143        
144        /**
145         * reads a XML Element Attribute ans cast it to a String
146         * @param el XML Element to read Attribute from it
147         * @param attributeName Name of the Attribute to read
148         * @return Attribute Value
149         */
150        public String toString(Element el,String attributeName) {
151            return el.getAttribute(attributeName);
152        }
153        
154        /**
155         * reads a XML Element Attribute ans cast it to a String
156         * @param el XML Element to read Attribute from it
157         * @param attributeName Name of the Attribute to read
158         * @param defaultValue if attribute doesn't exist return default value
159         * @return Attribute Value
160         */
161        public String toString(Element el,String attributeName, String defaultValue) {
162            String value = el.getAttribute(attributeName);
163            return (value==null)?defaultValue:value;
164        }
165    
166        /**
167         * reads a XML Element Attribute ans cast it to a File
168         * @param el XML Element to read Attribute from it
169         * @param attributeName Name of the Attribute to read
170         * @return Attribute Value
171         */
172        /*public File toFile(Element el,String attributeName) {
173            String attributeValue = el.getAttribute(attributeName);
174            if(attributeValue==null || attributeValue.trim().length()==0) return null;
175            return new File(attributeValue);
176        }*/
177    
178        public Resource toResource(Config config,Element el,String attributeName) {
179            String attributeValue = el.getAttribute(attributeName);
180            if(attributeValue==null || attributeValue.trim().length()==0) return null;
181            return config.getResource(attributeValue);
182        }
183        
184        /**
185         * reads a XML Element Attribute ans cast it to a boolean value
186         * @param el XML Element to read Attribute from it
187         * @param attributeName Name of the Attribute to read
188         * @return Attribute Value
189         */
190        public boolean toBoolean(Element el,String attributeName) {
191            return Caster.toBooleanValue(el.getAttribute(attributeName),false);
192        }
193        
194        /**
195         * reads a XML Element Attribute ans cast it to a boolean value
196         * @param el XML Element to read Attribute from it
197         * @param attributeName Name of the Attribute to read
198         * @param defaultValue if attribute doesn't exist return default value
199         * @return Attribute Value
200         */
201        public boolean toBoolean(Element el,String attributeName, boolean defaultValue) {
202            String value = el.getAttribute(attributeName);
203            if(value==null) return defaultValue;
204            return Caster.toBooleanValue(value,false);
205        }
206    
207        /**
208         * reads a XML Element Attribute ans cast it to a int value
209         * @param el XML Element to read Attribute from it
210         * @param attributeName Name of the Attribute to read
211         * @return Attribute Value
212         */
213        public int toInt(Element el,String attributeName) {
214            return Caster.toIntValue(el.getAttribute(attributeName),Integer.MIN_VALUE);
215        }
216        
217        public long toLong(Element el,String attributeName) {
218            return Caster.toLongValue(el.getAttribute(attributeName),Long.MIN_VALUE);
219        }
220        
221        /**
222         * reads a XML Element Attribute ans cast it to a int value
223         * @param el XML Element to read Attribute from it
224         * @param attributeName Name of the Attribute to read
225         * @param defaultValue if attribute doesn't exist return default value
226         * @return Attribute Value
227         */
228        public int toInt(Element el,String attributeName, int defaultValue) {
229            String value = el.getAttribute(attributeName);
230            if(value==null) return defaultValue;
231            int intValue=Caster.toIntValue(value,Integer.MIN_VALUE);
232            if(intValue==Integer.MIN_VALUE) return defaultValue;
233            return intValue;
234        }
235    
236        /**
237         * reads a XML Element Attribute ans cast it to a DateTime Object
238         * @param config 
239         * @param el XML Element to read Attribute from it
240         * @param attributeName Name of the Attribute to read
241         * @return Attribute Value
242         */
243        public DateTime toDateTime(Config config, Element el,String attributeName) {
244            String str=el.getAttribute(attributeName);
245            if(str==null) return null;
246            return DateCaster.toDateAdvanced(str,ThreadLocalPageContext.getTimeZone(config),null);
247        }
248        
249        /**
250         * reads a XML Element Attribute ans cast it to a DateTime
251         * @param el XML Element to read Attribute from it
252         * @param attributeName Name of the Attribute to read
253         * @param defaultValue if attribute doesn't exist return default value
254         * @return Attribute Value
255         */
256        public DateTime toDateTime(Element el,String attributeName, DateTime defaultValue) {
257            
258            String value = el.getAttribute(attributeName);
259            if(value==null) return defaultValue;
260            DateTime dtValue=Caster.toDate(value,false,null,null);
261            if(dtValue==null) return defaultValue;
262            return dtValue;
263        }
264    
265        /**
266         * reads a XML Element Attribute ans cast it to a Date Object
267         * @param el XML Element to read Attribute from it
268         * @param attributeName Name of the Attribute to read
269         * @return Attribute Value
270         */
271        public Date toDate(Config config,Element el,String attributeName) {
272            DateTime dt = toDateTime(config,el,attributeName);
273            if(dt==null) return null;
274            return new DateImpl(dt);
275        }
276        
277        /**
278         * reads a XML Element Attribute ans cast it to a Date
279         * @param el XML Element to read Attribute from it
280         * @param attributeName Name of the Attribute to read
281         * @param defaultValue if attribute doesn't exist return default value
282         * @return Attribute Value
283         */
284        public Date toDate(Element el,String attributeName, Date defaultValue) {
285            return new DateImpl(toDateTime(el,attributeName,defaultValue));
286        }
287    
288        /**
289         * reads a XML Element Attribute ans cast it to a Time Object
290         * @param config 
291         * @param el XML Element to read Attribute from it
292         * @param attributeName Name of the Attribute to read
293         * @return Attribute Value
294         */
295        public Time toTime(Config config, Element el,String attributeName) {
296            DateTime dt = toDateTime(config,el,attributeName);
297            if(dt==null) return null;
298            return new TimeImpl(dt);
299        }
300        
301        /**
302         * reads a XML Element Attribute ans cast it to a Date
303         * @param el XML Element to read Attribute from it
304         * @param attributeName Name of the Attribute to read
305         * @param defaultValue if attribute doesn't exist return default value
306         * @return Attribute Value
307         */
308        public Time toTime(Element el,String attributeName, Time defaultValue) {
309            return new TimeImpl(toDateTime(el,attributeName,defaultValue));
310        }
311    
312        /**
313         * reads 2 XML Element Attribute ans cast it to a Credential
314         * @param el XML Element to read Attribute from it
315         * @param attributeUser Name of the user Attribute to read
316         * @param attributePassword Name of the password Attribute to read
317         * @return Attribute Value
318         */
319        public Credentials toCredentials(Element el,String attributeUser, String attributePassword) {
320            String user = el.getAttribute(attributeUser);
321            String pass = el.getAttribute(attributePassword);
322            if(user==null) return null;
323            if(pass==null)pass="";
324            return new UsernamePasswordCredentials(user,pass);
325        }
326    
327        /**
328         * reads 2 XML Element Attribute ans cast it to a Credential
329         * @param el XML Element to read Attribute from it
330         * @param attributeUser Name of the user Attribute to read
331         * @param attributePassword Name of the password Attribute to read
332         * @param defaultCredentials
333         * @return Attribute Value
334         */
335        public Credentials toCredentials(Element el,String attributeUser, String attributePassword, Credentials defaultCredentials) {
336            String user = el.getAttribute(attributeUser);
337            String pass = el.getAttribute(attributePassword);
338            if(user==null) return defaultCredentials;
339            if(pass==null)pass="";
340            return new UsernamePasswordCredentials(user,pass);
341        }
342    
343        /**
344         * sets a string value to a XML Element
345         * @param el Element to set value on it
346         * @param key key to set
347         * @param value value to set
348         */
349        public void setString(Element el, String key, String value) {
350            if(value!=null)el.setAttribute(key,value);
351        }
352    
353        /**
354         * sets a file value to a XML Element
355         * @param el Element to set value on it
356         * @param key key to set
357         * @param value value to set
358         */
359        public void setFile(Element el, String key, File value) {
360            setFile(el, key, ResourceUtil.toResource(value));
361        }
362    
363        /**
364         * sets a file value to a XML Element
365         * @param el Element to set value on it
366         * @param key key to set
367         * @param value value to set
368         */
369        public void setFile(Element el, String key, Resource value) {
370            if(value!=null && value.toString().length()>0)el.setAttribute(key,value.getAbsolutePath());
371        }
372    
373        /**
374         * sets a boolean value to a XML Element
375         * @param el Element to set value on it
376         * @param key key to set
377         * @param value value to set
378         */
379        public void setBoolean(Element el, String key, boolean value) {
380            el.setAttribute(key,String.valueOf(value));
381        }
382    
383        /**
384         * sets a int value to a XML Element
385         * @param el Element to set value on it
386         * @param key key to set
387         * @param value value to set
388         */
389        public void setInt(Element el, String key, int value) {
390            el.setAttribute(key,String.valueOf(value));
391        }
392    
393        /**
394         * sets a datetime value to a XML Element
395         * @param el Element to set value on it
396         * @param key key to set
397         * @param value value to set
398         */
399        public void setDateTime(Element el, String key, DateTime value) {
400            if(value!=null){
401                String str = value.castToString(null);
402                if(str!=null)el.setAttribute(key,str);
403            }
404        }
405        
406        /**
407         * sets a Credentials to a XML Element
408         * @param el
409         * @param username
410         * @param password
411         * @param credentials
412         */
413        public void setCredentials(Element el, String username, String password, Credentials credentials) {
414            if(credentials==null) return;
415            UsernamePasswordCredentials upc=(UsernamePasswordCredentials) credentials;
416            if(upc.getUserName()!=null)el.setAttribute(username,upc.getUserName());
417            if(upc.getPassword()!=null)el.setAttribute(password,upc.getPassword());
418        }
419    }