Thread: [Htmlparser-cvs] htmlparser/src/org/htmlparser/util NodeList.java,1.59,1.60
Brought to you by:
derrickoswald
|
From: Derrick O. <der...@us...> - 2005-09-18 23:00:35
|
Update of /cvsroot/htmlparser/htmlparser/src/org/htmlparser/util In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv23605/src/org/htmlparser/util Modified Files: NodeList.java Log Message: Add remove(Node) method and code suggested by Matthew Buckett. Index: NodeList.java =================================================================== RCS file: /cvsroot/htmlparser/htmlparser/src/org/htmlparser/util/NodeList.java,v retrieving revision 1.59 retrieving revision 1.60 diff -C2 -d -r1.59 -r1.60 *** NodeList.java 19 Jun 2005 12:01:14 -0000 1.59 --- NodeList.java 18 Sep 2005 23:00:27 -0000 1.60 *************** *** 35,39 **** import org.htmlparser.visitors.NodeVisitor; ! public class NodeList implements Serializable { private static final int INITIAL_CAPACITY=10; //private static final int CAPACITY_INCREMENT=20; --- 35,40 ---- import org.htmlparser.visitors.NodeVisitor; ! public class NodeList implements Serializable ! { private static final int INITIAL_CAPACITY=10; //private static final int CAPACITY_INCREMENT=20; *************** *** 42,71 **** private int capacity; private int capacityIncrement; - private int numberOfAdjustments; ! public NodeList() { ! size = 0; ! capacity = INITIAL_CAPACITY; ! nodeData = newNodeArrayFor(capacity); ! capacityIncrement = capacity*2; ! numberOfAdjustments = 0; } ! /** * Create a one element node list. * @param node The initial node to add. */ ! public NodeList(Node node) { this (); add (node); } ! ! public void add(Node node) { ! if (size==capacity) ! adjustVectorCapacity(); ! nodeData[size++]=node; } ! /** * Add another node list to this one. --- 43,69 ---- private int capacity; private int capacityIncrement; ! public NodeList () ! { ! removeAll (); } ! /** * Create a one element node list. * @param node The initial node to add. */ ! public NodeList (Node node) { this (); add (node); } ! ! public void add (Node node) ! { ! if (size == capacity) ! adjustVectorCapacity (); ! nodeData[size++] = node; } ! /** * Add another node list to this one. *************** *** 77,192 **** add (list.nodeData[i]); } ! /** * Insert the given node at the head of the list. * @param node The new first element. */ ! public void prepend(Node node) { ! if (size==capacity) ! adjustVectorCapacity(); System.arraycopy (nodeData, 0, nodeData, 1, size); size++; nodeData[0]=node; } ! ! private void adjustVectorCapacity() { capacity += capacityIncrement; capacityIncrement *= 2; Node oldData [] = nodeData; ! nodeData = newNodeArrayFor(capacity); ! System.arraycopy(oldData, 0, nodeData, 0, size); ! numberOfAdjustments++; } ! ! private Node[] newNodeArrayFor(int capacity) { return new Node[capacity]; } ! ! public int size() { return size; } ! ! public Node elementAt(int i) { return nodeData[i]; } ! ! public int getNumberOfAdjustments() { ! return numberOfAdjustments; ! } ! ! public SimpleNodeIterator elements() { ! return new SimpleNodeIterator() { int count = 0; ! ! public boolean hasMoreNodes() { return count < size; } ! ! public Node nextNode() { ! synchronized (NodeList.this) { ! if (count < size) { ! return nodeData[count++]; } ! } ! throw new NoSuchElementException("Vector Enumeration"); } }; } ! ! public Node [] toNodeArray() { ! Node [] nodeArray = newNodeArrayFor(size); ! System.arraycopy(nodeData, 0, nodeArray, 0, size); return nodeArray; } ! ! public void copyToNodeArray(Node[] array) { ! System.arraycopy(nodeData, 0, array, 0, size); } ! ! public String asString() { ! StringBuffer buff = new StringBuffer(); for (int i=0;i<size;i++) ! buff.append(nodeData[i].toPlainTextString()); ! return buff.toString(); } ! /** * Convert this nodelist into the equivalent HTML. - * @deprecated Use {@link #toHtml}. * @return The contents of the list as HTML text. */ ! public String asHtml() { ! return (toHtml ()); } ! /** ! * Convert this nodelist into the equivalent HTML. ! * @return The contents of the list as HTML text. */ ! public String toHtml() { - StringBuffer buff = new StringBuffer(); - for (int i=0;i<size;i++) - buff.append(nodeData[i].toHtml()); - return buff.toString(); - } - - public Node remove(int index) { Node ret; ret = nodeData[index]; ! System.arraycopy(nodeData, index+1, nodeData, index, size-index-1); nodeData[size-1] = null; size--; return (ret); } ! ! public void removeAll() { size = 0; capacity = INITIAL_CAPACITY; ! nodeData = newNodeArrayFor(capacity); ! capacityIncrement = capacity*2; ! numberOfAdjustments = 0; } --- 75,243 ---- add (list.nodeData[i]); } ! /** * Insert the given node at the head of the list. * @param node The new first element. */ ! public void prepend (Node node) { ! if (size == capacity) ! adjustVectorCapacity (); System.arraycopy (nodeData, 0, nodeData, 1, size); size++; nodeData[0]=node; } ! ! private void adjustVectorCapacity () ! { capacity += capacityIncrement; capacityIncrement *= 2; Node oldData [] = nodeData; ! nodeData = newNodeArrayFor (capacity); ! System.arraycopy (oldData, 0, nodeData, 0, size); } ! ! private Node[] newNodeArrayFor (int capacity) ! { return new Node[capacity]; } ! ! public int size () ! { return size; } ! ! public Node elementAt (int i) ! { return nodeData[i]; } ! ! public SimpleNodeIterator elements () ! { ! return new SimpleNodeIterator () ! { int count = 0; ! ! public boolean hasMoreNodes () ! { return count < size; } ! ! public Node nextNode () ! { ! synchronized (NodeList.this) ! { ! if (count < size) ! { ! return nodeData[count++]; ! } } ! throw new NoSuchElementException ("Vector Enumeration"); } }; } ! ! public Node [] toNodeArray () ! { ! Node [] nodeArray = newNodeArrayFor (size); ! System.arraycopy (nodeData, 0, nodeArray, 0, size); return nodeArray; } ! ! public void copyToNodeArray (Node[] array) ! { ! System.arraycopy (nodeData, 0, array, 0, size); } ! ! public String asString () ! { ! StringBuffer buff = new StringBuffer (); for (int i=0;i<size;i++) ! buff.append (nodeData[i].toPlainTextString ()); ! return buff.toString (); } ! /** * Convert this nodelist into the equivalent HTML. * @return The contents of the list as HTML text. */ ! public String toHtml () { ! StringBuffer buff = new StringBuffer (); ! for (int i=0;i<size;i++) ! buff.append (nodeData[i].toHtml ()); ! return buff.toString (); } ! /** ! * Remove the node at index. ! * @param index The index of the node to remove. ! * @return The node that was removed. */ ! public Node remove (int index) { Node ret; + ret = nodeData[index]; ! System.arraycopy (nodeData, index+1, nodeData, index, size - index - 1); nodeData[size-1] = null; size--; + return (ret); } ! ! public void removeAll () ! { size = 0; capacity = INITIAL_CAPACITY; ! nodeData = newNodeArrayFor (capacity); ! capacityIncrement = capacity * 2; ! } ! ! /** ! * Check to see if the NodeList contains the supplied Node. ! * @param node The node to look for. ! * @return True is the Node is in this NodeList. ! */ ! public boolean contains (Node node) ! { ! return (-1 != indexOf (node)); ! } ! ! /** ! * Finds the index of the supplied Node. ! * @param node The node to look for. ! * @return The index of the node in the list or -1 if it isn't found. ! */ ! public int indexOf (Node node) ! { ! int ret; ! ! ret = -1; ! for (int i = 0; (i < size) && (-1 == ret); i++) ! if (nodeData[i].equals (node)) ! ret = i; ! ! return (ret); ! } ! ! /** ! * Remove the supplied Node from the list. ! * @param node The node to remove. ! * @return True if the node was found and removed from the list. ! */ ! public boolean remove (Node node) ! { ! int index; ! boolean ret; ! ! ret = false; ! if (-1 != (index = indexOf (node))) ! { ! remove (index); ! ret = true; ! } ! ! return (ret); } *************** *** 198,205 **** public String toString() { ! StringBuffer text = new StringBuffer(); ! for (int i=0;i<size;i++) ! text.append (nodeData[i]); ! return (text.toString ()); } --- 249,259 ---- public String toString() { ! StringBuffer ret; ! ! ret = new StringBuffer (); ! for (int i = 0; i < size; i++) ! ret.append (nodeData[i]); ! ! return (ret.toString ()); } |