Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,9 @@
import org.netbeans.api.xml.lexer.XMLTokenId;

public class XMLSyntaxParser {


private static final String BOM = "\uFEFF";

public Document parse(BaseDocument basedoc)
throws IOException, BadLocationException {
try {
Expand All @@ -44,22 +46,8 @@ public Document parse(BaseDocument basedoc)
List<Token> currentTokens = new ArrayList<Token>();
TokenHierarchy th = TokenHierarchy.get(basedoc);
TokenSequence<XMLTokenId> tokenSequence = th.tokenSequence();
org.netbeans.api.lexer.Token<XMLTokenId> token = tokenSequence.token();
// Add the text token, if any, before xml decalration to document node
if(token != null && token.id() == XMLTokenId.TEXT) {
currentTokens.add(Token.create(token.text().toString(),TokenType.TOKEN_CHARACTER_DATA));
if(tokenSequence.moveNext()) {
token = tokenSequence.token();
}
// if the xml decalration is not there assign this token to document
if(token.id() != XMLTokenId.PI_START) {
currentNode.setTokens(new ArrayList<Token>(currentTokens));
currentTokens.clear();
}
}

while (tokenSequence.moveNext()) {
token = tokenSequence.token();
org.netbeans.api.lexer.Token<XMLTokenId> token = tokenSequence.token();
XMLTokenId tokenId = token.id();
String image = token.text().toString();
TokenType tokenType = TokenType.TOKEN_WHITESPACE;
Expand Down Expand Up @@ -245,7 +233,7 @@ public Document parse(BaseDocument basedoc)
((Element)parent).appendChild(currentNode, false);
} else {//parent is Document
if(token.id() != XMLTokenId.BLOCK_COMMENT &&
token.text().toString().trim().length() > 0) {
!token.text().toString().isBlank() && !BOM.equals(token.text().toString().trim())) {
throw new IOException("Invalid token '" + token.text() +
"' found in document: " +
"Please use the text editor to resolve the issues...");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ public static Test suite() {
suite.addTest(new XMLSyntaxParserTest("testParseWSDL"));
// Disabled as referenced files were partly not donated by oracle to apache
// suite.addTest(new XMLSyntaxParserTest("testParsePerformace"));
suite.addTest(new XMLSyntaxParserTest("testParseBOM"));
return suite;
}

Expand Down Expand Up @@ -254,5 +255,20 @@ public void testParsePerformace() throws Exception {
//FlushVisitor fv = new FlushVisitor();
//String docBuf = fv.flushModel(doc);
//assertEquals("The document should be unaltered",basedoc.getText(0,basedoc.getLength()),docBuf);
}
}

/**
* Test of parse method, of class org.netbeans.modules.xmltools.xmlmodel.nodes.XMLSyntaxParser.
* XMLSyntaxParser should handle xml files with BOM
*/
public void testParseBOM() throws Exception {
BaseDocument basedoc = getDocument("nodes/testBOM.xml");
XMLSyntaxParser parser = new XMLSyntaxParser();
Document doc = parser.parse(basedoc);
assertNotNull("Document can not be null", doc);
FlushVisitor fv = new FlushVisitor();
String docBuf = fv.flushModel(doc);
assertEquals("The document should be unaltered",basedoc.getText(0,basedoc.getLength()),docBuf);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?xml version="1.0"?>
<company>
<employee ssn="xx-xx-xxxx" id="123"
address="16 Network Circle"
phone="123-456-7890" >Vidhya Narayanan
</employee>
<!-- -->
<employee/>
</company>
Loading