[jdom-interest] Parsing a comment like <!--- Hello

Dennis Sosnoski dms at sosnoski.com
Tue Nov 2 15:14:10 PST 2004


That does match the production given in the XML recommendation, so it is 
valid XML:

Comment   ::=   '<!--' ((Char - '-') | ('-' (Char - '-')))* '-->'

The problem is that third "if" in the code, which returns "Comment data 
cannot start with a hyphen." - according to the above production, 
comment data *can* start with a hyphen, providing the hyphen is followed 
by a non-hyphen. Surprised me!

  - Dennis

Elias Ross wrote:

>Somebody created a configuration file with a comment like this
>
><!--- This is a comment -->
>
>It passed validation with xmllint.  JDom however will *not* parse this
>comment.  (This is valid XML, by the way.)
>
>What happened was we tried to read this configuration file and blew up
>unexpectedly.  JDom and xmllint (and other XML parsers) should agree.
>
>src/java/org/jdom/Verifier.java:485
>
>    public static String checkCommentData(String data) {
>        String reason = null;
>        if ((reason = checkCharacterData(data)) != null) {
>            return reason;
>        }
>
>        if (data.indexOf("--") != -1) {
>            return "Comments cannot contain double hyphens (--)";
>        }
>        if (data.startsWith("-")) {
>            return "Comment data cannot start with a hyphen.";
>        }
>        if (data.endsWith("-")) {
>            return "Comment data cannot end with a hyphen.";
>        }
>                                                                                                                                                               
>        // If we got here, everything is OK
>        return null;
>    }
>  
>


More information about the jdom-interest mailing list