Class: Lutaml::Xml::Schema::Xsd::SchemaPath

Inherits:
Object
  • Object
show all
Defined in:
lib/lutaml/xml/schema/xsd/schema_path.rb

Constant Summary collapse

URI_SCHEMES =
%w[http https].freeze
WINDOWS_ABSOLUTE_PATH_REGEX =
%r{\A[A-Za-z]:[\\/]}
FORWARD_SLASH =
"/"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(location, schema_mappings: []) ⇒ SchemaPath

Returns a new instance of SchemaPath.



16
17
18
19
20
# File 'lib/lutaml/xml/schema/xsd/schema_path.rb', line 16

def initialize(location, schema_mappings: [])
  @location = location
  @schema_mappings = normalize_mappings(schema_mappings)
  assign_location(location)
end

Instance Attribute Details

#locationObject (readonly)

Returns the value of attribute location.



14
15
16
# File 'lib/lutaml/xml/schema/xsd/schema_path.rb', line 14

def location
  @location
end

#pathObject (readonly)

Returns the value of attribute path.



14
15
16
# File 'lib/lutaml/xml/schema/xsd/schema_path.rb', line 14

def path
  @path
end

#schema_mappingsObject (readonly)

Returns the value of attribute schema_mappings.



14
15
16
# File 'lib/lutaml/xml/schema/xsd/schema_path.rb', line 14

def schema_mappings
  @schema_mappings
end

#urlObject (readonly)

Returns the value of attribute url.



14
15
16
# File 'lib/lutaml/xml/schema/xsd/schema_path.rb', line 14

def url
  @url
end

Instance Method Details

#absolute_path?(path) ⇒ Boolean

Returns:

  • (Boolean)


93
94
95
96
# File 'lib/lutaml/xml/schema/xsd/schema_path.rb', line 93

def absolute_path?(path)
  path.to_s.start_with?(FORWARD_SLASH) ||
    path.to_s.match?(WINDOWS_ABSOLUTE_PATH_REGEX)
end

#absolute_url?(schema_location) ⇒ Boolean

Returns:

  • (Boolean)


98
99
100
101
102
# File 'lib/lutaml/xml/schema/xsd/schema_path.rb', line 98

def absolute_url?(schema_location)
  URI::DEFAULT_PARSER
    .make_regexp(URI_SCHEMES)
    .match?(schema_location.to_s)
end

#include_schema(schema_location) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/lutaml/xml/schema/xsd/schema_path.rb', line 34

def include_schema(schema_location)
  return unless schema_location

  resolved_location = resolve_schema_location(schema_location)
  if absolute_path?(resolved_location)
    return read_absolute_path(
      resolved_location,
      schema_location,
      mapped: resolved_location != schema_location,
    )
  end
  return http_get(resolved_location) if absolute_url?(resolved_location)

  schema_path = schema_location_path(resolved_location)
  if url?
    http_get(schema_path)
  else
    read_absolute_path(schema_path,
                       schema_location)
  end
end

#location?Boolean

Returns:

  • (Boolean)


30
31
32
# File 'lib/lutaml/xml/schema/xsd/schema_path.rb', line 30

def location?
  path? || url?
end

#location_for(schema_location) ⇒ Object



56
57
58
59
60
61
# File 'lib/lutaml/xml/schema/xsd/schema_path.rb', line 56

def location_for(schema_location)
  resolved_location = resolve_schema_location(schema_location)
  return resolved_location if absolute_path?(resolved_location) || absolute_url?(resolved_location)

  schema_location_path(resolved_location)
end

#path?Boolean

Returns:

  • (Boolean)


22
23
24
# File 'lib/lutaml/xml/schema/xsd/schema_path.rb', line 22

def path?
  !path.nil?
end

#relative_path?(schema_location) ⇒ Boolean

Returns:

  • (Boolean)


63
64
65
66
67
68
69
70
# File 'lib/lutaml/xml/schema/xsd/schema_path.rb', line 63

def relative_path?(schema_location)
  return false unless location? && schema_location

  resolved_location = resolve_schema_location(schema_location)
  return false if absolute_path?(resolved_location) || absolute_url?(resolved_location)

  File.exist?(schema_location_path(resolved_location))
end

#resolve_schema_location(schema_location) ⇒ Object



81
82
83
84
85
86
87
88
89
90
91
# File 'lib/lutaml/xml/schema/xsd/schema_path.rb', line 81

def resolve_schema_location(schema_location)
  return schema_location if schema_location.nil?
  return schema_location if schema_mappings.empty?

  schema_location_candidates(schema_location).each do |candidate|
    mapped = mapped_schema_location(candidate)
    return mapped if mapped
  end

  schema_location
end

#schema_location_path(schema_location) ⇒ Object



72
73
74
75
76
77
78
79
# File 'lib/lutaml/xml/schema/xsd/schema_path.rb', line 72

def schema_location_path(schema_location)
  if url?
    separator = FORWARD_SLASH unless url_separator_present?(schema_location)
    [url.to_s, schema_location].compact.join(separator)
  else
    File.join([path, schema_location].compact)
  end
end

#url?Boolean

Returns:

  • (Boolean)


26
27
28
# File 'lib/lutaml/xml/schema/xsd/schema_path.rb', line 26

def url?
  !url.nil?
end