有没有一种方法可以解析属性String
?例如,如果我有以下内容:
CN=Doe, John: Markets (LDN),OU=Users,DC=FOOCORP,DC=COM
并希望将其放入一个Attributes
或一组Attribute
-s中,是否可以使用一个实用程序类来进行所有正确的转义,或者我应该只使用自己的某种实现?
我有以下代码:
String cnBase = "CN=Doe\\, John: Markets (LDN),OU=Users,DC=FOOCORP,DC=COM";
StringTokenizer st = new StringTokenizer(cnBase, "=");
Attributes attributes = new BasicAttributes();
String attributeId = null;
String attributeValue = null;
String previousToken = null;
while (st.hasMoreTokens())
{
String token = st.nextToken();
if (previousToken == null && attributeId == null)
{
// Get the attribute's id
attributeId = token;
continue;
}
if (attributeId != null)
{
if (token.contains(","))
{
attributeValue = token.substring(0, token.lastIndexOf(","));
}
else
{
attributeValue = token;
}
}
if (attributeId != null && attributeValue != null)
{
// Add a new Attribute to the attributes object
Attribute attribute = new BasicAttribute(attributeId, attributeValue);
attributes.put(attribute);
System.out.println(attribute.toString());
attributeId = token.substring(token.lastIndexOf(",") + 1, token.length());
attributeValue = null;
}
previousToken = token;
}
我认为可以用更聪明的方式重写它。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句