view com.redhat.thermostat.tools.eclipse.plugin/src/com/redhat/thermostat/tools/eclipse/plugin/wizards/PluginXmlCreator.java @ 124:f425cf9c32e8

Externalize strings in project wizard
author Omair Majid <omajid@redhat.com>
date Wed, 26 Mar 2014 13:06:12 -0400
parents 1c4f192bf2e2
children
line wrap: on
line source

package com.redhat.thermostat.tools.eclipse.plugin.wizards;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;

import org.eclipse.core.resources.IContainer;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.IWorkspaceRoot;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Path;
import org.eclipse.core.runtime.Status;
import org.eclipse.osgi.util.NLS;

import com.redhat.thermostat.tools.eclipse.plugin.Activator;
import com.redhat.thermostat.tools.eclipse.plugin.Messages;
import com.redhat.thermostat.tools.eclipse.plugin.model.SchemaConstants;

public class PluginXmlCreator {

    private static final String FILE_NAME = "thermostat-plugin.xml"; //$NON-NLS-1$

    public interface Decisions {
        /** Invoked if a file already exists */
        public boolean overwriteExistingFile();
    }

    private IWorkspaceRoot root;

    public PluginXmlCreator(IWorkspaceRoot root) {
        this.root = root;
    }

    public IFile create(String projectName, IProgressMonitor monitor, Decisions decisions) throws CoreException, IOException {
        final IFile file = getFileFromProject(projectName);
        try (InputStream stream = getInitialContentStream()) {
            if (file.exists()) {
                boolean overwrite = decisions.overwriteExistingFile();
                if (overwrite) {
                    file.setContents(stream, true, true, monitor);
                }
            } else {
                file.create(stream, true, monitor);
            }
        }
        return file;
    }

    private IFile getFileFromProject(String projectName) throws CoreException {
        IResource resource = root.findMember(new Path(projectName));
        if (!resource.exists() || !(resource instanceof IContainer)) {
            throwCoreException(NLS.bind(Messages.PluginXmlCreator_containerDoesNotExist, projectName));
        }
        IContainer container = (IContainer) resource;

        return container.getFile(new Path(FILE_NAME));
    }

    private InputStream getInitialContentStream() {
        String contents = "<?xml version=\"1.0\"?>\n" //$NON-NLS-1$
                + "<plugin xmlns=\"" + SchemaConstants.SCHEMA_NAMESPACE + "\"\n" //$NON-NLS-1$ //$NON-NLS-2$
                + "        xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n" //$NON-NLS-1$
                + "        xsi:schemaLocation=\"" + SchemaConstants.getSchemaLocation() + "\">\n" //$NON-NLS-1$ //$NON-NLS-2$
                + "</plugin>"; //$NON-NLS-1$

        return new ByteArrayInputStream(contents.getBytes());
    }

    private void throwCoreException(String message) throws CoreException {
        IStatus status = new Status(IStatus.ERROR, Activator.PLUGIN_ID, IStatus.OK, message, null);
        throw new CoreException(status);
    }

}