开发者

Validating an XML NCName in Java

开发者 https://www.devze.com 2023-04-09 16:13 出处:网络
I\'m getting some values from Java annotations in an annotation processor to generate metadata. Some of these values are supposed to indicate XML element or attribute names. I\'d like to validate the

I'm getting some values from Java annotations in an annotation processor to generate metadata. Some of these values are supposed to indicate XML element or attribute names. I'd like to validate the input to find out if the provided values are actually legal NCNames according to the XML specification. Only the local name is important in this case, the namespace URI doesn't play a part here.

Is there some simple way of finding out if a string is a legal XML element or attribute name? Preferably I'd use some XML API that is readily available in Java SE. One of the reasons I'm doing this stuff in the first place i开发者_如何学运维s to cut back on dependencies. I'm using JDK 7 so I have access to the most up-to-date classes/methods.

So far, browsing through content handler classes and SAX/DOM stuff hasn't yielded any result.


If you're prepared to have Saxon on your class path you can do

new Name10Checker().isValidNCName(s);

I can't see anything simpler in the public JDK interface.


didn't find anything straightforward in any of the jdk 6 APIs (don't know about jdk 7). a quick but possibly "hackish" way to check would be to convert it to an xml doc and see if it parses:

String name = ...;
if(name.contains(">")) {
  return false;
}
String xmlDoc = "<" + name + "/>";
DocumentBuilder db = ...;
db.parse(new InputSource(new StringReader(xmlDoc)));


I ran into the same problem and found lots of implementations in foss libraries, and even an old implementation in a Java class library, which has been removed ages ago... So here's a few options to choose from:

  • Java Class Library: XMLUtils.isValidNCName(String ncName) (note: removed in 2004)
  • Apache Axis: NCName.isValid(String stValue)
  • Saxonica: NameChecker.isValidNCName(CharSequence ncName)
  • OWL API: XMLUtils.isNCName(java.lang.CharSequence s)
  • Validator.nu HTML Parser: NCName.isNCName(java.lang.String str)

So, if you're using one of these libraries anyway, you're fine.

As I am not, I'll go with a copy of the XMLUtils from the OWL API, which has no external dependencies, is available under non-restrictive licenses (LGPL and Apache 2.0) and consists of nice and clean code.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号