com.htmlhifive.tools.codeassist.core.proposal.H5ProposalCreater.java Source code

Java tutorial

Introduction

Here is the source code for com.htmlhifive.tools.codeassist.core.proposal.H5ProposalCreater.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;

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

import org.apache.commons.lang.StringUtils;
import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.jface.text.contentassist.ICompletionProposal;
import org.eclipse.wst.jsdt.core.IJavaScriptUnit;
import org.eclipse.wst.jsdt.core.IPackageFragment;
import org.eclipse.wst.jsdt.core.JavaScriptModelException;
import org.eclipse.wst.jsdt.ui.text.java.CompletionProposalCollector;
import org.eclipse.wst.jsdt.ui.text.java.IJavaCompletionProposal;

import com.htmlhifive.tools.codeassist.core.config.bean.AllBean;
import com.htmlhifive.tools.codeassist.core.config.bean.FunctionBean;
import com.htmlhifive.tools.codeassist.core.config.bean.ObjectLiteralBean;
import com.htmlhifive.tools.codeassist.core.config.bean.RootChildrenElem;
import com.htmlhifive.tools.codeassist.core.config.bean.VarReferenceBean;
import com.htmlhifive.tools.codeassist.core.exception.ProposalCheckException;
import com.htmlhifive.tools.codeassist.core.exception.ProposalCreateException;
import com.htmlhifive.tools.codeassist.core.logger.H5CodeAssistPluginLogger;
import com.htmlhifive.tools.codeassist.core.logger.H5CodeAssistPluginLoggerFactory;
import com.htmlhifive.tools.codeassist.core.messages.Messages;
import com.htmlhifive.tools.codeassist.core.proposal.build.CodeBuilder;
import com.htmlhifive.tools.codeassist.core.proposal.build.CodeBuilderFactory;
import com.htmlhifive.tools.codeassist.core.proposal.checker.CheckerFactory;
import com.htmlhifive.tools.codeassist.core.proposal.checker.DummyCodeInfo;
import com.htmlhifive.tools.codeassist.core.proposal.checker.ProposalChecker;
import com.htmlhifive.tools.codeassist.core.proposal.wrapper.ProposalWrapper;
import com.htmlhifive.tools.codeassist.core.proposal.wrapper.ProposalWrapperFactory;

/**
 * ???????.
 * 
 * @author NS Solutions Corporation
 * 
 */
public class H5ProposalCreater extends AbstractProposalCreator {

    /**
     * .
     */
    private static H5CodeAssistPluginLogger logger = H5CodeAssistPluginLoggerFactory
            .getLogger(H5ProposalCreater.class);

    /**
     * ????Hi5.
     */
    private AllBean bean;

    /**
     * .
     * 
     * @param context .
     * @param bean ????Hi5.
     */
    public H5ProposalCreater(ProposalContext context, AllBean bean) {

        super(context);
        this.bean = bean;
    }

    @Override
    public List<ICompletionProposal> createProposal() throws ProposalCreateException {

        try {
            ProposalChecker checker = null;
            RootChildrenElem targetElem = null;
            RootChildrenElem[] allElem = bean.getElemList();
            for (RootChildrenElem rootChildrenElem : allElem) {
                // ???????????.
                checker = CheckerFactory.createChecker(getContext().getCompilationUnit(), getContext().getProject(),
                        rootChildrenElem);
                // ???????????????.
                boolean createFlg = checker.check(getContext().getInvocationOffset());
                if (createFlg) {
                    targetElem = rootChildrenElem;
                    break;
                }
            }
            if (targetElem == null) {
                logger.log(Messages.DB0001, false);
                return Collections.emptyList();
            }
            logger.log(Messages.DB0001, true);
            // ? ???ObjectLiteral????.
            if (targetElem instanceof ObjectLiteralBean) {
                return createProposalForObjectLiteralBean((ObjectLiteralBean) targetElem,
                        checker.existDefaultCodeAssist(), checker.getDummyCodeInfo());
            }

        } catch (JavaScriptModelException e) {
            logger.log(Messages.EM0005, e);
        } catch (ProposalCheckException e) {
            logger.log(Messages.EM0006, e);
        }
        return Collections.emptyList();
    }

    /**
     * ??????????.
     * 
     * 
     * @param objBean .
     * @param existDefaultCodeAssist ???????.
     * @param codeInfo ?.
     * @return .
     * @throws JavaScriptModelException ?.
     */
    private List<ICompletionProposal> createProposalForObjectLiteralBean(ObjectLiteralBean objBean,
            boolean existDefaultCodeAssist, DummyCodeInfo[] codeInfo) throws JavaScriptModelException {

        // ?.
        List<ICompletionProposal> resultList = new ArrayList<ICompletionProposal>();
        ProposalContext context = getContext();

        // ?.
        StringBuilder sb = new StringBuilder();
        // ???
        IJavaScriptUnit targetUnit = context.getCompilationUnit();
        IPackageFragment fragment = context.getProject().getPackageFragments()[0];
        // js??.????.
        IJavaScriptUnit tempUnit = fragment.getJavaScriptUnit(NAME_TEMPJSFILE)
                .getWorkingCopy(new NullProgressMonitor());
        // ?.
        sb.append(targetUnit.getBuffer().getContents());
        for (DummyCodeInfo dummyCodeInfo : codeInfo) {
            // ??js??????.
            CodeBuilder builder = CodeBuilderFactory.createCodeBuilder(dummyCodeInfo);
            addObject(builder, objBean);
            builder.build(sb, dummyCodeInfo.getInsertPosition());
        }
        // ???js???
        tempUnit.getBuffer().setContents(sb.toString());
        // ???.
        CompletionProposalCollector collector = new CompletionProposalCollector(tempUnit);
        // .??collector???.
        tempUnit.codeComplete(context.getInvocationOffset(), collector);
        // ??
        IJavaCompletionProposal[] proposals = collector.getJavaCompletionProposals();
        logger.log(Messages.DB0002, existDefaultCodeAssist);
        if (existDefaultCodeAssist) {
            // ???.
            // ???????????.
            for (IJavaCompletionProposal proposal : proposals) {
                addOptionProposal(resultList, proposal, objBean);
            }
        } else {

            for (IJavaCompletionProposal proposal : proposals) {
                // ???????????resultList??.
                if (!addOptionProposal(resultList, proposal, objBean)) {
                    resultList.add(proposal);
                }
            }
        }
        return resultList;
    }

    /**
     * ???.
     * 
     * @param builder ?.
     * @param objBean ?.
     */
    private void addObject(CodeBuilder builder, ObjectLiteralBean objBean) {

        for (FunctionBean elem : objBean.getFunctions()) {
            builder.addFunction(elem);
        }
        for (VarReferenceBean elem : objBean.getVarRefs()) {
            builder.addField(elem);
        }

    }

    /**
     * ????????????.
     * 
     * @param resultList ?.
     * @param proposal ?
     * @param objBean .
     * @return ??true,????false.
     */
    private boolean addOptionProposal(List<ICompletionProposal> resultList, IJavaCompletionProposal proposal,
            ObjectLiteralBean objBean) {

        ProposalWrapper propWrapper = null;
        for (FunctionBean elem : objBean.getFunctions()) {
            if (isOptionFileProposal(proposal, elem)) {
                propWrapper = ProposalWrapperFactory.createProposalWrapper(proposal, elem);
                logger.log(Messages.DB0003, propWrapper.getDisplayString());
                resultList.add(propWrapper);
                return true;
            }
        }
        for (VarReferenceBean elem : objBean.getVarRefs()) {
            if (isOptionFileProposal(proposal, elem)) {
                propWrapper = ProposalWrapperFactory.createProposalWrapper(proposal);
                logger.log(Messages.DB0003, propWrapper.getDisplayString());
                resultList.add(propWrapper);
                return true;
            }
        }
        return false;
    }

    /**
     * ????????????.
     * 
     * @param proposal 
     * @param elem ??
     * @return ????????
     */
    private boolean isOptionFileProposal(IJavaCompletionProposal proposal, VarReferenceBean elem) {

        return StringUtils.startsWith(proposal.getDisplayString(), elem.getKey());
    }

    /**
     * ????????????.
     * 
     * @param proposal 
     * @param elem ??
     * @return ????????
     */
    private boolean isOptionFileProposal(IJavaCompletionProposal proposal, FunctionBean elem) {

        Pattern pattern = Pattern.compile(Pattern.quote(elem.getName()) + "\\(.*\\).*");
        return pattern.matcher(proposal.getDisplayString()).matches();
    }

}