001: /*
002: * Copyright (c) 1998-2007 Caucho Technology -- all rights reserved
003: *
004: * This file is part of Resin(R) Open Source
005: *
006: * Each copy or derived work must preserve the copyright notice and this
007: * notice unmodified.
008: *
009: * Resin Open Source is free software; you can redistribute it and/or modify
010: * it under the terms of the GNU General Public License as published by
011: * the Free Software Foundation; either version 2 of the License, or
012: * (at your option) any later version.
013: *
014: * Resin Open Source is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
017: * of NON-INFRINGEMENT. See the GNU General Public License for more
018: * details.
019: *
020: * You should have received a copy of the GNU General Public License
021: * along with Resin Open Source; if not, write to the
022: *
023: * Free Software Foundation, Inc.
024: * 59 Temple Place, Suite 330
025: * Boston, MA 02111-1307 USA
026: *
027: * @author Sam
028: */
029:
030: package com.caucho.quercus.lib.spl;
031:
032: import com.caucho.quercus.annotation.Name;
033: import com.caucho.quercus.annotation.Optional;
034: import com.caucho.quercus.env.ArrayValue;
035: import com.caucho.quercus.env.Callback;
036: import com.caucho.quercus.env.Env;
037: import com.caucho.quercus.env.StringValue;
038: import com.caucho.quercus.env.UnsetValue;
039: import com.caucho.quercus.env.Value;
040: import com.caucho.quercus.lib.ArrayModule;
041: import com.caucho.vfs.WriteStream;
042:
043: import java.io.IOException;
044: import java.util.IdentityHashMap;
045: import java.util.Map;
046:
047: public class ArrayIterator implements Traversable, SeekableIterator,
048: ArrayAccess, Countable {
049: public static final int STD_PROP_LIST = 0x00000001;
050: public static final int ARRAY_AS_PROPS = 0x00000002;
051:
052: private Env _env;
053: private final Value _value;
054: private int _flags;
055:
056: private java.util.Iterator<Map.Entry<Value, Value>> _iterator;
057: private Map.Entry<Value, Value> _current;
058:
059: @Name("__construct")
060: public ArrayIterator(Env env, @Optional
061: Value value, @Optional
062: int flags) {
063: _env = env;
064: _value = value;
065: _flags = flags;
066:
067: resetToFirst();
068: }
069:
070: private void resetToFirst() {
071: _iterator = _value.getIterator(_env);
072: if (_iterator.hasNext())
073: _current = _iterator.next();
074: else
075: _current = null;
076: }
077:
078: public void append(Value value) {
079: _value.put(value);
080: }
081:
082: public void asort(ArrayValue array, @Optional
083: long sortFlag) {
084: sortFlag = 0; // qa/4a46
085:
086: if (_value instanceof ArrayValue)
087: ArrayModule.asort(_env, (ArrayValue) _value, sortFlag);
088: }
089:
090: public int count() {
091: return _value.getCount(_env);
092: }
093:
094: public Value current() {
095: return _current == null ? UnsetValue.UNSET : _current
096: .getValue();
097: }
098:
099: public Value getArrayCopy() {
100: return _value.copy();
101: }
102:
103: public int getFlags() {
104: return _flags;
105: }
106:
107: public Value key() {
108: return _current == null ? UnsetValue.UNSET : _current.getKey();
109: }
110:
111: public void ksort(@Optional
112: long sortFlag) {
113: if (_value instanceof ArrayValue)
114: ArrayModule.ksort(_env, (ArrayValue) _value, sortFlag);
115: }
116:
117: public void natcasesort() {
118: if (_value instanceof ArrayValue)
119: ArrayModule.natcasesort((ArrayValue) _value);
120: }
121:
122: public void natsort() {
123: if (_value instanceof ArrayValue)
124: ArrayModule.natsort((ArrayValue) _value);
125: }
126:
127: public void next() {
128: if (_iterator.hasNext())
129: _current = _iterator.next();
130: else
131: _current = null;
132: }
133:
134: public boolean offsetExists(Value offset) {
135: return _value.get(offset).isset();
136: }
137:
138: public Value offsetGet(Value offset) {
139: return _value.get(offset);
140: }
141:
142: public Value offsetSet(Value offset, Value value) {
143: return _value.put(offset, value);
144: }
145:
146: public Value offsetUnset(Value offset) {
147: return _value.remove(offset);
148: }
149:
150: public void rewind() {
151: resetToFirst();
152: }
153:
154: public void setFlags(Value flags) {
155: _flags = flags.toInt();
156: }
157:
158: public void seek(int index) {
159: resetToFirst();
160:
161: for (int i = 0; i < index; i++) {
162: if (!_iterator.hasNext()) {
163: _current = null;
164: break;
165: }
166:
167: _current = _iterator.next();
168: }
169: }
170:
171: public void uasort(Callback func, @Optional
172: long sortFlag) {
173: if (_value instanceof ArrayValue)
174: ArrayModule.uasort(_env, (ArrayValue) _value, func,
175: sortFlag);
176: }
177:
178: public void uksort(Callback func, @Optional
179: long sortFlag) {
180: if (_value instanceof ArrayValue)
181: ArrayModule.uksort(_env, (ArrayValue) _value, func,
182: sortFlag);
183: }
184:
185: public boolean valid() {
186: return _current != null;
187: }
188:
189: static private void printDepth(WriteStream out, int depth)
190: throws java.io.IOException {
191: for (int i = depth; i > 0; i--)
192: out.print(' ');
193: }
194:
195: public void varDumpImpl(Env env, WriteStream out, int depth,
196: IdentityHashMap<Value, String> valueSet) throws IOException {
197: if ((_flags & STD_PROP_LIST) != 0) {
198: // XXX: env.getThis().varDumpObject(env, out, depth, valueSet);
199: } else {
200: Value arrayValue = _value;
201:
202: out.println("object (" + arrayValue.getCount(env) + ") {");
203:
204: depth++;
205:
206: java.util.Iterator<Map.Entry<Value, Value>> iterator = arrayValue
207: .getIterator(env);
208:
209: while (iterator.hasNext()) {
210: Map.Entry<Value, Value> entry = iterator.next();
211:
212: Value key = entry.getKey();
213: Value value = entry.getValue();
214:
215: printDepth(out, 2 * depth);
216:
217: out.print("[");
218:
219: if (key instanceof StringValue)
220: out.print("\"" + key + "\"");
221: else
222: out.print(key);
223:
224: out.println("]=>");
225:
226: printDepth(out, 2 * depth);
227:
228: value.varDump(env, out, depth, valueSet);
229:
230: out.println();
231: }
232:
233: depth--;
234:
235: printDepth(out, 2 * depth);
236:
237: out.print("}");
238: }
239: }
240: }
|