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.granite.builder.properties.GraniteProperties;
028    import org.granite.builder.util.XStreamUtil;
029    
030    /**
031     * @author Franck WOLFF
032     */
033    public class GranitePropertiesLoader {
034    
035        private static final String FILE_NAME = ".granite";
036        private static final String DEFAULT_CHARSET = "UTF-8";
037    
038            public static GraniteProperties load(IProject project) throws IOException {
039                    File file = getPropertiesFile(project);
040            if (file.exists()) {
041                    GraniteProperties properties = XStreamUtil.load(file, GraniteProperties.class, DEFAULT_CHARSET);
042                    ValidationResults results = new ValidationResults();
043                    properties.validate(results);
044                    if (results.hasErrors())
045                            throw new IOException("Illegal '.granite' file in your project. " + results);
046                    properties.setTimestamp(file.lastModified());
047                    return properties;
048            }
049            return GraniteProperties.getDefaultProperties();
050            }
051            
052            public static void save(IProject project, GraniteProperties properties) throws IOException {
053                    File file = getPropertiesFile(project);
054                    XStreamUtil.save(file, properties, DEFAULT_CHARSET);
055            }
056            
057            public static boolean isOutdated(IProject project, GraniteProperties properties) {
058                    File file = getPropertiesFile(project);
059                    return properties == null || (file.exists() && file.lastModified() > properties.getTimestamp());
060            }
061            
062        public static File getPropertiesFile(IProject project) {
063            return new File(project.getFile(FILE_NAME).getLocationURI());
064        }
065        
066        public static boolean exists(IProject project) {
067            File propertiesFile = getPropertiesFile(project);
068            return propertiesFile != null && propertiesFile.exists();
069        }
070    }