Java Properties Save saveBuildConfiguration(IFile ifile)

Here you can find the source of saveBuildConfiguration(IFile ifile)

Description

save Build Configuration

License

Open Source License

Declaration

public static void saveBuildConfiguration(IFile ifile) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2005 IBM Corporation 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
 * //from   ww  w.j  ava 2s.  c o m
 * Contributors:
 *     Sian January - initial implementation
 *******************************************************************************/

import java.io.BufferedWriter;
import java.io.File;

import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;

import java.util.Iterator;
import java.util.List;

import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IProject;

import org.eclipse.core.runtime.IPath;

import org.eclipse.jdt.core.IClasspathEntry;
import org.eclipse.jdt.core.IJavaProject;
import org.eclipse.jdt.core.JavaCore;
import org.eclipse.jdt.core.JavaModelException;

public class Main {
    public static void saveBuildConfiguration(IFile ifile) {
        File file = ifile.getLocation().toFile();
        IProject project = ifile.getProject();
        try {
            IJavaProject jp = JavaCore.create(project);
            IClasspathEntry[] entries = jp.getRawClasspath();
            List srcIncludes = new ArrayList();
            List srcExcludes = new ArrayList();
            List srcInclusionpatterns = new ArrayList();
            for (int i = 0; i < entries.length; i++) {
                IClasspathEntry entry = entries[i];
                if (entry.getEntryKind() == IClasspathEntry.CPE_SOURCE) {
                    IPath srcpath = entry.getPath();
                    srcpath = srcpath.removeFirstSegments(1);
                    String path = srcpath.toString().trim();
                    if (!path.endsWith("/")) { //$NON-NLS-1$
                        path = path + "/"; //$NON-NLS-1$
                    }
                    srcIncludes.add(path);
                    IPath[] inclusions = entry.getInclusionPatterns();
                    for (int j = 0; j < inclusions.length; j++) {
                        srcInclusionpatterns.add((path.length() > 1 ? path : "") + inclusions[j]); //$NON-NLS-1$   
                    }
                    IPath[] exclusions = entry.getExclusionPatterns();
                    for (int j = 0; j < exclusions.length; j++) {
                        srcExcludes.add((path.length() > 1 ? path : "") + exclusions[j]); //$NON-NLS-1$
                    }
                }
            }
            BufferedWriter bw = null;
            try {
                bw = new BufferedWriter(new FileWriter(file));
                printProperties(bw, "src.includes", srcIncludes); //$NON-NLS-1$
                printProperties(bw, "src.excludes", srcExcludes); //$NON-NLS-1$
                printProperties(bw, "src.inclusionpatterns", srcInclusionpatterns); //$NON-NLS-1$   
            } catch (IOException e) {
            } finally {
                if (bw != null) {
                    try {
                        bw.close();
                    } catch (IOException e) {
                    }
                }
            }
        } catch (JavaModelException e) {
        }
    }

    private static void printProperties(BufferedWriter bw, String key, List values) throws IOException {
        if (values.size() > 0) {
            bw.write(key + " = "); //$NON-NLS-1$
            boolean first = true;
            for (Iterator iter = values.iterator(); iter.hasNext();) {
                String value = (String) iter.next();
                if (!first) {
                    for (int i = 0; i < key.length(); i++) {
                        bw.write(' ');
                    }
                    bw.write("   "); //$NON-NLS-1$
                }
                first = false;
                bw.write(value);
                boolean last = !iter.hasNext();
                if (!last) {
                    bw.write(",\\"); //$NON-NLS-1$
                }
                bw.newLine();
            }
        }
    }
}

Related

  1. save()
  2. save(File file, Properties properties)
  3. save(Properties properties, File propertiesFile)
  4. save(Properties props, File file, String comment)
  5. save(String fileName, CharSequence string)
  6. saveConfig()
  7. saveConfig(File settingsFile, Properties properties)
  8. saveDefaultScriptDirectory(String path)
  9. SaveDeployedObjects(String outputDirectory)