InputStream resource = ClassLoader.getSystemResourceAsStream(configFileLabel+".possible");
URL res = ClassLoader.getSystemResource(configFileLabel+".possible");
System.out.println(" file="+(res == null ? "null" : res.getFile()));
if(resource == null) return null;
JSONObject possible = new JSONObject(resource, true);
if(possible == null && appLabel == null)
throw new RuntimeException("Could not load *.possible file for "+appLabel);
if(possible == null) return null;
System.out.print("(niche) loading "+configFileLabel);
InputStream configResource = ClassLoader.getSystemResourceAsStream(configFileLabel);
res = ClassLoader.getSystemResource(configFileLabel);
System.out.println(" file="+(res == null ? "null" : res.getFile()));
// if(configResource == null) return null;
// JSONArray allActual = new JSONArray(configResource);
// if(allActual == null)
// allActual = new JSONArray();
JSONArray allActual = null;
if(configResource == null)
allActual = new JSONArray("[{'apply':['"+MODE+"']}]");
else
allActual = new JSONArray(configResource, true);
tmp = new JSONObject();
for(int i=0; i < allActual.length(); i++) {
JSONObject next = allActual.getJSONObject(i);
if(!next.has("apply")) {
throw new RuntimeException("required key, \"apply\" not found in config block");
}
JSONArray apply = next.getJSONArray("apply");
inner:
for(int j=0; j < apply.length(); j++) {
if(apply.getString(j).equals(MODE)) {
// if(CONF.get(appLabel) == null)
// CONF.put(appLabel, next);
// else {
for(Iterator it = next.keys(); it.hasNext();) {
String next1 = (String)it.next();
tmp.put(next1, next.get(next1));
}
// }
break inner;
}
}
}
/*
* apply all default config keys (which are global across all modes)
*/
for(Iterator it = possible.keys(); it.hasNext();) {
String next = (String)it.next();
JSONObject desc = possible.getJSONObject(next);
if(desc.has("default")) {
assign(null, possible, 0, explodeKey(next.split("\\.")));
}
}
checked = new ArrayList<String>();
validate(possible, tmp, 0);
for(Iterator it = possible.keys(); it.hasNext();) {
String next = (String)it.next();
String parent = getParent(next);
if(parent != null && !checked.contains(parent)) {
continue;
}
JSONObject desc = possible.getJSONObject(next);
boolean required = true;
if(desc.has("required"))
required = desc.getBoolean("required");
if(required && !checked.contains(next))
throw new RuntimeException("Key, \""+configFileLabel+"."+next+"\", is required");
}
return tmp;