validateXmlString static method

ValidationReport validateXmlString(
  1. String xmlContent
)

Implementation

static ValidationReport validateXmlString(String xmlContent) {
  final issues = <ValidationIssue>[];

  if (xmlContent.trim().isEmpty) {
    issues.add(const ValidationIssue(
      severity: ValidationSeverity.error,
      code: 'XML_EMPTY',
      message: 'XML content is empty.',
    ));
    return ValidationReport(issues);
  }

  try {
    xml.XmlDocument.parse(xmlContent);
  } on xml.XmlParserException catch (e) {
    issues.add(ValidationIssue(
      severity: ValidationSeverity.error,
      code: 'XML_PARSE_ERROR',
      message: 'XML parse error: ${e.message}',
      path: 'line:${e.position}',
    ));
    return ValidationReport(issues);
  }

  // Check for odoo root element
  try {
    final doc = xml.XmlDocument.parse(xmlContent);
    final root = doc.rootElement;

    if (root.localName != 'odoo') {
      issues.add(ValidationIssue(
        severity: ValidationSeverity.warning,
        code: 'XML_ROOT_NOT_ODOO',
        message: 'Root element is <${root.localName}>, expected <odoo>.',
      ));
    }

    // Check for at least one record
    if (doc.findAllElements('record').isEmpty) {
      issues.add(const ValidationIssue(
        severity: ValidationSeverity.warning,
        code: 'XML_NO_RECORDS',
        message: 'No <record> elements found — is this a valid Odoo views file?',
      ));
    }
  } catch (_) {}

  return ValidationReport(issues);
}