Debian Codegraph @

src/com/drew/metadata/xmp/XmpDirectory.java

raw symbols SCIP
1 /*
2 * Copyright 2002-2019 Drew Noakes and contributors
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 *
16 * More information about this project is available at:
17 *
18 * https://drewnoakes.com/code/exif/
19 * https://github.com/drewnoakes/metadata-extractor
20 */
21 package com.drew.metadata.xmp;
22
23 import com.adobe.internal.xmp.XMPException;
24 import com.adobe.internal.xmp.XMPIterator;
25 import com.adobe.internal.xmp.XMPMeta;
26 import com.adobe.internal.xmp.impl.XMPMetaImpl;
27 import com.adobe.internal.xmp.options.IteratorOptions;
28 import com.adobe.internal.xmp.properties.XMPPropertyInfo;
29 import com.drew.lang.annotations.NotNull;
30 import com.drew.lang.annotations.Nullable;
31 import com.drew.metadata.Directory;
32
33 import java.util.Collections;
34 import java.util.HashMap;
35 import java.util.Map;
36
37 /**
38 * Wraps an instance of Adobe's {@link XMPMeta} object, which holds XMP data.
39 * <p />
40 * XMP uses a namespace and path format for identifying values, which does not map to metadata-extractor's
41 * integer based tag identifiers. Therefore, XMP data is extracted and exposed via {@link XmpDirectory#getXMPMeta()}
42 * which returns an instance of Adobe's {@link XMPMeta} which exposes the full XMP data set.
43 *
44 * @author Torsten Skadell
45 * @author Drew Noakes https://drewnoakes.com
46 */
47 @SuppressWarnings("WeakerAccess")
48 public class XmpDirectory extends Directory
49 {
50 public static final int TAG_XMP_VALUE_COUNT = 0xFFFF;
51
52 @NotNull
53 private static final HashMap<Integer, String> _tagNameMap = new HashMap<Integer, String>();
54
55 static {
56 _tagNameMap.put(TAG_XMP_VALUE_COUNT, "XMP Value Count");
57 }
58
59 @Nullable
60 private XMPMeta _xmpMeta;
61
62 public XmpDirectory()
63 {
64 this.setDescriptor(new XmpDescriptor(this));
65 }
66
67 @Override
68 @NotNull
69 public String getName()
70 {
71 return "XMP";
72 }
73
74 @Override
75 @NotNull
76 protected HashMap<Integer, String> getTagNameMap()
77 {
78 return _tagNameMap;
79 }
80
81 /**
82 * Gets a map of all XMP properties in this directory.
83 * <p>
84 * This is required because XMP properties are represented as strings, whereas the rest of this library
85 * uses integers for keys.
86 */
87 @NotNull
88 public Map<String, String> getXmpProperties()
89 {
90 Map<String, String> propertyValueByPath = new HashMap<String, String>();
91
92 if (_xmpMeta != null)
93 {
94 try {
95 IteratorOptions options = new IteratorOptions().setJustLeafnodes(true);
96 for (XMPIterator i = _xmpMeta.iterator(options); i.hasNext(); ) {
97 XMPPropertyInfo prop = (XMPPropertyInfo)i.next();
98 String path = prop.getPath();
99 String value = prop.getValue();
100 if (path != null && value != null) {
101 propertyValueByPath.put(path, value);
102 }
103 }
104 } catch (XMPException ignored) {
105 }
106 }
107
108 return Collections.unmodifiableMap(propertyValueByPath);
109 }
110
111 public void setXMPMeta(@NotNull XMPMeta xmpMeta)
112 {
113 _xmpMeta = xmpMeta;
114
115 try {
116 int valueCount = 0;
117 IteratorOptions options = new IteratorOptions().setJustLeafnodes(true);
118 for (XMPIterator i = _xmpMeta.iterator(options); i.hasNext(); ) {
119 XMPPropertyInfo prop = (XMPPropertyInfo)i.next();
120 if (prop.getPath() != null) {
121 valueCount++;
122 }
123 }
124 setInt(TAG_XMP_VALUE_COUNT, valueCount);
125 } catch (XMPException ignored) {
126 }
127 }
128
129 /**
130 * Gets the XMPMeta object used to populate this directory. It can be used for more XMP-oriented operations.
131 * If one does not exist it will be created.
132 */
133 @NotNull
134 public XMPMeta getXMPMeta()
135 {
136 if (_xmpMeta == null)
137 _xmpMeta = new XMPMetaImpl();
138 return _xmpMeta;
139 }
140 }