BrightSide Workbench Full Report + Source Code
PropertiesFile.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2011 Lluis TurrĂ³ Cutiller <http://www.turro.org/>
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU Affero General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU Affero General Public License for more details.
14  *
15  * You should have received a copy of the GNU Affero General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  */
18 package org.turro.file;
19 
20 import java.io.File;
21 import java.io.FileNotFoundException;
22 import java.io.FileReader;
23 import java.io.IOException;
24 import java.util.Properties;
25 import java.util.logging.Level;
26 import java.util.logging.Logger;
27 import org.turro.elephant.context.ElephantContext;
28 
33 public class PropertiesFile {
34 
35  public final static String FILES_PROPERTIES_FILE = ".files.properties";
36 
37  private File file;
38  private Properties props;
39 
40  public PropertiesFile(File file) {
41  this.file = file;
42  }
43 
44  public Properties getProperties() throws FileNotFoundException, IOException {
45  if(props == null) {
46  props = new Properties();
47  if(file.isDirectory()) {
48  loadProperties(props, file.getAbsolutePath());
49  } else {
50  String parentPath = getParentPath(file.getAbsolutePath());
51  if(parentPath != null) {
52  loadProperties(props, parentPath);
53  }
54  }
55  }
56  return props;
57  }
58 
59  private void loadProperties(Properties props, String path) {
60  String folders[] = path.split("\\/"),
61  current = "";
62  loadFrom(props, new File(current + "/" + FILES_PROPERTIES_FILE));
63  for(String folder : folders) {
64  current = current + "/" + folder;
65  loadFrom(props, new File(current + "/" + FILES_PROPERTIES_FILE));
66  }
67  }
68 
69  private void loadFrom(Properties props, File file) {
70  if(!file.exists() || !file.isFile()) {
71  return;
72  }
73  FileReader fr = null;
74  try {
75  fr = new FileReader(file);
76  props.load(fr);
77  fr.close();
78  } catch (IOException ex) {
79  Logger.getLogger(PropertiesFile.class.getName()).log(Level.SEVERE, ElephantContext.logMsg(null), ex);
80  } finally {
81  try {
82  fr.close();
83  } catch (IOException ex) {
84  Logger.getLogger(PropertiesFile.class.getName()).log(Level.SEVERE, ElephantContext.logMsg(null), ex);
85  }
86  }
87  }
88 
89  private String getParentPath(String path) {
90  int p = path.lastIndexOf("/");
91  if(p > -1) {
92  return path.substring(0, p);
93  }
94  return null;
95  }
96 }
static final String FILES_PROPERTIES_FILE