Example usage for org.dom4j.dom DOMElement getNextSibling

List of usage examples for org.dom4j.dom DOMElement getNextSibling

Introduction

In this page you can find the example usage for org.dom4j.dom DOMElement getNextSibling.

Prototype

public org.w3c.dom.Node getNextSibling() 

Source Link

Usage

From source file:hello.W2j.java

License:Apache License

private void readNextAutoIndexList(String autoTileHref) {
    DOMDocument autoTileContextDom = null;
    try {//from  w  w w .j a  v  a  2 s.  co  m
        Document autoTileDom = getDomFromStream(autoTileHref);
        String indexHrefAdd = ((Attribute) autoTileDom.selectSingleNode("/html/body//iframe/@src")).getValue();
        String hrefContent = autoTileHref + "" + indexHrefAdd;
        autoTileContextDom = getDomFromStream(hrefContent);
    } catch (Exception e) {
        List<Map<String, Object>> workIndexList = initIndexList(autoData);
        Map<String, Object> workContentItem = workIndexList.get(workIndexList.size() - 1);
        HashMap<String, Object> contextItem = new HashMap<String, Object>();
        contextItem.put("text", "BAD PAGE");
        contextItem.put("error", e.getMessage());
        contextItem.put("url", autoTileHref);
        workIndexList.add(contextItem);
        logger.error(e.getMessage());
        nextAutoTileElement = null;
        return;
    }
    if (autoTileContextDom == null)
        return;
    List<DOMElement> myPagePosition = (List<DOMElement>) autoTileContextDom
            .selectNodes("/html/body/div/p[@style and not(a)]");
    if (myPagePosition.size() == 0) {//bad link auto tile page not exist
        logger.debug(nextAutoTileElement.asXML());
        nextAutoTileElement = (DOMElement) nextAutoTileElement.getNextSibling();
        logger.debug(nextAutoTileElement.asXML());
        return;
    }
    Map<String, Object> contextItem = null;
    for (Element element : myPagePosition) {
        String text = element.getText();
        String style = element.attribute("style").getValue();
        String level = style.split(";")[0].split("padding-left:")[1].split("pt")[0];
        int levelInt = Integer.parseInt(level) / 10;
        if (levelInt < 0 && !autoData.containsKey("autoName")) {
            autoData.put("autoName", text);
        } else if (levelInt >= 0) {
            int indexOfcurrent = text.indexOf(">>");
            if (indexOfcurrent == 0) {
                levelInt = levelInt + 1;
                text = text.replace(">>", "").replace("<<", "").trim();
                logger.debug(autoTileAllIndexNr + "/" + levelInt + " -- " + text);
                //               logger.debug(autoTileIndexNr+"/"+autoTileAllIndexNr+"/"+levelInt+" -- "+text);
            }
            contextItem = new HashMap<String, Object>();
            contextItem.put("text", text);
            Map<String, Object> workContentItem = autoData;
            List<Map<String, Object>> workIndexList = initIndexList(workContentItem);
            for (int i = 0; i < levelInt; i++) {
                if (workIndexList.size() == 0)//not skip level
                    break;
                workContentItem = workIndexList.get(workIndexList.size() - 1);
                workIndexList = initIndexList(workContentItem);
            }
            workIndexList.add(contextItem);
        }
    }

    DOMElement lastElement = myPagePosition.get(myPagePosition.size() - 1);
    contextItem.put("url", autoTileHref);
    nextAutoTileElement = (DOMElement) lastElement.getNextSibling();
    //      nextAutoTileElement = getLastIndexElement(nextAutoTileElement);

    autoTileAllIndexNr++;

}