Class 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 single InputElement object, where InputElement is a subclass of HtmlElement.

    • 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 empty String 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 this HtmlElement as a Map of attribute names to values.
      java.lang.String getCharEncoding()
      Gets the character encoding that is used to decode the data contained in this HtmlElement, 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)
      Returns true if this HTML element contains the specified attribute.
      boolean isDisabled()
      Returns true if this HTML element has been disabled.
      boolean isSuccessfulControl()
      Returns true 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 empty String.
      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 this HtmlElement, 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.
      • Methods inherited from class java.lang.Object

        equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • HtmlElement

        public HtmlElement​(long cPtr,
                           boolean cMemoryOwn)
        For internal use only. Of no interest to the user.
        Parameters:
        cPtr - pointer to the CPP wrapped object
        cMemoryOwn - indicates if this object is responsible for memory management of the CPP object
    • 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 this HtmlElement as a Map of attribute names to values.
        Returns:
        all the attributes contained in this HtmlElement as a Map 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
        Returns true 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 this HtmlElement 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 empty String 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 specified name 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 empty String.
        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 value
        value - 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 calling getAttribute("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 calling setAttribute("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 calling getAttribute("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 calling setAttribute("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 calling getAttribute("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 calling setAttribute("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()
        Returns true if this HTML element has been disabled. An element is disabled if it has a "disabled" attribute, i.e. if hasAttribute("disabled") returns true.
        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 to setAttribute("disabled").

        setDisabled(false) is equivalent to removeAttribute("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:

        
         <html><body><form name="myForm">Select: <select name="mySelect"><option value="1">1</option><option value="2">2</option></select></form></body></html>
         
        Then this code would output 47, i.e. the offset of the space after the opening '<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:

        
         <html><body><form name="myForm">Select: <select name="mySelect"><option value="1">1</option><option value="2">2</option></select></form></body></html>
         
        Then this code would output 129, i.e. the offset of the '<' 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()
        Returns true 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 class java.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 this HtmlElement, 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 this HtmlElement, for example "UTF-8".
        Returns:
        the character encoding
        See Also:
        setCharEncoding(String)