Made comment tags that contain a start-comment bracket into two comments

This commit is contained in:
Vhati 2013-09-03 16:21:57 -04:00
parent e41a4fdd75
commit ef276e421f

View file

@ -28,7 +28,9 @@ import org.jdom2.input.JDOMParseException;
* *
* Sloppiness: * Sloppiness:
* Any closing tag, regardless of its name, closes the parent tag. * Any closing tag, regardless of its name, closes the parent tag.
* <!-- <!-- blah --> is valid (but the extra dashes will be discarded). * <!-- <!-- blah --> is valid.
* The example above will become two comments. Any extra dashes will
* be discarded.
* --> can occur alone (discarded). * --> can occur alone (discarded).
* An attribute name can start right after the quote from a prior value. * An attribute name can start right after the quote from a prior value.
* Namespace prefixes for nodes and attributes are unique. * Namespace prefixes for nodes and attributes are unique.
@ -109,9 +111,33 @@ public class SloppyXMLParser {
factory.addContent( parentNode, factory.text( whitespace ) ); factory.addContent( parentNode, factory.text( whitespace ) );
tmp = m.group( 2 ); tmp = m.group( 2 );
tmp = tmp.replaceAll( "^-+|(?<=-)-+|-+$", "" ); if ( tmp.length() == 0 ) {
Comment commentNode = factory.comment( tmp ); factory.addContent( parentNode, factory.comment( "" ) );
factory.addContent( parentNode, commentNode ); }
else {
Matcher splicedMatcher = Pattern.compile( "(\\s*)<!--" ).matcher( tmp );
int commentStart = 0;
while ( splicedMatcher.find() ) {
if ( splicedMatcher.start() - commentStart > 0 ) {
String splicedChunk = tmp.substring( commentStart, splicedMatcher.start() );
splicedChunk = splicedChunk.replaceAll( "^-+|(?<=-)-+|-+$", "" );
if ( splicedChunk.startsWith( " " ) ) splicedChunk += " ";
Comment commentNode = factory.comment( splicedChunk );
factory.addContent( parentNode, commentNode );
}
if ( splicedMatcher.group(1).length() > 0 ) {
// Whitespace between comments.
factory.addContent( parentNode, factory.text( splicedMatcher.group(1) ) );
}
commentStart = splicedMatcher.end();
}
if ( commentStart < tmp.length() ) {
String finalChunk = tmp.substring( commentStart );
finalChunk = finalChunk.replaceAll( "^-+|(?<=-)-+|-+$", "" );
Comment commentNode = factory.comment( finalChunk );
factory.addContent( parentNode, commentNode );
}
}
} }
else if ( chunkPtn == cdataPtn ) { else if ( chunkPtn == cdataPtn ) {
String whitespace = m.group( 1 ); String whitespace = m.group( 1 );