001: /*
002: * Copyright (c) 1998-2008 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 Nam Nguyen
028: */
029:
030: package com.caucho.quercus.lib.dom;
031:
032: import com.caucho.quercus.env.Env;
033: import com.caucho.quercus.env.LongValue;
034: import com.caucho.quercus.env.ObjectValue;
035: import com.caucho.quercus.env.TraversableDelegate;
036: import com.caucho.quercus.env.Value;
037:
038: import java.util.Iterator;
039: import java.util.Map;
040:
041: public class DOMNodeListDelegate implements TraversableDelegate {
042: public DOMNodeListDelegate() {
043: }
044:
045: public Iterator<Value> getKeyIterator(Env env, ObjectValue obj) {
046: return new DOMNodeListKeyIterator((DOMNodeList) obj
047: .toJavaObject());
048: }
049:
050: public Iterator<Value> getValueIterator(Env env, ObjectValue obj) {
051: return new DOMNodeListValueIterator(env, (DOMNodeList) obj
052: .toJavaObject());
053: }
054:
055: public Iterator<Map.Entry<Value, Value>> getIterator(Env env,
056: ObjectValue obj) {
057: return new DOMNodeListIterator(env, (DOMNodeList) obj
058: .toJavaObject());
059: }
060:
061: public class DOMNodeListKeyIterator implements Iterator<Value> {
062: private DOMNodeList _list;
063: private int _index;
064:
065: public DOMNodeListKeyIterator(DOMNodeList list) {
066: _list = list;
067: }
068:
069: public boolean hasNext() {
070: return _index < _list.getLength();
071: }
072:
073: public Value next() {
074: return LongValue.create(_index++);
075: }
076:
077: public void remove() {
078: throw new UnsupportedOperationException();
079: }
080: }
081:
082: public class DOMNodeListValueIterator implements Iterator<Value> {
083: private Env _env;
084: private DOMNodeList _list;
085: private int _index;
086:
087: public DOMNodeListValueIterator(Env env, DOMNodeList list) {
088: _env = env;
089: _list = list;
090: }
091:
092: public boolean hasNext() {
093: return _index < _list.getLength();
094: }
095:
096: public Value next() {
097: return _env.wrapJava(_list.item(_index++));
098: }
099:
100: public void remove() {
101: throw new UnsupportedOperationException();
102: }
103: }
104:
105: public class DOMNodeListIterator implements
106: Iterator<Map.Entry<Value, Value>> {
107: private Env _env;
108: private DOMNodeList _list;
109: private int _index;
110:
111: public DOMNodeListIterator(Env env, DOMNodeList list) {
112: _env = env;
113: _list = list;
114: }
115:
116: public boolean hasNext() {
117: return _index < _list.getLength();
118: }
119:
120: public Map.Entry<Value, Value> next() {
121: return new DOMNodeListEntry(_index, _env.wrapJava(_list
122: .item(_index++)));
123: }
124:
125: public void remove() {
126: throw new UnsupportedOperationException();
127: }
128: }
129:
130: public class DOMNodeListEntry implements Map.Entry<Value, Value> {
131: private int _key;
132: private Value _value;
133:
134: public DOMNodeListEntry(int index, Value value) {
135: _key = index;
136: _value = value;
137: }
138:
139: public Value getKey() {
140: return LongValue.create(_key);
141: }
142:
143: public Value getValue() {
144: return _value;
145: }
146:
147: public Value setValue(Value value) {
148: throw new UnsupportedOperationException();
149: }
150: }
151: }
|