
com.flamenk.article.manipulators.ListUtil Maven / Gradle / Ivy
/*
* Copyright 2013 Torindo Nesci.
*
* 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 com.flamenk.article.manipulators;
import com.flamenk.TagConstants;
import com.flamenk.dom.HtmlNode;
import static com.flamenk.dom.HtmlNodeDisplayMode.NOT_DISPLAY;
import com.flamenk.dom.HtmlNodeRange;
import static com.flamenk.util.NodeUtil.hasName;
import static com.flamenk.util.NodeUtil.isEmptyTextNode;
import com.google.common.base.Optional;
import java.util.Deque;
import java.util.Iterator;
/**
*
* @author Torindo Nesci
*/
final class ListUtil {
public static final int MAX_TOKENS_LIST_HEADER = 3;
private ListUtil() {
}
private static Optional optionalOf(T obj) {
if (obj == null) {
return Optional.absent();
}
return Optional.of(obj);
}
static void hideListHeader(HtmlNode listNode) {
assert listNode != null;
Optional header = headerOf(listNode);
if (header.isPresent()
&& header.get().getNumTextTokens() <= MAX_TOKENS_LIST_HEADER) {
header.get().setDisplayMode(NOT_DISPLAY);
}
}
static Optional listNodeFor(HtmlNode node) {
assert node != null;
int maxHops = 6;
HtmlNode listNode = null;
int count = 0;
for (Optional curNode = node.getParent();
curNode.isPresent() && count < maxHops;
curNode = curNode.get().getParent(), count++) {
if (hasName(curNode.get(), TagConstants.UL)
|| hasName(curNode.get(), TagConstants.OL)) {
listNode = curNode.get();
}
}
return optionalOf(listNode);
}
static boolean isLastList(HtmlNode listNode, HtmlNodeRange range) {
assert listNode != null;
assert range != null;
assert hasName(listNode, TagConstants.UL)
|| hasName(listNode, TagConstants.OL);
HtmlNode nodeAfterList = null;
HtmlNode beforeNext = maxNodeInSubTree(listNode);
for (HtmlNode next = nextNodeOf(beforeNext);
next != null && nodeAfterList == null;
next = nextNodeOf(next)) {
if (!isEmptyTextNode(next)) {
nodeAfterList = next;
}
}
return nodeAfterList == null || range.isNodeAfterRange(nodeAfterList);
}
static Optional headerOf(HtmlNode listNode) {
assert listNode != null;
assert hasName(listNode, TagConstants.UL)
|| hasName(listNode, TagConstants.OL);
HtmlNode headerNode = null;
for (HtmlNode prevNode = previousNodeOf(listNode);
prevNode != null && headerNode == null;
prevNode = previousNodeOf(prevNode)) {
if (!isEmptyTextNode(prevNode)
&& hasName(prevNode, TagConstants.TEXT)) {
headerNode = prevNode;
}
}
return optionalOf(headerNode);
}
private static HtmlNode previousNodeOf(HtmlNode node) {
Optional nodeOpt = Optional.of(node);
for (; nodeOpt.isPresent();
nodeOpt = nodeOpt.get().getParent()) {
Optional prevSibling = nodeOpt.get().getLeftSibling();
if (prevSibling.isPresent()) {
return maxNodeInSubTree(prevSibling.get());
}
}
return null;
}
private static HtmlNode nextNodeOf(HtmlNode node) {
Deque children = node.getChildren();
if (!children.isEmpty()) {
return children.getFirst();
}
Optional nodeOpt = Optional.of(node);
for (; nodeOpt.isPresent();
nodeOpt = nodeOpt.get().getParent()) {
Optional nextSibling = nodeOpt.get().getRightSibling();
if (nextSibling.isPresent()) {
return nextSibling.get();
}
}
return null;
}
private static HtmlNode maxNodeInSubTree(HtmlNode node) {
HtmlNode maxNode = null;
for (HtmlNode curNode = node;
maxNode == null;) {
Deque children = curNode.getChildren();
if (children.isEmpty()) {
maxNode = curNode;
} else {
Iterator childrenIt = children.descendingIterator();
HtmlNode child = childrenIt.next();
curNode = child;
}
}
return maxNode;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy