您的位置:首页 > 编程语言 > Java开发

关于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);
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: