Newer
Older
sisyphe-go / xml.go
@Nacim Nacim on 8 Feb 2022 1 KB add test
package main

import (
	"encoding/xml"
	"io/ioutil"
	"os"

	"github.com/sirupsen/logrus"
)

func processXML(message *LogMessage) {
	// queue for read xml (limit number of parallel read files)
	defer wg.Done()
	queueForConcurrent <- struct{}{}
	defer func() { <-queueForConcurrent }()

	message.xml.isWellFormed = isValidXML(message.path)
	logrus.WithFields(logrus.Fields{
		"corpusname":   message.corpusname,
		"name":         message.name,
		"startAt":      message.startAt,
		"extension":    message.extension,
		"path":         message.path,
		"mimetype":     message.mimetype,
		"size":         message.size,
		"isWellFormed": message.xml.isWellFormed,
		"xmlError":     message.xml.xmlError,
	}).Info("")
	incrementProcess()
	return
}

func isValidXML(path string) bool {
	xmlFile, err := os.Open(path)
	if err != nil {
		return false
	}
	// defer the closing of our xmlFile so that we can parse it later on
	defer xmlFile.Close()
	// read our opened xmlFile1 as a byte array. here I am checking if the file is valid or not
	byteValue, err := ioutil.ReadAll(xmlFile)
	if err != nil {
		return false
	}

	if xml.Unmarshal(byteValue, new(interface{})) != nil {
		return false
	}
	return true
}