NXvalidate  1
 All Classes Namespaces Files Functions Variables
NXproperties.java
Go to the documentation of this file.
1 /* NeXus - Neutron & X-ray Common Data Format
2  *
3  * NeXus file validation GUI tool.
4  *
5  * Copyright (C) 2010 Stephen Rankin
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this library; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20  *
21  * For further information, see <http://www.nexusformat.org/>
22  *
23  * NXproperties.java
24  *
25  */
26 
27 package org.nexusformat.nxvalidate;
28 
29 import java.io.File;
30 import java.net.MalformedURLException;
31 import java.net.URL;
32 import java.net.URLClassLoader;
33 import java.util.Properties;
34 import java.util.ResourceBundle;
35 
40 public class NXproperties {
41 
42  private ResourceBundle classpathProps = null;
43  private ResourceBundle userProps = null;
44  private Properties systemProps = null;
45  private String userHome = null;
46 
47  public NXproperties() throws MalformedURLException
48  {
49 
50  classpathProps =
51  ResourceBundle.getBundle("org/nexusformat/nxvalidate/resources/nxvalidate");
52 
53  systemProps = System.getProperties();
54 
55  //First we see if there is a user specific prop file.
56  userHome = systemProps.getProperty("user.home");
57 
58  String propsFileName =
59  userHome + File.pathSeparator + ".NXvalidate.properties";
60 
61  File propsFile = new File(propsFileName);
62 
63  URL resourceURL = null;
64 
65  if(propsFile.exists()){
66 
67  try {
68 
69  resourceURL = propsFile.getParentFile().toURI().toURL();
70 
71  } catch (MalformedURLException e) {
72 
73  throw e;
74  }
75 
76  URLClassLoader urlLoader =
77  new URLClassLoader(new java.net.URL[]{resourceURL});
78 
79  userProps = ResourceBundle.getBundle(".NXvalidate.properties",
80  java.util.Locale.getDefault(), urlLoader );
81 
82  }
83 
84  }
85 
86  public String getProperty(String name){
87 
88  String prop = null;
89 
90  //If we are using the user props
91  if(userProps != null)
92  {
93 
94  if(userProps.containsKey(name))
95  {
96  prop = userProps.getString(name);
97  }
98 
99  }
100  //If we are using the default props.
101  else if(prop == null)
102  {
103  if(classpathProps.containsKey(name))
104  {
105  prop = classpathProps.getString(name);
106  }
107  }
108 
109  return prop;
110 
111  }
112 
113 }