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

Java tutorial

Introduction

Here is the source code for com.htmlhifive.tools.jslint.engine.option.CheckOptionFileWrapperFactory.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.io.IOException;
import java.util.Properties;

import javax.xml.bind.JAXBException;

import org.apache.commons.lang.StringUtils;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Path;
import org.eclipse.core.runtime.Status;

import com.htmlhifive.tools.jslint.JSLintPlugin;
import com.htmlhifive.tools.jslint.engine.option.xml.JaxbUtil;
import com.htmlhifive.tools.jslint.engine.option.xml.JsCheckOption;
import com.htmlhifive.tools.jslint.messages.Messages;

/**
 * ??.
 * 
 * @author NS Solutions Corporation
 * 
 */
public final class CheckOptionFileWrapperFactory {
    /**
     * .
     */
    private CheckOptionFileWrapperFactory() {

    }

    /**
     * ???.
     * 
     * @param file ?.
     * @return ?.
     * @throws CoreException ?.
     */
    public static CheckOptionFileWrapper createCheckOptionFileWrapper(IFile file) throws CoreException {

        return createCheckOptionFileWrapper(file, file.getFileExtension());
    }

    /**
     * ???.
     * 
     * @param path ?.
     * @return ?.
     * @throws CoreException ?.
     */
    public static CheckOptionFileWrapper createCheckOptionFileWrapper(String path) throws CoreException {

        IFile file = ResourcesPlugin.getWorkspace().getRoot().getFile(new Path(path));
        if (file != null) {
            return createCheckOptionFileWrapper(file);
        }
        // TODO ????.
        return null;
    }

    /**
     * ???.
     * 
     * @param file ?.
     * @param extension ?.
     * @return ?.
     * @throws CoreException ?.
     */
    private static CheckOptionFileWrapper createCheckOptionFileWrapper(IFile file, String extension)
            throws CoreException {

        try {
            if (!file.exists()) {
                JaxbUtil.saveJsCheckOption(new JsCheckOption(), file);
            }
            if (StringUtils.endsWith(extension, "xml")) {
                return new CheckOptionXmlWrapper(file);
            } else if (StringUtils.endsWith(extension, "properties")) {
                Properties prop = new Properties();
                prop.load(file.getContents());
                // TODO ?.
                return new CheckOptionPropertyWrapper(prop);
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        } catch (JAXBException e) {
            throw new CoreException(
                    new Status(IStatus.ERROR, JSLintPlugin.PLUGIN_ID, Messages.EM0008.getText(), e));
        }
        return null;
    }
}