【发布时间】:2013-11-06 19:24:21
【问题描述】:
有没有办法从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;
}
我认为可以用更聪明的方式重写。
【问题讨论】:
-
这是一个 DN,而不是一个属性。
-
好的,标准 JDK API 中是否有一个类来保存它以及可以从
String解析它的相应类?