XMLSyntaxParser: tolerate a UTF-8 BOM at the start of XML documents - #9540
Open
NicolaIsotta wants to merge 1 commit into
Open
XMLSyntaxParser: tolerate a UTF-8 BOM at the start of XML documents#9540NicolaIsotta wants to merge 1 commit into
NicolaIsotta wants to merge 1 commit into
Conversation
XMLSyntaxParser rejected any leading text before the document element with "Invalid token ... found in document", so a BOM character (U+FEFF) caused every BOM-prefixed XML file to fail parsing. Treat a trimmed BOM the same as blank/whitespace when validating text under the Document node. This also adds a test with a BOM xml file and removes some dead code (tokenSequence.token() is always null before the first moveNext())
Contributor
Author
|
NB: I discovered this bug while working on a JSF project whose faces-config.xml had the BOM. |
mbien
reviewed
Aug 6, 2026
Comment on lines
48
to
-59
| 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(); | ||
| } | ||
| } |
Member
There was a problem hiding this comment.
I am not sure why this was there, but the tests, including your new test case, seem to pass with it.
What was the reason you removed it?
| public class XMLSyntaxParser { | ||
|
|
||
|
|
||
| private static final String BOM = "\uFEFF"; |
Member
There was a problem hiding this comment.
suggestion: rename to BYTE_ORDER_MARK or add comment to disambiguate from the common XML TLA for bill of materials.
Comment on lines
-248
to
+236
| token.text().toString().trim().length() > 0) { | ||
| !token.text().toString().isBlank() && !BOM.equals(token.text().toString().trim())) { |
Member
There was a problem hiding this comment.
- the field
imageholds the value fortoken.text().toString()and could be used here i think - the difference between the whitespace semantics of
isBlank()andtrim()is significant here, right? I would recommend to add a comment thattrim()must be kept, otherwise someone might change it to strip() or wonder why this code looks almost identical to the one of the top levelBLOCK_COMMENTcase and adjust both.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
XMLSyntaxParser rejected any leading text before the document element with "Invalid token ... found in document", so a BOM character (U+FEFF) caused every BOM-prefixed XML file to fail parsing. Treat a trimmed BOM the same as blank/whitespace when validating text under the Document node.
This also adds a test with a BOM xml file and removes some dead code (tokenSequence.token() is always null before the first moveNext())
^Add meaningful description above
Click to collapse/expand PR instructions
By opening a pull request you confirm that, unless explicitly stated otherwise, the changes -
LLMs, Commit messages and PR description:
git log) that all commits have a valid name and email address for you in the Author field.Assisted-by: MODEL_NAME MODEL_VERSIONline appended to the commit message.Assisted-byline from above)If you're a first time contributor, see the Contributing guidelines for more information.
If you're a committer, please label the PR before pressing "Create pull request" so that the right test jobs can run.
PR approval and merge checklist:
If this PR targets the delivery branch: don't merge. (full wiki article)