Class: XmpToolkitRuby::XmpFile
- Inherits:
-
Object
- Object
- XmpToolkitRuby::XmpFile
- Defined in:
- lib/xmp_toolkit_ruby/xmp_file.rb
Overview
XmpFile provides a high-level Ruby interface for managing XMP metadata in files such as JPEG, TIFF, PNG, and PDF. It wraps the underlying Adobe XMP SDK calls, offering simplified methods to open, read, update, and write XMP packets, as well as to retrieve file and packet information.
Core Features
-
Open files for read or update, with optional fallback flags
-
Read raw and parsed XMP metadata
-
Update metadata by bulk XML or by individual property/schema
-
Write changes back to the file
-
Retrieve file-level info (format, handler flags, open flags)
-
Retrieve packet-level info (packet size, padding)
-
Support for localized text properties (alt-text arrays)
Instance Attribute Summary collapse
-
#fallback_flags ⇒ Integer?
readonly
Optional fallback flags if opening with primary flags fails.
-
#file_path ⇒ String
readonly
Path to the file on disk containing XMP metadata.
-
#open_flags ⇒ Integer
readonly
Flags used for the primary open operation.
Class Method Summary collapse
-
.register_namespace(namespace, suggested_prefix) ⇒ String
Register a custom namespace URI for subsequent property operations.
-
.with_xmp_file(file_path, open_flags: XmpFileOpenFlags::OPEN_FOR_READ, plugin_path: XmpToolkitRuby::PLUGINS_PATH, fallback_flags: nil, auto_terminate_toolkit: true) {|xmp_file| ... } ⇒ void
Open a file with XMP support, yielding a managed XmpFile instance.
Instance Method Summary collapse
-
#close ⇒ void
Close the file and clear internal state.
-
#file_info ⇒ Hash{String=>Object}
Retrieve a hash of file-level metadata and flags.
-
#initialize(file_path, open_flags: XmpFileOpenFlags::OPEN_FOR_READ, fallback_flags: nil) ⇒ XmpFile
constructor
Initialize an XmpFile for a given path.
-
#localized_property(schema_ns:, alt_text_name:, generic_lang:, specific_lang:) ⇒ String?
Retrieve a localized (alt-text) value from an XMP array.
-
#meta ⇒ Hash
Get parsed XMP metadata and packet boundaries.
-
#open ⇒ void
Open the file for XMP operations.
-
#open? ⇒ Boolean
Whether the file is currently open for XMP operations.
-
#packet_info ⇒ Hash
Retrieve low-level packet information (size, offset, padding).
-
#property(namespace, property) ⇒ String?
Retrieve the value of a simple XMP property.
-
#update_localized_property(schema_ns:, alt_text_name:, generic_lang:, specific_lang:, item_value:, options:) ⇒ void
Update an alternative-text (localized string) property.
-
#update_meta(xmp_data, mode: :upsert) ⇒ void
Bulk update XMP metadata using an RDF/XML string.
-
#update_property(namespace, property, value) ⇒ void
Update a single property in the XMP schema.
-
#write ⇒ void
Persist all pending XMP updates to the file.
Constructor Details
#initialize(file_path, open_flags: XmpFileOpenFlags::OPEN_FOR_READ, fallback_flags: nil) ⇒ XmpFile
Initialize an XmpFile for a given path.
101 102 103 104 105 106 107 108 109 |
# File 'lib/xmp_toolkit_ruby/xmp_file.rb', line 101 def initialize(file_path, open_flags: XmpFileOpenFlags::OPEN_FOR_READ, fallback_flags: nil) @file_path = file_path.to_s raise ArgumentError, "File path '#{@file_path}' must exist and be readable" unless File.readable?(@file_path) @open_flags = open_flags @fallback_flags = fallback_flags @open = false @xmp_wrapper = XmpWrapper.new end |
Instance Attribute Details
#fallback_flags ⇒ Integer? (readonly)
Optional fallback flags if opening with primary flags fails.
41 42 43 |
# File 'lib/xmp_toolkit_ruby/xmp_file.rb', line 41 def fallback_flags @fallback_flags end |
#file_path ⇒ String (readonly)
Path to the file on disk containing XMP metadata.
33 34 35 |
# File 'lib/xmp_toolkit_ruby/xmp_file.rb', line 33 def file_path @file_path end |
#open_flags ⇒ Integer (readonly)
Flags used for the primary open operation. See XmpFileOpenFlags.
37 38 39 |
# File 'lib/xmp_toolkit_ruby/xmp_file.rb', line 37 def open_flags @open_flags end |
Class Method Details
.register_namespace(namespace, suggested_prefix) ⇒ String
Register a custom namespace URI for subsequent property operations.
50 51 52 53 |
# File 'lib/xmp_toolkit_ruby/xmp_file.rb', line 50 def register_namespace(namespace, suggested_prefix) warn "XMP Toolkit not initialized; loading default plugins from \#{XmpToolkitRuby::PLUGINS_PATH}" unless XmpToolkitRuby::XmpToolkit.initialized? XmpWrapper.register_namespace(namespace, suggested_prefix) end |
.with_xmp_file(file_path, open_flags: XmpFileOpenFlags::OPEN_FOR_READ, plugin_path: XmpToolkitRuby::PLUGINS_PATH, fallback_flags: nil, auto_terminate_toolkit: true) {|xmp_file| ... } ⇒ void
This method returns an undefined value.
Open a file with XMP support, yielding a managed XmpFile instance. This method ensures the XMP toolkit is initialized and terminated, and that the file is closed and written (if modified).
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 |
# File 'lib/xmp_toolkit_ruby/xmp_file.rb', line 68 def with_xmp_file( file_path, open_flags: XmpFileOpenFlags::OPEN_FOR_READ, plugin_path: XmpToolkitRuby::PLUGINS_PATH, fallback_flags: nil, auto_terminate_toolkit: true ) XmpToolkitRuby.check_file!(file_path, need_to_read: true, need_to_write: XmpFileOpenFlags.contains?(open_flags, :open_for_update)) XmpToolkitRuby::XmpToolkit.initialize_xmp(plugin_path) unless XmpToolkitRuby.sdk_initialized? xmp_file = new(file_path, open_flags: open_flags, fallback_flags: fallback_flags) xmp_file.open yield xmp_file ensure xmp_file.write if xmp_file && XmpFileOpenFlags.contains?(xmp_file.open_flags, :open_for_update) xmp_file&.close XmpToolkitRuby::XmpToolkit.terminate if auto_terminate_toolkit && XmpToolkitRuby.sdk_initialized? end |
Instance Method Details
#close ⇒ void
This method returns an undefined value.
Close the file and clear internal state.
285 286 287 288 289 290 |
# File 'lib/xmp_toolkit_ruby/xmp_file.rb', line 285 def close return unless open? @open = false @xmp_wrapper.close end |
#file_info ⇒ Hash{String=>Object}
Retrieve a hash of file-level metadata and flags.
145 146 147 148 149 150 151 152 153 154 155 156 157 |
# File 'lib/xmp_toolkit_ruby/xmp_file.rb', line 145 def file_info @file_info ||= begin info = @xmp_wrapper.file_info { "handler_flags" => XmpToolkitRuby::XmpFileHandlerFlags.flags_for(info["handler_flags"]), "handler_flags_orig" => info["handler_flags"], "format" => XmpToolkitRuby::XmpFileFormat.name_for(info["format"]), "format_orig" => info["format"], "open_flags" => XmpToolkitRuby::XmpFileOpenFlags.flags_for(info["open_flags"]), "open_flags_orig" => info["open_flags"] } end end |
#localized_property(schema_ns:, alt_text_name:, generic_lang:, specific_lang:) ⇒ String?
Retrieve a localized (alt-text) value from an XMP array.
Locates the alt-text array identified by ‘alt_text_name` in the given `schema_ns`, then returns the string matching the requested generic and specific language codes.
251 252 253 254 255 256 257 258 259 260 |
# File 'lib/xmp_toolkit_ruby/xmp_file.rb', line 251 def localized_property(schema_ns:, alt_text_name:, generic_lang:, specific_lang:) open @xmp_wrapper.localized_property( schema_ns: schema_ns, alt_text_name: alt_text_name, generic_lang: generic_lang, specific_lang: specific_lang ) end |
#meta ⇒ Hash
Get parsed XMP metadata and packet boundaries.
rubocop:disable Metrics/AbcSize
174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 |
# File 'lib/xmp_toolkit_ruby/xmp_file.rb', line 174 def raw = @xmp_wrapper. return {} if raw.nil? || raw.empty? doc = Nokogiri::XML(raw) pis = doc.xpath("//processing-instruction('xpacket')") begin_pi = pis.detect { |pi| pi.content.start_with?("begin=") } attrs = begin_pi.content.scan(/(\w+)="([^"]*)"/).to_h pis.remove { "begin" => attrs["begin"], "packet_id" => attrs["id"], "xmp_data" => doc.root.to_xml, "xmp_data_orig" => raw } end |
#open ⇒ void
Emits warning if toolkit not initialized.
This method returns an undefined value.
Open the file for XMP operations. If initialization flags fail and fallback_flags is provided, attempts a second open with fallback flags.
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 |
# File 'lib/xmp_toolkit_ruby/xmp_file.rb', line 118 def open return if open? warn "XMP Toolkit not initialized; using default plugin path" unless XmpToolkitRuby::XmpToolkit.initialized? begin @xmp_wrapper.open(file_path, open_flags).tap { @open = true } rescue IOError => e @xmp_wrapper.close @open = false raise e unless fallback_flags @xmp_wrapper.open(file_path, fallback_flags).tap { @open = true } end end |
#open? ⇒ Boolean
Returns Whether the file is currently open for XMP operations.
135 136 137 |
# File 'lib/xmp_toolkit_ruby/xmp_file.rb', line 135 def open? @open end |
#packet_info ⇒ Hash
Retrieve low-level packet information (size, offset, padding).
162 163 164 |
# File 'lib/xmp_toolkit_ruby/xmp_file.rb', line 162 def packet_info @packet_info ||= @xmp_wrapper.packet_info end |
#property(namespace, property) ⇒ String?
Retrieve the value of a simple XMP property.
This will open the file (if not already open), query the underlying SDK for the given namespace + property, and return whatever value is stored.
234 235 236 237 |
# File 'lib/xmp_toolkit_ruby/xmp_file.rb', line 234 def property(namespace, property) open @xmp_wrapper.property(namespace, property) end |
#update_localized_property(schema_ns:, alt_text_name:, generic_lang:, specific_lang:, item_value:, options:) ⇒ void
This method returns an undefined value.
Update an alternative-text (localized string) property.
271 272 273 274 275 276 277 278 279 280 281 |
# File 'lib/xmp_toolkit_ruby/xmp_file.rb', line 271 def update_localized_property(schema_ns:, alt_text_name:, generic_lang:, specific_lang:, item_value:, options:) open @xmp_wrapper.update_localized_property( schema_ns: schema_ns, alt_text_name: alt_text_name, generic_lang: generic_lang, specific_lang: specific_lang, item_value: item_value, options: ) end |
#update_meta(xmp_data, mode: :upsert) ⇒ void
This method returns an undefined value.
Bulk update XMP metadata using an RDF/XML string.
209 210 211 212 |
# File 'lib/xmp_toolkit_ruby/xmp_file.rb', line 209 def (xmp_data, mode: :upsert) open @xmp_wrapper.(xmp_data, mode: mode) end |
#update_property(namespace, property, value) ⇒ void
This method returns an undefined value.
Update a single property in the XMP schema.
220 221 222 223 |
# File 'lib/xmp_toolkit_ruby/xmp_file.rb', line 220 def update_property(namespace, property, value) open @xmp_wrapper.update_property(namespace, property, value) end |
#write ⇒ void
This method returns an undefined value.
Persist all pending XMP updates to the file.
198 199 200 201 202 |
# File 'lib/xmp_toolkit_ruby/xmp_file.rb', line 198 def write raise "File not open; cannot write" unless open? @xmp_wrapper.write end |