View Javadoc

1   /**
2    * Copyright (c) 2002-2015, JWebUnit team.
3    *
4    * This file is part of JWebUnit.
5    *
6    * JWebUnit is free software: you can redistribute it and/or modify
7    * it under the terms of the GNU Lesser General Public License as published by
8    * the Free Software Foundation, either version 3 of the License, or
9    * (at your option) any later version.
10   *
11   * JWebUnit is distributed in the hope that it will be useful,
12   * but WITHOUT ANY WARRANTY; without even the implied warranty of
13   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14   * GNU General Public License for more details.
15   *
16   * You should have received a copy of the GNU Lesser General Public License
17   * along with JWebUnit.  If not, see <http://www.gnu.org/licenses/>.
18   */
19  /**
20   *
21   */
22  package net.sourceforge.jwebunit.htmlunit;
23  
24  import com.gargoylesoftware.htmlunit.html.DomElement;
25  import com.gargoylesoftware.htmlunit.html.DomNode;
26  import com.gargoylesoftware.htmlunit.html.HtmlElement;
27  import com.gargoylesoftware.htmlunit.html.HtmlInput;
28  import com.gargoylesoftware.htmlunit.html.HtmlOption;
29  import com.gargoylesoftware.htmlunit.html.HtmlTextArea;
30  import net.sourceforge.jwebunit.api.IElement;
31  
32  import java.util.ArrayList;
33  import java.util.List;
34  
35  /**
36   * HtmlUnit implementation of IElement wrapper.
37   *
38   * @author jmwright
39   *
40   */
41  public class HtmlUnitElementImpl implements IElement {
42  
43    /**
44     * The wrapped element.
45     */
46    private DomElement element;
47  
48    public HtmlUnitElementImpl(DomElement element) {
49      if (element == null)
50        throw new NullPointerException("Cannot create an IElement for a null element.");
51      this.element = element;
52    }
53  
54    /*
55     * (non-Javadoc)
56     * 
57     * @see net.sourceforge.jwebunit.api.IElement#attribute(java.lang.String)
58     */
59    public String getAttribute(String name) {
60      if ("value".equals(name) && element instanceof HtmlOption) {
61        // for options, we want text if no value was specified
62        return ((HtmlOption) element).getValueAttribute();
63      } else {
64        if (!element.hasAttribute(name))
65          return null;
66  
67        return element.getAttribute(name);
68      }
69    }
70  
71    /*
72     * (non-Javadoc)
73     * 
74     * @see net.sourceforge.jwebunit.api.IElement#name()
75     */
76    public String getName() {
77      return element.getNodeName();
78    }
79  
80    /*
81     * (non-Javadoc)
82     * 
83     * @see net.sourceforge.jwebunit.api.IElement#getChildren()
84     */
85    public List<IElement> getChildren() {
86      List<IElement> children = new ArrayList<IElement>();
87      for (DomElement e : element.getChildElements()) {
88        if (e != null)
89          children.add(new HtmlUnitElementImpl(e));
90      }
91      return children;
92    }
93  
94    /*
95     * (non-Javadoc)
96     * 
97     * @see net.sourceforge.jwebunit.api.IElement#getParent()
98     */
99    public IElement getParent() {
100     DomNode p = element.getParentNode();
101     while (true) {
102       if (p == null)
103         return null;
104 
105       if (p instanceof HtmlElement)
106         return new HtmlUnitElementImpl((HtmlElement) p);
107 
108       // get next parent
109       p = p.getParentNode();
110     }
111   }
112 
113   /*
114    * (non-Javadoc)
115    * 
116    * @see net.sourceforge.jwebunit.api.IElement#getTextContent()
117    */
118   public String getTextContent() {
119     return element.getTextContent();
120   }
121 
122   /*
123    * (non-Javadoc)
124    * 
125    * @see net.sourceforge.jwebunit.api.IElement#getElement(java.lang.String)
126    */
127   public IElement getElement(String xpath) {
128     // if this fails with a ClassCastException, use getElements().get(0) (performance penalty)
129     return new HtmlUnitElementImpl((HtmlElement) element.getFirstByXPath(xpath));
130   }
131 
132   /*
133    * (non-Javadoc)
134    * 
135    * @see net.sourceforge.jwebunit.api.IElement#getElements(java.lang.String)
136    */
137   public List<IElement> getElements(String xpath) {
138     List<IElement> elements = new ArrayList<IElement>();
139     for (Object o : element.getByXPath(xpath)) {
140       if (o instanceof HtmlElement)
141         elements.add(new HtmlUnitElementImpl((HtmlElement) o));
142     }
143     return elements;
144   }
145 
146   public String toString() {
147     return "IElement[name=" + getName() + " wrapped=" + element + "]";
148   }
149 
150   /*
151    * (non-Javadoc)
152    * 
153    * @see net.sourceforge.jwebunit.api.IElement#setAttribute(java.lang.String)
154    */
155   public void setAttribute(String string) {
156     element.setAttributeNS(null, string, "1");
157   }
158 
159   /*
160    * (non-Javadoc)
161    * 
162    * @see net.sourceforge.jwebunit.api.IElement#setAttribute(java.lang.String, java.lang.String)
163    */
164   public void setAttribute(String name, String value) {
165     if ("value".equals(name) && element instanceof HtmlInput) {
166       // for HtmlInputs, we want to run any onChange code if the value changes
167       ((HtmlInput) element).setValueAttribute(value);
168     } else {
169       element.setAttribute(name, value);
170     }
171   }
172 
173   /*
174    * (non-Javadoc)
175    * 
176    * @see net.sourceforge.jwebunit.api.IElement#setTextContent(java.lang.String)
177    */
178   public void setTextContent(String value) {
179     if (element instanceof HtmlTextArea) {
180       ((HtmlTextArea) element).setText(value);
181     } else {
182       element.setTextContent(value);
183     }
184   }
185 
186   @Override
187   public int hashCode() {
188     final int prime = 31;
189     int result = 1;
190     result = prime * result + ((element == null) ? 0 : element.hashCode());
191     return result;
192   }
193 
194   @Override
195   public boolean equals(Object obj) {
196     if (this == obj)
197       return true;
198     if (obj == null)
199       return false;
200     if (getClass() != obj.getClass())
201       return false;
202     final HtmlUnitElementImpl other = (HtmlUnitElementImpl) obj;
203     if (element == null) {
204       if (other.element != null)
205         return false;
206     } else if (!element.equals(other.element))
207       return false;
208     return true;
209   }
210 
211   /**
212    * Return the unwrapped HtmlUnit element that this IElement represents.
213    *
214    * @return the HtmlUnit element this IElement represents.
215    */
216   public DomElement getHtmlElement() {
217     return element;
218   }
219 
220 }