org.attoparser.markup.dom.impl.AbstractNestableNode Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of attoparser Show documentation
Show all versions of attoparser Show documentation
Powerful, fast and easy to use HTML and XML parser for Java
/*
* =============================================================================
*
* Copyright (c) 2012, The ATTOPARSER team (http://www.attoparser.org)
*
* 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.attoparser.markup.dom.impl;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import org.attoparser.markup.dom.INestableNode;
import org.attoparser.markup.dom.INode;
/**
*
* Abstract base class for all implementation of {@link INestableNode}.
*
*
* @author Daniel Fernández
*
* @since 1.1
*
*/
public abstract class AbstractNestableNode
extends AbstractNode
implements INestableNode {
private List children = null;
private int childrenLen = 0;
protected AbstractNestableNode() {
super();
}
public boolean hasChildren() {
return this.childrenLen != 0;
}
public int numChildren() {
return this.childrenLen;
}
public List getChildren() {
if (this.childrenLen == 0) {
return Collections.emptyList();
}
return Collections.unmodifiableList(this.children);
}
@SuppressWarnings("unchecked")
public List getChildrenOfType(final Class type) {
if (this.childrenLen == 0) {
return Collections.emptyList();
}
final List selectedChildren = new ArrayList();
for (final INode child : this.children) {
if (type.isInstance(child)) {
selectedChildren.add((T)child);
}
}
return Collections.unmodifiableList(selectedChildren);
}
public INode getFirstChild() {
if (this.childrenLen == 0) {
return null;
}
return this.children.get(0);
}
@SuppressWarnings("unchecked")
public T getFirstChildOfType(final Class type) {
if (this.childrenLen == 0) {
return null;
}
for (final INode child : this.children) {
if (type.isInstance(child)) {
return (T) child;
}
}
return null;
}
public void addChild(final INode newChild) {
if (newChild != null) {
if (this.childrenLen == 0) {
this.children = new ArrayList();
}
this.children.add(newChild);
this.childrenLen++;
newChild.setParent(this);
}
}
public final void insertChild(final int index, final INode newChild) {
if (newChild != null) {
if (this.childrenLen == 0) {
this.children = new ArrayList();
}
if (index <= this.childrenLen) {
this.children.add(index, newChild);
this.childrenLen++;
newChild.setParent(this);
}
}
}
public final void insertChildBefore(final INode before, final INode newChild) {
if (newChild != null) {
if (this.childrenLen > 0) {
for (int i = 0; i < this.childrenLen; i++) {
final INode currentChild = this.children.get(i);
if (currentChild == before) {
insertChild(i, newChild);
return;
}
}
}
}
}
public final void insertChildAfter(final INode after, final INode newChild) {
if (newChild != null) {
if (this.childrenLen > 0) {
for (int i = 0; i < this.childrenLen; i++) {
final INode currentChild = this.children.get(i);
if (currentChild == after) {
insertChild(i + 1, newChild);
return;
}
}
}
}
}
public final void removeChild(final INode child) {
if (child != null && child.getParent() == this) {
final Iterator childrenIter = this.children.iterator();
while (childrenIter.hasNext()) {
final INode nodeChild = childrenIter.next();
if (nodeChild == child) {
childrenIter.remove();
this.childrenLen--;
break;
}
}
if (this.childrenLen == 0) {
this.children = null;
}
}
}
public final void clearChildren() {
this.children = null;
this.childrenLen = 0;
}
}