Skip to content

Commit

Permalink
Fix a bug with broken comments handling in the prettyprinter
Browse files Browse the repository at this point in the history
  • Loading branch information
misdoro committed Oct 28, 2015
1 parent ef1f3c3 commit 373ada1
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 4 deletions.
12 changes: 8 additions & 4 deletions xsams-io/src/main/java/org/vamdc/xsams/io/PrettyPrint.java
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ protected class NoSpaceStream extends InputStream{
private int quote='\0';//Quote used
private boolean wasEscaped=false;//Is this symbol backslashed?
private boolean intag=false;
private boolean incomment=false;
private Stack<String> tagStack=new Stack<String>();
private Stack<String> fullTagStack=new Stack<String>();
private StringBuilder tag = new StringBuilder();
Expand Down Expand Up @@ -152,7 +153,7 @@ else if (notInQuote())

if (newbyte == '>' && notInQuote()){
processTagName();
intag=false;//Getting out of tag
intag=incomment;//Getting out of tag
}else{
tag.append((char)newbyte);
}
Expand Down Expand Up @@ -193,11 +194,14 @@ else if (notInQuote())

private void processTagName() throws IOException {
String fullTagName=tag.toString();
tag=new StringBuilder();

if (fullTagName.startsWith("!--")){
if (fullTagName.startsWith("!--")){
incomment=!fullTagName.endsWith("--");
return;
}

tag=new StringBuilder();


if (fullTagName.startsWith("/")){
processEndTag(fullTagName.substring(1));
}else if (!fullTagName.endsWith("/")){
Expand Down
19 changes: 19 additions & 0 deletions xsams-io/src/test/java/org/vamdc/xsams/io/PrettyPrintTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,25 @@ public void testPrettyNoMatchTag(){
}
}

@Test
public void testPrettyComments(){
String xml="<?xml version=\"1.0\" encoding=\"UTF-8\"?><taga><!-- <tagb>value<tagc>value2</tagc>\n</tagb>--><tagc>a</tagc></taga>";
String expected="<?xml version=\"1.0\" encoding=\"UTF-8\"?><taga>\n"+
" <!-- <tagb>value<tagc>value2</tagc>\n</tagb>-->\n"+
" <tagc>a</tagc>\n"+
"</taga>\n";
PrettyPrint pretty=new PrettyPrint();
try {
InputStream prettyStream=pretty.transform(getStream(xml));
String result = getString(prettyStream);
assertTrue(equalsNoSpace(xml,result));
assertEquals(result,expected);
System.out.println(result);
} catch (Exception e) {
fail(e.getMessage());
}
}

private boolean equalsNoSpace(String string1,String string2){
String nsp1 = string1.replaceAll(PrettyPrintTest.spacePattern,"");
String nsp2 = string2.replaceAll(PrettyPrintTest.spacePattern,"");
Expand Down

0 comments on commit 373ada1

Please sign in to comment.