001    package railo.runtime.op;
002    
003    import java.io.InputStream;
004    import java.io.Serializable;
005    
006    import org.w3c.dom.Document;
007    import org.xml.sax.InputSource;
008    
009    import railo.commons.date.DateTimeUtil;
010    import railo.commons.io.IOUtil;
011    import railo.commons.io.res.Resource;
012    import railo.commons.io.res.util.ResourceUtil;
013    import railo.runtime.config.RemoteClient;
014    import railo.runtime.db.DatasourceConnection;
015    import railo.runtime.db.SQL;
016    import railo.runtime.engine.ThreadLocalPageContext;
017    import railo.runtime.exp.ExpressionException;
018    import railo.runtime.exp.PageException;
019    import railo.runtime.spooler.ExecutionPlan;
020    import railo.runtime.spooler.SpoolerTask;
021    import railo.runtime.spooler.remote.RemoteClientTask;
022    import railo.runtime.text.xml.XMLUtil;
023    import railo.runtime.type.Array;
024    import railo.runtime.type.ArrayImpl;
025    import railo.runtime.type.Collection.Key;
026    import railo.runtime.type.KeyImpl;
027    import railo.runtime.type.List;
028    import railo.runtime.type.Query;
029    import railo.runtime.type.QueryImpl;
030    import railo.runtime.type.Struct;
031    import railo.runtime.type.StructImpl;
032    import railo.runtime.type.dt.Date;
033    import railo.runtime.type.dt.DateImpl;
034    import railo.runtime.type.dt.DateTime;
035    import railo.runtime.type.dt.DateTimeImpl;
036    import railo.runtime.type.dt.Time;
037    import railo.runtime.type.dt.TimeImpl;
038    import railo.runtime.type.dt.TimeSpan;
039    import railo.runtime.type.dt.TimeSpanImpl;
040    import railo.runtime.type.scope.ClusterEntry;
041    import railo.runtime.type.scope.ClusterEntryImpl;
042    import railo.runtime.util.Creation;
043    
044    /**
045     * implemention of the ctration object
046     */
047    public final class CreationImpl implements Creation {
048    
049        private static CreationImpl singelton;
050    
051        /**
052         * @return singleton instance
053         */
054        public static Creation getInstance() {
055            if(singelton==null)singelton=new CreationImpl();
056            return singelton;
057        }
058    
059        /**
060         * @see railo.runtime.util.Creation#createArray()
061         */
062        public Array createArray() {
063            return new ArrayImpl();
064        }
065    
066            /**
067             * @see railo.runtime.util.Creation#createArray(java.lang.String, java.lang.String, boolean, boolean)
068             */
069            public Array createArray(String list, String delimeter,boolean removeEmptyItem, boolean trim) {
070                    if(removeEmptyItem)return List.listToArrayRemoveEmpty(list, delimeter);
071                    if(trim)return List.listToArrayTrim(list, delimeter);
072                    return List.listToArray(list, delimeter);
073            }
074            
075        /**
076         * @see railo.runtime.util.Creation#createArray(int)
077         */
078        public Array createArray(int dimension) throws PageException {
079            return new ArrayImpl(dimension);
080        }
081    
082        /**
083         * @see railo.runtime.util.Creation#createStruct()
084         */
085        public Struct createStruct() {
086            return new StructImpl();
087        }
088    
089        /**
090         * @see railo.runtime.util.Creation#createStruct(int)
091         */
092        public Struct createStruct(int type) {
093            return new StructImpl(type);
094        }
095    
096        /**
097         * @see railo.runtime.util.Creation#createQuery(java.lang.String[], int, java.lang.String)
098         */
099        public Query createQuery(String[] columns, int rows, String name) {
100            return new QueryImpl(columns,rows,name);
101        }
102        
103        /**
104         *
105         * @see railo.runtime.util.Creation#createQuery(railo.runtime.db.DatasourceConnection, railo.runtime.db.SQL, int, java.lang.String)
106         */
107        public Query createQuery(DatasourceConnection dc, SQL sql, int maxrow, String name) throws PageException {
108                    return new QueryImpl(dc,sql,maxrow,-1,-1,name);
109            }
110        
111        public Query createQuery(DatasourceConnection dc, SQL sql, int maxrow, int fetchsize, int timeout, String name) throws PageException {
112                    return new QueryImpl(dc,sql,maxrow,fetchsize,timeout,name);
113            }
114        
115        /**
116         * @see railo.runtime.util.Creation#createDateTime(long)
117         */
118        public DateTime createDateTime(long time) {
119            return new DateTimeImpl(time,false);
120        }
121    
122        /**
123         * @see railo.runtime.util.Creation#createTimeSpan(int, int, int, int)
124         */
125        public TimeSpan createTimeSpan(int day,int hour,int minute,int second) {
126            return new TimeSpanImpl(day,hour,minute,second);
127        }
128    
129        /**
130         * @see railo.runtime.util.Creation#createDate(long)
131         */
132        public Date createDate(long time) {
133            return new DateImpl(time);
134        }
135    
136        /**
137         * @see railo.runtime.util.Creation#createTime(long)
138         */
139        public Time createTime(long time) {
140            return new TimeImpl(time,false);
141        }
142    
143        /**
144         * @see railo.runtime.util.Creation#createDateTime(int, int, int, int, int, int, int)
145         */
146        public DateTime createDateTime(int year, int month, int day, int hour, int minute, int second, int millis) throws ExpressionException {
147            return DateTimeUtil.getInstance().toDateTime(ThreadLocalPageContext.getTimeZone(),year,month,day,hour,minute,second,millis);
148        }
149    
150        /**
151         * @see railo.runtime.util.Creation#createDate(int, int, int)
152         */
153        public Date createDate(int year, int month, int day) throws ExpressionException {
154            return new DateImpl(DateTimeUtil.getInstance().toDateTime(null,year,month,day, 0, 0, 0,0));
155        }
156    
157        /**
158         * @see railo.runtime.util.Creation#createTime(int, int, int, int)
159         */
160        public Time createTime(int hour, int minute, int second, int millis) {
161            return new TimeImpl(
162                            DateTimeUtil.getInstance().toTime(null,1899,12,30,hour,minute,second,millis,0),false);
163        }
164    
165        /**
166         * @see railo.runtime.util.Creation#createDocument()
167         */
168        public Document createDocument() throws PageException {
169            try {
170                return XMLUtil.newDocument();
171            } catch (Exception e) {
172                throw Caster.toPageException(e);
173            }
174        }
175    
176        /**
177         * @see railo.runtime.util.Creation#createDocument(railo.commons.io.res.Resource, boolean)
178         */
179        public Document createDocument(Resource res, boolean isHTML) throws PageException {
180            InputStream is=null;
181            try {
182                return XMLUtil.parse(new InputSource(is=res.getInputStream()),null,isHTML);
183            } catch (Exception e) {
184                throw Caster.toPageException(e);
185            }
186            finally {
187                    IOUtil.closeEL(is);
188            }
189        }
190    
191        /**
192         * @see railo.runtime.util.Creation#createDocument(java.lang.String, boolean)
193         */
194        public Document createDocument(String xml, boolean isHTML) throws PageException {
195            try {
196                return XMLUtil.parse(XMLUtil.toInputSource(null, xml),null,isHTML);
197            } catch (Exception e) {
198                throw Caster.toPageException(e);
199            }
200        }
201    
202        /**
203         * @see railo.runtime.util.Creation#createDocument(java.io.InputStream, boolean)
204         */
205        public Document createDocument(InputStream is, boolean isHTML) throws PageException {
206            try {
207                return XMLUtil.parse(new InputSource(is),null,isHTML);
208            } catch (Exception e) {
209                throw Caster.toPageException(e);
210            }
211        }
212    
213            /**
214             * @see railo.runtime.util.Creation#createKey(java.lang.String)
215             */
216            public Key createKey(String key) {
217                    return KeyImpl.init(key);
218            }
219    
220            public SpoolerTask createRemoteClientTask(ExecutionPlan[] plans,RemoteClient remoteClient,Struct attrColl,String callerId, String type) {
221                    return new RemoteClientTask(plans,remoteClient,attrColl,callerId, type);
222            }
223    
224            public ClusterEntry createClusterEntry(Key key,Serializable value, int offset) {
225                    return new ClusterEntryImpl(key,value,offset);
226            }
227    
228            public Resource createResource(String path, boolean existing) throws PageException {
229                    if(existing)return ResourceUtil.toResourceExisting(ThreadLocalPageContext.get(), path);
230                    return ResourceUtil.toResourceNotExisting(ThreadLocalPageContext.get(), path);
231            }
232    
233    
234    }