001: /*
002: * Copyright (c) 2002-2008 Gargoyle Software Inc. All rights reserved.
003: *
004: * Redistribution and use in source and binary forms, with or without
005: * modification, are permitted provided that the following conditions are met:
006: *
007: * 1. Redistributions of source code must retain the above copyright notice,
008: * this list of conditions and the following disclaimer.
009: * 2. Redistributions in binary form must reproduce the above copyright notice,
010: * this list of conditions and the following disclaimer in the documentation
011: * and/or other materials provided with the distribution.
012: * 3. The end-user documentation included with the redistribution, if any, must
013: * include the following acknowledgment:
014: *
015: * "This product includes software developed by Gargoyle Software Inc.
016: * (http://www.GargoyleSoftware.com/)."
017: *
018: * Alternately, this acknowledgment may appear in the software itself, if
019: * and wherever such third-party acknowledgments normally appear.
020: * 4. The name "Gargoyle Software" must not be used to endorse or promote
021: * products derived from this software without prior written permission.
022: * For written permission, please contact info@GargoyleSoftware.com.
023: * 5. Products derived from this software may not be called "HtmlUnit", nor may
024: * "HtmlUnit" appear in their name, without prior written permission of
025: * Gargoyle Software Inc.
026: *
027: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
028: * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
029: * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GARGOYLE
030: * SOFTWARE INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
031: * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
032: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
033: * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
034: * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
035: * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
036: * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
037: */
038: package com.gargoylesoftware.htmlunit.javascript;
039:
040: import java.util.Iterator;
041:
042: import org.apache.commons.collections.map.ListOrderedMap;
043: import org.mozilla.javascript.Scriptable;
044:
045: import com.gargoylesoftware.htmlunit.html.HtmlAttr;
046: import com.gargoylesoftware.htmlunit.html.HtmlElement;
047:
048: /**
049: * A collection of nodes that can be accessed by name.
050: *
051: * @version $Revision: 2132 $
052: * @author Daniel Gredler
053: * @see <a href="http://www.w3.org/TR/DOM-Level-2-Core/core.html#ID-1780488922">DOM Level 2 Core Spec</a>
054: * @see <a href="http://msdn2.microsoft.com/en-us/library/ms763824.aspx">IXMLDOMNamedNodeMap</a>
055: */
056: public class NamedNodeMap extends SimpleScriptable implements
057: ScriptableWithFallbackGetter {
058:
059: private static final long serialVersionUID = -1910087049570242560L;
060:
061: private ListOrderedMap nodes_;
062:
063: /**
064: * Rhino requires default constructors.
065: */
066: public NamedNodeMap() {
067: // Empty.
068: }
069:
070: /**
071: * Creates a new named node map for the specified element.
072: *
073: * @param element the owning element
074: */
075: public NamedNodeMap(final HtmlElement element) {
076: nodes_ = new ListOrderedMap();
077: for (final Iterator i = element.getAttributeEntriesIterator(); i
078: .hasNext();) {
079: final HtmlAttr attr = (HtmlAttr) i.next();
080: nodes_.put(attr.getName(), attr);
081: }
082: setParentScope(element.getScriptObject());
083: setPrototype(getPrototype(getClass()));
084: }
085:
086: /**
087: * Returns the element at the specified index, or <tt>NOT_FOUND</tt> if the index is invalid.
088: *
089: * {@inheritDoc}
090: */
091: public final Object get(final int index, final Scriptable start) {
092: final NamedNodeMap map = (NamedNodeMap) start;
093: if (index >= 0 && index < map.nodes_.size()) {
094: final HtmlAttr attr = (HtmlAttr) map.nodes_.getValue(index);
095: return attr.getScriptObject();
096: } else {
097: return NOT_FOUND;
098: }
099: }
100:
101: /**
102: * Returns the element with the specified name, or <tt>NOT_FOUND</tt> if the name is invalid.
103: *
104: * {@inheritDoc}
105: */
106: public Object getWithFallback(final String name) {
107: final HtmlAttr attr = (HtmlAttr) nodes_.get(name);
108: if (attr != null) {
109: return attr.getScriptObject();
110: } else {
111: return NOT_FOUND;
112: }
113: }
114:
115: /**
116: * Gets the specified attribute.
117: * @param name attribute name.
118: * @return The attribute node, <code>null</code> if the attribute is not defined
119: */
120: public Object jsxFunction_getNamedItem(final String name) {
121: final HtmlAttr attr = (HtmlAttr) nodes_.get(name);
122: if (attr != null) {
123: return attr.getScriptObject();
124: } else {
125: return null;
126: }
127: }
128:
129: /**
130: * Returns the number of attributes in this named node map.
131: * @return the number of attributes in this named node map
132: */
133: public int jsxGet_length() {
134: return nodes_.size();
135: }
136:
137: }
|