Class: SvgConform::Validator

Inherits:
Object
  • Object
show all
Defined in:
lib/svg_conform/validator.rb

Overview

Main validator class for SVG conformance checking

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Validator

Returns a new instance of Validator.



8
9
10
11
12
13
14
# File 'lib/svg_conform/validator.rb', line 8

def initialize(options = {})
  @options = {
    fix: false,
    strict: false,
    mode: :sax, # Testing SAX mode
  }.merge(options)
end

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



6
7
8
# File 'lib/svg_conform/validator.rb', line 6

def options
  @options
end

Instance Method Details

#available_profilesObject

Get available profiles



82
83
84
# File 'lib/svg_conform/validator.rb', line 82

def available_profiles
  SvgConform::Profiles.available_profiles
end

#validate(input, profile: :svg_1_2_rfc, **options) ⇒ Object

Validate SVG content string or document object Accepts:

  • String (XML content) → uses SAX for efficient parsing

  • Moxml::Document or Moxml::Element → serializes once, then SAX validates

  • Nokogiri::XML::Document or Nokogiri::XML::Element → serializes once, then SAX validates

  • Any adapter document object (Ox, Oga, REXML, LibXML) → serializes once, then SAX validates

IMPORTANT: Always uses SAX validation to safely handle large SVG files. DOM validation can hang on large files, so we serialize once and validate with SAX.



43
44
45
46
47
48
49
50
51
# File 'lib/svg_conform/validator.rb', line 43

def validate(input, profile: :svg_1_2_rfc, **options)
  merged_options = @options.merge(options)

  # Normalize input to string, then use SAX validation
  svg_content = normalize_input_to_string(input)

  # Always use SAX mode for safe validation (handles large files)
  validate_content_sax(svg_content, profile: profile, **merged_options)
end

#validate_document(document, profile: :svg_1_2_rfc, **options) ⇒ Object

Validate a Document object (DOM only)



54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/svg_conform/validator.rb', line 54

def validate_document(document, profile: :svg_1_2_rfc, **options)
  merged_options = @options.merge(options)
  profile_obj = resolve_profile(profile)

  # Perform validation
  result = profile_obj.validate(document)

  # Apply fixes if requested
  result.apply_fixes if merged_options[:fix] && result.fixable?

  result
end

#validate_file(file_path, profile: :svg_1_2_rfc, **options) ⇒ Object

Validate an SVG file



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/svg_conform/validator.rb', line 17

def validate_file(file_path, profile: :svg_1_2_rfc, **options)
  unless File.exist?(file_path)
    raise ValidationError,
          "File not found: #{file_path}"
  end

  merged_options = @options.merge(options)
  mode = determine_mode(file_path, merged_options[:mode])

  case mode
  when :sax
    validate_file_sax(file_path, profile: profile, **merged_options)
  when :dom
    validate_file_dom(file_path, profile: profile, **merged_options)
  end
end

#validate_files(file_paths, profile: :svg_1_2_rfc, **options) ⇒ Object

Validate multiple files



68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/svg_conform/validator.rb', line 68

def validate_files(file_paths, profile: :svg_1_2_rfc, **options)
  results = {}

  file_paths.each do |file_path|
    results[file_path] =
      validate_file(file_path, profile: profile, **options)
  rescue StandardError => e
    results[file_path] = create_error_result(file_path, e)
  end

  results
end