Package com.facilita.fc.web
Class HtmlElement
- java.lang.Object
-
- com.facilita.fc.web.HtmlElement
-
- Direct Known Subclasses:
Form
,InputElement
,OptionElement
,SelectElement
,TextAreaElement
public class HtmlElement extends java.lang.Object
Represents an HTML element within an HTML form. An HTML element is everything from the start tag to the end tag.e.g.
<input type="text" name="firstname">
would be represented by a singleInputElement
object, whereInputElement
is a subclass ofHtmlElement
.
-
-
Constructor Summary
Constructors Constructor Description HtmlElement(long cPtr, boolean cMemoryOwn)
For internal use only.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description HtmlElement
clone()
Creates a copy of this HTML element.void
delete()
int
getAfterEndTagPosition()
Gets the character offset into the HTML document at which the 1st character (not including < > tag characters) in this HTML element appears.int
getAfterStartTagPosition()
Gets the character offset into the HTML document at which the 1st character (not including < > tag characters) in this HTML element appears.java.lang.String
getAttribute(java.lang.String name)
Gets the value of the specified attribute, or an emptyString
if the attribute is not contained within this HTML element.java.util.Map<java.lang.String,java.lang.String>
getAttributes()
Gets all the attributes contained in thisHtmlElement
as aMap
of attribute names to values.java.lang.String
getCharEncoding()
Gets the character encoding that is used to decode the data contained in thisHtmlElement
, for example"UTF-8"
.java.lang.String
getContent()
Gets the content of this HTML element.int
getContentPosition()
Gets the character offset into the HTML document at which this HTML element's content starts.static long
getCPtr(HtmlElement obj)
For internal use only.java.lang.String
getId()
Gets the value of the"id"
attribute for this HTML element.java.lang.String
getName()
Gets the value of the"name"
attribute for this HTML element.java.lang.String
getTag()
Gets the tag name of this HTML element.java.lang.String
getValue()
Gets the value of the"value"
attribute for this HTML element.boolean
hasAttribute(java.lang.String name)
Returnstrue
if this HTML element contains the specified attribute.boolean
isDisabled()
Returnstrue
if this HTML element has been disabled.boolean
isSuccessfulControl()
Returnstrue
if this HTML element currently represents a successful control.void
removeAttribute(java.lang.String name)
Removes an attribute from this HTML element.void
setAttribute(java.lang.String name)
Sets the value of the specified attribute to an emptyString
.void
setAttribute(java.lang.String name, java.lang.String value)
Sets the value of the specified attribute.void
setCharEncoding(java.lang.String charEncoding)
Sets the character encoding that is used to decode the data contained in thisHtmlElement
, for example"UTF-8"
.void
setContent(java.lang.String content)
Sets the content of this HTML element.void
setDisabled(boolean disable)
Sets or removes the"disabled"
attribute for this HTML element.void
setId(java.lang.String id)
Sets the value of the"id"
attribute for this HTML element.void
setName(java.lang.String name)
Sets the value of the"name"
attribute for this HTML element.void
setValue(java.lang.String value)
Sets the value of the"value"
attribute for this HTML element.
-
-
-
Method Detail
-
getCPtr
public static long getCPtr(HtmlElement obj)
For internal use only. Of no interest to the user.- Parameters:
obj
- a reference to an object of this class- Returns:
- a long containing the address of the CPP wrapped object
-
delete
public void delete()
-
getAttributes
public java.util.Map<java.lang.String,java.lang.String> getAttributes() throws java.io.UnsupportedEncodingException, com.facilita.exception.InternalError
Gets all the attributes contained in thisHtmlElement
as aMap
of attribute names to values.- Returns:
- all the attributes contained in this
HtmlElement
as aMap
of attribute names to values - Throws:
java.io.UnsupportedEncodingException
- The specified encoding is not supported.com.facilita.exception.InternalError
- An internal error occurred.
-
hasAttribute
public boolean hasAttribute(java.lang.String name) throws java.io.UnsupportedEncodingException
Returnstrue
if this HTML element contains the specified attribute.e.g.
if (element.hasAttribute("checked")) { // The element has the "checked" attribute }
- Parameters:
name
- the name of the attribute to look for- Returns:
true
if thisHtmlElement
contains this attribute- Throws:
java.io.UnsupportedEncodingException
- The specified encoding is not supported.
-
getAttribute
public java.lang.String getAttribute(java.lang.String name) throws java.io.UnsupportedEncodingException
Gets the value of the specified attribute, or an emptyString
if the attribute is not contained within this HTML element.e.g.
// Get the value of the "name" attribute for the HTML element String name = element.getAttribute("name");
- Parameters:
name
- the name of the attribute from which the value should be returned- Returns:
- the value of the attribute, or an empty
String
if no attribute with the specifiedname
could be found - Throws:
java.io.UnsupportedEncodingException
- The specified encoding is not supported.- See Also:
setAttribute(String, String)
-
setAttribute
public void setAttribute(java.lang.String name) throws java.io.UnsupportedEncodingException
Sets the value of the specified attribute to an emptyString
.- Parameters:
name
- the name of the attribute on which to set the value- Throws:
java.io.UnsupportedEncodingException
- The specified encoding is not supported.- See Also:
setAttribute(String, String)
-
setAttribute
public void setAttribute(java.lang.String name, java.lang.String value) throws java.io.UnsupportedEncodingException
Sets the value of the specified attribute. The attribute will be added if it does not already exist.- Parameters:
name
- the name of the attribute on which to set the valuevalue
- the new value of the attribute- Throws:
java.io.UnsupportedEncodingException
- The specified encoding is not supported.- See Also:
getAttribute(String)
-
removeAttribute
public void removeAttribute(java.lang.String name) throws java.io.UnsupportedEncodingException
Removes an attribute from this HTML element. This method has no effect if the attribute does not exist.- Parameters:
name
- the name of the attribute to remove- Throws:
java.io.UnsupportedEncodingException
- The specified encoding is not supported.- See Also:
setAttribute(String, String)
,getAttribute(String)
-
getContent
public java.lang.String getContent() throws java.io.UnsupportedEncodingException
Gets the content of this HTML element. This includes everything between the HTML tags, but excludes the tags themselves and any attributes.- Returns:
- the content of the HTML element
- Throws:
java.io.UnsupportedEncodingException
- The specified encoding is not supported.- See Also:
setContent(String)
-
setContent
public void setContent(java.lang.String content) throws java.io.UnsupportedEncodingException
Sets the content of this HTML element. This includes everything between the HTML tags, but excludes the tags themselves and any attributes.- Parameters:
content
- the content to set- Throws:
java.io.UnsupportedEncodingException
- The specified encoding is not supported.- See Also:
getContent()
-
getName
public java.lang.String getName() throws java.io.UnsupportedEncodingException
Gets the value of the"name"
attribute for this HTML element. Calling this method is equivalent to callinggetAttribute("name")
.- Returns:
- the value of the
"name"
attribute - Throws:
java.io.UnsupportedEncodingException
- The specified encoding is not supported.- See Also:
getAttribute(String)
,setName(String)
-
setName
public void setName(java.lang.String name) throws java.io.UnsupportedEncodingException
Sets the value of the"name"
attribute for this HTML element. Calling this method is equivalent to callingsetAttribute("name", name)
.- Parameters:
name
- the new value of the"name"
attribute- Throws:
java.io.UnsupportedEncodingException
- The specified encoding is not supported.- See Also:
getName()
,setAttribute(String, String)
-
getValue
public java.lang.String getValue() throws java.io.UnsupportedEncodingException
Gets the value of the"value"
attribute for this HTML element. Calling this method is equivalent to callinggetAttribute("value")
.- Returns:
- the value of the
"value"
attribute - Throws:
java.io.UnsupportedEncodingException
- The specified encoding is not supported.- See Also:
getAttribute(String)
,setValue(String)
-
setValue
public void setValue(java.lang.String value) throws java.io.UnsupportedEncodingException
Sets the value of the"value"
attribute for this HTML element. Calling this method is equivalent to callingsetAttribute("value", value)
.- Parameters:
value
- the new value of the"value"
attribute- Throws:
java.io.UnsupportedEncodingException
- The specified encoding is not supported.- See Also:
getValue()
,setAttribute(String, String)
-
getId
public java.lang.String getId() throws java.io.UnsupportedEncodingException
Gets the value of the"id"
attribute for this HTML element. Calling this method is equivalent to callinggetAttribute("id")
.- Returns:
- the value of the
"id"
attribute - Throws:
java.io.UnsupportedEncodingException
- The specified encoding is not supported.- See Also:
getAttribute(String)
,setId(String)
-
setId
public void setId(java.lang.String id) throws java.io.UnsupportedEncodingException
Sets the value of the"id"
attribute for this HTML element. Calling this method is equivalent to callingsetAttribute("id", id)
.- Parameters:
id
- the new value of the"id"
attribute- Throws:
java.io.UnsupportedEncodingException
- The specified encoding is not supported.- See Also:
getId()
,setAttribute(String, String)
-
getTag
public java.lang.String getTag()
Gets the tag name of this HTML element.e.g. the HTML element
<input type="text" name="firstname">
has a tag name of"input"
.- Returns:
- the tag name of this HTML element
-
isDisabled
public boolean isDisabled()
Returnstrue
if this HTML element has been disabled. An element is disabled if it has a"disabled"
attribute, i.e. ifhasAttribute("disabled")
returnstrue
.- Returns:
true
if this element has the"disabled"
attribute
-
setDisabled
public void setDisabled(boolean disable)
Sets or removes the"disabled"
attribute for this HTML element.setDisabled(true)
is equivalent tosetAttribute("disabled")
.setDisabled(false)
is equivalent toremoveAttribute("disabled")
.- Parameters:
disable
-true
to disable the HTML element,false
to enable it
-
getAfterStartTagPosition
public int getAfterStartTagPosition()
Gets the character offset into the HTML document at which the 1st character (not including < > tag characters) in this HTML element appears.e.g. if the content of the HTTP
Response
object is:
Then this code would output 47, i.e. the offset of the space after the opening<html><body><form name="myForm">Select: <select name="mySelect"><option value="1">1</option><option value="2">2</option></select></form></body></html>
'<select'
:Form form = response.extractForm("myForm"); SelectElement select = form.getSelectElement("mySelect"); writeMessage("AfterStartTagPosition : " + select.getAfterStartTagPosition());
- Returns:
- the character offset into the HTML document at which the 1st character in this HTML element appears
- See Also:
getAfterEndTagPosition()
-
getAfterEndTagPosition
public int getAfterEndTagPosition()
Gets the character offset into the HTML document at which the 1st character (not including < > tag characters) in this HTML element appears.e.g. if the content of the HTTP
Response
object is:
Then this code would output 129, i.e. the offset of the<html><body><form name="myForm">Select: <select name="mySelect"><option value="1">1</option><option value="2">2</option></select></form></body></html>
'<'
after the closing'</select>'
:Form form = response.extractForm("myForm"); SelectElement select = form.getSelectElement("mySelect"); writeMessage("AfterEndTagPosition : " + select.getAfterEndTagPosition());
- Returns:
- the character offset into the HTML document at which the last character in this HTML element appears
- See Also:
getAfterStartTagPosition()
-
getContentPosition
public int getContentPosition()
Gets the character offset into the HTML document at which this HTML element's content starts.- Returns:
- the character offset into the HTML document at which this HTML element's content starts
-
isSuccessfulControl
public boolean isSuccessfulControl()
Returnstrue
if this HTML element currently represents a successful control. A successful control is one that is valid for submission, i.e. one that will be sent to the web server when the form is submitted.- Returns:
true
if this HTML element is a successful control
-
clone
public HtmlElement clone()
Creates a copy of this HTML element.- Overrides:
clone
in classjava.lang.Object
- Returns:
- a new
HtmlElement
object which is an exact copy of this HTML element
-
setCharEncoding
public void setCharEncoding(java.lang.String charEncoding)
Sets the character encoding that is used to decode the data contained in thisHtmlElement
, for example"UTF-8"
.- Parameters:
charEncoding
- the character encoding to set- See Also:
getCharEncoding()
-
getCharEncoding
public java.lang.String getCharEncoding()
Gets the character encoding that is used to decode the data contained in thisHtmlElement
, for example"UTF-8"
.- Returns:
- the character encoding
- See Also:
setCharEncoding(String)
-
-