diff --git a/pkg/format/format.go b/pkg/format/format.go index 7037c54..c1be66a 100644 --- a/pkg/format/format.go +++ b/pkg/format/format.go @@ -3,7 +3,6 @@ package format import ( "bytes" "encoding/json" - "encoding/xml" "strings" "github.com/goccy/go-yaml" @@ -59,15 +58,24 @@ func IsYAML(b []byte) bool { } // IsXML checks whether bytes are in XML format. +// Function does not guarantee that standard xml.Unmarshal will work b, instead +// it only looks for characteristics of XML formatted data. func IsXML(b []byte) bool { - var v any - err := xml.Unmarshal(b, &v) - if err == nil { + str := string(b) + idx := strings.Index(strings.TrimSpace(str), "") && strings.Contains(str, "<")) { + return false + } + + if strings.Count(str, "<") >= (strings.Count(str, "")) { + return true + } + + return false } // IsHTML checks whether bytes are in HTML format. diff --git a/pkg/format/format_test.go b/pkg/format/format_test.go index d97f6ac..b66fdad 100644 --- a/pkg/format/format_test.go +++ b/pkg/format/format_test.go @@ -72,21 +72,9 @@ func TestIsXML(t *testing.T) { }{ {name: "json", args: args{bytes: []byte(`{"name": "abc"}`)}, want: false}, {name: "plain text", args: args{bytes: []byte(`abcd efgh`)}, want: false}, - {name: "xml", args: args{bytes: []byte(` - - - Gambardella, Matthew - XML Developer's Guide - Computer - 44.95 - 2000-10-01 - An in-depth look at creating applications - with XML. - -`)}, want: true}, {name: "yaml", args: args{bytes: []byte(`--- name: "abc"`)}, want: false}, - {name: "yaml", args: args{bytes: []byte(` + {name: "xml #1", args: args{bytes: []byte(` @@ -111,6 +99,30 @@ name: "abc"`)}, want: false}, `)}, want: true}, + {name: "xml #2", args: args{bytes: []byte(` + + + Gambardella, Matthew + XML Developer's Guide + Computer + 44.95 + 2000-10-01 + An in-depth look at creating applications + with XML. + +`)}, want: true}, + {name: "xml #3", args: args{bytes: []byte(` + + + Gambardella, Matthew + XML Developer's Guide + Computer + 44.95 + 2000-10-01 + An in-depth look at creating applications + with XML. + +`)}, want: true}, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) {