- 论坛徽章:
- 0
|
CODE:
[Copy to clipboard]
package com.3a.util.paraconf;
/**
* Ini configuration file helper.
* By 3a
* 2007-1-16
*/
import java.io.*;
import java.util.*;
import java.util.regex.*;
import java.lang.reflect.*;
import com.3a.test.FtpSection;
public class ConfigFile implements ConfigFileListener {
String PATTERN_REM = "^#.*$";
String PATTERN_SECTION = "^\\[([^\\]]*).*$";
String PATTERN_PROPERTY = "(^[^#][^=]*)=(.*)$";
String _fileName = null;
ArrayList _sections = null;
ArrayList _listeners = null;
Class _sectionClass = ConfigFileSection.class;
public ConfigFile(String fileName) {
_fileName = fileName;
_sections = new ArrayList();
_listeners = new ArrayList();
}
public void setSectionType(Class sectionClass) {
_sectionClass = sectionClass;
}
public void addListener(ConfigFileListener listener){
_listeners.add(listener);
}
public void addSection(ConfigFileSection section){
_sections.add(section);
}
ConfigFileSection genSection(Constructor generator, String sectionName) throws InstantiationException, IllegalAccessException, InvocationTargetException {
Object[] args = { sectionName };
return (ConfigFileSection) generator.newInstance(args);
}
String parseSectionName(Pattern pattern, String line) {
Matcher matcher = pattern.matcher(line);
return matcher.matches() ? matcher.group(1) : null;
}
boolean parseProperty(Pattern pattern, String line, String[] property) {
boolean result = false;
Matcher matcher = pattern.matcher(line);
if (matcher.matches()){
property[0] = matcher.group(1);
property[1] = matcher.group(2);
result = true;
}
return result;
}
public boolean loadFromFile() {
boolean result = false;
String line = null;
String sectionName = null;
String[] property = new String[2];
ConfigFileSection section = null;
BufferedReader reader = null;
Pattern sectionPattern = Pattern.compile(PATTERN_SECTION);
Pattern propertyPattern = Pattern.compile(PATTERN_PROPERTY);
Pattern remPattern = Pattern.compile(PATTERN_REM);
try {
Class[] _param = { String.class };
Constructor generator = _sectionClass.getConstructor(_param);
reader = new BufferedReader(new FileReader(_fileName));
while ( (line = reader.readLine()) != null) {
// read comment
if (line == null || line.trim().length() == 0 || remPattern.matcher(line).matches()) continue;
// read section
else if ( (sectionName = parseSectionName(sectionPattern, line)) != null) {
section = genSection(generator, sectionName);
System.out.println("ConfigFile.loading section ... " + sectionName);
_sections.add(section);
}
// read properties
else if (section != null && parseProperty(propertyPattern, line, property))
section.addProperty(property[0], property[1]);
}
result = true;
}
catch (Exception e) {
e.printStackTrace();
}
try { reader.close(); } catch (Exception e) {}
return result;
}
public void save() throws IOException {
saveToFile(_fileName);
}
void fireOnSave(ConfigFileSection section, int index){
ConfigFileEvent event = new ConfigFileEvent(this, section, index);
for (int i = 0; i
CODE:
[Copy to clipboard]
package com.3a.util.paraconf;
public interface ConfigFileListener {
public void onSave(ConfigFileEvent event);
}
CODE:
[Copy to clipboard]
package com.3a.util.paraconf;
import java.util.*;
public class ConfigFileSection {
HashMap _data = null;
String _name = null;
String NEW_LINE = "\r\n";
public ConfigFileSection(){
_data = new HashMap();
}
public ConfigFileSection(String sectionName) {
this();
_name = sectionName;
}
public void setName(String name){
_name = name;
}
public String getName(){
return _name;
}
public String getProperty(String property){
return (String) _data.get(property);
}
public void addProperty(String property, String value){
_data.put(property, value);
}
public void changeProperty(String property, String value){
if (_data.containsKey(property)) _data.put(property, value);
}
public String toString(){
StringBuffer result = new StringBuffer(2000).append("[").append(_name).append("]").append(NEW_LINE);
String key;
for (Iterator keys = _data.keySet().iterator(); keys.hasNext(); ){
key = (String) keys.next();
result.append(key).append("=").append(_data.get(key)).append(NEW_LINE);
}
return result.toString();
}
}
CODE:
[Copy to clipboard]
package com.3a.test;
import com.3a.util.paraconf.ConfigFileSection;
import com.3a.util.common.*;
public class FtpSection extends ConfigFileSection {
static final String SECTION_GLOBAL_ID = "0";
static final String SECTION_SITE_ID = "1";
static final String SECTION_HOST_ID = "2";
static final String SECTION_GLOBAL = "Global";
static final String SECTION_HOST = "HOST";
static final String DESCRIBE_GLOBAL = "全局配置";
static final String DESCRIBE_SITE = "站点信息";
static final String DESCRIBE_HOST = "主机";
public FtpSection(String sectionName){
super(sectionName);
}
public String getSectionType() {
String name = getName();
return SECTION_GLOBAL.equals(name) ? SECTION_GLOBAL_ID : (name.startsWith(SECTION_HOST) ? SECTION_HOST_ID : SECTION_SITE_ID);
}
public String getSectionDescribe(){
String type = getSectionType();
return SECTION_GLOBAL_ID.equals(type) ? DESCRIBE_GLOBAL : (SECTION_HOST_ID.equals(type) ? DESCRIBE_HOST : DESCRIBE_SITE);
}
}
CODE:
[Copy to clipboard]
package com.3a.util.paraconf;
public class ConfigFileEvent {
ConfigFile _controller = null;
ConfigFileSection _section = null;
int _index;
public ConfigFileEvent(ConfigFile controller, ConfigFileSection section, int index) {
_controller = controller;
_section = section;
_index = index;
}
public int getIndex(){ return _index; }
public ConfigFile getController(){ return _controller; }
public ConfigFileSection getSection(){ return _section; }
}
本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u/24501/showart_239322.html |
|