关于Java property 文件,我的一些看法(2)

我设想的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);

}

}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值