Clover coverage report - Ledge Web - SNAPSHOT
Coverage timestamp: Fri Nov 17 2006 05:20:55 CET
file stats: LOC: 332   Methods: 14
NCLOC: 227   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
DOMTreeWalker.java 0% 0% 0% 0%
coverage
 1    package org.objectledge.web.test;
 2   
 3    import java.util.ArrayList;
 4   
 5    import org.w3c.dom.CharacterData;
 6    import org.w3c.dom.Element;
 7    import org.w3c.dom.Node;
 8    import org.w3c.dom.NodeList;
 9   
 10    /**
 11    * An utility class for traversig DOM trees.
 12    *
 13    * @author <a href="mailto:pablo@caltha.pl">PaweÅ‚ Potempski</a>
 14    * @version $Id: DOMTreeWalker.java,v 1.4 2005/05/20 02:04:20 rafal Exp $
 15    */
 16    public class DOMTreeWalker
 17    {
 18    private Element root;
 19   
 20    private ArrayList<Object> elements;
 21   
 22    private int currentIndex;
 23   
 24    /**
 25    * Create a DOMTreeWalker instance.
 26    *
 27    * @param root the traversal root.
 28    */
 29  0 public DOMTreeWalker(Element root)
 30    {
 31  0 this.root = root;
 32  0 this.elements = new ArrayList<Object>();
 33  0 this.currentIndex = 0;
 34  0 loadList();
 35    }
 36   
 37    /**
 38    * Resets the traversal back to origin.
 39    */
 40  0 public void reset()
 41    {
 42  0 currentIndex = 0;
 43    }
 44   
 45    /**
 46    * Loads all elements to the lookup buffer.
 47    */
 48  0 public void loadList()
 49    {
 50  0 loadNode(root);
 51    }
 52   
 53    /**
 54    * Loads the specified element and it's descendants to the lookup buffer.
 55    *
 56    * @param node the node to be loaded.
 57    */
 58  0 private void loadNode(Node node)
 59    {
 60  0 if(node instanceof CharacterData)
 61    {
 62  0 String data = ((CharacterData)node).getData();
 63  0 elements.add(data);
 64    }
 65  0 if(node instanceof Element)
 66    {
 67  0 elements.add(node);
 68  0 NodeList children = node.getChildNodes();
 69  0 for (int i = 0; i < children.getLength(); i++)
 70    {
 71  0 loadNode(children.item(i));
 72    }
 73    }
 74    }
 75   
 76    /**
 77    * Looks up element of a specified type that occurs after specific text in documents's
 78    * character data.
 79    *
 80    * @param text the text preceeding the element.
 81    * @param tagName the element type to find.
 82    * @return the first matching element.
 83    */
 84  0 public Element findElementAfterText(String text, String tagName)
 85    {
 86  0 for(; currentIndex < elements.size(); currentIndex++)
 87    {
 88  0 Object ob = elements.get(currentIndex);
 89  0 if(ob instanceof String)
 90    {
 91  0 if(((String)ob).contains(text))
 92    {
 93  0 break;
 94    }
 95    }
 96    }
 97  0 for(; currentIndex < elements.size(); currentIndex++)
 98    {
 99  0 Object ob = elements.get(currentIndex);
 100  0 if(ob instanceof Element)
 101    {
 102  0 if(tagName != null)
 103    {
 104  0 String tag = ((Element)ob).getNodeName();
 105  0 if(tagName.equals(tag.toLowerCase()))
 106    {
 107  0 return (Element)ob;
 108    }
 109    }
 110    else
 111    {
 112  0 return (Element)ob;
 113    }
 114    }
 115    }
 116  0 return null;
 117    }
 118   
 119    /**
 120    * Finds the element that contains the specific text in it's character data.
 121    *
 122    * @param text the text to find.
 123    * @param tagName the type of element to find.
 124    * @return the matching element.
 125    */
 126  0 public Element findElementWithText(String text, String tagName)
 127    {
 128  0 Element matchedElement = null;
 129  0 Element currentElement = null;
 130   
 131  0 for(; currentIndex < elements.size(); currentIndex++)
 132    {
 133  0 Object ob = elements.get(currentIndex);
 134  0 if(ob instanceof Element)
 135    {
 136  0 if(tagName != null)
 137    {
 138  0 String tag = ((Element)ob).getNodeName();
 139  0 if(tagName.equals(tag.toLowerCase()))
 140    {
 141  0 currentElement = (Element)ob;
 142    }
 143    }
 144    else
 145    {
 146  0 currentElement = (Element)ob;
 147    }
 148    }
 149  0 if(ob instanceof String)
 150    {
 151  0 if(((String)ob).contains(text))
 152    {
 153  0 matchedElement = currentElement;
 154    }
 155    }
 156    }
 157  0 return matchedElement;
 158    }
 159   
 160    /**
 161    * Returns next character data block in the lookup buffer.
 162    *
 163    * @return next character data block in the lookup buffer.
 164    */
 165  0 public String getNextText()
 166    {
 167  0 return getNextText(0);
 168    }
 169   
 170    /**
 171    * Returns character data block in the lookup buffer in specific distance from current
 172    * position.
 173    *
 174    * @param skip number of character data blocks to skip.
 175    * @return the charcter data block.
 176    */
 177  0 public String getNextText(int skip)
 178    {
 179  0 currentIndex = currentIndex + skip + 1;
 180  0 for(; currentIndex < elements.size(); currentIndex++)
 181    {
 182  0 Object ob = elements.get(currentIndex);
 183  0 if(ob instanceof String)
 184    {
 185  0 return (String)ob;
 186    }
 187    }
 188  0 return null;
 189    }
 190   
 191    /**
 192    * Returns the next element in the lookup buffer.
 193    *
 194    * @return the next element in the lookup buffer.
 195    */
 196  0 public Element getNextElement()
 197    {
 198  0 return getNextElement(0);
 199    }
 200   
 201    /**
 202    * Returns element in the lookup buffer in specific distance from current position.
 203    *
 204    * @param skip number of elements to skip.
 205    * @return the element.
 206    */
 207  0 public Element getNextElement(int skip)
 208    {
 209  0 return getNextElement(skip, null);
 210    }
 211   
 212    /**
 213    * Returns element in the lookup buffer in specific distance from current position.
 214    *
 215    * @param skip number of elements to skip.
 216    * @param tagName the expected tag name.
 217    * @return matched element, or null.
 218    */
 219  0 public Element getNextElement(int skip, String tagName)
 220    {
 221  0 currentIndex = currentIndex + skip + 1;
 222  0 for(; currentIndex < elements.size(); currentIndex++)
 223    {
 224  0 Object ob = elements.get(currentIndex);
 225  0 if(ob instanceof Element)
 226    {
 227  0 if(tagName != null)
 228    {
 229  0 String tag = ((Element)ob).getNodeName();
 230  0 if(tagName.equals(tag.toLowerCase()))
 231    {
 232  0 return (Element)ob;
 233    }
 234    }
 235    else
 236    {
 237  0 return (Element)ob;
 238    }
 239    }
 240    }
 241  0 return null;
 242    }
 243   
 244    /**
 245    * Go to the specific element in the lookup buffer.
 246    *
 247    * @param element the element.
 248    */
 249  0 public void gotoElement(Element element)
 250    {
 251  0 for(currentIndex = 0; currentIndex < elements.size(); currentIndex++)
 252    {
 253  0 Object node = elements.get(currentIndex);
 254  0 if(node instanceof Element)
 255    {
 256  0 Element el = (Element)node;
 257  0 if(el.getNodeName().equals(element.getNodeName()))
 258    {
 259  0 if(el.equals(element))
 260    {
 261  0 break;
 262    }
 263    }
 264    }
 265    }
 266    }
 267   
 268    /**
 269    * Count elements of specific type in the lookup buffer.
 270    *
 271    * @param tagName the element type.
 272    * @return the count of elements of specific type in the lookup buffer.
 273    */
 274  0 public int countTags(String tagName)
 275    {
 276  0 int i = 0;
 277  0 int counter = 0;
 278  0 for(; i < elements.size(); i++)
 279    {
 280  0 Object ob = elements.get(i);
 281  0 if(ob instanceof Element)
 282    {
 283  0 if(tagName != null)
 284    {
 285  0 String tag = ((Element)ob).getNodeName();
 286  0 if(tagName.equals(tag.toLowerCase()))
 287    {
 288  0 counter++;
 289    }
 290    }
 291    else
 292    {
 293  0 counter++;
 294    }
 295    }
 296    }
 297  0 return counter;
 298    }
 299   
 300    /**
 301    * Count child elements of a specified element of the specific types.
 302    *
 303    * @param node the node.
 304    * @param tagName types of the child nodes.
 305    * @return the count of child elements of a specified element of the specific types.
 306    */
 307  0 public static int countElements(Node node, String tagName)
 308    {
 309  0 int counter = 0;
 310  0 NodeList list = node.getChildNodes();
 311  0 for(int i = 0; i< list.getLength(); i++)
 312    {
 313  0 Node child = list.item(i);
 314  0 if(child instanceof Element)
 315    {
 316  0 if(tagName != null)
 317    {
 318  0 String tag = ((Element)child).getNodeName();
 319  0 if(tagName.equals(tag.toLowerCase()))
 320    {
 321  0 counter++;
 322    }
 323    }
 324    else
 325    {
 326  0 counter++;
 327    }
 328    }
 329    }
 330  0 return counter;
 331    }
 332    }