Class: SvgConform::Validator
- Inherits:
-
Object
- Object
- SvgConform::Validator
- Defined in:
- lib/svg_conform/validator.rb
Overview
Main validator class for SVG conformance checking
Instance Attribute Summary collapse
-
#options ⇒ Object
readonly
Returns the value of attribute options.
Instance Method Summary collapse
-
#available_profiles ⇒ Object
Get available profiles.
-
#initialize(options = {}) ⇒ Validator
constructor
A new instance of Validator.
-
#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.
-
#validate_document(document, profile: :svg_1_2_rfc, **options) ⇒ Object
Validate a Document object (DOM only).
-
#validate_file(file_path, profile: :svg_1_2_rfc, **options) ⇒ Object
Validate an SVG file.
-
#validate_files(file_paths, profile: :svg_1_2_rfc, **options) ⇒ Object
Validate multiple files.
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 = { fix: false, strict: false, mode: :sax, # Testing SAX mode }.merge() end |
Instance Attribute Details
#options ⇒ Object (readonly)
Returns the value of attribute options.
6 7 8 |
# File 'lib/svg_conform/validator.rb', line 6 def @options end |
Instance Method Details
#available_profiles ⇒ Object
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.merge() # 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, **) 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.merge() profile_obj = resolve_profile(profile) # Perform validation result = profile_obj.validate(document) # Apply fixes if requested result.apply_fixes if [: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, **) unless File.exist?(file_path) raise ValidationError, "File not found: #{file_path}" end = @options.merge() mode = determine_mode(file_path, [:mode]) case mode when :sax validate_file_sax(file_path, profile: profile, **) when :dom validate_file_dom(file_path, profile: profile, **) 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, **) results = {} file_paths.each do |file_path| results[file_path] = validate_file(file_path, profile: profile, **) rescue StandardError => e results[file_path] = create_error_result(file_path, e) end results end |