private static void loadCSS(String address, HTMLDocument document) throws Exception { CSSData cssData = new CSSData(); document.putResource("CSS.DATA", cssData); NodePath nodePath = pathParser.toPath("HEAD"); HTMLNode head = extractor.lookNode(document.getRoot(), nodePath); URLUtils urlUtils = new URLUtils(); NodeIterator iterator = head.iterator(); while (iterator.hasNext()) { HTMLNode node = iterator.next(); if (!node.isNode(Name.LINK)) continue; Attributes attributes = node.getAttributes(); Attribute attribute = attributes.get("type"); if (attribute == null) continue; if (!"text/css".equalsIgnoreCase(attribute.getValue())) continue; attribute = attributes.get("href"); if (attribute == null) continue; String link = attribute.getValue(); if (link == null) continue; link = urlUtils.createURL(new URL(address), link); System.out.println(link); byte[] bytes = loadContent(link); String css = new String(bytes, "utf-8"); cssData.addValue(css); } }
boolean hasForm(HTMLNode node) { NodeIterator iterator = node.iterator(); while (iterator.hasNext()) { HTMLNode n = iterator.next(); if (n.isNode(Name.FORM)) return true; if (isFormElement(n)) return true; } return false; }
private boolean isLinkDiv(HTMLNode node) { if (node.getChildren() == null) return false; List<HTMLNode> ignores = new ArrayList<HTMLNode>(); NodeIterator iterator = node.iterator(); while (iterator.hasNext()) { HTMLNode n = iterator.next(); if (n.isNode(Name.A) && !linkNodeChecker.isValid(new CheckModel(n), 0)) ignores.add(n); } int counter = 0; iterator = node.iterator(ignores); while (iterator.hasNext()) { HTMLNode n = iterator.next(); if (n.isNode(Name.CONTENT)) { counter += countWord(n); } } return counter < 5 && ignores.size() > 1; }
private int countWord(HTMLNode node) { if (node == null) return 0; NodeIterator nodeIterator = node.iterator(); int word = 0; while (nodeIterator.hasNext()) { HTMLNode iterNode = nodeIterator.next(); if (getAncestor(iterNode, Name.A, 0, 5) != null) continue; if (iterNode.isNode(Name.CONTENT)) { String text = iterNode.getTextValue(); word += textCounter.countWord(text, 0, text.length()); } } return word; }
public void remove(HTMLNode root, HTMLNode first, HTMLNode last) { boolean remove = true; List<HTMLNode> removes = new ArrayList<HTMLNode>(); NodeIterator nodeIterator = root.iterator(); // System.out.println(new String(first.getValue())); while (nodeIterator.hasNext()) { HTMLNode node = nodeIterator.next(); switch (node.getName()) { case UL: if (isLinkContainer(node)) removes.add(node); break; case DIV: case TD: if (isLinkDiv(node)) removes.add(node); break; case CONTENT: // System.out.println(new String(iterNode.getValue())); if (node == first) remove = false; if (remove) { removes.add(node); // System.out.println(new String(node.getValue())); } if (node == last) remove = true; break; default: break; } } // System.out.println(new String(last.getValue())); for (int i = 0; i < removes.size(); i++) { HTMLNode node = removes.get(i); HTMLNode parent = node.getParent(); // System.out.println(parent.getTextValue()); while (parent != null) { // System.out.println(parent.getTextValue()); node.setValue(new char[] {}); parent.removeChild(node); int word = countWord(parent); if (word > 15) break; node = parent; parent = node.getParent(); } } }
private boolean isLinkContainer(HTMLNode node) { List<HTMLNode> children = node.getChildren(); if (children == null) return false; if (isListNode(children)) { NodeIterator nodeIterator = node.iterator(); int counter = 0; while (nodeIterator.hasNext()) { HTMLNode iterNode = nodeIterator.next(); if (iterNode.isNode(Name.A)) counter++; } return counter >= children.size() - 3; } for (int i = 0; i < children.size(); i++) { if (isLinkContainer(children.get(i))) return true; } return false; }