Java有序读取配置文件,有序读取ini配置文件

JSON 2016-08-22 15:49:59 14949

本项目中对 shiro  的权限配置,需要有序配置,因为权限控制是个漏斗形式匹配模式。

如:第一个匹配不中就找第二个,这样的话如果顺序随机或者乱了的话,直接匹配第二个了。或者第三个。

本工具实现于给ini 配置文件读取,其实换做其他配置文件(properties )都可以。你看下思路即可了解。

提供了三个构造方法:

一、参数为文件(File)。

/**
 * 读取
 * @param file 文件
 * @throws FileNotFoundException 
 */
public INI4j(File file) throws FileNotFoundException {
	this.init(new BufferedReader(new FileReader(file)));
}

二、参数为Path文件路径

/***
 * 重载读取
 * @param path 给文件路径
 * @throws FileNotFoundException 
 */
public INI4j(String path) throws FileNotFoundException {
    this.init(new BufferedReader(new FileReader(path)));
}

三、参数为ClassPathResource。

/***
 * 重载读取
 * @param source ClassPathResource 文件,如果文件在resource 里,那么直接 new ClassPathResource("file name");
 * @throws IOException 
 */
public INI4j(ClassPathResource source) throws IOException {
	this(source.getFile());
}

全部代码:

package com.sojson.core.config;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.LinkedHashMap;
import java.util.Map;

import org.springframework.core.io.ClassPathResource;
/**
 * 
 * 开发公司:SOJSON在线工具 <p>
 * 版权所有:© www.sojson.com<p>
 * 博客地址:https://www.sojson.com/blog/  <p>
 * <p>
 * 
 * 有序读取 ini配置文件
 * 
 * <p>
 * 
 * 区分 责任人 日期    说明<br/>
 * 创建 周柏成 2016年6月2日  <br/>
 *
 * @author zhou-baicheng
 * @email  so@sojson.com
 * @version 1.0,2016年6月2日 <br/>
 * 
 */
public class INI4j {
    
    /**
     * 用linked hash map 来保持有序的读取
     * 
     */
    final LinkedHashMap<String,LinkedHashMap<String, String>>  coreMap = new LinkedHashMap<String, LinkedHashMap<String,String>>();
    /**
     * 当前Section的引用
     */
    String currentSection = null;
     
	/**
	 * 读取
	 * @param file 文件
	 * @throws FileNotFoundException 
	 */
	public INI4j(File file) throws FileNotFoundException {
		this.init(new BufferedReader(new FileReader(file)));
	}
	/***
	 * 重载读取
	 * @param path 给文件路径
	 * @throws FileNotFoundException 
	 */
	public INI4j(String path) throws FileNotFoundException {
	    this.init(new BufferedReader(new FileReader(path)));
	}
	/***
	 * 重载读取
	 * @param source ClassPathResource 文件,如果文件在resource 里,那么直接 new ClassPathResource("file name");
	 * @throws IOException 
	 */
	public INI4j(ClassPathResource source) throws IOException {
		this(source.getFile());
	}
 
    void init(BufferedReader bufferedReader){
    	try {
    		read(bufferedReader);
    	} catch (IOException e) {
    		e.printStackTrace();
    		throw new RuntimeException("IO Exception:" + e);
    	}
    }
    /**
     * 读取文件
     * @param reader
     * @throws IOException
     */
    void read(BufferedReader reader) throws IOException {
        String line = null;
        while((line=reader.readLine())!=null) {
            parseLine(line);
        }
    }
     
    /**
     * 转换
     * @param line
     */
    void parseLine(String line) {
        line = line.trim();
        // 此部分为注释
        if(line.matches("^\\#.*$")) {
            return;
        }else if (line.matches("^\\[\\S+\\]$")) {
            // section
            String section = line.replaceFirst("^\\[(\\S+)\\]$","$1");
            addSection(section);
        }else if (line.matches("^\\S+=.*$")) {
            // key ,value
            int i = line.indexOf("=");
            String key = line.substring(0, i).trim();
            String value =line.substring(i + 1).trim();
            addKeyValue(currentSection,key,value);
        }
    }
 
 
    /**
     * 增加新的Key和Value
     * @param currentSection
     * @param key
     * @param value
     */
    void addKeyValue(String currentSection,String key, String value) {
        if(!coreMap.containsKey(currentSection)) {
            return;
        }
        Map<String, String> childMap = coreMap.get(currentSection);
        childMap.put(key, value);
    }
 
 
    /**
     * 增加Section
     * @param section
     */
    void addSection(String section) {
        if (!coreMap.containsKey(section)) {
            currentSection = section;
            LinkedHashMap<String,String> childMap = new LinkedHashMap<String,String>();
            coreMap.put(section, childMap);
        }
    }
     
    /**
     * 获取配置文件指定Section和指定子键的值
     * @param section
     * @param key
     * @return
     */
    public String get(String section,String key){
        if(coreMap.containsKey(section)) {
            return  get(section).containsKey(key) ?  get(section).get(key): null;
        }
        return null;
    }
     
     
     
    /**
     * 获取配置文件指定Section的子键和值
     * @param section
     * @return
     */
    public Map<String, String> get(String section){
        return  coreMap.containsKey(section) ? coreMap.get(section) : null;
    }
     
    /**
     * 获取这个配置文件的节点和值
     * @return
     */
    public LinkedHashMap<String, LinkedHashMap<String, String>> get(){
        return coreMap;
    }
     
}

调用方式:

ClassPathResource cp = new ClassPathResource("shiro_base_auth.ini");
INI4j ini = null;
try {
	ini = new INI4j(cp.getFile());
} catch (IOException e) {
	// TODO Auto-generated catch block
	e.printStackTrace();
}

配置文件书写规范(本范例配置文件):

[base_auth]
/u/**=anon
/user/**=simple,login
/js/**=anon
/css/**=anon 
/open/**=anon

#不用校验地址是否有权限
/permission/selectPermissionById.shtml=simple,login
/member/onlineDetails/**=simple,login
/role/mypermission.shtml=simple,login
/role/getPermissionTree.shtml=simple,login
/role/selectRoleByUserId.shtml=simple,login


#需要根据地址校验有无权限
/permission/**=simple,login,permission
/role/**=simple,login,permission
/member/**=simple,login,permission




/**=simple,login

版权所属:SO JSON在线解析

原文地址:https://www.sojson.com/blog/139.html

转载时必须以链接形式注明原始出处及本声明。

本文主题:

如果本文对你有帮助,那么请你赞助我,让我更有激情的写下去,帮助更多的人。

关于作者
一个低调而闷骚的男人。
相关文章
Shiro教程(六)Shiro整体的配置文件
Elasticsearch教程,Elasticsearch配置文件 — elasticsearch.yml
Shiro教程,Shiro 配置文件详细解释,Shiro自定义Filter配置
Springboot + Freemarker 集成配置
基于Centos7以上的JAVA相关配置(一)
Shiro教程(四)Shiro + Redis配置
Urlrewrite Java 伪静态 urlrewrite.xml 配置参数描述
Java web.xml 配置技巧—动态欢迎页地址
Java API接口返回不是JSON的解决方案,SpringMVC返回JSON配置
Shiro教程(三)Shiro web.xml中Filter配置配置注意事项
最新文章
Java赋值运算符 13
XML内部实体和外部实体 163
Java面向对象编程概念 143
PHP回显语句 91
Linux—文件树 116
C语言while循环和do while循环 131
Python元组剖析 200
MySQL触发器教程 314
sql使用布尔运算符和关系运算符 258
C语言的变量和常量 296
最热文章
最新MyEclipse8.5注册码,有效期到2020年 (已经更新) 682077
苹果电脑Mac怎么恢复出厂系统?苹果系统怎么重装系统? 674719
免费天气API,全国天气 JSON API接口,可以获取五天的天气预报 602129
免费天气API,天气JSON API,不限次数获取十五天的天气预报 577209
Jackson 时间格式化,时间注解 @JsonFormat 用法、时差问题说明 552926
我为什么要选择RabbitMQ ,RabbitMQ简介,各种MQ选型对比 509367
Elasticsearch教程(四) elasticsearch head 插件安装和使用 480007
Jackson 美化输出JSON,优雅的输出JSON数据,格式化输出JSON数据... ... 264571
Java 信任所有SSL证书,HTTPS请求抛错,忽略证书请求完美解决 244254
Elasticsearch教程(一),全程直播(小白级别) 225569
支付扫码

所有赞助/开支都讲公开明细,用于网站维护:赞助名单查看

查看我的收藏

正在加载... ...