Example usage for org.w3c.dom Node getPreviousSibling

List of usage examples for org.w3c.dom Node getPreviousSibling

Introduction

In this page you can find the example usage for org.w3c.dom Node getPreviousSibling.

Prototype

public Node getPreviousSibling();

Source Link

Document

The node immediately preceding this node.

Usage

From source file:org.sonar.plugins.xml.checks.IllegalTabCheck.java

/**
 * Find Illegal tabs in whitespace.//from   ww  w  .  ja  v  a  2 s  . c om
 */
private void findIllegalTabs(Node node) {

    // check whitespace in the node
    for (Node sibling = node.getPreviousSibling(); sibling != null; sibling = sibling.getPreviousSibling()) {
        if (sibling.getNodeType() == Node.TEXT_NODE) {
            String text = sibling.getTextContent();
            if (StringUtils.isWhitespace(text) && StringUtils.contains(text, "\t")) {
                createNewViolation(SaxParser.getLineNumber(sibling));
                break; // one violation for this node is enough
            }
        }
    }

    // check the child elements of the node
    for (Node child = node.getFirstChild(); !validationReady && child != null; child = child.getNextSibling()) {
        switch (child.getNodeType()) {
        case Node.ELEMENT_NODE:
            findIllegalTabs(child);
            break;
        default:
            break;
        }
    }
}

From source file:org.sonar.plugins.xml.checks.IndentCheck.java

/**
 * Collect the indenting whitespace before this node.
 *///from w  w w .  j  a  v a2 s  .  c o  m
private int collectIndent(Node node) {
    int indent = 0;
    for (Node sibling = node.getPreviousSibling(); sibling != null; sibling = sibling.getPreviousSibling()) {
        switch (sibling.getNodeType()) {
        case Node.COMMENT_NODE:
        case Node.ELEMENT_NODE:
            return indent;
        case Node.TEXT_NODE:
            String text = sibling.getTextContent();
            if (StringUtils.isWhitespace(text)) {
                for (int i = text.length() - 1; i >= 0; i--) {
                    char c = text.charAt(i);
                    switch (c) {
                    case '\n':
                        // newline found, we are done
                        return indent;
                    case '\t':
                        // add tabsize
                        indent += tabSize;
                        break;
                    case ' ':
                        // add one space
                        indent++;
                        break;
                    default:
                        break;
                    }
                }
            } else {
                // non whitespace found, we are done
                return indent;
            }
            break;
        default:
            break;
        }
    }
    return indent;
}

From source file:utils.form.WebFormUserInputsCollector.java

private void fillOutNonLabeledFieldLabelDomPointer(UserInputDom valueHolder, Node searchStartingNode,
        Node searchUpEndingNode, boolean endingNodeInclusive, boolean leftLabeled) {
    Node tempParent2 = searchStartingNode;

    if (leftLabeled) {

        while (tempParent2.getPreviousSibling() == null && tempParent2 != searchUpEndingNode) {
            tempParent2 = tempParent2.getParentNode();
        }//w  ww . j  a  v a 2  s.co m
        // if tempParent2 is form node, we will use the form's
        // previous sibling as the label node;
        // Or we will use the nearest input sibling node as the
        // label node;
        if (tempParent2 == searchUpEndingNode && !endingNodeInclusive)
            return;
        valueHolder.setLabelDomPointer(tempParent2.getPreviousSibling());
    } else {
        while (tempParent2.getNextSibling() == null && tempParent2 != searchUpEndingNode) {
            tempParent2 = tempParent2.getParentNode();
        }
        // if tempParent2 is form node, we will use the form's
        // previous sibling as the label node;
        // Or we will use the nearest input sibling node as the
        // label node;
        if (tempParent2 == searchUpEndingNode && !endingNodeInclusive)
            return;

        valueHolder.setLabelDomPointer(tempParent2.getNextSibling());
    }

}

From source file:utils.form.WebFormUserInputsCollector.java

private void fillOutUserViewablePreviousSibling(UserInputDom valueHolder, Node searchStartingNode,
        Node searchUpEndingNode, boolean endingNodeInclusive) {
    Node tempParent2 = searchStartingNode;

    while (tempParent2.getPreviousSibling() == null && tempParent2 != searchUpEndingNode) {
        tempParent2 = tempParent2.getParentNode();
    }/*from www  . ja  va2  s  .  com*/
    // if tempParent2 is form node, we will use the form's
    // previous sibling as the label node;
    // Or we will use the nearest input sibling node as the
    // label node;
    if (tempParent2 == searchUpEndingNode && !endingNodeInclusive)
        return;
    valueHolder.setPreviousUserViewableHtmlSibling(tempParent2.getPreviousSibling());

}

From source file:utils.form.WebFormUserInputsCollector.java

private boolean siblingHasInput(Node node) {
    boolean retVal = false;
    for (int i = 0; i < USER_CHANGABLE_INPUT_TAGS.length; i++) {
        boolean nextSiblingIsInput = false;
        if (node.getNextSibling() != null) {
            nextSiblingIsInput = Arrays.asList(USER_CHANGABLE_INPUT_TAGS)
                    .contains($(node.getNextSibling()).tag().toLowerCase())
                    || $(node.getNextSibling()).find(USER_CHANGABLE_INPUT_TAGS[i]).isNotEmpty();
        }/* w  w w . j a v a2 s . com*/
        boolean previousSiblingIsInput = false;

        if (node.getPreviousSibling() != null) {
            previousSiblingIsInput = Arrays.asList(USER_CHANGABLE_INPUT_TAGS)
                    .contains($(node.getPreviousSibling()).tag().toLowerCase())
                    || $(node.getPreviousSibling()).find(USER_CHANGABLE_INPUT_TAGS[i]).isNotEmpty();
        }
        retVal = nextSiblingIsInput || previousSiblingIsInput;
        if (retVal)
            break;
    }
    return retVal;
}

From source file:utils.form.WebFormUserInputsCollector.java

private boolean leftRightDirectedSiblingHasNoInput(Node node, boolean leftLabeled) {
    boolean retVal = true;
    for (int i = 0; i < USER_CHANGABLE_INPUT_TAGS.length; i++) {

        if (leftLabeled) {
            if (node.getPreviousSibling() == null) {
                retVal = true;/*from ww w .ja va  2  s  .  com*/
            } else if (Arrays.asList(USER_CHANGABLE_INPUT_TAGS)
                    .contains(node.getPreviousSibling().getNodeName().toLowerCase()))
                retVal = false;
            else
                retVal = retVal && $(node.getPreviousSibling()).find(USER_CHANGABLE_INPUT_TAGS[i]).isEmpty();
        }
        if (!leftLabeled) {
            if (node.getNextSibling() == null) {
                retVal = true;
            } else if (Arrays.asList(USER_CHANGABLE_INPUT_TAGS)
                    .contains(node.getNextSibling().getNodeName().toLowerCase()))
                retVal = false;
            else
                retVal = retVal && $(node.getNextSibling()).find(USER_CHANGABLE_INPUT_TAGS[i]).isEmpty();
        }
    }
    return retVal;
}

From source file:utils.form.WebFormUserInputsCollector.java

private boolean leftRightDirectedSiblingWithLabel(Node node, boolean leftLabeled) {
    boolean retVal = false;
    if (leftLabeled) {
        if (node.getPreviousSibling() != null) {
            retVal = $(node.getPreviousSibling()).find("label").isNotEmpty()
                    || node.getPreviousSibling().getNodeName().equalsIgnoreCase("label");
        }/*from  w  ww.  j  a  v a  2s  . c om*/
    } else {
        if (node.getNextSibling() != null) {
            retVal = $(node.getNextSibling()).find("label").isNotEmpty()
                    || node.getNextSibling().getNodeName().equalsIgnoreCase("label");
        }
    }
    return retVal;
}

From source file:utils.form.WebFormUserInputsCollector.java

private List<Element> getLeftRightDirectedSiblingLabelInNode(Node node, boolean leftLabeled) {
    List<Element> labels2 = new ArrayList<Element>();

    if (leftLabeled) {
        if (node.getPreviousSibling().getNodeName().equalsIgnoreCase("label"))
            labels2.add((Element) node.getPreviousSibling());
        else/*from ww w  . j  a  v  a2 s .c o m*/
            labels2 = $(node.getPreviousSibling()).find("label").get();
    } else {
        if (node.getNextSibling().getNodeName().equalsIgnoreCase("label"))
            labels2.add((Element) node.getPreviousSibling());
        else
            labels2 = $(node.getNextSibling()).find("label").get();
    }
    return labels2;
}

From source file:utils.form.WebFormUserInputsCollector.java

private UserInputDom initUserInputDomInsideOfForm(Document domDoc, Node inputNode, Node form) {
    // NodeList allInputNodes, Node inputNodeParentForm) {
    UserInputDom retVal = new UserInputDom(inputNode);

    boolean leftLabeled = isLeftLabeled(inputNode);

    retVal.setxPath($(inputNode).xpath());
    retVal.setParentFormPointer(form);//ww  w.  j ava2s.com

    Node maxInputParentNoOtherInput = getMaxInputParentNoOtherInput(inputNode, true);

    // Node tempParent = inputNode.getParentNode();
    boolean singleInputFieldForm = false;

    // Node maxInputParentNoOtherInput = tempParent;
    // while (($(tempParent).find("input").get().size() + $(tempParent)
    // .find("textarea").get().size()) <= 1) {
    // maxInputParentNoOtherInput = tempParent;
    // if (tempParent.getNodeName().equalsIgnoreCase("form")) {
    // singleFieldForm = true;
    // break;
    // }
    // tempParent = tempParent.getParentNode();
    // }
    // if signlefieldform leastInputsCommonParent is the form node
    Node leastInputsCommonParent;
    if (maxInputParentNoOtherInput.getNodeName().equalsIgnoreCase("form")) {
        leastInputsCommonParent = maxInputParentNoOtherInput;
        singleInputFieldForm = true;
    } else
        leastInputsCommonParent = maxInputParentNoOtherInput.getParentNode();

    boolean singleFieldFormInputHasNoSibling = false;
    Node maxInputParentNoOtherChild = getMaxInputParentNoOtherChild(inputNode);
    // Node tempParent2 = inputNode.getParentNode();
    // Node maxInputParentNoOtherChild = inputNode;
    // while ($(tempParent2).children().get().size() <= 1) {
    // maxInputParentNoOtherChild = tempParent2;
    if (maxInputParentNoOtherChild.getNodeName().equalsIgnoreCase("form")) {
        singleFieldFormInputHasNoSibling = true;
        // new a return result in a single input and no input
        // sibling form
        List<Node> temp1 = new ArrayList<Node>();
        temp1.add(maxInputParentNoOtherChild.getChildNodes().item(0));
        retVal.setMachineLearningDomHtmlPointer(temp1);

        retVal.setLabelDomPointer(maxInputParentNoOtherChild.getPreviousSibling());

        if (leftLabeled) {
            List<Node> temp = new ArrayList<Node>();
            temp.add(maxInputParentNoOtherChild.getNextSibling());
            retVal.setAdditionalInfoNodes(temp);
        }
        retVal.setPreviousUserViewableHtmlSibling(maxInputParentNoOtherChild.getPreviousSibling());
        retVal.setNextUserViewableHtmlSibling(maxInputParentNoOtherChild.getNextSibling());
        // break;
    }
    // tempParent2 = tempParent2.getParentNode();
    // }
    // TODO the parent of maxInputParentNoOtherChild might have input
    // fields, might not have input fields
    Node leastNonInputSiblingsParent = maxInputParentNoOtherChild.getParentNode();
    if (singleInputFieldForm && !singleFieldFormInputHasNoSibling) {
        List<Node> temp = new ArrayList<Node>();
        for (int i = 0; i < leastInputsCommonParent.getChildNodes().getLength(); i++) {
            temp.add(leastInputsCommonParent.getChildNodes().item(i));
        }
        retVal.setMachineLearningDomHtmlPointer(temp);

        List<Element> labels = $(leastInputsCommonParent).find("label").get();
        if (labels.size() > 0) {
            retVal.setLabelDomPointer(labels.get(0));
        } else {

            fillOutNonLabeledFieldLabelDomPointer(retVal, maxInputParentNoOtherChild, leastInputsCommonParent,
                    false, leftLabeled);
            // tempParent2 = maxInputParentNoOtherChild;
            //
            // while (tempParent2.getPreviousSibling() == null
            // && !tempParent2.getNodeName().equalsIgnoreCase(
            // "form")) {
            // tempParent2 = tempParent2.getParentNode();
            // }
            // // if tempParent2 is form node, we will use the form's
            // // previous sibling as the label node;
            // // Or we will use the nearest input sibling node as the
            // // label node;
            // retVal.setLabelDomPointer(tempParent2.getPreviousSibling());

        }

        // fill out addtional info nodes
        if (leftLabeled)
            fillOutAddtionalInfoNode(retVal, maxInputParentNoOtherChild, leastInputsCommonParent, false, false);
        // tempParent2 = maxInputParentNoOtherChild;
        // List<Node> additionalInfoNodes = new ArrayList<Node>();
        // while (tempParent2.getNextSibling() == null
        // && !tempParent2.getNodeName().equalsIgnoreCase("form")) {
        // tempParent2 = tempParent2.getParentNode();
        // Node tempNode = tempParent2;
        // while (tempNode.getNextSibling() != null) {
        // additionalInfoNodes.add(tempNode.getNextSibling());
        // if (tempNode.getNodeName().equalsIgnoreCase("form")) break;
        // tempNode = tempNode.getNextSibling();
        // }
        // }
        // retVal.setAdditionalInfoNodes(additionalInfoNodes);

        // fill out non empty user viewable previous sibling
        fillOutUserViewablePreviousSibling(retVal, maxInputParentNoOtherChild, leastInputsCommonParent, false);
        // tempParent2 = maxInputParentNoOtherChild;
        //
        // while (tempParent2.getPreviousSibling() == null
        // && !tempParent2.getNodeName().equalsIgnoreCase(
        // "form")) {
        // tempParent2 = tempParent2.getParentNode();
        // }
        // // if tempParent2 is form node, we will use the form's
        // // previous sibling as the label node;
        // // Or we will use the nearest input sibling node as the
        // // label node;
        // retVal.setPreviousUserViewableHtmlSibling(tempParent2.getPreviousSibling());

        // set non empty user viewable next sibling
        fillOutUserViewableNextSibling(retVal, maxInputParentNoOtherChild, leastInputsCommonParent, false);
        // tempParent2 = maxInputParentNoOtherChild;
        // while (tempParent2.getNextSibling() == null
        // && !tempParent2.getNodeName().equalsIgnoreCase("form")) {
        // tempParent2 = tempParent2.getParentNode();
        // }
        // retVal.setNextUserViewableHtmlSibling(tempParent2.getNextSibling());

    } else if (!singleFieldFormInputHasNoSibling) {

        List<Element> labels = $(maxInputParentNoOtherInput).find("label").get();
        if (labels.size() > 0) {

            List<Node> tempList = new ArrayList<Node>();
            tempList.add(maxInputParentNoOtherInput);
            retVal.setMachineLearningDomHtmlPointer(tempList);

            retVal.setLabelDomPointer(labels.get(0));
            if (leftLabeled)
                fillOutAddtionalInfoNode(retVal, maxInputParentNoOtherChild, leastNonInputSiblingsParent, false,
                        false);
            fillOutUserViewablePreviousSibling(retVal, maxInputParentNoOtherChild, leastNonInputSiblingsParent,
                    false);
            fillOutUserViewableNextSibling(retVal, maxInputParentNoOtherChild, leastNonInputSiblingsParent,
                    false);
        } else {
            if (leftRightDirectedSiblingHasNoInput(maxInputParentNoOtherInput, leftLabeled)
                    && leftRightDirectedSiblingWithLabel(maxInputParentNoOtherInput, leftLabeled)) {
                List<Element> labels2 = getLeftRightDirectedSiblingLabelInNode(maxInputParentNoOtherInput,
                        leftLabeled);

                List<Node> tempList = new ArrayList<Node>();
                if (leftLabeled) {
                    tempList.add(maxInputParentNoOtherInput.getPreviousSibling());
                    tempList.add(maxInputParentNoOtherInput);
                } else {
                    tempList.add(maxInputParentNoOtherInput);
                    tempList.add(maxInputParentNoOtherInput.getNextSibling());
                }
                retVal.setMachineLearningDomHtmlPointer(tempList);

                retVal.setLabelDomPointer(labels2.get(0));

                if (leftLabeled && leftRightDirectedSiblingHasNoInput(maxInputParentNoOtherInput, false))
                    fillOutAddtionalInfoNode(retVal, maxInputParentNoOtherInput,
                            maxInputParentNoOtherInput.getParentNode(), false, true);
                fillOutUserViewablePreviousSibling(retVal, maxInputParentNoOtherInput,
                        maxInputParentNoOtherInput.getParentNode(), false);
                fillOutUserViewableNextSibling(retVal, maxInputParentNoOtherInput,
                        maxInputParentNoOtherInput.getParentNode(), false);
            } else if (this.siblingHasInput(maxInputParentNoOtherInput)) {
                // most likely field (label equivelant information has been
                // inside Node
                // maxInputParentNoOtherInput
                List<Node> tempList = new ArrayList<Node>();
                tempList.add(maxInputParentNoOtherInput);
                retVal.setMachineLearningDomHtmlPointer(tempList);

                fillOutNonLabeledFieldLabelDomPointer(retVal, maxInputParentNoOtherChild,
                        maxInputParentNoOtherInput, false, leftLabeled);
                if (leftLabeled)
                    fillOutAddtionalInfoNode(retVal, maxInputParentNoOtherChild, maxInputParentNoOtherInput,
                            false, false);
                fillOutUserViewablePreviousSibling(retVal, maxInputParentNoOtherChild,
                        maxInputParentNoOtherInput, false);
                fillOutUserViewableNextSibling(retVal, maxInputParentNoOtherChild, maxInputParentNoOtherInput,
                        false);

            } else if (leftRightDirectedSiblingHasNoInput(maxInputParentNoOtherInput, leftLabeled)) {
                // most likely field information is out of
                // maxInputParentNoOtherInput in its siblings with no label
                // tag
                List<Node> tempList = new ArrayList<Node>();
                if (leftLabeled) {
                    tempList.add(maxInputParentNoOtherInput.getPreviousSibling());
                    tempList.add(maxInputParentNoOtherInput);
                } else {
                    tempList.add(maxInputParentNoOtherInput);
                    tempList.add(maxInputParentNoOtherInput.getNextSibling());
                }
                retVal.setMachineLearningDomHtmlPointer(tempList);

                if (leftLabeled)
                    retVal.setLabelDomPointer(maxInputParentNoOtherInput.getPreviousSibling());
                else
                    retVal.setLabelDomPointer(maxInputParentNoOtherInput.getNextSibling());

                if (leftLabeled && leftRightDirectedSiblingHasNoInput(maxInputParentNoOtherInput, false))
                    fillOutAddtionalInfoNode(retVal, maxInputParentNoOtherInput,
                            maxInputParentNoOtherInput.getParentNode(), false, true);

                fillOutUserViewablePreviousSibling(retVal, maxInputParentNoOtherInput,
                        maxInputParentNoOtherInput.getParentNode(), false);
                fillOutUserViewableNextSibling(retVal, maxInputParentNoOtherInput,
                        maxInputParentNoOtherInput.getParentNode(), false);

            }
        }
        // if (leastNonInputSiblingsParent != leastInputsCommonParent) {
        // tempParent2 = leastNonInputSiblingsParent;
        // while (tempParent2.getParentNode() != leastInputsCommonParent) {
        // tempParent2.getParentNode();
        // }
        // Node maxNonInputSiblingsParent = tempParent2;
        //
        // } else {
        // List<Element> allInputSiblings = $(leastInputsCommonParent)
        // .children().get();
        // for (int indexOfDirectChild; indexOfDirectChild <
        // allInputSiblings
        // .size(); indexOfDirectChild++) {
        // // if the sibling has form in it, then ignore this
        // // sibling since it and its children inputs are not
        // // related to this current processing input.
        // if ($(allInputSiblings.get(indexOfDirectChild))
        // .find("form").get().size() > 0) {
        // continue;
        // }
        //
        // int sizeOfInputElementInSibling = $(
        // allInputSiblings.get(indexOfDirectChild))
        // .find("input").get().size();
        // if (sizeOfInputElementInSibling == 1) {
        // // input block which might have label and additional
        // // info
        // Node nextSibling = allInputSiblings.get(
        // indexOfDirectChild).getNextSibling();
        // Node previousSibling = allInputSiblings.get(
        // indexOfDirectChild).getPreviousSibling();
        // if (previousSibling == null) {
        // // first child in least common parent
        //
        // } else if (nextSibling == null) {
        // // last child in least common parent
        // } else {
        // // neither first nor last child.
        // if ($(nextSibling).find("input").get().size() == 1) {
        // // this html block most likely a label
        // }
        // }
        // } else if (sizeOfInputElementInSibling == 0) {
        // // might be label or other information of the next
        // // input or might be non input related infor.
        // continue;
        //
        // } else if (sizeOfInputElementInSibling > 1) {
        // // html block has more than one input, need to find
        // // the least parent for these inputs.
        // // TODO need a nest function to handle this type of
        // // html block
        // }
        // }
        // }
    }
    return retVal;
}