KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sourceforge > jwebunit > LinkImagePredicate


1 /********************************************************************************
2  * Copyright (c) 2001, ThoughtWorks, Inc.
3  * Distributed open-source, see full license under licenses/jwebunit_license.txt
4 **********************************/

5 package net.sourceforge.jwebunit;
6
7 import org.w3c.dom.Element JavaDoc;
8 import org.w3c.dom.NodeList JavaDoc;
9 import com.meterware.httpunit.WebLink;
10 import com.meterware.httpunit.HTMLElementPredicate;
11
12 /**
13  * Matches to a Link element containing an image with a specified filename string as a suffix.
14  *
15  * @author Justin Sampson
16  */

17 public class LinkImagePredicate implements HTMLElementPredicate
18     {
19
20         public
21         boolean matchesCriteria(Object JavaDoc webLink, Object JavaDoc criteria)
22         {
23             Element JavaDoc a = (Element JavaDoc) ((WebLink) webLink).getDOMSubtree();
24             NodeList JavaDoc imgs = getChildImageElement(a);
25
26             if (imgs == null)
27             {
28                 return false;
29             }
30
31             for(int i=0; i< imgs.getLength(); i++)
32             {
33                 Element JavaDoc img = (Element JavaDoc) imgs.item(i);
34                 String JavaDoc source = img.getAttribute("src");
35                 
36                 if(source.endsWith((String JavaDoc) criteria))
37                     return true;
38             }
39             return false;
40         }
41
42         private
43         NodeList JavaDoc getChildImageElement(Element JavaDoc htmlElement)
44         {
45             NodeList JavaDoc nodes = htmlElement.getElementsByTagName("img");
46             return nodes.getLength() == 0 ? null : nodes;
47         }
48     }
49
Popular Tags