com.htmlhifive.tools.jslint.engine.option.CheckOptionXmlWrapper.java Source code

Java tutorial

Introduction

Here is the source code for com.htmlhifive.tools.jslint.engine.option.CheckOptionXmlWrapper.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.jslint.engine.option;

import java.util.ArrayList;
import java.util.List;

import javax.xml.bind.JAXBException;

import org.apache.commons.lang.StringUtils;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.runtime.CoreException;

import com.htmlhifive.tools.jslint.engine.option.xml.JaxbUtil;
import com.htmlhifive.tools.jslint.engine.option.xml.JsCheckOption;
import com.htmlhifive.tools.jslint.engine.option.xml.ObjectFactory;
import com.htmlhifive.tools.jslint.engine.option.xml.XmlOption;
import com.htmlhifive.tools.jslint.logger.JSLintPluginLogger;
import com.htmlhifive.tools.jslint.logger.JSLintPluginLoggerFactory;
import com.htmlhifive.tools.jslint.messages.Messages;

/**
 * ??Xml?.
 * 
 * @author NS Solutions Corporation
 * 
 */
public class CheckOptionXmlWrapper implements CheckOptionFileWrapper {

    /**
     * .
     */
    private static JSLintPluginLogger logger = JSLintPluginLoggerFactory.getLogger(CheckOptionXmlWrapper.class);

    /**
     * ?.
     */
    private final JsCheckOption checkOptions;

    /**
     * (.xml).
     */
    private final IFile xmlOption;

    /**
     * .
     * 
     * @param xmlOption .xml?.
     * @throws JAXBException xml?.
     * @throws CoreException ?.
     */
    public CheckOptionXmlWrapper(IFile xmlOption) throws CoreException, JAXBException {
        if (!xmlOption.exists()) {
            throw new IllegalArgumentException("????");
        }
        checkOptions = JaxbUtil.readJsCheckOption(xmlOption.getContents());
        // this(xmlOption.getContents());
        this.xmlOption = xmlOption;

    }

    // /**
    // * .
    // *
    // * @param optionXml .xml?.
    // * @throws JAXBException xml?.
    // */
    // public CheckOptionXmlWrapper(InputStream optionXml) throws JAXBException
    // {
    //
    // checkOptions = JaxbUtil.readJsCheckOption(optionXml);
    // }

    /*
     * (? Javadoc)
     * 
     * @see
     * com.htmlhifive.tool.jslint.engine.option.CheckOptionWrapper#getOption
     * (java.lang.String)
     */
    @Override
    public CheckOption getOption(String key, String engine) {

        for (XmlOption optiontype : checkOptions.getOption()) {
            try {
                if (StringUtils.equals(optiontype.getKey(), key)
                        && StringUtils.equals(optiontype.getEngine(), engine)) {
                    CheckOption option = new CheckOption(key, optiontype.getEngine(),
                            Class.forName(optiontype.getType()), optiontype.getDescription(),
                            optiontype.getDetail());
                    option.setEnable(Boolean.valueOf(optiontype.isState()));
                    option.setValue(optiontype.getValue());
                    return option;
                }
            } catch (ClassNotFoundException e) {
                logger.put(Messages.EM0007, e, optiontype.getType());
            }
        }

        return null;
    }

    /*
     * (? Javadoc)
     * 
     * @see
     * com.htmlhifive.tool.jslint.engine.option.CheckOptionWrapper#getOptions()
     */
    @Override
    public CheckOption[] getOptions(Engine engine) {

        List<CheckOption> checkOptionList = new ArrayList<CheckOption>();
        for (XmlOption optionType : this.checkOptions.getOption()) {
            if (contains(engine, optionType)) {
                checkOptionList.add(getOption(optionType.getKey(), optionType.getEngine()));
            }
        }
        return (CheckOption[]) checkOptionList.toArray(new CheckOption[checkOptionList.size()]);
    }

    /**
     * ?????????.
     * 
     * @param engines .
     * @param optionType 
     * @return ???????true,????????false
     */
    private boolean contains(Engine engines, XmlOption optionType) {

        String[] engineKeys = StringUtils.split(optionType.getEngine(), ",");
        if (engineKeys == null) {
            return false;
        }
        for (String engineKey : engineKeys) {
            if (engines.getKey().equals(engineKey)) {
                return true;
            }
        }
        return false;
    }

    @Override
    public CheckOption[] getEnableOptions(Engine engine) {

        List<CheckOption> checkOptionList = new ArrayList<CheckOption>();
        for (XmlOption optionType : this.checkOptions.getOption()) {
            if (contains(engine, optionType) && optionType.isState()) {
                checkOptionList.add(getOption(optionType.getKey(), optionType.getEngine()));
            }
        }
        return (CheckOption[]) checkOptionList.toArray(new CheckOption[checkOptionList.size()]);
    }

    @Override
    public void saveOption() {

        JaxbUtil.saveJsCheckOption(checkOptions, xmlOption);
        // try {
        // Marshaller marshall = jc.createMarshaller();
        // marshall.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT,
        // Boolean.valueOf(true));
        // marshall.marshal(checkOptions, new File(xmlOption.getLocationURI()));
        // xmlOption.refreshLocal(IResource.DEPTH_ONE, null);
        // } catch (JAXBException e) {
        // logger.put(Messages.EM0006, e, xmlOption.getName());
        // // throw new
        // // RuntimeException(Messages.EM0006.format(xmlOption.getName()), e);
        // } catch (CoreException e) {
        // logger.put(Messages.EM0100, e);
        // // throw new RuntimeException(Messages.EM0100.getText(), e);
        // }
    }

    @Override
    public void addOption(CheckOption option) {

        for (XmlOption xmlOption : checkOptions.getOption()) {
            if (StringUtils.equals(xmlOption.getKey(), option.getKey())
                    && StringUtils.equals(xmlOption.getEngine(), option.getEngine())) {
                return;
            }
        }
        this.checkOptions.getOption().add(convertToXmlOption(option));

    }

    /**
     * XmlOption???.
     * 
     * @param option ??.
     * @return XmlOption
     */
    private XmlOption convertToXmlOption(CheckOption option) {

        ObjectFactory factory = new ObjectFactory();
        XmlOption xmlOption = factory.createXmlOption();
        updateOption(xmlOption, option);

        return xmlOption;
    }

    /**
     * XmlOptionCheckOption??.
     * 
     * @param from ?XmlOption
     * @param to CheckOption
     */
    private void updateOption(XmlOption from, CheckOption to) {

        from.setDescription(to.getDescription());
        from.setDetail(to.getDetail());
        from.setKey(to.getKey());
        from.setState(to.isEnable());
        from.setType(to.getClazz().getName());
        from.setValue(to.getValue());
        from.setEngine(to.getEngine());

    }

    @Override
    public void updateOption(CheckOption option) {

        for (XmlOption xmlOption : checkOptions.getOption()) {
            if (StringUtils.equals(xmlOption.getKey(), option.getKey())
                    && StringUtils.equals(xmlOption.getEngine(), option.getEngine())) {
                updateOption(xmlOption, option);
            }
        }
    }
}