Skip to content

Package: DrewMetadataLoader

DrewMetadataLoader

nameinstructionbranchcomplexitylinemethod
DrewMetadataLoader()
M: 3 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
getEXIFDirectories(Node)
M: 53 C: 0
0%
M: 8 C: 0
0%
M: 5 C: 0
0%
M: 14 C: 0
0%
M: 1 C: 0
0%
getExifLoader(IIOMetadata)
M: 16 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 2 C: 0
0%
M: 1 C: 0
0%
getIPTCDirectories(Node)
M: 53 C: 0
0%
M: 8 C: 0
0%
M: 5 C: 0
0%
M: 14 C: 0
0%
M: 1 C: 0
0%
getIptcLoader(IIOMetadata)
M: 16 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 2 C: 0
0%
M: 1 C: 0
0%

Coverage

1: /*
2: * *********************************************************************************************************************
3: *
4: * Mistral: open source imaging engine
5: * http://tidalwave.it/projects/mistral
6: *
7: * Copyright (C) 2003 - 2023 by Tidalwave s.a.s. (http://tidalwave.it)
8: *
9: * *********************************************************************************************************************
10: *
11: * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
12: * the License. You may obtain a copy of the License at
13: *
14: * http://www.apache.org/licenses/LICENSE-2.0
15: *
16: * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
17: * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
18: * specific language governing permissions and limitations under the License.
19: *
20: * *********************************************************************************************************************
21: *
22: * git clone https://bitbucket.org/tidalwave/mistral-src
23: * git clone https://github.com/tidalwave-it/mistral-src
24: *
25: * *********************************************************************************************************************
26: */
27: package it.tidalwave.image.metadata.loader;
28:
29: import javax.annotation.Nonnull;
30: import java.util.ArrayList;
31: import java.util.Collection;
32: import java.util.Optional;
33: import javax.imageio.metadata.IIOMetadata;
34: import javax.imageio.metadata.IIOMetadataNode;
35: import com.drew.lang.ByteArrayReader;
36: import com.drew.metadata.Metadata;
37: import com.drew.metadata.exif.ExifReader;
38: import com.drew.metadata.exif.ExifSubIFDDirectory;
39: import com.drew.metadata.iptc.IptcDirectory;
40: import org.w3c.dom.Node;
41:
42: /***********************************************************************************************************************
43: *
44: * @author Fabrizio Giudici
45: *
46: **********************************************************************************************************************/
47: public class DrewMetadataLoader implements MetadataLoader
48: {
49: public static final int EXIF = 0xE1;
50: public static final int IPTC = 0xED;
51:
52: /*******************************************************************************************************************
53: *
54: *
55: ******************************************************************************************************************/
56: @Override
57: public Optional<DirectoryLoader> getExifLoader (@Nonnull final IIOMetadata iioMetadata)
58: {
59: final var node = iioMetadata.getAsTree(iioMetadata.getNativeMetadataFormatName());
60: return Optional.of(new DirectoryDrewLoader(new ArrayList<>(getEXIFDirectories(node)), 0));
61: }
62:
63: /*******************************************************************************************************************
64: *
65: *
66: ******************************************************************************************************************/
67: @Override
68: public Optional<DirectoryLoader> getIptcLoader (@Nonnull final IIOMetadata iioMetadata)
69: {
70: final var node = iioMetadata.getAsTree(iioMetadata.getNativeMetadataFormatName());
71: return Optional.of(new DirectoryDrewLoader(new ArrayList<>(getIPTCDirectories(node)), 0));
72: }
73:
74: /*******************************************************************************************************************
75: *
76: *
77: ******************************************************************************************************************/
78: private static Collection<ExifSubIFDDirectory> getEXIFDirectories (final Node node)
79: {
80:• if (node.getNodeName().equals("unknown"))
81: {
82:• if (Integer.parseInt(node.getAttributes().getNamedItem("MarkerTag").getNodeValue()) == EXIF)
83: {
84: final var data = (byte[])((IIOMetadataNode)node).getUserObject();
85: final var metadata = new Metadata();
86: new ExifReader().extract(new ByteArrayReader(data), metadata);
87: return metadata.getDirectoriesOfType(ExifSubIFDDirectory.class);
88: }
89: }
90:
91: var child = node.getFirstChild();
92:
93:• while (child != null)
94: {
95: final var directory = getEXIFDirectories(child);
96:
97:• if (directory != null)
98: {
99: return directory;
100: }
101:
102: child = child.getNextSibling();
103: }
104:
105: return null;
106: }
107:
108: /*******************************************************************************************************************
109: *
110: *
111: ******************************************************************************************************************/
112: private static Collection<IptcDirectory> getIPTCDirectories (final Node node)
113: {
114:• if (node.getNodeName().equals("unknown"))
115: {
116:• if (Integer.parseInt(node.getAttributes().getNamedItem("MarkerTag").getNodeValue()) == IPTC)
117: {
118: final var data = (byte[])((IIOMetadataNode)node).getUserObject();
119: final var metadata = new Metadata();
120: new ExifReader().extract(new ByteArrayReader(data), metadata);
121: return metadata.getDirectoriesOfType(IptcDirectory.class);
122: }
123: }
124:
125: var child = node.getFirstChild();
126:
127:• while (child != null)
128: {
129: final var directory = getIPTCDirectories(child);
130:
131:• if (directory != null)
132: {
133: return directory;
134: }
135:
136: child = child.getNextSibling();
137: }
138:
139: return null;
140: }
141: }