001 /*
002 GRANITE DATA SERVICES
003 Copyright (C) 2011 GRANITE DATA SERVICES S.A.S.
004
005 This file is part of Granite Data Services.
006
007 Granite Data Services is free software; you can redistribute it and/or modify
008 it under the terms of the GNU Library General Public License as published by
009 the Free Software Foundation; either version 2 of the License, or (at your
010 option) any later version.
011
012 Granite Data Services is distributed in the hope that it will be useful, but
013 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
014 FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public License
015 for more details.
016
017 You should have received a copy of the GNU Library General Public License
018 along with this library; if not, see <http://www.gnu.org/licenses/>.
019 */
020
021 package org.granite.builder.properties;
022
023 import java.io.File;
024 import java.io.IOException;
025
026 import org.eclipse.core.resources.IProject;
027 import org.eclipse.core.runtime.CoreException;
028 import org.granite.builder.properties.GraniteProperties;
029 import org.granite.builder.util.FileUtil;
030 import org.granite.builder.util.XStreamUtil;
031
032 /**
033 * @author Franck WOLFF
034 */
035 public class GranitePropertiesLoader {
036
037 private static final String FILE_NAME = ".granite";
038 private static final String DEFAULT_CHARSET = "UTF-8";
039
040 public static GraniteProperties load(IProject project) throws IOException {
041 File file = getPropertiesFile(project);
042 if (file.exists()) {
043 GraniteProperties properties = XStreamUtil.load(file, GraniteProperties.class, DEFAULT_CHARSET);
044 ValidationResults results = new ValidationResults();
045 properties.validate(results);
046 if (results.hasErrors())
047 throw new IOException("Illegal '.granite' file in your project. " + results);
048 properties.setTimestamp(file.lastModified());
049 return properties;
050 }
051 return GraniteProperties.getDefaultProperties();
052 }
053
054 public static void save(IProject project, GraniteProperties properties) throws IOException {
055 File file = getPropertiesFile(project);
056 XStreamUtil.save(file, properties, DEFAULT_CHARSET);
057 }
058
059 public static boolean isOutdated(IProject project, GraniteProperties properties) {
060 File file = getPropertiesFile(project);
061 return properties == null || (file.exists() && file.lastModified() > properties.getTimestamp());
062 }
063
064 public static File getPropertiesFile(IProject project) {
065 try {
066 return FileUtil.getLocationFile(project.getFile(FILE_NAME));
067 }
068 catch (CoreException e) {
069 throw new RuntimeException("Could not get " + FILE_NAME + " location file", e);
070 }
071 }
072
073 public static boolean exists(IProject project) {
074 File propertiesFile = getPropertiesFile(project);
075 return propertiesFile != null && propertiesFile.exists();
076 }
077 }