Implement a more better logging solution
This commit is contained in:
@@ -2,7 +2,7 @@ package processor
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
"modify/logger"
|
||||
"modify/processor/xpath"
|
||||
"strings"
|
||||
|
||||
@@ -15,69 +15,91 @@ type XMLProcessor struct{}
|
||||
|
||||
// ProcessContent implements the Processor interface for XMLProcessor
|
||||
func (p *XMLProcessor) ProcessContent(content string, path string, luaExpr string) (string, int, int, error) {
|
||||
logger.Debug("Processing XML content with XPath: %s", path)
|
||||
|
||||
// Parse XML document
|
||||
// We can't really use encoding/xml here because it requires a pre defined struct
|
||||
// And we HAVE TO parse dynamic unknown XML
|
||||
logger.Trace("Parsing XML document")
|
||||
doc, err := xmlquery.Parse(strings.NewReader(content))
|
||||
if err != nil {
|
||||
logger.Error("Failed to parse XML: %v", err)
|
||||
return content, 0, 0, fmt.Errorf("error parsing XML: %v", err)
|
||||
}
|
||||
|
||||
// Find nodes matching the XPath pattern
|
||||
logger.Debug("Executing XPath query: %s", path)
|
||||
nodes, err := xpath.Get(doc, path)
|
||||
if err != nil {
|
||||
logger.Error("Failed to execute XPath: %v", err)
|
||||
return content, 0, 0, fmt.Errorf("error executing XPath: %v", err)
|
||||
}
|
||||
|
||||
matchCount := len(nodes)
|
||||
logger.Debug("Found %d nodes matching XPath", matchCount)
|
||||
if matchCount == 0 {
|
||||
logger.Warning("No nodes matched the XPath pattern: %s", path)
|
||||
return content, 0, 0, nil
|
||||
}
|
||||
|
||||
// Apply modifications to each node
|
||||
modCount := 0
|
||||
for _, node := range nodes {
|
||||
for i, node := range nodes {
|
||||
logger.Trace("Processing node #%d: %s", i+1, node.Data)
|
||||
|
||||
L, err := NewLuaState()
|
||||
if err != nil {
|
||||
logger.Error("Failed to create Lua state: %v", err)
|
||||
return content, 0, 0, fmt.Errorf("error creating Lua state: %v", err)
|
||||
}
|
||||
defer L.Close()
|
||||
|
||||
logger.Trace("Converting XML node to Lua")
|
||||
err = p.ToLua(L, node)
|
||||
if err != nil {
|
||||
logger.Error("Failed to convert XML node to Lua: %v", err)
|
||||
return content, modCount, matchCount, fmt.Errorf("error converting to Lua: %v", err)
|
||||
}
|
||||
|
||||
err = L.DoString(BuildLuaScript(luaExpr))
|
||||
luaScript := BuildLuaScript(luaExpr)
|
||||
logger.Trace("Executing Lua script: %s", luaScript)
|
||||
err = L.DoString(luaScript)
|
||||
if err != nil {
|
||||
logger.Error("Failed to execute Lua script: %v", err)
|
||||
return content, modCount, matchCount, fmt.Errorf("error executing Lua: %v", err)
|
||||
}
|
||||
|
||||
result, err := p.FromLua(L)
|
||||
if err != nil {
|
||||
logger.Error("Failed to get result from Lua: %v", err)
|
||||
return content, modCount, matchCount, fmt.Errorf("error getting result from Lua: %v", err)
|
||||
}
|
||||
log.Printf("%#v", result)
|
||||
logger.Trace("Lua returned result: %#v", result)
|
||||
|
||||
modified := false
|
||||
modified = L.GetGlobal("modified").String() == "true"
|
||||
if !modified {
|
||||
log.Printf("No changes made to node at path: %s", node.Data)
|
||||
logger.Debug("No changes made to node at path: %s", node.Data)
|
||||
continue
|
||||
}
|
||||
|
||||
// Apply modification based on the result
|
||||
if updatedValue, ok := result.(string); ok {
|
||||
// If the result is a simple string, update the node value directly
|
||||
logger.Debug("Updating node with string value: %s", updatedValue)
|
||||
xpath.Set(doc, path, updatedValue)
|
||||
} else if nodeData, ok := result.(map[string]interface{}); ok {
|
||||
// If the result is a map, apply more complex updates
|
||||
logger.Debug("Updating node with complex data structure")
|
||||
updateNodeFromMap(node, nodeData)
|
||||
}
|
||||
|
||||
modCount++
|
||||
logger.Debug("Successfully modified node #%d", i+1)
|
||||
}
|
||||
|
||||
logger.Info("XML processing complete: %d modifications from %d matches", modCount, matchCount)
|
||||
|
||||
// Serialize the modified XML document to string
|
||||
if doc.FirstChild != nil && doc.FirstChild.Type == xmlquery.DeclarationNode {
|
||||
// If we have an XML declaration, start with it
|
||||
|
Reference in New Issue
Block a user