Package: DefaultMetadata
DefaultMetadata
name | instruction | branch | complexity | line | method | ||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
DefaultMetadata(String, EditableImage) |
|
|
|
|
|
||||||||||||||||||||
DefaultMetadata(String, EditableImage, MetadataInterpolatorFactory) |
|
|
|
|
|
||||||||||||||||||||
getDirectory(Class) |
|
|
|
|
|
||||||||||||||||||||
getMap(ResourceProperties, Key) |
|
|
|
|
|
||||||||||||||||||||
interpolateString(String, ResourceProperties) |
|
|
|
|
|
||||||||||||||||||||
lambda$getDirectory$0(Class) |
|
|
|
|
|
||||||||||||||||||||
lambda$log$1(TIFF, Directory.Tag) |
|
|
|
|
|
||||||||||||||||||||
lambda$log$2(EXIF, Directory.Tag) |
|
|
|
|
|
||||||||||||||||||||
lambda$log$3(IPTC, Directory.Tag) |
|
|
|
|
|
||||||||||||||||||||
lambda$log$4(XMP, Directory.Tag) |
|
|
|
|
|
||||||||||||||||||||
lambda$log$5(String, String) |
|
|
|
|
|
||||||||||||||||||||
log() |
|
|
|
|
|
||||||||||||||||||||
static {...} |
|
|
|
|
|
||||||||||||||||||||
toString() |
|
|
|
|
|
Coverage
1: /*
2: * #%L
3: * *********************************************************************************************************************
4: *
5: * NorthernWind - lightweight CMS
6: * http://northernwind.tidalwave.it - git clone https://bitbucket.org/tidalwave/northernwind-src.git
7: * %%
8: * Copyright (C) 2011 - 2023 Tidalwave s.a.s. (http://tidalwave.it)
9: * %%
10: * *********************************************************************************************************************
11: *
12: * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
13: * the License. You may obtain a copy of the License at
14: *
15: * http://www.apache.org/licenses/LICENSE-2.0
16: *
17: * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
18: * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
19: * specific language governing permissions and limitations under the License.
20: *
21: * *********************************************************************************************************************
22: *
23: *
24: * *********************************************************************************************************************
25: * #L%
26: */
27: package it.tidalwave.northernwind.frontend.media.impl;
28:
29: import javax.annotation.Nonnull;
30: import javax.inject.Inject;
31: import java.util.HashMap;
32: import java.util.List;
33: import java.util.Map;
34: import java.util.TreeMap;
35: import org.springframework.beans.factory.annotation.Configurable;
36: import it.tidalwave.util.Key;
37: import it.tidalwave.image.EditableImage;
38: import it.tidalwave.image.metadata.Directory;
39: import it.tidalwave.image.metadata.EXIF;
40: import it.tidalwave.image.metadata.IPTC;
41: import it.tidalwave.image.metadata.TIFF;
42: import it.tidalwave.image.metadata.XMP;
43: import it.tidalwave.northernwind.core.model.ResourceProperties;
44: import it.tidalwave.northernwind.frontend.media.impl.interpolator.MetadataInterpolator.Context;
45: import it.tidalwave.northernwind.frontend.media.impl.interpolator.MetadataInterpolatorFactory;
46: import lombok.AllArgsConstructor;
47: import lombok.ToString;
48: import lombok.extern.slf4j.Slf4j;
49: import static java.util.Collections.emptyList;
50: import static it.tidalwave.util.FunctionalCheckedExceptionWrappers.*;
51: import static it.tidalwave.northernwind.frontend.media.impl.EmbeddedMediaMetadataProvider.*;
52:
53: /***********************************************************************************************************************
54: *
55: * A default implementation of {@link Metadata}.
56: *
57: * @author Fabrizio Giudici
58: *
59: **********************************************************************************************************************/
60:•@Configurable @AllArgsConstructor @ToString @Slf4j
61: class DefaultMetadata implements Metadata
62: {
63: @Nonnull
64: private final String mediaName;
65:
66: @Nonnull
67: private final EditableImage image;
68:
69: @Inject
70: private MetadataInterpolatorFactory interpolatorFactory;
71:
72: /*******************************************************************************************************************
73: *
74: *
75: *
76: ******************************************************************************************************************/
77: public DefaultMetadata (@Nonnull final String mediaName, @Nonnull final EditableImage image)
78:• {
79: this.mediaName = mediaName;
80: this.image = image;
81:• }
82:
83: /*******************************************************************************************************************
84: *
85: * {@inheritDoc}
86: *
87: ******************************************************************************************************************/
88: @Override @Nonnull
89: public <T extends Directory> T getDirectory (@Nonnull final Class<T> metadataClass)
90: {
91: return image.getMetadata(metadataClass).orElseGet(_s(() -> metadataClass.getConstructor().newInstance()));
92: }
93:
94: /*******************************************************************************************************************
95: *
96: * {@inheritDoc}
97: *
98: ******************************************************************************************************************/
99: @Override @Nonnull
100: public String interpolateString (@Nonnull final String template,
101: @Nonnull final ResourceProperties properties)
102: {
103:• if (log.isDebugEnabled())
104: {
105: log();
106: }
107:
108: // FIXME: use format as an interpolated string to get properties both from EXIF and IPTC
109: // final String string = formatted(iptc.getObject(517, String.class));
110: final var context = new Context(this, getMap(properties, P_CAMERA_IDS), getMap(properties, P_LENS_IDS));
111:
112: var result = template;
113:
114:• for (final var interpolator : interpolatorFactory.getInterpolators())
115: {
116:• if (result.contains("$" + interpolator.getMacro() + "$"))
117: {
118: result = interpolator.interpolate(result, context);
119: }
120: }
121:
122: return result;
123: }
124:
125: /*******************************************************************************************************************
126: *
127: ******************************************************************************************************************/
128: private void log()
129: {
130: final var tiff = image.getMetadata(TIFF.class).orElseGet(TIFF::new);
131: final var exif = image.getMetadata(EXIF.class).orElseGet(EXIF::new);
132: final var iptc = image.getMetadata(IPTC.class).orElseGet(IPTC::new);
133: final var xmp = image.getMetadata(XMP.class).orElseGet(XMP::new);
134: final Map<String, String> xmpProperties = new TreeMap<>(xmp.getXmpProperties());
135:
136: tiff.forEachTag(t -> log.debug("{}: TIFF[{}]: {}", mediaName, t.getName(), tiff.getRaw(t.getCode())));
137: exif.forEachTag(t -> log.debug("{}: EXIF[{}]: {}", mediaName, t.getName(), exif.getRaw(t.getCode())));
138: iptc.forEachTag(t -> log.debug("{}: IPTC[{}]: {}", mediaName, t.getName(), iptc.getRaw(t.getCode())));
139: xmp.forEachTag(t -> log.debug("{}: XMP[{}]: {}", mediaName, t.getName(), xmp.getRaw(t.getCode())));
140: xmpProperties.forEach((k, v) -> log.debug("XMPprop({}).{}: {}", mediaName, k, v));
141: }
142:
143: /*******************************************************************************************************************
144: *
145: ******************************************************************************************************************/
146: @Nonnull
147: private static Map<String, String> getMap (@Nonnull final ResourceProperties siteNodeProperties,
148: @Nonnull final Key<List<String>> key)
149: {
150: final var properties = siteNodeProperties.getGroup(P_GROUP_ID);
151: final Map<String, String> lensMap = new HashMap<>();
152:
153:• for (final var s : properties.getProperty(key).orElse(emptyList()))
154: {
155: final var split = s.split(":");
156: lensMap.put(split[0].trim(), split[1].trim());
157: }
158:
159: return lensMap;
160: }
161: }