com.htmlhifive.tools.codeassist.core.proposal.checker.CheckerUtils.java Source code

Java tutorial

Introduction

Here is the source code for com.htmlhifive.tools.codeassist.core.proposal.checker.CheckerUtils.java

Source

/*
 * Copyright (C) 2012 NS Solutions Corporation
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */
package com.htmlhifive.tools.codeassist.core.proposal.checker;

import java.util.List;
import java.util.regex.Pattern;

import org.apache.commons.lang.StringUtils;
import org.eclipse.wst.jsdt.core.ast.IASTNode;
import org.eclipse.wst.jsdt.core.ast.IAssignment;
import org.eclipse.wst.jsdt.core.ast.ILocalDeclaration;
import org.eclipse.wst.jsdt.core.ast.IObjectLiteralField;
import org.eclipse.wst.jsdt.core.ast.IStatement;
import org.eclipse.wst.jsdt.internal.codeassist.complete.CompletionOnMemberAccess;
import org.eclipse.wst.jsdt.internal.compiler.ast.CompilationUnitDeclaration;

import com.htmlhifive.tools.codeassist.core.config.bean.FunctionBean;
import com.htmlhifive.tools.codeassist.core.config.bean.ObjectLiteralBean;
import com.htmlhifive.tools.codeassist.core.proposal.collector.FunctionNameVisitor;
import com.htmlhifive.tools.codeassist.core.proposal.collector.MemberAccessVisitor;
import com.htmlhifive.tools.codeassist.core.proposal.collector.NodeCollector;

/**
 * 
 * ??.
 * 
 * @author NS Solutions Corporation
 * 
 */
@SuppressWarnings("restriction")
public final class CheckerUtils {

    /**
     * (??)?.
     */
    private static final Pattern KEY_PATTERN = Pattern.compile("[A-Za-z]\\w*");

    /**
     * .
     */
    private CheckerUtils() {

        // no create
    }

    /**
     * ?????????.
     * 
     * @param codeAssistStr ?.
     * @return ??
     */
    public static String getRootObject(String codeAssistStr) {

        String[] codeAssistStrParts = StringUtils.split(codeAssistStr, ".");
        // ?????.
        String beforePart = null;
        for (String part : codeAssistStrParts) {
            if (KEY_PATTERN.matcher(part).matches() || beforePart == null) {
                beforePart = part;
                continue;
            }
            return beforePart;
        }
        return beforePart;
    }

    /**
     * ??????.
     * 
     * @param collector 
     * @param unitDeclaration jsFile?.
     * @return 
     */
    static SuffixAssistNodeInfo getAssistNodeInfo(NodeCollector collector,
            CompilationUnitDeclaration unitDeclaration) {

        // ?????
        IStatement[] collectedNodes = (IStatement[]) collector.getNodes();
        // List<IStatement> relateControllerNodes = new ArrayList<IStatement>();
        SuffixAssistNodeInfo info = new SuffixAssistNodeInfo();
        // ??
        MemberAccessVisitor visitor = new MemberAccessVisitor();
        unitDeclaration.traverse(visitor);
        info.setMemberAccess(visitor.getMemberAccess());
        for (IStatement collectedNode : collectedNodes) {
            visitor = new MemberAccessVisitor();
            collectedNode.traverse(visitor);
            if (visitor.getMemberAccess() != null) {
                // relateControllerNodes.add(controller);
                info.setMemberAccess(visitor.getMemberAccess());
                // ??????
                info.addTargetNodeList(collectedNode);
            }
        }
        return info;
    }

    /**
     * ???????.
     * 
     * @param bean 
     * @param codeAssistNode 
     * @return ????????
     */
    static boolean checkCodeAssistNode(ObjectLiteralBean bean, CompletionOnMemberAccess codeAssistNode) {

        FunctionNameVisitor visitor = new FunctionNameVisitor();
        codeAssistNode.traverse(visitor);
        List<String> funcNames = visitor.getLastFunctionNames();
        if (funcNames.size() == 0) {
            return true;
        }
        FunctionBean[] elems = bean.getFunctions();
        for (FunctionBean elem : elems) {
            if (funcNames.contains(elem.getName())) {
                return true;
            }
        }
        return false;
    }

    /**
     * astNode???????????.<br>
     * ????????-1?.<br>
     * ILocalDeclaration?IObjectLiteralField?.
     * 
     * @param suffixPattern ???.
     * @param astNode 
     * @return ?????.
     */
    static int getInitializerSourceEnd(Pattern suffixPattern, IASTNode astNode) {

        String name = null;
        int result = 0;
        if (astNode instanceof ILocalDeclaration) {
            ILocalDeclaration localDec = (ILocalDeclaration) astNode;
            name = String.valueOf(localDec.getName());
            result = localDec.getInitialization().sourceEnd();
        } else if (astNode instanceof IObjectLiteralField) {
            IObjectLiteralField objectLit = (IObjectLiteralField) astNode;
            name = objectLit.getFieldName().toString();
            result = objectLit.getInitializer().sourceEnd();
        } else if (astNode instanceof IAssignment) {
            // xxxSuffix.prototype?
            IAssignment assignment = (IAssignment) astNode;
            // xxxSuffix??
            name = StringUtils.split(assignment.getLeftHandSide().toString(), '.')[0];
            result = assignment.getExpression().sourceEnd();
        }
        if (name != null && suffixPattern.matcher(name).matches()) {
            return result;
        }
        return -1;
    }
}