-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathxmp.el
More file actions
3974 lines (3436 loc) · 154 KB
/
Copy pathxmp.el
File metadata and controls
3974 lines (3436 loc) · 154 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
;;; xmp.el --- -*- lexical-binding: t; -*-
;; Copyright (C) 2024 AKIYAMA Kouhei
;; Author: AKIYAMA Kouhei <misohena@gmail.com>
;; Keywords: Files, Metadata, XMP, XML
;; This program is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.
;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;; You should have received a copy of the GNU General Public License
;; along with this program. If not, see <https://www.gnu.org/licenses/>.
;;; Commentary:
;; Get properties from file:
;;
;; (xmp-file-get-properties "test/xmp-test-value-types.xmp" 'all nil t)
;;
;; (xmp-file-get-properties "test/xmp-test-uzumaki.jpg" 'all)
;;
;; (xmp-file-get-properties "XMPSpecificationPart1.pdf" 'all)
;;
;; (xmp-file-get-properties "test/xmp-test-uzumaki.jpg"
;; (list (xmp-xml-ename xmp-xmp: "Rating")
;; (xmp-xml-ename xmp-dc: "title")))
;;
;; (xmp-pvalue-as-text
;; (xmp-file-get-property "test/xmp-test-uzumaki.jpg"
;; (xmp-xml-ename xmp-xmp: "Rating")))
;; Set properties to file:
;;
;; (xmp-file-set-properties "tmp-example.xmp"
;; (list
;; (cons xmp-xmp:Rating "5")
;; (cons xmp-dc:title
;; (xmp-pvalue-make-alt
;; (list
;; (xmp-pvalue-make-text
;; "Test Title"
;; (list (xmp-pvalue-make-named xmp-xml:lang 'text "x-default")))
;; (xmp-pvalue-make-text
;; "Test Title"
;; (list (xmp-pvalue-make-named xmp-xml:lang 'text "en")))
;; (xmp-pvalue-make-text
;; "テストタイトル"
;; (list (xmp-pvalue-make-named xmp-xml:lang 'text "ja"))))))))
;;
;; (xmp-file-set-property "tmp-example.xmp" xmp-xmp:Rating "3")
;;
;;;; References:
;; XMP1 : Extensible Metadata Platform (XMP) Specification Part1 [April, 2012]
;; https://developer.adobe.com/xmp/docs/XMPSpecifications/
;; XMP2 : Extensible Metadata Platform (XMP) Specification Part2 [Feb, 2022]
;; https://developer.adobe.com/xmp/docs/XMPSpecifications/
;; XMP3 : Extensible Metadata Platform (XMP) Specification Part3 [Jan, 2020]
;; https://developer.adobe.com/xmp/docs/XMPSpecifications/
;;; Code:
(require 'cl-lib)
(require 'xmp-xml)
(require 'xmp-file-reader)
;;;; Declarations
(autoload 'xmp-exif-read-exif-as-xmp-property-elements-from-bytes "xmp-exif")
(autoload 'xmp-exif-read-xmp-xml-from-tiff-file "xmp-exif")
(autoload 'xmp-pdf-read-metadata "xmp-pdf")
(autoload 'xmp-id3-read-file-as-xmp-dom "xmp-file-dynamic-media")
(autoload 'xmp-isobmff-read-xmp-dom "xmp-file-dynamic-media")
(autoload 'xmp-sqlite-cache-db-make-file-entry "xmp-sqlite")
(autoload 'xmp-sqlite-cache-db-get-file-entry "xmp-sqlite")
(autoload 'xmp-sqlite-cache-db-remove-file-entry "xmp-sqlite")
(autoload 'xmp-sqlite-cache-db-remove-file-entries-in-dir "xmp-sqlite")
(autoload 'xmp-sqlite-cache-db-remove-invalid-file-entries "xmp-sqlite")
(autoload 'xmp-sqlite-cache-db-remove-invalid-file-entries-in-dir "xmp-sqlite")
(autoload 'xmp-sqlite-cache-db-remove-dir-entries "xmp-sqlite")
(autoload 'xmp-sqlite-cache-db-clear "xmp-sqlite")
(autoload 'xmp-sqlite-cache-db-get-files-in-dir "xmp-sqlite")
(autoload 'xmp-sqlite-mod-db-set-file-properties "xmp-sqlite")
(autoload 'xmp-sqlite-mod-db-get-file-properties-info "xmp-sqlite")
(autoload 'xmp-sqlite-mod-db-get-file-properties "xmp-sqlite")
(autoload 'xmp-sqlite-mod-db-remove-file-properties "xmp-sqlite")
(autoload 'xmp-sqlite-mod-db-remove-file-properties-all "xmp-sqlite")
(autoload 'xmp-sqlite-mod-db-get-files-in-dir "xmp-sqlite")
(autoload 'xmp-sqlite-mod-db-get-stray-files "xmp-sqlite")
(autoload 'xmp-sqlite-mod-db-get-stray-files-in-dir "xmp-sqlite")
(autoload 'xmp-sqlite-mod-db-get-stray-files-under-dir "xmp-sqlite")
;;;; Message Text Translation
(defun xmp-msg (str)
"Process message text STR for users.
The current implementation returns STR as is. If you want to translate
the message text into other languages, please rewrite this function."
str)
(defun xmp-msg-n (str)
"Return STR as is.
This function is intended to be used as a marker for detecting message
text from source code."
str)
;;;; Customize
(defgroup xmp nil
"Extensible Metadata Platform."
:tag "XMP"
:prefix "xmp-"
:group 'files)
;;;; Names
;;;;; Namespace Information
;; The namespace defined here enables processing using namespaces as
;; shown below.
;; Using constants to refer to namespace names:
;; xmp-xmp:
;; => :http://ns.adobe.com/xap/1.0/
;; Namespace namespace name to prefix conversion:
;; (xmp-xml-default-ns-prefix "http://ns.adobe.com/xap/1.0/")
;; => "xmp"
;; (xmp-xml-default-ns-prefix (xmp-xml-ns-name "http://ns.adobe.com/xap/1.0/"))
;; => "xmp"
;; Namespace prefix to namespace name conversion:
;; (xmp-xml-default-ns-prefix-to-ns-name "xmp")
;; => :http://ns.adobe.com/xap/1.0/
;; Parsing a property name string with a prefix:
;; (xmp-xml-ename-from-prefixed-string "xmp:Rating")
;; => (:http://ns.adobe.com/xap/1.0/ . "Rating")
;; Converting a property expanded name to a string with a prefix:
;; (xmp-xml-ename-string xmp-xmp:Rating)
;; => "xmp:Rating"
;;;;;; Predefined Namespaces
(eval-and-compile
(defconst xmp-predefined-namespaces
;; (<namespace name string> . (<prefix> ...more info))
'(("elxmp://el-xmp/xmlns/"
"elxmp")
("adobe:ns:meta/"
"x") ;;[XMP1 7.3.3]
("http://www.w3.org/1999/02/22-rdf-syntax-ns#"
"rdf") ;;[XMP1 6.2]
("http://purl.org/dc/elements/1.1/"
"dc") ;;[XMP1 8.3]
("http://ns.adobe.com/xap/1.0/"
"xmp") ;;[XMP1 8.4] [XMP2 2.1]
("http://ns.adobe.com/xap/1.0/rights/"
"xmpRights") ;;[XMP1 8.5]
("http://ns.adobe.com/xap/1.0/mm/"
"xmpMM") ;;[XMP1 8.6] [XMP2 2.2]
("http://ns.adobe.com/xmp/Identifier/qual/1.0/"
"xmpidq") ;;[XMP1 8.7]
("http://ns.adobe.com/xap/1.0/bj/"
"xmpBJ") ;;[XMP2 2.3]
("http://ns.adobe.com/xap/1.0/t/pg/"
"xmpTPg") ;;[XMP2 2.4]
("http://ns.adobe.com/xmp/1.0/DynamicMedia/"
"xmpDM") ;;[XMP2 1.2.6] [XMP2 2.5]
("http://ns.adobe.com/pdf/1.3/"
"pdf") ;;[XMP2 3.1]
("http://ns.adobe.com/photoshop/1.0/"
"photoshop") ;;[XMP2 3.2]
("http://ns.adobe.com/camera-raw-settings/1.0/"
"crs") ;;[XMP2 3.3]
("http://ns.adobe.com/exif/1.0/"
"exif") ;;[XMP2 3.4]
("http://ns.adobe.com/xap/1.0/g/"
"xmpG") ;;[XMP2 1.2.2.1]
("http://ns.adobe.com/xap/1.0/sType/Dimensions#"
"stDim") ;;[XMP2 1.2.2.2]
("http:ns.adobe.com/xap/1.0/sType/Font#"
"stFnt") ;;[XMP2 1.2.2.3]
("http://ns.adobe.com/xap/1.0/g/img/"
"xmpGImg") ;;[XMP2 1.2.2.4]
("http://ns.adobe.com/xap/1.0/sType/ResourceEvent#"
"stEvt") ;;[XMP2 1.2.4]
("http://ns.adobe.com/xap/1.0/sType/ResourceRef#"
"stRef") ;;[XMP1 8.2.2.9] [XMP2 1.2.4.1]
("http://ns.adobe.com/xap/1.0/sType/Version#"
"stVer") ;;[XMP2 1.2.4.2]
("http://ns.adobe.com/xap/1.0/sType/Job#"
"stJob") ;;[XMP2 1.2.5.1]
("http://cipa.jp/exif/1.0/"
"exifEX") ;;[CIPA DC-010-2024 5.1]
("http://ns.adobe.com/tiff/1.0/"
"tiff") ;;https://developer.adobe.com/xmp/docs/XMPNamespaces/tiff/
("http://iptc.org/std/Iptc4xmpCore/1.0/xmlns/"
"Iptc4xmpCore") ;; https://developer.adobe.com/xmp/docs/XMPNamespaces/Iptc4xmpCore/
))
;; Register ns-name-prefix-alist to xmp-xml.el
(xmp-xml-register-ns-name-prefix-group
'xmp-predefined
(mapcar (lambda (ns-info)
(cons (xmp-xml-ns-name (nth 0 ns-info)) (nth 1 ns-info)))
xmp-predefined-namespaces)
0)
;; (xmp-xml-default-ns-prefix :http://ns.adobe.com/xap/1.0/) => "xmp"
;; (xmp-xml-default-ns-prefix "http://ns.adobe.com/xap/1.0/") => "xmp"
)
;; Define namespace name variables
;; (defconst xmp-<ns-prefix>: <ns-name>)
;; (defconst xmp-x: ...)
;; (defconst xmp-xmp: ...)
;; (defconst xmp-rdf: ...)
;; etc.
(defmacro xmp-define-ns-const ()
`(progn
,@(cl-loop for (ns-name . ns-info) in xmp-predefined-namespaces
for ns-prefix = (car ns-info)
collect
`(defconst ,(intern (format "xmp-%s:" ns-prefix))
(xmp-xml-ns-name ,ns-name)
;; TODO: Generate docstring
))))
(xmp-define-ns-const)
;; Define predefined namespace prefix alist variable
;; (defconst xmp-predefined-ns-name-prefix-alist
;; '((:adobe:ns:meta/ . "x")
;; (:http://www.w3.org/1999/02/22-rdf-syntax-ns\# . "rdf") ...))
(defmacro xmp-define-ns-namespaces-const ()
`(defconst xmp-predefined-ns-name-prefix-alist
(list
,@(cl-loop for (ns-name . ns-info) in xmp-predefined-namespaces
for ns-prefix = (car ns-info)
collect `(cons (xmp-xml-ns-name ,ns-name) ,ns-prefix)))))
(xmp-define-ns-namespaces-const)
;;;;;; User Defined Namespaces
(defvar xmp-user-defined-namespaces)
(defun xmp-user-defined-namespaces-update ()
;; Register ns-name-prefix-alist to xmp-xml.el
(xmp-xml-register-ns-name-prefix-group
'xmp-user
(mapcar (lambda (ns-info)
(cons (xmp-xml-ns-name (nth 0 ns-info)) (nth 1 ns-info)))
xmp-user-defined-namespaces)
-1))
(defcustom xmp-user-defined-namespaces nil
"A list of namespaces added by user.
Specify namespaces that are not included in `xmp-predefined-namespaces'."
:group 'xmp
:type '(repeat (list (string :tag "Namespace Name (URI)")
(string :tag "Default Prefix")))
:set (lambda (var val)
(set var val)
;; Update prefix alist
(xmp-user-defined-namespaces-update)))
;;;;; Predefined Element and Attribute Names
(eval-and-compile
(defconst xmp-predefined-names
;; ( (<namespace name string> . ( (<local-name> ...more info)... ) )... )
'(("x"
("xmpmeta") ;; Element
("xmptk")) ;; Attribute
("rdf"
("RDF")
("Description")
("Bag")
("Seq")
("Alt")
("li")
("value") ;; Element or Attribute
("type") ;; Element or Attribute
("about") ;; Attribute
("resource")
("parseType")
("datatype")
("ID")
("nodeID")))))
;; Define expanded name variables for elements/attributes
;; (defconst xmp-x:metadata ...)
;; (defconst xmp-rdf:RDF ...)
;; etc.
(defmacro xmp-define-predefined-names ()
`(xmp-define-predefined-names-1 ,xmp-predefined-names))
(defmacro xmp-define-predefined-names-1 (predefined-names)
`(progn
,@(cl-loop for (ns-prefix . name-info-list) in predefined-names
nconc
(cl-loop for name-info in name-info-list
for name = (car name-info)
for var = (intern (format "xmp-%s:%s" ns-prefix name))
for ns-var = (intern (format "xmp-%s:" ns-prefix))
collect
`(defconst ,var (xmp-xml-ename ,ns-var ,name))))))
(xmp-define-predefined-names)
(defconst xmp-names:lang-ID-nodeID (list xmp-xml:lang
xmp-rdf:ID
xmp-rdf:nodeID))
;;;;; Property Type Information
(defconst xmp-predefined-property-types
'(Text URI Boolean Real Integer GUID Date AgentName RenditionClass
ResourceRef MIMEType LangAlt
BagText BagProperName BagLocale BagDate
SeqText SeqProperName SeqLocale SeqDate))
(defun xmp-property-type-derived-from-text-p (type)
(memq type '(Text URI Real Integer AgentName GUID MIMEType Boolean Date)))
(defun xmp-property-type-derived-from-bag-text-p (type)
(memq type '(BagText BagProperName BagLocale BagDate)))
(defun xmp-property-type-derived-from-seq-text-p (type)
(memq type '(SeqText SeqProperName SeqLocale SeqDate)))
;;;;; Property Information
;;;;;; Predefined Properties
(eval-and-compile
(defconst xmp-predefined-properties
;; ( (<namespace prefix string> . ( (<local-name> ...more info)... ) )... )
'(;; [XMP1 8.2.2.9 ResourceRef]
("stRef"
("documentID" GUID)
("filePath" URI)
("instanceID" GUID)
("renditionClass" RenditionClass)
("renditionParams" Text))
;; [XMP1 8.3 Dublin Core namespace]
("dc"
("contributor" BagProperName)
("coverage" Text)
("creator" SeqProperName)
("date" SeqDate)
("description" LangAlt)
("format" MIMEType)
("identifier" Text)
("language" BagLocale)
("publisher" BagProperName)
("relation" BagText)
("rights" LangAlt)
("source" Text)
("subject" BagText)
("title" LangAlt)
("type" BagText))
;; [XMP1 8.4 XMP namespace]
("xmp"
("CreateDate" Date)
("CreatorTool" AgentName)
("Identifier" BagText)
("Label" Text)
("MetadataDate" Date)
("ModifyDate" Date)
("Rating" Real))
;; [XMP1 8.5 XMP Rights Management namespace]
("xmpRights"
("Certificate" Text)
("Marked" Boolean)
("Owner" BagProperName)
("UsageTerms" LangAlt)
("WebStatement" Text))
;; [XMP1 8.6 XMP Media Management namespace]
("xmpMM"
("DerivedFrom" ResourceRef)
("DocumentID" GUID)
("InstanceID" GUID)
("OriginalDocumentID" GUID)
("RenditionClass" RenditionClass)
("RenditionParams" Text))
;; [XMP1 8.7 xmpidq namespace]
("xmpidq"
("Scheme" Text))
;; Exif
("exif"
("DateTimeOriginal" Date)
("DateTimeDigitized" Date))
;; XMP Dynamic Media (xmp-file-dynamic-media.el)
("xmpDM"
("artist" Text)
("album" Text)
("logComment" Text)
("trackNumber" Integer)
("genre" Text)
("composer" Text)
("engineer" Text)
("discNumber" Text))
)))
;; Define expanded name variables for properties
;; (defconst xmp-xmp:Rating ...)
;; (defconst xmp-dc:title ...)
;; etc.
(defmacro xmp-define-predefined-properties ()
`(xmp-define-predefined-names-1 ,xmp-predefined-properties))
(xmp-define-predefined-properties)
;;;;;; User Defined Properties
(defcustom xmp-user-defined-properties nil
"A list of XMP properties added by user.
Specify properties that are not included in `xmp-predefined-properties'.
When adding a namespace to this variable, register the namespace
information in `xmp-user-defined-namespaces'."
:group 'xmp
:type `(alist
:tag "Namespaces and Properties Alist"
:key-type (string :tag "Namespace Prefix")
:value-type (repeat
:tag "Properties"
(list
(string :tag "Name")
(choice :tag "Type"
symbol
,@(mapcar
(lambda (s) `(const ,s))
xmp-predefined-property-types))))))
;;;;;; Defined Property Information
(defun xmp-defined-property-type--get (ename prop-info-alist)
"Get the type of property ENAME from PROP-INFO-ALIST."
(when-let* ((ns-prefix-str (xmp-xml-default-ns-prefix
(xmp-xml-ename-ns ename)))
(ns-info (assoc ns-prefix-str
prop-info-alist
#'string=))
(prop-info (assoc (xmp-xml-ename-local ename)
(cdr ns-info)
#'string=)))
(nth 1 prop-info)))
(defun xmp-defined-property-type (ename)
"Return a symbol that represents the type of the property ENAME."
(or (xmp-defined-property-type--get ename xmp-user-defined-properties)
(xmp-defined-property-type--get ename xmp-predefined-properties)))
;; TEST: (xmp-defined-property-type xmp-xmp:Rating) => Real
(defun xmp-defined-property-prefixed-name-list--make (prop-info-alist)
"Create a list of property name strings with namespace prefixes from
PROP-INFO-ALIST."
(cl-loop for (ns-prefix-str . prop-info-list) in prop-info-alist
nconc (cl-loop for prop-info in prop-info-list
collect (if ns-prefix-str
(concat ns-prefix-str ":" (car prop-info))
(car prop-info)))))
(defun xmp-defined-property-prefixed-name-list ()
"Return the property name string with namespace prefix for all XMP
properties defined in this library."
(nconc
(xmp-defined-property-prefixed-name-list--make xmp-user-defined-properties)
(xmp-defined-property-prefixed-name-list--make xmp-predefined-properties)))
;; EXAMPLE: (xmp-defined-property-prefixed-name-list)
(defvar xmp-read-property-prefixed-name--hist nil)
(defun xmp-read-property-prefixed-name (prompt)
"Read a property name with a namespace prefix from the minibuffer."
(let* ((defined-names (xmp-defined-property-prefixed-name-list))
(candidate-names
;; Merge defined names and previously entered names.
(nconc
(seq-difference xmp-read-property-prefixed-name--hist defined-names)
defined-names)))
(completing-read prompt candidate-names
nil nil nil
'xmp-read-property-prefixed-name--hist)))
;; EXAMPLE: (xmp-read-property-prefixed-name "Property: ")
(defun xmp-read-property-ename (prompt &optional default)
"Read an expanded name of a property from the minibuffer."
(let ((str (xmp-read-property-prefixed-name prompt)))
(if (string-empty-p str)
default
(xmp-xml-ename-from-prefixed-string str))))
;; EXAMPLE: (xmp-read-property-ename "Property: ")
;;;; DOM manipulation
;;;;; DOM Tree Creation
(defun xmp-empty-dom ()
"Create an empty XMP XML DOM with no properties.
The root node is an x:xmpmeta element that contains one child rdf:RDF
element that contains an empty rdf:Description element."
;; <x:xmpmeta>
(xmp-xml-element
xmp-x:xmpmeta
nil
;; <rdf:RDF>
(list
(xmp-xml-element
xmp-rdf:RDF
nil
;; <rdf:Description>
(list
(xmp-empty-top-description))))))
;; EXAMPLE: (xmp-xml-print nil (xmp-empty-dom) xmp-default-ns-name-prefix-alist)
;; EXAMPLE: (xmp-xml-collect-used-ns (xmp-empty-dom))
(defun xmp-empty-top-description (&optional about)
"Create an empty top-level rdf:Description element.
The element is intended to be added to an rdf:RDF element.
The element has an about attribute whose value is ABOUT. If omitted, it
defaults to the empty string\"\"."
(xmp-xml-element xmp-rdf:Description
(list
(xmp-xml-attr xmp-rdf:about (or about "")))))
;;;;; RDF Element (XMP DOM)
(defun xmp-find-rdf (dom)
"Return rdf:RDF element in DOM.
If DOM is an x:xmpmeta element, return the first rdf:RDF element among
its children. If DOM is itself an rdf:RDF element, return it as is."
(cond
;; Unwrap <x:xmpmeta> ... <rdf:RDF>...</rdf:RDF> ... </x:xmpmeta>
((xmp-xml-element-enamed-p dom xmp-x:xmpmeta)
(xmp-xml-element-child-find-by-ename dom xmp-rdf:RDF))
;; <rdf:RDF> ... </rdf:RDF>
((xmp-xml-element-enamed-p dom xmp-rdf:RDF)
dom)))
(defun xmp-find-description (dom &optional prop-ename about)
"Return an top-level rdf:Description element from DOM that has a
property named PROP-ENAME.
The property specified by PROP-ENAME is searched from both attributes
and child elements. If PROP-ENAME is nil, return the first top-level
rdf:Description element.
ABOUT is a string that matches the about attribute of rdf:Description
elements. If omitted, it is the empty string."
(when-let ((rdf (xmp-find-rdf dom)))
(seq-find (lambda (node)
(and
(xmp-target-description-p node about)
(or
(null prop-ename)
;; From the DESC's attributes
;; (simple, non-URI, unqualified values)
;; [XMP1 7.9.2.2]
(xmp-xml-element-attr-find-by-ename node prop-ename)
;; From the DESC's children
;; [XMP1 C.2.5]
(xmp-xml-element-child-find-by-ename node prop-ename))))
(xmp-xml-element-children rdf))))
(defun xmp-remove-all-descriptions (dom &optional about)
"Remove all top-level Description elements in the DOM that match ABOUT."
(when-let ((rdf (xmp-find-rdf dom)))
(xmp-xml-element-child-remove-if
rdf
(lambda (child-node)
(xmp-target-description-p child-node about)))
(null (xmp-xml-element-children rdf))))
(defun xmp-get-properties (dom prop-ename-list &optional about noerror)
"Return a list of XMP properties contained in DOM.
Each element in the returned list is a cons cells whose car is the
expanded name of the property and whose cdr is the pvalue (the parsed
value: as returned by `xmp-parse-property-element').
PROP-ENAME-LIST is a list of the expanded names of the properties to
retrieve, or the symbol `all'. nil means to retrieve none. The symbol
`all' means to enumerate all properties. The order or duplication of
properties in the list does not affect the value returned.
ABOUT specifies a string that matches the about attribute of Description
elements. Get properties only from matching Description
elements. Specifying nil is the same as specifying an empty string.
If NOERROR is non-nil, then nil is returned if an error occurs, whenever
possible."
(cl-loop for prop-elt in (xmp-get-property-elements dom prop-ename-list
about)
for pvalue = (xmp-parse-property-element prop-elt noerror)
when pvalue ;; <ns:Prop /> => nil or parse error (noerror is non-nil)
collect pvalue))
(defun xmp-get-property-elements (dom prop-ename-list &optional about)
"Return a list of elements that represent the XMP properties contained in
DOM.
The elements of the list are child elements of the top-level
Descriptions (directly under the RDF element), except that if properties
are expressed in the form of attribute values of top-level Description
elements (property attributes), a temporary property element is created
and returned.
PROP-ENAME-LIST is a list of the expanded names of the properties to
retrieve or the symbol `all'. nil means to retrieve none. The symbol
`all' means to enumerate all properties. The order or duplication of
properties in the list does not affect the value returned.
ABOUT specifies a string that matches the about attribute of Description
elements. Get properties only from matching Description
elements. Specifying nil is the same as specifying an empty string."
(when prop-ename-list
(when-let ((rdf (xmp-find-rdf dom)))
;; At the top level there are only Description elements.
;; [XMP1 C.2.3]
(cl-loop for desc in (xmp-xml-element-children rdf)
when (xmp-target-description-p desc about)
nconc (xmp-desc-get-property-elements desc prop-ename-list)))))
(defun xmp-get-property-element (dom prop-ename &optional about)
"Return the property element with the name PROP-ENAME in DOM, or nil
if none.
ABOUT specifies a string that matches the about attribute of Description
elements. Get property only from matching Description
elements. Specifying nil is the same as specifying an empty string."
(when-let ((rdf (xmp-find-rdf dom)))
(seq-some (lambda (desc)
(when (xmp-target-description-p desc about)
(xmp-desc-get-property-element desc prop-ename)))
(xmp-xml-element-children rdf))))
(defun xmp-set-property-value (dom prop-ename value &optional about)
"Add a property element to DOM.
The property element to be added is created from PROP-ENAME and VALUE
using the `xmp-property-element-from' function.
Any existing property (whether attribute or element) with the same
property name will be deleted."
(let ((desc (or
(xmp-find-description dom prop-ename about)
(xmp-find-description dom nil about)
(xmp-xml-element-insert-last
dom
(xmp-empty-top-description about)))))
(xmp-desc-set-property-value desc prop-ename value)))
(defun xmp-set-property-elements-if-not-exists (dom prop-elem-list
&optional about)
"Insert the property elements specified by PROP-ELEM-LIST into the
DOM. However, if a property with the same name already exists in the
DOM, do not insert it and discard it."
(when prop-elem-list
(when-let ((rdf (xmp-find-rdf dom)))
;; Prepare for `cl-delete-if' and `xmp-xml-element-nconc-last'
(setq prop-elem-list (copy-sequence prop-elem-list))
;; Remove property elements from PROP-ELEM-LIST that are already in DOM
(let ((children (xmp-xml-element-children rdf)))
;; For each rdf:Description
(while (and children prop-elem-list)
(let ((desc (pop children)))
(when (xmp-target-description-p desc about)
;; For each properties
(xmp-desc-some-property-elements
desc
(lambda (prop-ename _place _elem _attr)
;; Remove elements with same property name from PROP-ELEM-LIST
(setq prop-elem-list
(cl-delete-if
(lambda (elem2)
(xmp-xml-ename-equal (xmp-xml-element-ename elem2)
prop-ename))
prop-elem-list))
;; Stop if PROP-ELEM-LIST is empty
(null prop-elem-list))
'all)))))
;; Insert property elements
(let ((desc (or (xmp-find-description dom nil about)
(xmp-xml-element-insert-last
dom
(xmp-empty-top-description about)))))
(xmp-xml-element-nconc-last desc prop-elem-list)
;; Insert namespace declarations that are used and do not exist
(let (used-ns-list nsdecls)
;; Collect used namespace names from PROP-ELEM-LIST
(dolist (prop-elem prop-elem-list)
(setq used-ns-list
(xmp-xml-collect-used-ns prop-elem used-ns-list)))
;; Collect namespace declarations from the root to the desc element
(setq nsdecls
(nconc
(xmp-xml-collect-nsdecls-on-node desc)
(xmp-xml-collect-nsdecls-on-node rdf)
(unless (eq dom rdf)
(xmp-xml-collect-nsdecls-on-node dom))))
;; Insert missing namespace declarations
(dolist (ns-name used-ns-list)
(unless (seq-some (lambda (nsdecl) (equal (car nsdecl) ns-name))
nsdecls)
(when-let ((prefix (xmp-xml-default-ns-prefix ns-name)))
(xmp-xml-insert-nsdecl dom ns-name prefix)))))))))
(defun xmp-dump-properties (stream dom &optional about sort)
"Output XMP properties contained in DOM to STREAM in a
user-friendly format.
ABOUT specifies a string that matches the about attribute of Description
elements. Get properties only from matching Description
elements. Specifying nil is the same as specifying an empty string."
(let ((props (xmp-get-properties dom 'all about t)))
(when sort
(setq props (xmp-xml-ename-alist-sort props)))
(xmp-dump-named-pvalue-list stream props (xmp-xml-collect-nsdecls dom) 0)))
;; EXAMPLE: (xmp-dump-properties nil (xmp-file-read-rdf "test/xmp-test-uzumaki.jpg") nil t)
;;;;; Description Element
(defun xmp-target-description-p (node about)
"Return non-nil if NODE is a rdf:Description element to be processed.
ABOUT specifies a string that matches the about attribute."
(and (xmp-xml-element-enamed-p node xmp-rdf:Description)
(equal
;; If there is no "about" attribute, treat it as an
;; empty string. [XMP1 7.4][XMP1 C.2.4]
(or (xmp-xml-element-attr-value node xmp-rdf:about) "")
(or about ""))))
(defun xmp-desc-get-property-elements (desc prop-ename-list)
"Return the list of XMP property elements present within the
rdf:Description element DESC.
PROP-ENAME-LIST is a list of the expanded names of the properties to
retrieve or the symbol `all'. nil means to retrieve none. The symbol
`all' means to enumerate all properties. The order or duplication of
properties in the list does not affect the value returned.
If the properties are expressed in the form of attribute values of the
Description element (property attributes), a temporary property element
will be created and returned."
(when prop-ename-list
(let (result)
(xmp-desc-some-property-elements
desc
(lambda (_prop-ename place elem attr)
(cond
((eq place 'attribute)
;; Convert attribute to element
(push (xmp-property-element-from-attr attr) result))
((eq place 'element)
(push elem result)))
;; Continue
nil)
prop-ename-list)
(nreverse result))))
(defun xmp-desc-some-property-elements (desc predicate prop-ename-list)
"Call PREDICATE for each XMP property that element DESC has, and when a
non-nil value is returned, exit and return that value.
PREDICATE is passed four arguments: PROP-ENAME, PLACE, ELEMENT, and
ATTRIBUTE.
PROP-ENAME is the property name (expanded name).
PLACE represents where the property resides and is either the symbol
`attribute' or the symbol `element'.
ELEMENT is the element in which the property resides. If PLACE is
`attribute', it is DESC. If PLACE is `element', it is the property
element that represents the property.
ATTRIBUTE is an attribute object that represents a property when PLACE
is `attribute', or nil when PLACE is `element'.
PROP-ENAME-LIST limits the properties to be processed. When it is the
symbol all, all properties are targeted. When it is a list of property
names, the properties included in the list are targeted."
(or
;; Enumerate from the DESC's attributes
;; (simple, non-URI, unqualified values)
;; [XMP1 7.9.2.2]
(let ((attrs (xmp-xml-element-attributes desc))
result)
(while (and attrs (null result))
(let ((attr (pop attrs)))
(when (and
;; Exclude nsdecl (xmlns= and xmlns:??=)
(not (xmp-xml-attr-nsdecl-p attr))
;; Exclude non-property attributes
;; [XMP1 C.2.4]
(not (member (xmp-xml-ename-ns (xmp-xml-attr-ename attr))
;; rdf:*, xml:* : Maybe excessive?
;; xmlns:* is namespace declaration
(list xmp-rdf: xmp-xml:)))
;; Filter by prop-ename-list
(or (eq prop-ename-list 'all)
(xmp-xml-ename-member (xmp-xml-attr-ename attr)
prop-ename-list)))
(setq result (funcall predicate
(xmp-xml-attr-ename attr)
'attribute desc attr)))))
result)
;; Enumerate from the DESC's children
;; [XMP1 C.2.5]
(let ((children (xmp-xml-element-children desc))
result)
(while (and children (null result))
(let ((child (pop children)))
(when (and
(xmp-xml-element-p child)
;; Filter by prop-ename-list
(or (eq prop-ename-list 'all)
(xmp-xml-ename-member (xmp-xml-element-ename child)
prop-ename-list)))
(setq result (funcall predicate
(xmp-xml-element-ename child)
'element child nil)))))
result)))
(defun xmp-desc-get-property-element (desc prop-ename)
(or
;; Get from the DESC's attributes
;; (simple, non-URI, unqualified values)
;; [XMP1 7.9.2.2]
(when-let ((attr (xmp-xml-element-attr-find-by-ename
desc prop-ename)))
(xmp-property-element-from-attr attr))
;; Get from the DESC's children
;; [XMP1 C.2.5]
(xmp-xml-element-child-find-by-ename desc prop-ename)))
(defun xmp-desc-remove-property (desc prop-ename)
"Remove the property named PROP-ENAME from the Description element DESC.
The property will be removed whether it is expressed as an attribute or
a child element."
(xmp-xml-element-attr-remove-by-ename desc prop-ename)
(xmp-xml-element-child-remove-by-ename desc prop-ename))
(defun xmp-desc-set-property-element (desc prop-elem)
"Add the property element PROP-ELEM to the Description element DESC.
Any existing property (whether attribute or element) with the same
property name will be deleted."
(xmp-desc-remove-property desc (xmp-xml-element-ename prop-elem))
(xmp-xml-element-insert-last desc prop-elem))
(defun xmp-desc-set-property-value (desc prop-ename value)
"Add a property element to the Description element DESC.
The property element to be added is created from PROP-ENAME and VALUE
using the `xmp-property-element-from' function.
Any existing property (whether attribute or element) with the same
property name will be deleted."
(xmp-desc-set-property-element desc
(xmp-property-element-from prop-ename value)))
;;;; Parse Property Element (Property Element to PValue)
;; Property elements appear as children of nodeElement (Description,
;; array, and typed nodes).
;;
;; <rdf:Description> <ns:Prop>(<-propertyElt)
;; <rdf:Description> <rdf:value>(<-propertyElt)
;; <rdf:Description> <ns:Qual>(<-propertyElt)
;; <rdf:Description> <ns:Field>(<-propertyElt)
;; <rdf:Bag> <rdf:li>(<-propertyElt)
;; <rdf:Seq> <rdf:li>(<-propertyElt)
;; <rdf:Alt> <rdf:li>(<-propertyElt)
;; <ns:Type> propertyElt...
;; [XMP1 C.2.5] propertyElt ::
;; - resourcePropertyElt : Struct, Array, Qualified
;; - literalPropertyElt : Simple
;; - parseTypeResourcePropertyElt : <ns:Prop parseType="Resource">
;; - parseTypeLiteralPropertyElt : Not allowed
;; - parseTypeCollectionPropertyElt : Not allowed
;; - parseTypeOtherPropertyElt : Not allowed
;; - emptyPropertyElt : Simple, Struct
(defun xmp-property-element-type (elem)
;; [XMP1 C.2.5]
(cond
;; If there are more than three attributes (counting xml:lang),
;; this is an emptyPropertyElt.
((>= (xmp-xml-element-attributes-count-without-nsdecl elem) 3)
'empty)
;; Look for an attribute that is not xml:lang or rdf:ID.
;; If none is found, look at the XML content of the propertyElt.
((not (seq-find (lambda (attr)
(not (or (xmp-xml-attr-nsdecl-p attr)
(xmp-xml-attr-enamed-p attr xmp-xml:lang)
(xmp-xml-attr-enamed-p attr xmp-rdf:ID))))
(xmp-xml-element-attributes elem)))
(cond
;; If there is no content, this is an emptyPropertyElt.
((null (xmp-xml-element-children elem))
'empty)
;; If the only content is character data, this is a literalPropertyElt.
((seq-every-p #'xmp-xml-text-node-p (xmp-xml-element-children elem))
'literal)
;; Otherwise this is a resourcePropertyElt.
(t
'resource)))
;; Otherwise (if an attribute is found that is not xml:lang or rdf:ID):
(t
(cond
;; If the attribute name is rdf:datatype, this is a literalPropertyElt.
((xmp-xml-element-attr-find-by-ename elem xmp-rdf:datatype)
'literal)
((when-let ((parseType
(xmp-xml-element-attr-value elem xmp-rdf:parseType)))
(cond
;; If the attribute value is Literal, this is a
;; parseTypeLiteralPropertyElt.
((equal parseType "Literal") 'parseTypeLiteral)
;; If the attribute value is Resource, this is a
;; parseTypeResourcePropertyElt.
((equal parseType "Resource") 'parseTypeResource)
;; If the attribute value is Collection, this is a
;; parseTypeCollectionPropertyElt.
((equal parseType "Collection") 'parseTypeCollection)
;; Otherwise, this is a parseTypeOtherPropertyElt.
(t 'parseTypeOther))))
;; If the attribute name is not rdf:parseType, this is an
;; emptyPropertyElt.
(t
'empty)))))
(defun xmp-parse-property-element (prop-elem &optional noerror)
"Parse the property element PROP-ELEM and return the parsed value (pvalue).
If NOERROR is non-nil, then nil is returned if an error occurs, whenever
possible."
(if noerror
(ignore-errors (xmp-parse-property-element--impl prop-elem))
(xmp-parse-property-element--impl prop-elem)))
(defun xmp-parse-property-element--impl (prop-elem)
(pcase (xmp-property-element-type prop-elem)
('empty
(xmp-parse-property-element--empty prop-elem))
('resource
(xmp-parse-property-element--resource prop-elem))
('literal
(xmp-parse-property-element--literal prop-elem))
('parseTypeResource
(xmp-parse-property-element--parse-type-resource prop-elem))
(_ (error "Unsupported property element type"))))
(defun xmp-parse-property-element--resource (prop-elem)
;; [XMP1 C.2.6] resourcePropertyElt
;; <ns:Prop>
;; nodeElement(Description|Bag|Seq|Alt|<typedNode>)
;; </>
(let* ((prop-ename (xmp-xml-element-ename prop-elem))
(xml-lang (xmp-xml-element-attr-value prop-elem xmp-xml:lang))
(init-qualifiers (when xml-lang
(list (xmp-pvalue-make-named xmp-xml:lang 'text
xml-lang))))
(node-elem (car (xmp-xml-element-children prop-elem))))
(cond