- 论坛徽章:
- 0
|
- 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 < _listeners.size(); i++) ((ConfigFileListener) _listeners.get(i)).onSave(event);
- }
- public boolean saveToFile(String fileName) throws IOException {
- boolean result = false;
- BufferedWriter writer = null;
- try {
- writer = new BufferedWriter(new FileWriter(fileName));
- for (int i = 0; i < _sections.size(); i++){
- fireOnSave((ConfigFileSection) _sections.get(i), i);
- writer.write(_sections.get(i).toString());
- }
- writer.flush();
- result = true;
- }
- catch (Exception e) {
- e.printStackTrace();
- }
- try { writer.close(); } catch (Exception e) {}
- return result;
- }
- public ConfigFileSection getSection(int index){
- return (ConfigFileSection) _sections.get(index);
- }
- public int getSectionCount(){
- return _sections != null ? _sections.size() : 0;
- }
- public void removeSection(String section){
- ConfigFileSection tmp = null;
- for (int i=0; i < _sections.size(); i++){
- tmp = (ConfigFileSection) _sections.get(i);
- if (section.equals(tmp.getName())){ _sections.remove(i); break; }
- }
- }
- public ConfigFileSection getSection(String section) {
- ConfigFileSection result = null, tmp = null;
- for (int i=0; i < _sections.size(); i++){
- tmp = (ConfigFileSection) _sections.get(i);
- if (section.equals(tmp.getName())){ result = tmp; break; }
- }
- return result;
- }
- public void onSave(ConfigFileEvent event){
- FtpSection section = (FtpSection) event.getSection();
- if ( FtpSection.SECTION_MSC_ID.equals(section.getSectionType()) )
- section.addProperty("Index", String.valueOf(event.getIndex()));
- }
- public static void main(String[] args) throws Exception {
- //**
- ConfigFile configFile = new ConfigFile("g:/ftp.ini");
- configFile.setSectionType(FtpSection.class);
- configFile.addListener(this);
- configFile.loadFromFile();
- FtpSection section = (FtpSection) configFile.getSection("HOST01");
- System.out.println("UFO: "+section.getSectionType() +"," + section.getSectionDescribe());
- section.addProperty("Name", "3a");
- section.changeProperty("Passwd", "3a");
- configFile.removeSection("Global");
- configFile.saveToFile("e:/ftp.ini");
- /**/
- }
- }
复制代码
- package com.3a.util.paraconf;
- public interface ConfigFileListener {
- public void onSave(ConfigFileEvent event);
- }
复制代码
[ 本帖最后由 awk就是awp加ak 于 2007-1-22 11:26 编辑 ] |
|