Search in sources :

Example 1 with JsonTreeNode

use of org.talend.repository.ui.wizards.metadata.connection.files.json.JsonTreeNode in project tdi-studio-se by Talend.

the class JSONXPathProposalProvider method getProposals4JsonPath.

private IContentProposal[] getProposals4JsonPath(String contents, int position) {
    AbstractTreePopulator treePopulator = linker.getTreePopulator();
    if (!(treePopulator instanceof JsonTreePopulator)) {
        return null;
    }
    XmlNodeRetriever nodeRetriever = linker.getNodeRetriever();
    if (!(nodeRetriever instanceof JsonNodeRetriever)) {
        return null;
    }
    JsonNodeRetriever jsonNodeRetriever = (JsonNodeRetriever) nodeRetriever;
    String beforeCursorExp = null;
    boolean isAbsoluteExpression = contents.trim().startsWith(linker.getRootSeperator());
    beforeCursorExp = contents.substring(0, position);
    int lastIndexFieldSeperator = beforeCursorExp.lastIndexOf(linker.getFieldSeperator());
    String currentExpr = null;
    if (0 <= lastIndexFieldSeperator) {
        currentExpr = beforeCursorExp.substring(0, lastIndexFieldSeperator);
    } else {
        currentExpr = beforeCursorExp;
    }
    currentExpr = currentExpr.trim();
    String currentWord = extractLastWord(beforeCursorExp);
    if (!isAbsoluteExpression && lastIndexFieldSeperator < 0) {
        currentWord = currentExpr;
        currentExpr = "";
    }
    if (currentWord != null) {
        currentWord = currentWord.trim();
    }
    List<JsonTreeNode> proposalNodes = jsonNodeRetriever.retrieveProposalJsonTreeNode((JsonTreePopulator) treePopulator, currentExpr, currentWord, isRelativeTable, isAbsoluteExpression);
    List<IContentProposal> proposals = new ArrayList<IContentProposal>();
    if (proposalNodes != null && !proposalNodes.isEmpty()) {
        Iterator<JsonTreeNode> iter = proposalNodes.iterator();
        while (iter.hasNext()) {
            JsonTreeNode jsonTreeNode = iter.next();
            JsonPathContentProposal proposal = new JsonPathContentProposal(jsonTreeNode.getLabel());
            proposals.add(proposal);
        }
    }
    return proposals.toArray(new IContentProposal[proposals.size()]);
}
Also used : JsonTreeNode(org.talend.repository.ui.wizards.metadata.connection.files.json.JsonTreeNode) XmlNodeRetriever(org.talend.commons.runtime.xml.XmlNodeRetriever) ArrayList(java.util.ArrayList) IContentProposal(org.eclipse.jface.fieldassist.IContentProposal) AbstractTreePopulator(org.talend.repository.ui.wizards.metadata.connection.files.json.AbstractTreePopulator) JsonTreePopulator(org.talend.repository.ui.wizards.metadata.connection.files.json.JsonTreePopulator)

Example 2 with JsonTreeNode

use of org.talend.repository.ui.wizards.metadata.connection.files.json.JsonTreeNode in project tdi-studio-se by Talend.

the class JsonNodeRetriever method retrieveProposalJsonTreeNode.

public List<JsonTreeNode> retrieveProposalJsonTreeNode(JsonTreePopulator populator, String jsonPath, String word, boolean isRelativeTable, boolean isAbsolutePath) {
    List<JsonTreeNode> rootJsonTreeNodes = populator.getRootJsonTreeNodes();
    if (rootJsonTreeNodes == null || rootJsonTreeNodes.isEmpty()) {
        return null;
    }
    List<JsonTreeNode> proposalNodes = new ArrayList<JsonTreeNode>();
    String fullPath = null;
    if (isRelativeTable) {
        if (isAbsolutePath) {
            fullPath = jsonPath;
        } else {
            fullPath = currentLoopXPath;
            if (jsonPath != null && !jsonPath.isEmpty()) {
                //$NON-NLS-1$
                fullPath = fullPath + "." + jsonPath;
            }
        }
    } else {
        if (isAbsolutePath) {
            fullPath = jsonPath;
        } else {
            return rootJsonTreeNodes;
        }
    }
    JsonTreeNode treeNode = SchemaPopulationUtil.getJsonTreeNodeByJsonPath(rootJsonTreeNodes.toArray(), SchemaPopulationUtil.getFilteredJsonPath(fullPath));
    Object[] treeNodeChildren = treeNode.getChildren();
    if (treeNodeChildren == null || treeNodeChildren.length <= 0) {
        return null;
    }
    if (word != null && !word.isEmpty()) {
        word = word.toLowerCase();
    } else {
        word = null;
    }
    for (Object obj : treeNodeChildren) {
        if (!(obj instanceof JsonTreeNode)) {
            continue;
        }
        JsonTreeNode proposalNode = (JsonTreeNode) obj;
        if (word != null) {
            String name = proposalNode.getLabel();
            if (name != null && name.toLowerCase().startsWith(word)) {
                proposalNodes.add(proposalNode);
            }
        } else {
            proposalNodes.add(proposalNode);
        }
    }
    return proposalNodes;
}
Also used : JsonTreeNode(org.talend.repository.ui.wizards.metadata.connection.files.json.JsonTreeNode) ArrayList(java.util.ArrayList)

Aggregations

ArrayList (java.util.ArrayList)2 JsonTreeNode (org.talend.repository.ui.wizards.metadata.connection.files.json.JsonTreeNode)2 IContentProposal (org.eclipse.jface.fieldassist.IContentProposal)1 XmlNodeRetriever (org.talend.commons.runtime.xml.XmlNodeRetriever)1 AbstractTreePopulator (org.talend.repository.ui.wizards.metadata.connection.files.json.AbstractTreePopulator)1 JsonTreePopulator (org.talend.repository.ui.wizards.metadata.connection.files.json.JsonTreePopulator)1