/** * Construct VIPS block tree from viewport. * * <p>Starts from <body> element. * * @param element Box that represents element * @param node Visual structure tree node */ private void constructVipsBlockTree(Box element, VipsBlock node) { node.setBox(element); if (!(element instanceof TextBox)) { for (Box box : ((ElementBox) element).getSubBoxList()) { node.addChild(new VipsBlock()); constructVipsBlockTree(box, node.getChildren().get(node.getChildren().size() - 1)); } } }
/** * Counts number of visual blocks in visual structure * * @param vipsBlock Visual structure */ private void getVisualBlocksCount(VipsBlock vipsBlock) { if (vipsBlock.isVisualBlock()) _visualBlocksCount++; for (VipsBlock vipsBlockChild : vipsBlock.getChildren()) { if (!(vipsBlockChild.getBox() instanceof TextBox)) getVisualBlocksCount(vipsBlockChild); } }
/** * Tries to divide DOM elements and finds visual blocks. * * @param vipsBlock Visual structure */ private void divideVipsBlockTree(VipsBlock vipsBlock) { _currentVipsBlock = vipsBlock; ElementBox elementBox = (ElementBox) vipsBlock.getBox(); // System.err.println(elementBox.getNode().getNodeName()); // System.out.println(elementBox.getText()); if (elementBox.getElement().getAttribute("id").equals("logosLine")) { System.out.println(); } // With VIPS rules it tries to determine if element is dividable if (applyVipsRules(elementBox) && vipsBlock.isDividable() && !vipsBlock.isVisualBlock()) { // if element is dividable, let's divide it _currentVipsBlock.setAlreadyDivided(true); for (VipsBlock vipsBlockChild : vipsBlock.getChildren()) { if (!(vipsBlockChild.getBox() instanceof TextBox)) divideVipsBlockTree(vipsBlockChild); } } else { if (vipsBlock.isDividable()) { // System.err.println("Element " + elementBox.getNode().getNodeName() + " is visual block"); vipsBlock.setIsVisualBlock(true); vipsBlock.setDoC(11); } if (!verifyValidity(elementBox)) { _currentVipsBlock.setIsVisualBlock(false); } /* if (vipsBlock.isVisualBlock()) //System.err.println("Element " + elementBox.getNode().getNodeName() + " is visual block"); else System.err.println("Element " + elementBox.getNode().getNodeName() + " is not visual block");*/ } }
/** * Finds previous sibling node's VIPS block. * * @param node Node * @param vipsBlock Actual VIPS block * @param foundBlock VIPS block for given node */ private void findPreviousSiblingNodeVipsBlock(Node node, VipsBlock vipsBlock) { if (vipsBlock.getBox().getNode().equals(node)) { _tempVipsBlock = vipsBlock; return; } else for (VipsBlock vipsBlockChild : vipsBlock.getChildren()) findPreviousSiblingNodeVipsBlock(node, vipsBlockChild); }
/** * VIPS Rule Three * * <p>If the DOM node is the root node of the sub-DOM tree (corresponding to the block), and there * is only one sub DOM tree corresponding to this block, divide this node. * * @param node Input node * @return True, if rule is applied, otherwise false. */ private boolean ruleThree(ElementBox node) { // System.err.println("Applying rule Three on " + node.getNode().getNodeName() + " node"); if (!node.isRootElement()) return false; boolean result = true; int cnt = 0; for (VipsBlock vipsBlock : _vipsBlocks.getChildren()) { if (vipsBlock.getBox().getNode().getNodeName().equals(node.getNode().getNodeName())) { result = true; isOnlyOneDomSubTree(node.getNode(), vipsBlock.getBox().getNode(), result); if (result) cnt++; } } return (cnt == 1) ? true : false; }
/** * VIPS Rule Seven * * <p>If the background color of this node is different from one of its children’s, divide this * node and at the same time, the child node with different background color will not be divided * in this round. Set the DoC value (6-8) for the child node based on the <html> tag of the * child node and the size of the child node. * * @param node Input node * @return True, if rule is applied, otherwise false. */ private boolean ruleSeven(ElementBox node) { // System.err.println("Applying rule Seven on " + node.getNode().getNodeName() + " node"); if (node.getSubBoxList().isEmpty()) return false; if (isTextNode(node)) return false; // String nodeBgColor = node.getStylePropertyValue("background-color"); String nodeBgColor = _currentVipsBlock.getBgColor(); for (VipsBlock vipsStructureChild : _currentVipsBlock.getChildren()) { if (!(vipsStructureChild.getBgColor().equals(nodeBgColor))) { vipsStructureChild.setIsDividable(false); vipsStructureChild.setIsVisualBlock(true); // TODO DoC values vipsStructureChild.setDoC(7); return true; } } return false; }
private void findVisualBlocks(VipsBlock vipsBlock, List<VipsBlock> list) { if (vipsBlock.isVisualBlock()) list.add(vipsBlock); for (VipsBlock vipsStructureChild : vipsBlock.getChildren()) findVisualBlocks(vipsStructureChild, list); }