/*
* Created on 2004/07/27
* Author aki@www.xucker.jpn.org
* License Apache2.0 or Common Public License
*/
package org.jpn.xucker.commons.util;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.URL;
import java.net.URLDecoder;
import java.util.Hashtable;
import java.util.prefs.AbstractPreferences;
import java.util.prefs.BackingStoreException;
import java.util.prefs.InvalidPreferencesFormatException;
import java.util.prefs.PreferenceChangeEvent;
import java.util.prefs.PreferenceChangeListener;
import java.util.prefs.Preferences;
/**
*
*
*/
public class _FilePreference extends AbstractPreferences implements PreferenceChangeListener {
private File file;
//private Hashtable hash=new Hashtable();
public _FilePreference(File file){
super(null,"");
System.out.println("import"+file.getAbsolutePath());
this.addPreferenceChangeListener(this);
this.file=file;
if(file.exists()){
try {
importPreferences(new FileInputStream(file));
//Preferences root=FilePreference.systemRoot();
// init(root);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InvalidPreferencesFormatException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
/* (non-Javadoc)
* @see java.util.prefs.AbstractPreferences#flushSpi()
*/
protected void flushSpi() throws BackingStoreException {
try {
synchronized(this.getClass()){
//lock();
exportNode(new FileOutputStream(file));
System.out.println("output:"+file.getAbsolutePath());
//unlock();
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (BackingStoreException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/* (non-Javadoc)
* @see java.util.prefs.AbstractPreferences#removeNodeSpi()
*/
protected void removeNodeSpi() throws BackingStoreException {
// TODO Auto-generated method stub
// System.out.println("remove");
}
/* (non-Javadoc)
* @see java.util.prefs.AbstractPreferences#syncSpi()
*/
protected void syncSpi() throws BackingStoreException {
// TODO Auto-generated method stub
// System.out.println("sync");
}
/* (non-Javadoc)
* @see java.util.prefs.AbstractPreferences#childrenNamesSpi()
*/
protected String[] childrenNamesSpi() throws BackingStoreException {
// TODO Auto-generated method stub
// System.out.println("childenNames");
return null;
}
/* (non-Javadoc)
* @see java.util.prefs.AbstractPreferences#keysSpi()
*/
protected String[] keysSpi() throws BackingStoreException {
//Preferences root=;
return _FilePreference.systemRoot().keys();
// TODO Auto-generated method stub
// System.out.println("keysSpi");
// return (String[])hash.keySet().toArray(new String[hash.size()]);
}
/* (non-Javadoc)
* @see java.util.prefs.AbstractPreferences#removeSpi(java.lang.String)
*/
protected void removeSpi(String arg0) {
// TODO Auto-generated method stub
// System.out.println("removeSpi");
}
/* (non-Javadoc)
* @see java.util.prefs.AbstractPreferences#getSpi(java.lang.String)
*/
protected String getSpi(String name) {
// System.out.println("getSpi");
return _FilePreference.systemRoot().get(name,"");
}
/* (non-Javadoc)
* @see java.util.prefs.AbstractPreferences#putSpi(java.lang.String, java.lang.String)
*/
protected void putSpi(String key, String value) {
// TODO Auto-generated method stub
// System.out.println("putSpi "+key+","+value);
_FilePreference.systemRoot().put(key,value);
}
/* (non-
* Javadoc)
* @see java.util.prefs.AbstractPreferences#childSpi(java.lang.String)
*/
protected AbstractPreferences childSpi(String arg0) {
// TODO Auto-generated method stub
// System.out.println("childSpi");
return null;
}
public static File searchFileAroundClass(String fileName) {
ClassLoader loader = _FilePreference.class.getClassLoader();
String path = _FilePreference.class.getName().replaceAll("\\.", "/");
path = path + ".class";
URL url = loader.getResource(path);
String classPath = url.toString();
//System.out.println(classPath);
if (classPath.startsWith("jar:")) {
int last = classPath.lastIndexOf("!");
classPath = classPath.substring("jar:".length(), last);
}
if (classPath.startsWith("file:/")) {
File checkFile = new File(URLDecoder.decode(classPath
.substring("file:/".length()))).getParentFile();
while (true) {
File tmp = new File(checkFile, fileName);
// System.out.println(tmp.getAbsolutePath());
if (tmp.exists()) {
return tmp;
}
else {
checkFile = checkFile.getParentFile();
if (checkFile == null) {
break;
}
}
}
return null;
} else {
return null;
}
}
/* (non-Javadoc)
* @see java.util.prefs.PreferenceChangeListener#preferenceChange(java.util.prefs.PreferenceChangeEvent)
*/
public void preferenceChange(PreferenceChangeEvent event) {
// System.out.println("event");
}
}
|