-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathxml_parser.go
More file actions
395 lines (350 loc) · 12 KB
/
Copy pathxml_parser.go
File metadata and controls
395 lines (350 loc) · 12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
package parser
import (
"encoding/xml"
"fmt"
"io"
"log/slog"
"strings"
)
// parseXMLToGeneric parses XML content into a generic map structure for template use
// Returns a hierarchical structure where attributes are flattened with elementName/attributeName format
func parseXMLToGeneric(xmlContent string) (map[string]interface{}, error) {
if strings.TrimSpace(xmlContent) == "" {
slog.Debug("Empty XML content provided")
return nil, fmt.Errorf("empty XML content")
}
// Parse XML into hierarchical format with flattened attributes
parsedRoot, err := parseXMLHierarchical(xmlContent)
if err != nil {
slog.Debug("XML parsing failed", "error", err, "xml_length", len(xmlContent))
return nil, err
}
slog.Debug("XML parsing successful", "root_parsed", parsedRoot != nil)
return parsedRoot, nil
}
// parseXMLHierarchical parses XML into a hybrid structure with both flattened paths and nested maps
func parseXMLHierarchical(xmlContent string) (map[string]interface{}, error) {
decoder := xml.NewDecoder(strings.NewReader(xmlContent))
for {
token, err := decoder.Token()
if err != nil {
if err == io.EOF {
return nil, fmt.Errorf("no root element found")
}
return nil, err
}
switch t := token.(type) {
case xml.StartElement:
// Parse with both flattened and hierarchical structures
result := make(map[string]interface{})
nestedResult, err := parseXMLElementHybrid(decoder, t, "", result)
if err != nil {
return nil, err
}
// Add the root element as a nested structure
result[t.Name.Local] = nestedResult
// Add root element attributes at the top level for optimized structure
for _, attr := range t.Attr {
attrName := attr.Name.Local
attrValue := attr.Value
rootAttrKey := fmt.Sprintf("%s/%s", t.Name.Local, attrName)
result[rootAttrKey] = attrValue
}
return result, nil
}
}
}
// parseXMLElementHybrid creates both flattened paths and nested map structures
func parseXMLElementHybrid(decoder *xml.Decoder, startElement xml.StartElement, parentPath string, flatResult map[string]interface{}) (map[string]interface{}, error) {
elementName := startElement.Name.Local
nestedResult := make(map[string]interface{})
var currentPath string
if parentPath == "" {
currentPath = elementName
} else {
currentPath = parentPath + "/" + elementName
}
slog.Debug("Processing element hybrid", "name", elementName, "parentPath", parentPath, "currentPath", currentPath)
// Add element attributes to both flattened and nested structures
for _, attr := range startElement.Attr {
attrName := attr.Name.Local
attrValue := attr.Value
// Flattened path: full path from root
flatAttrKey := fmt.Sprintf("%s/%s", currentPath, attrName)
slog.Debug("Adding flattened attribute", "key", flatAttrKey, "value", attrValue)
// Handle multiple attributes with same name (arrays) in flattened structure
if existingAttr, exists := flatResult[flatAttrKey]; exists {
switch existingAttrArray := existingAttr.(type) {
case []interface{}:
flatResult[flatAttrKey] = append(existingAttrArray, attrValue)
default:
flatResult[flatAttrKey] = []interface{}{existingAttr, attrValue}
}
} else {
flatResult[flatAttrKey] = attrValue
}
// Note: We don't add attributes to the element's own nested structure here
// They will be added at the parent level by the parent element's processing
}
var textContent strings.Builder
hasChildren := false
for {
token, err := decoder.Token()
if err != nil {
return nil, err
}
switch t := token.(type) {
case xml.StartElement:
hasChildren = true
childName := t.Name.Local
// Parse child recursively
childNested, err := parseXMLElementHybrid(decoder, t, currentPath, flatResult)
if err != nil {
return nil, err
}
// Add child to nested structure
if existingChild, exists := nestedResult[childName]; exists {
switch existingArray := existingChild.(type) {
case []interface{}:
nestedResult[childName] = append(existingArray, childNested)
default:
nestedResult[childName] = []interface{}{existingArray, childNested}
}
} else {
nestedResult[childName] = childNested
}
// Add child's attributes to this parent level with childName/attrName format
// This creates the optimized structure where attributes are at the same level as the element
childStartElement := t // t is the xml.StartElement for the child
for _, attr := range childStartElement.Attr {
attrName := attr.Name.Local
attrValue := attr.Value
childAttrKey := fmt.Sprintf("%s/%s", childName, attrName)
// Handle arrays for multiple children with same name and same attributes
if existingAttr, exists := nestedResult[childAttrKey]; exists {
switch existingAttrArray := existingAttr.(type) {
case []interface{}:
nestedResult[childAttrKey] = append(existingAttrArray, attrValue)
default:
nestedResult[childAttrKey] = []interface{}{existingAttr, attrValue}
}
} else {
nestedResult[childAttrKey] = attrValue
}
}
case xml.CharData:
text := strings.TrimSpace(string(t))
if text != "" {
if textContent.Len() > 0 {
textContent.WriteString(" ")
}
textContent.WriteString(text)
}
case xml.EndElement:
if t.Name.Local == elementName {
finalText := strings.TrimSpace(textContent.String())
// Handle text content for both structures
if !hasChildren && finalText != "" {
// Text-only element
slog.Debug("Storing text element", "path", currentPath, "text", finalText)
// Store in flattened structure with array support
if existingValue, exists := flatResult[currentPath]; exists {
switch existingArray := existingValue.(type) {
case []interface{}:
flatResult[currentPath] = append(existingArray, finalText)
default:
flatResult[currentPath] = []interface{}{existingValue, finalText}
}
} else {
flatResult[currentPath] = finalText
}
nestedResult[elementName] = finalText
} else if finalText != "" {
// Element with both children and text
nestedResult["_text"] = finalText
} else if !hasChildren {
// Empty element
if existingValue, exists := flatResult[currentPath]; exists {
switch existingArray := existingValue.(type) {
case []interface{}:
flatResult[currentPath] = append(existingArray, "")
default:
flatResult[currentPath] = []interface{}{existingValue, ""}
}
} else {
flatResult[currentPath] = ""
}
}
// For elements with children, store the nested structure in flattened path
if hasChildren {
if existingValue, exists := flatResult[currentPath]; exists {
switch existingArray := existingValue.(type) {
case []interface{}:
flatResult[currentPath] = append(existingArray, nestedResult)
default:
flatResult[currentPath] = []interface{}{existingValue, nestedResult}
}
} else {
flatResult[currentPath] = nestedResult
}
}
return nestedResult, nil
}
}
}
}
// XMLHelper provides template functions for XML manipulation
type XMLHelper struct{}
// GetXMLAttribute extracts a specific attribute from an XML node map
// Usage: {{xmlAttr .BodyXML "key" "attr1"}} to get the 'attr1' attribute from 'key' element
// Works with format (key/attr)
func (h XMLHelper) GetXMLAttribute(xmlMap map[string]interface{}, elementName, attrName string) string {
// Try new flattened format
attrKey := fmt.Sprintf("%s/%s", elementName, attrName)
if attr, exists := xmlMap[attrKey]; exists {
switch attrVal := attr.(type) {
case string:
return attrVal
case []interface{}:
if len(attrVal) > 0 {
if str, ok := attrVal[0].(string); ok {
return str
}
}
}
}
return ""
}
// GetXMLAttributeArray extracts all attribute values as an array
// Usage: {{xmlAttrArray .BodyXML "item" "id"}} to get all 'id' attributes from 'item' elements
func (h XMLHelper) GetXMLAttributeArray(xmlMap map[string]interface{}, elementName, attrName string) []string {
attrKey := fmt.Sprintf("%s/%s", elementName, attrName)
var result []string
if attr, exists := xmlMap[attrKey]; exists {
switch attrVal := attr.(type) {
case string:
result = append(result, attrVal)
case []interface{}:
for _, val := range attrVal {
if str, ok := val.(string); ok {
result = append(result, str)
}
}
}
}
return result
}
// GetXMLValue extracts the value of an XML element
// Usage: {{xmlValue .BodyXML "key"}} to get the value of 'key' element
// For arrays: returns the first element
func (h XMLHelper) GetXMLValue(xmlMap map[string]interface{}, elementName string) interface{} {
if value, exists := xmlMap[elementName]; exists {
switch val := value.(type) {
case []interface{}:
if len(val) > 0 {
return val[0]
}
return ""
default:
return val
}
}
return ""
}
// GetXMLValueArray extracts all values of an XML element as an array
// Usage: {{xmlValueArray .BodyXML "item"}} to get all 'item' element values
func (h XMLHelper) GetXMLValueArray(xmlMap map[string]interface{}, elementName string) []interface{} {
if value, exists := xmlMap[elementName]; exists {
switch val := value.(type) {
case []interface{}:
return val
default:
return []interface{}{val}
}
}
return []interface{}{}
}
// GetXMLText extracts text content from an XML element
func (h XMLHelper) GetXMLText(xmlMap map[string]interface{}, elementName string) string {
// Try new flattened format
if value := h.GetXMLValue(xmlMap, elementName); value != nil {
if textStr, ok := value.(string); ok {
return textStr
}
}
return ""
}
// GetXMLTextArray extracts all text content from XML elements as string array
func (h XMLHelper) GetXMLTextArray(xmlMap map[string]interface{}, elementName string) []string {
values := h.GetXMLValueArray(xmlMap, elementName)
var result []string
for _, val := range values {
if str, ok := val.(string); ok {
result = append(result, str)
}
}
return result
}
// HasXMLAttribute checks if an XML element has a specific attribute
// Usage: {{hasXMLAttr .BodyXML "key" "attr1"}}
func (h XMLHelper) HasXMLAttribute(xmlMap map[string]interface{}, elementName, attrName string) bool {
attrKey := fmt.Sprintf("%s/%s", elementName, attrName)
_, exists := xmlMap[attrKey]
return exists
}
// HasXMLElement checks if an XML element exists
// Usage: {{hasXMLElement .BodyXML "key"}}
func (h XMLHelper) HasXMLElement(xmlMap map[string]interface{}, elementName string) bool {
_, exists := xmlMap[elementName]
return exists
}
// IsXMLArray checks if an XML element is an array (has multiple values)
// Usage: {{isXMLArray .BodyXML "item"}}
func (h XMLHelper) IsXMLArray(xmlMap map[string]interface{}, elementName string) bool {
if value, exists := xmlMap[elementName]; exists {
_, isArray := value.([]interface{})
return isArray
}
return false
}
// XMLArrayLength returns the length of an XML element array
// Usage: {{xmlArrayLen .BodyXML "item"}}
func (h XMLHelper) XMLArrayLength(xmlMap map[string]interface{}, elementName string) int {
if value, exists := xmlMap[elementName]; exists {
switch val := value.(type) {
case []interface{}:
return len(val)
default:
return 1 // Single element
}
}
return 0
}
// ListXMLAttributes returns all attribute names for a specific element
// Usage: {{range xmlAttrs .BodyXML "key"}}{{.}}{{end}}
func (h XMLHelper) ListXMLAttributes(xmlMap map[string]interface{}, elementName string) []string {
var attrs []string
prefix := elementName + "/"
for key := range xmlMap {
if strings.HasPrefix(key, prefix) {
attrName := strings.TrimPrefix(key, prefix)
// Make sure it's a direct attribute, not a nested element
if !strings.Contains(attrName, "/") {
attrs = append(attrs, attrName)
}
}
}
return attrs
}
// ListXMLElements returns all element names from the XML map
// Usage: {{range xmlElements .BodyXML}}{{.}}{{end}}
func (h XMLHelper) ListXMLElements(xmlMap map[string]interface{}) []string {
var elements []string
for key := range xmlMap {
// Skip attribute keys (those containing "/")
if !strings.Contains(key, "/") {
elements = append(elements, key)
}
}
return elements
}