Search in sources :

Example 1 with XPathContentProposal

use of org.talend.commons.ui.swt.proposal.xpath.XPathContentProposal in project tdi-studio-se by Talend.

the class JSONXPathProposalProvider method addTooManyNodesContentProposal.

/**
     * DOC amaumont Comment method "addTooManyNodesContentProposal".
     * 
     * @param proposals
     */
private void addTooManyNodesContentProposal(List<IContentProposal> proposals) {
    XPathContentProposal contentProposal = new XPathContentProposal("<< Too many nodes, proposed values may be incomplete ... >>");
    proposals.add(contentProposal);
}
Also used : XPathContentProposal(org.talend.commons.ui.swt.proposal.xpath.XPathContentProposal)

Example 2 with XPathContentProposal

use of org.talend.commons.ui.swt.proposal.xpath.XPathContentProposal in project tdi-studio-se by Talend.

the class JSONXPathProposalProvider method getProposals4XPath.

/**
     * DOC cmeng Comment method "getProposals4XPath".
     * 
     * @param contents
     * @param position
     * @return
     */
private IContentProposal[] getProposals4XPath(String contents, int position) {
    int nodeFieldMax = 500;
    int nodeLoopMax = 3000;
    int nodeLoopNumberLimit = 10;
    int nodeFieldNumberLimit = 10;
    int currentNodeNumber = 0;
    List<IContentProposal> proposals = new ArrayList<IContentProposal>();
    String beforeCursorExp = null;
    boolean isRelativeExpression = !contents.trim().startsWith(SLASH);
    if (isRelativeTable && isRelativeExpression) {
        // beforeCursorExp = linker.getCurrentLoopXPath() + SLASH + contents;
        beforeCursorExp = contents;
    } else {
        beforeCursorExp = contents.substring(0, position);
    }
    int lastIndexSlash = beforeCursorExp.lastIndexOf(SLASH);
    int lastIndexPipe = beforeCursorExp.lastIndexOf(PIPE);
    String currentExpr = null;
    if (isRelativeTable && isRelativeExpression) {
        currentExpr = beforeCursorExp;
    }
    if (lastIndexSlash == -1 || lastIndexSlash < lastIndexPipe && lastIndexPipe != -1) {
        currentExpr = EMPTY_STRING;
    } else if (lastIndexPipe < lastIndexSlash && lastIndexPipe != -1) {
        currentExpr = beforeCursorExp.substring(lastIndexPipe + 1, lastIndexSlash + 1);
    } else if (lastIndexSlash != -1) {
        currentExpr = beforeCursorExp.substring(0, lastIndexSlash + 1);
    // currentExpr = beforeCursorExp;
    } else {
        currentExpr = beforeCursorExp;
    }
    currentExpr = currentExpr.trim();
    String currentWord = extractLastWord(beforeCursorExp);
    boolean expressionIsEmpty = currentExpr.trim().length() == 0;
    // String xPathExpression =
    //
    // // + " | " +
    // createXPathExpression(beforeCursorExp)
    // ;
    // System.out.println("#############################");
    // System.out.println("currentExpr='" + currentExpr + "'");
    // System.out.println("beforeCursorExp='"+beforeCursorExp+"'");
    // System.out.println("currentWord='"+currentWord+"'");
    // System.out.println("1");
    ArrayList<Node> allLoopNodes = linker.getAllLoopNodes();
    boolean resultsMayBeIncomplete = false;
    boolean breakAll = false;
    Set<String> alreadyAdded = new HashSet<String>();
    //
    if (isRelativeTable && (isRelativeExpression || expressionIsEmpty)) {
        int allNodesLoopSize = allLoopNodes.size();
        // System.out.println("nodeLoop size list : " + allNodesLoopSize);
        currentNodeNumber += allNodesLoopSize;
        if (allNodesLoopSize > nodeLoopMax) {
            resultsMayBeIncomplete = true;
        } else {
            int nodeLoopNumberOfLoop = allNodesLoopSize;
            if (allNodesLoopSize > nodeLoopNumberLimit) {
                nodeLoopNumberOfLoop = nodeLoopNumberLimit;
                resultsMayBeIncomplete = true;
            }
            for (int i = 0; i < nodeLoopNumberOfLoop; i++) {
                Node nodeLoop = allLoopNodes.get(i);
                // System.out.println("nodeLoop : " + i);
                List<Node> nodeList = null;
                try {
                    nodeList = this.linker.getNodeRetriever().retrieveNodeListFromNode(modifyXpathToSearchAllChildren(currentExpr, true), nodeLoop);
                } catch (XPathExpressionException e) {
                    ExceptionHandler.process(e);
                }
                if (nodeList != null && nodeList.size() == 0) {
                    try {
                        nodeList = linker.getNodeRetriever().retrieveNodeListFromNode(modifyXpathToSearchAllChildren(beforeCursorExp, true), nodeLoop);
                    } catch (XPathExpressionException e) {
                        ExceptionHandler.process(e);
                    }
                }
                if (nodeList != null) {
                    int allNodesFieldsLoopSize = nodeList.size();
                    currentNodeNumber += allNodesFieldsLoopSize;
                    if (allNodesFieldsLoopSize > nodeFieldMax) {
                        resultsMayBeIncomplete = true;
                        breakAll = true;
                    } else {
                        int nodeFieldNumberOfLoop = allNodesFieldsLoopSize;
                        if (allNodesFieldsLoopSize > nodeFieldMax) {
                            nodeFieldNumberOfLoop = nodeFieldNumberLimit;
                            resultsMayBeIncomplete = true;
                        }
                        for (int j = 0; j < nodeFieldNumberOfLoop; ++j) {
                            // System.out.println("nodeField : " + j);
                            Node node = nodeList.get(j);
                            String nodeName = node.getNodeName();
                            String absoluteXPathFromNode = linker.getNodeRetriever().getAbsoluteXPathFromNode(node);
                            if ((currentWord.length() > 0 && nodeName.startsWith(currentWord) || currentWord.length() == 0 || currentWord.equals(//$NON-NLS-1$
                            "/")) && !alreadyAdded.contains(absoluteXPathFromNode)) {
                                XPathContentProposal contentProposal = new XPathContentProposal(node);
                                if (isRelativeTable && isRelativeExpression) {
                                    contentProposal.setRelative(isRelativeTable);
                                    contentProposal.setFirstRelativeNode(contents.indexOf(SLASH) == -1);
                                }
                                proposals.add(contentProposal);
                                alreadyAdded.add(absoluteXPathFromNode);
                            }
                        }
                    // for (int j = 0; j < nodeListLength; ++j) {
                    }
                    if (breakAll) {
                        break;
                    }
                }
            // if (nodeList != null) {
            }
        // for (int i = 0; i < lstSize; i++) {
        }
    // } else {
    }
    // if (!estimationError) {
    // ///////////////////////////////////////////////////////////////////////////////////////////////
    // ///////////////////////////////////////////////////////////////////////////////////////////////
    // ///////////////////////////////////////////////////////////////////////////////////////////////
    // ///////////////////////////////////////////////////////////////////////////////////////////////
    // XPath requests for absolute XPath
    //
    List<Node> nodeList = null;
    if (!expressionIsEmpty) {
        try {
            nodeList = this.linker.getNodeRetriever().retrieveNodeList(modifyXpathToSetFirstAscendant(currentExpr));
        } catch (XPathExpressionException e) {
            ExceptionHandler.process(e);
        }
    }
    if (nodeList != null || expressionIsEmpty) {
        if (!expressionIsEmpty && nodeList.size() > nodeLoopMax) {
            resultsMayBeIncomplete = true;
        } else {
            try {
                nodeList = this.linker.getNodeRetriever().retrieveNodeList(modifyXpathToSearchAllChildren(currentExpr, false));
            } catch (XPathExpressionException e) {
                ExceptionHandler.process(e);
            }
        }
        if (nodeList != null) {
            for (int j = 0; j < nodeList.size(); ++j) {
                // System.out.println("nodeField : " + j);
                Node node = nodeList.get(j);
                String nodeName = node.getNodeName();
                String absoluteXPathFromNode = linker.getNodeRetriever().getAbsoluteXPathFromNode(node);
                if ((currentWord.length() > 0 && nodeName.startsWith(currentWord) || currentWord.length() == 0 || currentWord.equals(//$NON-NLS-1$
                "/")) && !alreadyAdded.contains(absoluteXPathFromNode)) {
                    // System.out.println(absoluteXPathFromNode);
                    XPathContentProposal contentProposal = new XPathContentProposal(node);
                    proposals.add(contentProposal);
                    alreadyAdded.add(absoluteXPathFromNode);
                }
            }
        // for (int j = 0; j < nodeListLength; ++j) {
        }
    // if (nodeList != null) {
    }
    if (resultsMayBeIncomplete) {
        addTooManyNodesContentProposal(proposals);
    }
    IContentProposal[] res = new IContentProposal[proposals.size()];
    res = proposals.toArray(res);
    return res;
}
Also used : XPathExpressionException(javax.xml.xpath.XPathExpressionException) JsonTreeNode(org.talend.repository.ui.wizards.metadata.connection.files.json.JsonTreeNode) Node(org.w3c.dom.Node) ArrayList(java.util.ArrayList) IContentProposal(org.eclipse.jface.fieldassist.IContentProposal) XPathContentProposal(org.talend.commons.ui.swt.proposal.xpath.XPathContentProposal) HashSet(java.util.HashSet)

Aggregations

XPathContentProposal (org.talend.commons.ui.swt.proposal.xpath.XPathContentProposal)2 ArrayList (java.util.ArrayList)1 HashSet (java.util.HashSet)1 XPathExpressionException (javax.xml.xpath.XPathExpressionException)1 IContentProposal (org.eclipse.jface.fieldassist.IContentProposal)1 JsonTreeNode (org.talend.repository.ui.wizards.metadata.connection.files.json.JsonTreeNode)1 Node (org.w3c.dom.Node)1