|
| 1 | +package xml |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "encoding/xml" |
| 6 | + "fmt" |
| 7 | + "io" |
| 8 | + "strings" |
| 9 | + |
| 10 | + "github.com/observiq/stanza/entry" |
| 11 | + "github.com/observiq/stanza/operator" |
| 12 | + "github.com/observiq/stanza/operator/helper" |
| 13 | +) |
| 14 | + |
| 15 | +func init() { |
| 16 | + operator.Register("xml_parser", func() operator.Builder { return NewXMLParserConfig("") }) |
| 17 | +} |
| 18 | + |
| 19 | +// NewXMLParserConfig creates a new XML parser config with default values |
| 20 | +func NewXMLParserConfig(operatorID string) *XMLParserConfig { |
| 21 | + return &XMLParserConfig{ |
| 22 | + ParserConfig: helper.NewParserConfig(operatorID, "xml_parser"), |
| 23 | + } |
| 24 | +} |
| 25 | + |
| 26 | +// XMLParserConfig is the configuration of an XML parser operator. |
| 27 | +type XMLParserConfig struct { |
| 28 | + helper.ParserConfig `yaml:",inline"` |
| 29 | +} |
| 30 | + |
| 31 | +// Build will build an XML parser operator. |
| 32 | +func (c XMLParserConfig) Build(context operator.BuildContext) ([]operator.Operator, error) { |
| 33 | + parserOperator, err := c.ParserConfig.Build(context) |
| 34 | + if err != nil { |
| 35 | + return nil, err |
| 36 | + } |
| 37 | + |
| 38 | + xmlParser := &XMLParser{ |
| 39 | + ParserOperator: parserOperator, |
| 40 | + } |
| 41 | + |
| 42 | + return []operator.Operator{xmlParser}, nil |
| 43 | +} |
| 44 | + |
| 45 | +// XMLParser is an operator that parses XML. |
| 46 | +type XMLParser struct { |
| 47 | + helper.ParserOperator |
| 48 | +} |
| 49 | + |
| 50 | +// Process will parse an entry for XML. |
| 51 | +func (x *XMLParser) Process(ctx context.Context, entry *entry.Entry) error { |
| 52 | + return x.ParserOperator.ProcessWith(ctx, entry, parse) |
| 53 | +} |
| 54 | + |
| 55 | +// parse will parse an xml value |
| 56 | +func parse(value interface{}) (interface{}, error) { |
| 57 | + strValue, ok := value.(string) |
| 58 | + if !ok { |
| 59 | + return nil, fmt.Errorf("value passed to parser is not a string") |
| 60 | + } |
| 61 | + |
| 62 | + reader := strings.NewReader(strValue) |
| 63 | + decoder := xml.NewDecoder(reader) |
| 64 | + token, err := decoder.Token() |
| 65 | + if err != nil { |
| 66 | + return nil, fmt.Errorf("failed to decode as xml: %w", err) |
| 67 | + } |
| 68 | + |
| 69 | + elements := []*Element{} |
| 70 | + var parent *Element |
| 71 | + var current *Element |
| 72 | + |
| 73 | + for token != nil { |
| 74 | + switch token := token.(type) { |
| 75 | + case xml.StartElement: |
| 76 | + parent = current |
| 77 | + current = newElement(token) |
| 78 | + current.Parent = parent |
| 79 | + |
| 80 | + if parent != nil { |
| 81 | + parent.Children = append(parent.Children, current) |
| 82 | + } else { |
| 83 | + elements = append(elements, current) |
| 84 | + } |
| 85 | + case xml.EndElement: |
| 86 | + current = parent |
| 87 | + if parent != nil { |
| 88 | + parent = parent.Parent |
| 89 | + } |
| 90 | + case xml.CharData: |
| 91 | + if current != nil { |
| 92 | + current.Content = getValue(token) |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + token, err = decoder.Token() |
| 97 | + if err != nil && err != io.EOF { |
| 98 | + return nil, fmt.Errorf("failed to get next xml token: %w", err) |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 102 | + switch len(elements) { |
| 103 | + case 0: |
| 104 | + return nil, fmt.Errorf("no xml elements found") |
| 105 | + case 1: |
| 106 | + return convertToMap(elements[0]), nil |
| 107 | + default: |
| 108 | + return convertToMaps(elements), nil |
| 109 | + } |
| 110 | +} |
0 commit comments