001: /*
002: ItsNat Java Web Application Framework
003: Copyright (C) 2007 Innowhere Software Services S.L., Spanish Company
004: Author: Jose Maria Arranz Santamaria
005:
006: This program is free software: you can redistribute it and/or modify
007: it under the terms of the GNU Affero General Public License as published by
008: the Free Software Foundation, either version 3 of the License, or
009: (at your option) any later version. See the GNU Affero General Public
010: License for more details. See the copy of the GNU Affero General Public License
011: included in this program. If not, see <http://www.gnu.org/licenses/>.
012: */
013:
014: package org.itsnat.impl.core.domutil;
015:
016: import org.itsnat.impl.core.ItsNatDocumentImpl;
017: import org.itsnat.impl.core.util.*;
018: import org.w3c.dom.Element;
019: import org.w3c.dom.Node;
020: import org.w3c.dom.html.HTMLCollection;
021: import org.w3c.dom.html.HTMLElement;
022:
023: /**
024: * Permite que un HTMLCollection pueda ser visto como un NodeList
025: * Es una lista "esclava" pues si Xerces funciona bien el HTMLCollection
026: * se actualizará automáticamente cuando directamente añadamos o quitemos
027: * via DOM elementos.
028: * Esta actualización automática ante cambios hace que la HTMLCollection
029: * sea más rápida (en teoría) que la colección ElementListFreeSlaveImpl dentro
030: * de las "esclavas"
031: *
032: * @author jmarranz
033: */
034: public abstract class HTMLCollectionAsElementListImpl extends
035: ElementListFreeSlaveImpl {
036: /**
037: * Creates a new instance of ItsNatHTMLElementCollectionImpl
038: */
039: public HTMLCollectionAsElementListImpl(HTMLElement parentElement,
040: ItsNatDocumentImpl itsNatDoc) {
041: super (parentElement, itsNatDoc);
042:
043: // Se supone que parentElement es el padre de los elementos de la colección
044: }
045:
046: public int getLength() {
047: return getHTMLCollection().getLength();
048: }
049:
050: public Element getElementAt(int index) {
051: return (Element) getHTMLCollection().item(index);
052: }
053:
054: public boolean isEmpty() {
055: return (getHTMLCollection().getLength() == 0);
056: }
057:
058: public Element getFirstElement() {
059: return (Element) getHTMLCollection().item(0);
060: }
061:
062: public Element getLastElement() {
063: return (Element) getHTMLCollection().item(getLength() - 1);
064: }
065:
066: public int indexOfElement(Element node) {
067: return indexOfElement(node, getHTMLCollection());
068: }
069:
070: public int lastIndexOfElement(Element node) {
071: return lastIndexOfElement(node, getHTMLCollection());
072: }
073:
074: public static int indexOfElement(Element node, HTMLCollection col) {
075: if (node == null)
076: return -1;
077:
078: int len = col.getLength();
079: for (int i = 0; i < len; i++) {
080: Element child = (Element) col.item(i);
081: if (child == node)
082: return i;
083: }
084: return -1; // El nodo no forma parte de la lista
085: }
086:
087: public static int lastIndexOfElement(Element node,
088: HTMLCollection col) {
089: if (node == null)
090: return -1;
091:
092: int len = col.getLength();
093: for (int i = len - 1; i >= 0; i--) {
094: Element child = (Element) col.item(i);
095: if (child == node)
096: return i;
097: }
098: return -1; // El nodo no forma parte de la lista
099: }
100:
101: public void fillElements(Element[] elemList) {
102: for (int i = 0; i < elemList.length; i++) {
103: Element elem = getElementAt(i);
104: elemList[i] = elem;
105: }
106: }
107:
108: public Element getNextSiblingElement(int index, Element ref) {
109: return getElementAt(index + 1);
110: }
111:
112: public Element getPreviousSiblingElement(int index, Element ref) {
113: return getElementAt(index - 1);
114: }
115:
116: public abstract HTMLCollection getHTMLCollection();
117:
118: }
|