Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Copyright 2011 Tomas Schlosser
   *
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
   *
   *     http://www.apache.org/licenses/LICENSE-2.0
   *
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS,
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
 package org.anadix.html;
 
 
Abstract class representing any HTML element. It has generic method to retrieve element's attribute by name. It also stores id of an element, name of the tag it represents and parent element. The optional attributes are position of the element in document, source and text content. The mandatory attributes are unmodifiable as they define the element. The optional ones ones can be assigned and changed as necessary. The only constructor is package protected as instances of HTML element should only be created through HTMLElementFactory.

Author(s):
tomason
Version:
$Id: $
 
 public abstract class HtmlElement implements ElementComparable<HtmlElement> {
 	private final BigInteger id;
 	private final String name;
 	private final HtmlElement parent;
 	private final Attributes attributes;
 
 	private Position position;
 	private String source;
 	private String textContent;
 
 			BigInteger id,
 			String name,
 			HtmlElement parent,
 			Attributes attributes) {
 		this(idnameparentattributesnullnullnull);
 	}
 
 	private HtmlElement(
 			BigInteger id,
 			String name,
 			HtmlElement parent,
 			Attributes attributes,
 			String source,
 			Position position,
 			String textContent) {
 		if (id == null) {
 			throw new NullPointerException("id can't be null");
 		}
 		if (name == null || name.length() < 1) {
 			throw new NullPointerException("name can't be null");
 		}
 		if (attributes == null) {
 			throw new NullPointerException("atributes can't be null");
 		}
 
 		this. = id;
 		this. = name;
 		this. = parent;
 		this. = attributes;
 
 		this. = source;
 		this. = position;
 		this. = textContent;
 	}

Gets the id of the element (not the attribute id but the id as assigned by pasrser)

Returns:
id of the element
 
 	public BigInteger getId() {
 		return ;
 	}

Gets the name of the element (not the attribute name but the name of the tag)

Returns:
name of the element
 
 	public String getName() {
 		return ;
 	}

Gets the parent of the element

Returns:
enclosing HtmlElement
	public HtmlElement getParent() {
		return ;
	}


Gets the list of attributes of the element

Returns:
list of attributes of the element
		return ;
	}


Gets value of attribute by name

Parameters:
name attribute which value should be returned
Returns:
value of requested attribute or null if attribute is not set
	public String getAttribute(String name) {
		return getAttributes().getAttribute(name);
	}


Sets the source of the element

Parameters:
source source of the element
	public void setSource(String source) {
		if (source == null || source.length() == 0) {
			throw new NullPointerException("source can't be null");
		}
		this. = source;
	}

Gets the source of the element

Returns:
source of the element
	public String getSource() {
		return ;
	}


Sets the position of the element

Parameters:
position position of the element
	public void setPosition(Position position) {
		if (position == null) {
			throw new NullPointerException("position can't be null");
		}
		this. = position;
	}

Gets the position of the element

Returns:
position of the element
	public Position getPosition() {
		return ;
	}

Gets the text content of the element

Returns:
text content of the element
	public String getTextContent() {
		return ;
	}

Sets the text content of the element

Parameters:
textContent text content of the element
	public void setTextContent(String textContent) {
		if (textContent == null || textContent.length() == 0) {
			throw new NullPointerException("text content can't be null");
		}
		this. = textContent;
	}

Compares this object with the specified object for order.

Parameters:
o HtmlElement to compare this with
Returns:
negative integer if this object is less than, zero if this object is equal to, or a positive integer if this object is greater than the specified object
	public int compareTo(HtmlElement o) {
		return getId().compareTo(o.getId());
	}

	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + (( == null) ? 0 : .hashCode());
		return result;
	}

	public boolean equals(Object obj) {
		if (this == obj) {
			return true;
		}
		if (obj == null) {
			return false;
		}
		if (getClass() != obj.getClass()) {
			return false;
		}
		HtmlElement other = (HtmlElementobj;
		if ( == null) {
			if (other.id != null) {
				return false;
			}
else if (!.equals(other.id)) {
			return false;
		}
		return true;
	}

	public String toString() {
		return String.format("%s[id='%s', position='%s', attributes='%s', source='%s']",
	}
New to GrepCode? Check out our FAQ X