关于Java property 文件,我的一些看法(2)
2011-10-27 17:18
471 查看
我设想的properties文件具有的元素以及对应的正则验证相关代码:
package com.all.about.config.prop;
import java.util.regex.Pattern;
public class PropertyRegex {
private
static String
PROP_KEY =
"([^\\.=#][^=#]*[^\\.=#])";
private
static String
PROP_VAL =
"([^#]+)";
private
static String
PROP_REGEX =
"\\s*"+PROP_KEY+"\\s*=\\s*"+PROP_VAL+"\\s*";
private
static String
SINGLE_COMMENT_REGEX =
"#.*";
private
static String
MULTI_COMMENT_REGEX_S =
"/\\*.*";
private
static String
MULTI_COMMENT_REGEX_E =
".*\\*/";
private
static String
NEW_SECTION =
"\\[[^\\[\\]]+\\s+(ns\\s*=\\s*"+PROP_KEY+")\\s*\\]";
public
static
boolean isProperty(String property) {
return Pattern.matches(PROP_REGEX,
property);
}
public
static
boolean isSingleLineComment(String comment) {
return Pattern.matches(SINGLE_COMMENT_REGEX,
comment);
}
public
static
boolean beginMultipleLineComment(String comment) {
return Pattern.matches(MULTI_COMMENT_REGEX_S,
comment);
}
public
static
boolean endMultipleLineComment(String comment) {
return Pattern.matches(MULTI_COMMENT_REGEX_E,
comment);
}
public
static
boolean isNewSection(String section) {
return Pattern.matches(NEW_SECTION,
section);
}
}
---------------------------------------
package com.all.about.config.prop;
import org.junit.Assert;
import org.junit.Test;
public class PropertyRegexTest {
@Test
public
void isProperty() {
Assert.assertEquals(PropertyRegex.isProperty("key=value"),
true);
Assert.assertEquals(PropertyRegex.isProperty("k#ey=value"),
false);
Assert.assertEquals(PropertyRegex.isProperty("=key=value"),
false);
Assert.assertEquals(PropertyRegex.isProperty("#key=value"),
false);
Assert.assertEquals(PropertyRegex.isProperty(".key=value"),
false);
Assert.assertEquals(PropertyRegex.isProperty("key.=value"),
false);
Assert.assertEquals(PropertyRegex.isProperty("key1.key2=value"),
true);
Assert.assertEquals(PropertyRegex.isProperty("...=value"),
false);
}
@Test
public
void isSingleLineComment() {
Assert.assertEquals(PropertyRegex.isSingleLineComment("#single line comment"),
true);
Assert.assertEquals(PropertyRegex.isSingleLineComment("single line comment#"),
false);
Assert.assertEquals(PropertyRegex.isSingleLineComment("#/*"),
true);
}
@Test
public
void beginMultipleLineComment() {
Assert.assertEquals(PropertyRegex.beginMultipleLineComment("/*multiple lines comment start"),
true);
Assert.assertEquals(PropertyRegex.beginMultipleLineComment("#/*multiple lines comment start"),
false);
Assert.assertEquals(PropertyRegex.beginMultipleLineComment("/*#multiple lines comment start"),
true);
}
@Test
public
void endMultipleLineComment() {
Assert.assertEquals(PropertyRegex.endMultipleLineComment("multiple lines comment end */"),
true);
Assert.assertEquals(PropertyRegex.endMultipleLineComment("#multiple lines comment end */"),
true);
Assert.assertEquals(PropertyRegex.endMultipleLineComment("*/ multiple lines comment end"),
false);
}
@Test
public
void isNewSection() {
Assert.assertEquals(PropertyRegex.isNewSection("[]"),
false);
Assert.assertEquals(PropertyRegex.isNewSection("[New Section ns=namespace]"),
true);
Assert.assertEquals(PropertyRegex.isNewSection("[[]]"),
false);
}
}
package com.all.about.config.prop;
import java.util.regex.Pattern;
public class PropertyRegex {
private
static String
PROP_KEY =
"([^\\.=#][^=#]*[^\\.=#])";
private
static String
PROP_VAL =
"([^#]+)";
private
static String
PROP_REGEX =
"\\s*"+PROP_KEY+"\\s*=\\s*"+PROP_VAL+"\\s*";
private
static String
SINGLE_COMMENT_REGEX =
"#.*";
private
static String
MULTI_COMMENT_REGEX_S =
"/\\*.*";
private
static String
MULTI_COMMENT_REGEX_E =
".*\\*/";
private
static String
NEW_SECTION =
"\\[[^\\[\\]]+\\s+(ns\\s*=\\s*"+PROP_KEY+")\\s*\\]";
public
static
boolean isProperty(String property) {
return Pattern.matches(PROP_REGEX,
property);
}
public
static
boolean isSingleLineComment(String comment) {
return Pattern.matches(SINGLE_COMMENT_REGEX,
comment);
}
public
static
boolean beginMultipleLineComment(String comment) {
return Pattern.matches(MULTI_COMMENT_REGEX_S,
comment);
}
public
static
boolean endMultipleLineComment(String comment) {
return Pattern.matches(MULTI_COMMENT_REGEX_E,
comment);
}
public
static
boolean isNewSection(String section) {
return Pattern.matches(NEW_SECTION,
section);
}
}
---------------------------------------
package com.all.about.config.prop;
import org.junit.Assert;
import org.junit.Test;
public class PropertyRegexTest {
@Test
public
void isProperty() {
Assert.assertEquals(PropertyRegex.isProperty("key=value"),
true);
Assert.assertEquals(PropertyRegex.isProperty("k#ey=value"),
false);
Assert.assertEquals(PropertyRegex.isProperty("=key=value"),
false);
Assert.assertEquals(PropertyRegex.isProperty("#key=value"),
false);
Assert.assertEquals(PropertyRegex.isProperty(".key=value"),
false);
Assert.assertEquals(PropertyRegex.isProperty("key.=value"),
false);
Assert.assertEquals(PropertyRegex.isProperty("key1.key2=value"),
true);
Assert.assertEquals(PropertyRegex.isProperty("...=value"),
false);
}
@Test
public
void isSingleLineComment() {
Assert.assertEquals(PropertyRegex.isSingleLineComment("#single line comment"),
true);
Assert.assertEquals(PropertyRegex.isSingleLineComment("single line comment#"),
false);
Assert.assertEquals(PropertyRegex.isSingleLineComment("#/*"),
true);
}
@Test
public
void beginMultipleLineComment() {
Assert.assertEquals(PropertyRegex.beginMultipleLineComment("/*multiple lines comment start"),
true);
Assert.assertEquals(PropertyRegex.beginMultipleLineComment("#/*multiple lines comment start"),
false);
Assert.assertEquals(PropertyRegex.beginMultipleLineComment("/*#multiple lines comment start"),
true);
}
@Test
public
void endMultipleLineComment() {
Assert.assertEquals(PropertyRegex.endMultipleLineComment("multiple lines comment end */"),
true);
Assert.assertEquals(PropertyRegex.endMultipleLineComment("#multiple lines comment end */"),
true);
Assert.assertEquals(PropertyRegex.endMultipleLineComment("*/ multiple lines comment end"),
false);
}
@Test
public
void isNewSection() {
Assert.assertEquals(PropertyRegex.isNewSection("[]"),
false);
Assert.assertEquals(PropertyRegex.isNewSection("[New Section ns=namespace]"),
true);
Assert.assertEquals(PropertyRegex.isNewSection("[[]]"),
false);
}
}
相关文章推荐
- 关于Java property 文件,我的一些看法(1)
- 关于Java解压文件的一些坑及经验分享(MALFORMED异常)
- 关于Java解压文件的一些坑及经验分享
- 关于Java解压文件的一些坑及经验分享(MALFORMED异常)
- 关于操作java中的SD卡文件的一些总结
- 黑马程序员-java中关于文件的一些基本操作
- 4.关于JAVA中文乱码的一些看法
- 关于java一些打包发布可执行文件工具(exe4j+install4j)
- think in java中 7.6.7章中关于内部类覆盖的一些看法
- 关于C/C++语言中头文件的使用的一些看法
- 关于java的property文件
- 关于Java对相对目录的文件读写和调用方法的一些总结
- 关于所谓U盘有占用空间,却看不到文件的一些看法
- Java读取Property配置文件
- java上传下载文件部署到linux系统下的一些问题
- 轻松python之文件专题-关于zip文件的一些操作
- 文章标题 Java中io流的一些简单操作(包含文件复制,向硬盘中写入文本文件,以及io流高级应用序列化和反序列化)
- Java关于文件I/O的事例
- 关于java读取文件时遇到Unicode乱码情况
- 一些关于Java的小东西(3)