Java Properties Save saveProperties(final IResource modelResource, final Properties props)

Here you can find the source of saveProperties(final IResource modelResource, final Properties props)

Description

Store the properties

License

Open Source License

Declaration

public static void saveProperties(final IResource modelResource, final Properties props) 

Method Source Code

//package com.java2s;
/**// w w  w.jav a  2 s  .c  o  m
 * <copyright>
 *
 * Copyright (c) 2009, 2010 Springsite BV (The Netherlands) and others
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *   Martin Taal - Initial API and implementation
 *
 * </copyright>
 *
 * $Id: EclipseGeneratorUtils.java,v 1.9 2011/08/25 12:34:30 mtaal Exp $
 */

import java.io.File;

import java.io.FileOutputStream;
import java.io.IOException;

import java.io.OutputStream;

import java.util.Properties;

import org.eclipse.core.resources.IResource;

import org.eclipse.core.resources.ResourcesPlugin;

import org.eclipse.core.runtime.Path;

public class Main {
    /** Store the properties */
    public static void saveProperties(final IResource modelResource, final Properties props) {
        final File file = getPropertiesFile(modelResource);
        try {
            final OutputStream os = new FileOutputStream(file);
            props.store(os, null);
            os.close();

            // refresh
            modelResource.getParent().refreshLocal(IResource.DEPTH_INFINITE, null);
        } catch (final Exception e) {
            throw new IllegalStateException("Exception for file on path " //$NON-NLS-1$
                    + file.getAbsolutePath(), e);
        }
    }

    /** get the properties files, creates a prop file if it does not exist yet */
    public static File getPropertiesFile(final IResource modelResource) {
        final String ecorePath = modelResource.getLocation().toOSString();
        if (!(ecorePath.endsWith(".ecore") //$NON-NLS-1$
                || ecorePath.endsWith(".xsd") //$NON-NLS-1$
                || ecorePath.endsWith(".genprops"))) { //$NON-NLS-1$
            throw new IllegalArgumentException("Path does not point to a real ecore file: " + ecorePath); //$NON-NLS-1$
        }
        final String genPropsPath = ecorePath.substring(0, ecorePath.lastIndexOf(".")) //$NON-NLS-1$
                + ".genprops"; //$NON-NLS-1$
        final File file = new File(genPropsPath);
        try {
            if (!file.exists()) {
                file.createNewFile();
            }
            return file;
        } catch (final IOException e) {
            throw new IllegalStateException("IOException for file on path " //$NON-NLS-1$
                    + genPropsPath, e);
        }
    }

    /** Checks if a certain file already exists in the workspace path */
    public static boolean exists(final String wsPath) {
        return ResourcesPlugin.getWorkspace().getRoot().getFile(new Path(wsPath)).exists();
    }
}

Related

  1. saveOccurrencesAsText(String fileName, TreeMap distribution, int frequency, char[] ignore)
  2. saveOutputFile(String prefix, String suffix, String data)
  3. saveParamsToFile(String fileName, String[] params)
  4. saveParamToFile(String fileName, String param, String value)
  5. saveProperties()
  6. saveProperties(Map map, File file, String encoding)
  7. saveProperties(Map map, Writer tmpWriter)
  8. saveProperties(Properties p, String file)
  9. saveProperties(Properties p, String sFile)