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 java.util.Iterator;
017: import org.itsnat.core.domutil.ItsNatTreeWalker;
018: import org.itsnat.impl.core.ItsNatDocumentImpl;
019: import org.itsnat.impl.core.util.*;
020: import org.w3c.dom.Element;
021: import org.w3c.dom.Node;
022: import org.w3c.dom.html.HTMLElement;
023:
024: /**
025: * El DOM es el que manda, puede modificarse añadiendo nuevos elementos
026: * directamente via DOM, la lista "se entera" de los cambios, el coste: más lento.
027: *
028: * @author jmarranz
029: */
030: public class ElementListFreeSlaveDefaultImpl extends
031: ElementListFreeSlaveImpl {
032: /**
033: * Creates a new instance of ElementListFreeSlaveDefaultImpl
034: */
035: public ElementListFreeSlaveDefaultImpl(Element parentElement,
036: ItsNatDocumentImpl itsNatDoc) {
037: super (parentElement, itsNatDoc);
038: }
039:
040: public static boolean isEmpty(Element parentElement) {
041: // Este algoritmo es mucho más rápido que (getLength() != 0)
042: // pues no necesita contar todos
043: // Filtramos los nodos de texto
044: Element child = ItsNatTreeWalker
045: .getFirstChildElement(parentElement);
046: return (child == null);
047: }
048:
049: public boolean isEmpty() {
050: return isEmpty(parentElement);
051: }
052:
053: public int getLength() {
054: // Filtramos los nodos de texto
055: return ItsNatTreeWalker.getChildElementCount(parentElement);
056: }
057:
058: public static Element getElementAt(Element parentElement, int index) {
059: if (index < 0)
060: return null;
061:
062: // Filtramos los nodos de texto
063: int count = 0;
064: Element child = ItsNatTreeWalker
065: .getFirstChildElement(parentElement);
066: while (child != null) {
067: if (count == index)
068: return child;
069: count++;
070: child = ItsNatTreeWalker.getNextSiblingElement(child);
071: }
072: return null; // Fuera de rango
073: }
074:
075: public Element getElementAt(int index) {
076: return getElementAt(parentElement, index);
077: }
078:
079: public Element getFirstElement() {
080: // Filtramos los nodos de texto
081: return ItsNatTreeWalker.getFirstChildElement(parentElement); // Puede ser null
082: }
083:
084: public Element getLastElement() {
085: // Filtramos los nodos de texto
086: return ItsNatTreeWalker.getLastChildElement(parentElement);
087: }
088:
089: public int indexOfElement(Element node) {
090: return indexOfElement(node, parentElement);
091: }
092:
093: public int lastIndexOfElement(Element node) {
094: return lastIndexOfElement(node, parentElement);
095: }
096:
097: public static int indexOfElement(Element node, Element parentElement) {
098: if (node == null)
099: return -1;
100:
101: // Filtramos los nodos de texto etc
102: int count = 0;
103: Element child = ItsNatTreeWalker
104: .getFirstChildElement(parentElement);
105: while (child != null) {
106: if (child == node)
107: return count;
108: count++;
109: child = ItsNatTreeWalker.getNextSiblingElement(child);
110: }
111: return -1; // El nodo no forma parte de la lista
112: }
113:
114: public static int lastIndexOfElement(Element node,
115: Element parentElement) {
116: if (node == null)
117: return -1;
118:
119: // Buscamos hacia adelante pues si buscamos desde el final necesitaremos
120: // el getLength() que de todas formas recorrerá todos los elementos
121:
122: // Filtramos los nodos de texto etc
123: int count = 0;
124: int index = -1;
125: Element child = ItsNatTreeWalker
126: .getFirstChildElement(parentElement);
127: while (child != null) {
128: if (child == node)
129: index = count;
130: count++;
131: child = ItsNatTreeWalker.getNextSiblingElement(child);
132: }
133: return index; // Recordará el último
134: }
135:
136: public void fillElements(Element[] elemList) {
137: if (elemList.length > 0) {
138: int i = 0;
139: Element child = ItsNatTreeWalker
140: .getFirstChildElement(parentElement);
141: while (child != null) {
142: elemList[i] = child;
143:
144: i++;
145: child = ItsNatTreeWalker.getNextSiblingElement(child);
146: }
147: }
148: }
149:
150: public Element getNextSiblingElement(int index, Element ref) {
151: return getNextSiblingElement(ref);
152: }
153:
154: public Element getPreviousSiblingElement(int index, Element ref) {
155: return getPreviousSiblingElement(ref);
156: }
157: }
|