KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > es > ESArray


1 /*
2  * Copyright (c) 1998-2006 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  * Free SoftwareFoundation, Inc.
23  * 59 Temple Place, Suite 330
24  * Boston, MA 02111-1307 USA
25  *
26  * @author Scott Ferguson
27  */

28
29 package com.caucho.es;
30
31 import com.caucho.util.CharBuffer;
32 import com.caucho.util.IntMap;
33
34 import java.util.ArrayList JavaDoc;
35 import java.util.Iterator JavaDoc;
36
37 /**
38  * JavaScript object
39  */

40 class ESArray extends ESObject {
41   static ESId LENGTH = ESId.intern("length");
42
43   /**
44    * Create a new object based on a prototype
45    */

46   ESArray()
47   {
48     super("Array", null);
49     put(LENGTH, ESNumber.create(0), DONT_ENUM|DONT_DELETE);
50   }
51
52   public void setProperty(int i, ESBase value) throws Throwable JavaDoc
53   {
54     super.setProperty(ESString.create(i), value);
55
56     int length = getProperty(LENGTH).toInt32();
57     if (i >= length)
58       super.setProperty(LENGTH, ESNumber.create(i + 1));
59   }
60
61   public void setProperty(ESString key, ESBase value) throws Throwable JavaDoc
62   {
63     if (key.equals(LENGTH)) {
64       int oldLength;
65       int newLength;
66
67       try {
68     oldLength = getProperty(LENGTH).toInt32();
69     newLength = value.toInt32();
70       } catch (ESException e) {
71     return;
72       }
73  
74       for (int i = newLength; i < oldLength; i++) {
75     try {
76       delete(ESString.create(i));
77     } catch (Exception JavaDoc e) {
78     }
79       }
80
81       if (newLength < 0)
82     newLength = 0;
83
84       super.setProperty(LENGTH, ESNumber.create(newLength));
85       return;
86     }
87
88     super.setProperty(key, value);
89
90     try {
91       int keyValue = Integer.parseInt(key.toString());
92       int length = getProperty(LENGTH).toInt32();
93
94       if (keyValue >= length)
95     super.setProperty(LENGTH, ESNumber.create(keyValue + 1));
96     } catch (Exception JavaDoc e) {
97     }
98   }
99
100   static ESString arrayToSource(ESObject obj, IntMap map, boolean isLoopPass)
101     throws Throwable JavaDoc
102   {
103     Global resin = Global.getGlobalProto();
104     CharBuffer cb = new CharBuffer();
105
106     int mark = map.get(obj);
107     
108     if (mark > 0 && isLoopPass)
109       return null;
110     else if (mark > 0) {
111       cb.append("#" + mark + "=");
112       map.put(obj, -mark);
113     } else if (mark == 0 && isLoopPass) {
114       map.put(obj, resin.addMark());
115       return null;
116     } else if (mark < 0 && ! isLoopPass) {
117       return ESString.create("#" + -mark + "#");
118     }
119
120     if (isLoopPass)
121       map.put(obj, 0);
122
123     int len = obj.getProperty(LENGTH).toInt32();
124
125     boolean noValue = true;
126     cb.append("[");
127     for (int i = 0; i < len; i++) {
128       if (i != 0)
129     cb.append(", ");
130
131       ESBase value = obj.hasProperty(i);
132
133       if (value != null && value != esNull &&
134       value != esUndefined && value != esEmpty) {
135     if (isLoopPass)
136       value.toSource(map, isLoopPass);
137     else
138       cb.append(value.toSource(map, isLoopPass));
139       }
140       else if (i + 1 == len)
141     cb.append(",");
142     }
143     cb.append("]");
144
145     return ESString.create(cb.toString());
146   }
147
148   public Iterator JavaDoc keys() throws ESException
149   {
150     ArrayList JavaDoc values = new ArrayList JavaDoc();
151
152     try {
153       int len = getProperty(LENGTH).toInt32();
154       for (int i = 0; i < len; i++) {
155         Object JavaDoc prop = getProperty(i);
156         // spec says add the key integers
157
values.add(String.valueOf(i));
158       }
159     } catch (Throwable JavaDoc e) {
160       throw ESWrapperException.create(e);
161     }
162
163     return values.iterator();
164   }
165
166   public ESString toSource(IntMap map, boolean isLoopPass) throws Throwable JavaDoc
167   {
168     return arrayToSource(this, map, isLoopPass);
169   }
170
171   public ESObject dup()
172   {
173     return new ESArray(false);
174   }
175
176   protected ESArray(boolean dummy) {}
177 }
178
Popular Tags