001    package railo.runtime.text.xml.struct;
002    
003    import java.util.ArrayList;
004    import java.util.Iterator;
005    import java.util.List;
006    
007    import org.w3c.dom.Element;
008    import org.w3c.dom.Node;
009    
010    import railo.runtime.PageContext;
011    import railo.runtime.dump.DumpData;
012    import railo.runtime.dump.DumpProperties;
013    import railo.runtime.exp.PageException;
014    import railo.runtime.exp.PageRuntimeException;
015    import railo.runtime.text.xml.XMLCaster;
016    import railo.runtime.type.Collection;
017    import railo.runtime.type.dt.DateTime;
018    import railo.runtime.type.util.ArraySupport;
019    import railo.runtime.type.util.ArrayUtil;
020    import railo.runtime.type.wrap.ArrayAsArrayList;
021    import railo.runtime.util.ArrayIterator;
022    
023    public class XMLMultiElementArray extends ArraySupport {
024    
025            private static final long serialVersionUID = -2673749147723742450L;
026            private XMLMultiElementStruct struct;
027    
028            public XMLMultiElementArray(XMLMultiElementStruct struct) {
029                    this.struct=struct;
030            }
031    
032            /**
033             * @see railo.runtime.type.Array#append(java.lang.Object)
034             */
035            public Object append(Object o) throws PageException {
036                    return setE(size()+1,o);
037            }
038    
039            /**
040             * @see railo.runtime.type.Array#appendEL(java.lang.Object)
041             */
042            public Object appendEL(Object o) {
043                    return setEL(size()+1,o);
044            }
045    
046    
047            /**
048             * @see railo.runtime.type.Array#containsKey(int)
049             */
050            public boolean containsKey(int key) {
051                    return get(key,null)!=null;
052            }
053    
054            /**
055             *
056             * @see railo.runtime.type.Array#get(int, java.lang.Object)
057             */
058            public Object get(int key, Object defaultValue) {
059                    return struct.get(key,defaultValue);
060            }
061    
062            /**
063             *
064             * @see railo.runtime.type.Array#getE(int)
065             */
066            public Object getE(int key) throws PageException {
067                    return struct.get(key);
068            }
069    
070    
071            /**
072             * @see railo.runtime.type.Array#getDimension()
073             */
074            public int getDimension() {
075                    return struct.getInnerArray().getDimension();
076            }
077    
078            /**
079             * @see railo.runtime.type.Array#insert(int, java.lang.Object)
080             */
081            public boolean insert(int index, Object value) throws PageException {
082            Element element=XMLCaster.toElement(struct.getOwnerDocument(),value);
083            boolean rtn = struct.getInnerArray().insert(index, element);
084            Object obj = struct.getInnerArray().get(index,null);
085            
086            if(obj instanceof Element) {
087                    Element el = ((Element)obj);
088                    el.getParentNode().insertBefore(XMLCaster.toRawNode(element), el);
089            }
090            else  {
091                    struct.getParentNode().appendChild(XMLCaster.toRawNode(element));
092            }
093            return rtn;
094            }
095    
096            /**
097             *
098             * @see railo.runtime.type.Array#intKeys()
099             */
100            public int[] intKeys() {
101                    return struct.getInnerArray().intKeys();
102            }
103    
104            /**
105             * @see railo.runtime.type.Array#prepend(java.lang.Object)
106             */
107            public Object prepend(Object value) throws PageException {
108            Element element=XMLCaster.toElement(struct.getOwnerDocument(),value);
109            Object obj = struct.getInnerArray().get(1,null);
110            
111            if(obj instanceof Element) {
112                    Element el = ((Element)obj);
113                    el.getParentNode().insertBefore(XMLCaster.toRawNode(element), el);
114            }
115            else  {
116                    struct.getParentNode().appendChild(XMLCaster.toRawNode(element));
117            }
118            return struct.getInnerArray().prepend(element);
119            }
120    
121            /**
122             *
123             * @see railo.runtime.type.Array#removeE(int)
124             */
125            public Object removeE(int key) throws PageException {
126                    return struct.remove(key);
127            }
128    
129            /**
130             *
131             * @see railo.runtime.type.Array#removeEL(int)
132             */
133            public Object removeEL(int key) {
134                    return struct.removeEL(key);
135            }
136    
137            /**
138             *
139             * @see railo.runtime.type.Array#resize(int)
140             */
141            public void resize(int to) throws PageException {
142                    throw new PageRuntimeException("resizing of xml nodelist not allowed");
143            }
144    
145            /**
146             *
147             * @see railo.runtime.type.Array#setE(int, java.lang.Object)
148             */
149            public Object setE(int key, Object value) throws PageException {
150                    return struct.set(key, value);
151            }
152    
153            /**
154             *
155             * @see railo.runtime.type.Array#setEL(int, java.lang.Object)
156             */
157            public Object setEL(int key, Object value) {
158                    return struct.setEL(key, value);
159            }
160    
161            /**
162             *
163             * @see railo.runtime.type.Array#sort(java.lang.String, java.lang.String)
164             */
165            public void sort(String sortType, String sortOrder) throws PageException {
166                    if(size()<=1) return;
167                    
168                    struct.getInnerArray().sort(sortType, sortOrder);
169                    
170                    Object[] nodes = struct.getInnerArray().toArray();
171                    Node last=(Node) nodes[nodes.length-1],current;
172                    Node parent=last.getParentNode();
173                    for(int i=nodes.length-2;i>=0;i--) {
174                            current=(Node) nodes[i];
175                            parent.insertBefore(current, last);
176                            last=current;
177                    }// MUST testen
178            }
179            /**
180             *
181             * @see railo.runtime.type.Array#toArray()
182             */
183            public Object[] toArray() {
184                    return struct.getInnerArray().toArray();
185            }
186    
187            /**
188             *
189             * @see railo.runtime.type.Array#toArrayList()
190             */
191            public ArrayList toArrayList() {
192                    return ArrayAsArrayList.toArrayList(this);
193            }
194    
195            /**
196             *
197             * @see railo.runtime.type.Collection#clear()
198             */
199            public void clear() {//MUST
200            }
201    
202            /**
203             *
204             * @see railo.runtime.type.Collection#containsKey(java.lang.String)
205             */
206            public boolean containsKey(String key) {
207                    return struct.containsKey(key);
208            }
209    
210            /**
211             * @see railo.runtime.type.Collection#containsKey(railo.runtime.type.Collection.Key)
212             */
213            public boolean containsKey(Key key) {
214                    return struct.containsKey(key);
215            }
216    
217            /**
218             * @see railo.runtime.type.Collection#duplicate(boolean)
219             */
220            public Collection duplicate(boolean deepCopy) {
221                    return new XMLMultiElementArray((XMLMultiElementStruct)struct.duplicate(deepCopy));
222            }
223            
224    
225            /**
226             * @see railo.runtime.type.Collection#get(java.lang.String)
227             */
228            public Object get(String key) throws PageException {
229                    return struct.get(key);
230            }
231    
232            /**
233             * @see railo.runtime.type.Collection#get(railo.runtime.type.Collection.Key)
234             */
235            public Object get(Key key) throws PageException {
236                    return struct.get(key);
237            }
238    
239            /**
240             * @see railo.runtime.type.Collection#get(java.lang.String, java.lang.Object)
241             */
242            public Object get(String key, Object defaultValue) {
243                    return struct.get(key,defaultValue);
244            }
245    
246            /**
247             * @see railo.runtime.type.Collection#get(railo.runtime.type.Collection.Key, java.lang.Object)
248             */
249            public Object get(Key key, Object defaultValue) {
250                    return struct.get(key,defaultValue);
251            }
252    
253            /**
254             *
255             * @see railo.runtime.type.Collection#keys()
256             */
257            public Key[] keys() {
258                    return struct.getInnerArray().keys();
259            }
260    
261            /**
262             *
263             * @see railo.runtime.type.Collection#keysAsString()
264             */
265            public String[] keysAsString() {
266                    return struct.getInnerArray().keysAsString();
267            }
268    
269            /**
270             * @see railo.runtime.type.Collection#remove(java.lang.String)
271             */
272            public Object remove(String key) throws PageException {
273                    return struct.remove(key);
274            }
275    
276            /**
277             * @see railo.runtime.type.Collection#remove(railo.runtime.type.Collection.Key)
278             */
279            public Object remove(Key key) throws PageException {
280                    return struct.remove(key);
281            }
282    
283            /**
284             * @see railo.runtime.type.Collection#removeEL(railo.runtime.type.Collection.Key)
285             */
286            public Object removeEL(Key key) {
287                    return struct.removeEL(key);
288            }
289    
290            /**
291             *
292             * @see railo.runtime.type.Collection#set(java.lang.String, java.lang.Object)
293             */
294            public Object set(String key, Object value) throws PageException {
295                    return struct.set(key, value);
296            }
297    
298            /**
299             *
300             * @see railo.runtime.type.Collection#set(railo.runtime.type.Collection.Key, java.lang.Object)
301             */
302            public Object set(Key key, Object value) throws PageException {
303                    return struct.set(key, value);
304            }
305    
306            /**
307             * @see railo.runtime.type.Collection#setEL(java.lang.String, java.lang.Object)
308             */
309            public Object setEL(String key, Object value) {
310                    return struct.setEL(key, value);
311            }
312    
313            /**
314             * @see railo.runtime.type.Collection#setEL(railo.runtime.type.Collection.Key, java.lang.Object)
315             */
316            public Object setEL(Key key, Object value) {
317                    return struct.setEL(key, value);
318            }
319    
320            /**
321             * @see railo.runtime.type.Collection#size()
322             */
323            public int size() {
324                    return struct.getInnerArray().size();
325            }
326    
327            /**
328             * @see railo.runtime.dump.Dumpable#toDumpData(railo.runtime.PageContext, int)
329             */
330            public DumpData toDumpData(PageContext pageContext, int maxlevel, DumpProperties dp) {
331                    return struct.toDumpData(pageContext, maxlevel,dp);
332            }
333    
334            /**
335             * @see railo.runtime.type.Iteratorable#iterator()
336             */
337            public Iterator iterator() {
338                    return struct.getInnerArray().iterator();
339            }
340    
341            /**
342             * @see railo.runtime.type.Iteratorable#keyIterator()
343             */
344            public Iterator keyIterator() {
345                    return new ArrayIterator(keysAsString());
346            }
347    
348            /**
349             *
350             * @see railo.runtime.op.Castable#castToBooleanValue()
351             */
352            public boolean castToBooleanValue() throws PageException {
353                    return struct.castToBooleanValue();
354            }
355        
356        /**
357         * @see railo.runtime.op.Castable#castToBoolean(java.lang.Boolean)
358         */
359        public Boolean castToBoolean(Boolean defaultValue) {
360            return struct.castToBoolean(defaultValue);
361        }
362    
363            /**
364             * @see railo.runtime.op.Castable#castToDateTime()
365             */
366            public DateTime castToDateTime() throws PageException {
367                    return struct.castToDateTime();
368            }
369        
370        /**
371         * @see railo.runtime.op.Castable#castToDateTime(railo.runtime.type.dt.DateTime)
372         */
373        public DateTime castToDateTime(DateTime defaultValue) {
374            return struct.castToDateTime(defaultValue);
375        }
376    
377            /**
378             * @see railo.runtime.op.Castable#castToDoubleValue()
379             */
380            public double castToDoubleValue() throws PageException {
381                    return struct.castToDoubleValue();
382            }
383        
384        /**
385         * @see railo.runtime.op.Castable#castToDoubleValue(double)
386         */
387        public double castToDoubleValue(double defaultValue) {
388            return struct.castToDoubleValue(defaultValue);
389        }
390    
391            /**
392             *
393             * @see railo.runtime.op.Castable#castToString()
394             */
395            public String castToString() throws PageException {
396                    return struct.castToString();
397            }
398            
399            /**
400             * @see railo.runtime.type.util.StructSupport#castToString(java.lang.String)
401             */
402            public String castToString(String defaultValue) {
403                    return struct.castToString(defaultValue);
404            }
405    
406            /**
407             * @see railo.runtime.op.Castable#compareTo(java.lang.String)
408             */
409            public int compareTo(String str) throws PageException {
410                    return struct.compareTo(str);
411            }
412    
413            /**
414             * @see railo.runtime.op.Castable#compareTo(boolean)
415             */
416            public int compareTo(boolean b) throws PageException {
417                    return struct.compareTo(b);
418            }
419    
420            /**
421             * @see railo.runtime.op.Castable#compareTo(double)
422             */
423            public int compareTo(double d) throws PageException {
424                    return struct.compareTo(d);
425            }
426    
427            /**
428             * @see railo.runtime.op.Castable#compareTo(railo.runtime.type.dt.DateTime)
429             */
430            public int compareTo(DateTime dt) throws PageException {
431                    return struct.compareTo(dt);
432            }
433            
434            /**
435             * @see java.lang.Object#clone()
436             */
437            public Object clone() {
438                    return duplicate(true);
439            }
440    
441            public boolean add(Object o) {
442                    // TODO Auto-generated method stub
443                    return false;
444            }
445    
446            /**
447             * @see railo.runtime.type.Sizeable#sizeOf()
448             */
449            public long sizeOf() {
450                    return ArrayUtil.sizeOf((List)this);
451            }
452    
453    }