Skip to content

Commit 9769867

Browse files
committed
Unify methods with WithLineNumbers parser option flag
Signed-off-by: Marek Aufart <maufart@redhat.com>
1 parent bd4beb3 commit 9769867

File tree

3 files changed

+39
-41
lines changed

3 files changed

+39
-41
lines changed

options.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ import (
66
)
77

88
type ParserOptions struct {
9-
Decoder *DecoderOptions
9+
Decoder *DecoderOptions
10+
WithLineNumbers bool
1011
}
1112

1213
func (options ParserOptions) apply(parser *parser) {

parse.go

Lines changed: 36 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,27 @@ func Parse(r io.Reader) (*Node, error) {
3939

4040
// ParseWithOptions is like parse, but with custom options
4141
func ParseWithOptions(r io.Reader, options ParserOptions) (*Node, error) {
42+
var data []byte
43+
var lineStarts []int
44+
45+
// If line numbers are requested, read all data for position tracking
46+
if options.WithLineNumbers {
47+
var err error
48+
data, err = io.ReadAll(r)
49+
if err != nil {
50+
return nil, err
51+
}
52+
r = bytes.NewReader(data)
53+
54+
// Pre-calculate line starts
55+
lineStarts = []int{0}
56+
for i, b := range data {
57+
if b == '\n' {
58+
lineStarts = append(lineStarts, i+1)
59+
}
60+
}
61+
}
62+
4263
p := createParser(r)
4364
options.apply(p)
4465
var err error
@@ -62,6 +83,20 @@ func ParseWithOptions(r io.Reader, options ParserOptions) (*Node, error) {
6283
if !valid {
6384
return nil, fmt.Errorf("xmlquery: invalid XML document")
6485
}
86+
87+
// If line numbers were requested, annotate the parsed document
88+
if options.WithLineNumbers {
89+
annotator := &lineNumberAnnotator{
90+
data: data,
91+
lineStarts: lineStarts,
92+
}
93+
94+
err = annotator.annotateLineNumbers(p.doc)
95+
if err != nil {
96+
return nil, err
97+
}
98+
}
99+
65100
return p.doc, nil
66101
}
67102

@@ -730,45 +765,7 @@ func (p *lineNumberAnnotator) findProcessingInstructionPosition(target string) i
730765
return 1
731766
}
732767

733-
// ParseWithLineNumbers returns the parse tree for the XML from the given Reader with line number annotations.
734-
func ParseWithLineNumbers(r io.Reader) (*Node, error) {
735-
return ParseWithLineNumbersAndOptions(r, ParserOptions{})
736-
}
737768

738-
// ParseWithLineNumbersAndOptions is like ParseWithLineNumbers, but with custom options
739-
func ParseWithLineNumbersAndOptions(r io.Reader, options ParserOptions) (*Node, error) {
740-
// Read all data first so we can track positions
741-
data, err := io.ReadAll(r)
742-
if err != nil {
743-
return nil, err
744-
}
745-
746-
// Parse with the standard parser first
747-
doc, err := ParseWithOptions(bytes.NewReader(data), options)
748-
if err != nil {
749-
return nil, err
750-
}
751-
752-
// Now annotate with line numbers using the sophisticated algorithm
753-
parser := &lineNumberAnnotator{
754-
data: data,
755-
lineStarts: []int{0},
756-
}
757-
758-
// Pre-calculate line starts
759-
for i, b := range data {
760-
if b == '\n' {
761-
parser.lineStarts = append(parser.lineStarts, i+1)
762-
}
763-
}
764-
765-
err = parser.annotateLineNumbers(doc)
766-
if err != nil {
767-
return nil, err
768-
}
769-
770-
return doc, nil
771-
}
772769

773770
// LoadURLWithLineNumbers loads the XML document from the specified URL with line number annotations.
774771
func LoadURLWithLineNumbers(url string) (*Node, error) {
@@ -779,7 +776,7 @@ func LoadURLWithLineNumbers(url string) (*Node, error) {
779776
defer resp.Body.Close()
780777

781778
if xmlMIMERegex.MatchString(resp.Header.Get("Content-Type")) {
782-
return ParseWithLineNumbers(resp.Body)
779+
return ParseWithOptions(resp.Body, ParserOptions{WithLineNumbers: true})
783780
}
784781
return nil, fmt.Errorf("invalid XML document(%s)", resp.Header.Get("Content-Type"))
785782
}

parse_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -677,7 +677,7 @@ func TestLineNumbers(t *testing.T) {
677677
</book>
678678
</bookstore>`
679679

680-
doc, err := ParseWithLineNumbers(strings.NewReader(s))
680+
doc, err := ParseWithOptions(strings.NewReader(s), ParserOptions{WithLineNumbers: true})
681681
if err != nil {
682682
t.Fatal(err)
683683
}

0 commit comments

Comments
 (0)