| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 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.XMPMetaFactory; |
| 27 |
import com.adobe.internal.xmp.impl.ByteBuffer; |
| 28 |
import com.adobe.internal.xmp.options.ParseOptions; |
| 29 |
import com.adobe.internal.xmp.properties.XMPPropertyInfo; |
| 30 |
import com.drew.imaging.jpeg.JpegSegmentMetadataReader; |
| 31 |
import com.drew.imaging.jpeg.JpegSegmentType; |
| 32 |
import com.drew.lang.SequentialByteArrayReader; |
| 33 |
import com.drew.lang.SequentialReader; |
| 34 |
import com.drew.metadata.Directory; |
| 35 |
import com.drew.lang.annotations.NotNull; |
| 36 |
import com.drew.lang.annotations.Nullable; |
| 37 |
import com.drew.metadata.Metadata; |
| 38 |
import com.drew.metadata.StringValue; |
| 39 |
|
| 40 |
import java.io.IOException; |
| 41 |
import java.util.Collection; |
| 42 |
import java.util.Collections; |
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
public class XmpReader implements JpegSegmentMetadataReader |
| 59 |
{ |
| 60 |
@NotNull |
| 61 |
private static final String XMP_JPEG_PREAMBLE = "http://ns.adobe.com/xap/1.0/\0"; |
| 62 |
@NotNull |
| 63 |
private static final String XMP_EXTENSION_JPEG_PREAMBLE = "http://ns.adobe.com/xmp/extension/\0"; |
| 64 |
@NotNull |
| 65 |
private static final String SCHEMA_XMP_NOTES = "http://ns.adobe.com/xmp/note/"; |
| 66 |
@NotNull |
| 67 |
private static final String ATTRIBUTE_EXTENDED_XMP = "xmpNote:HasExtendedXMP"; |
| 68 |
|
| 69 |
|
| 70 |
@NotNull |
| 71 |
private static final ParseOptions PARSE_OPTIONS = new ParseOptions().setXMPNodesToLimit(Collections.singletonMap("photoshop:DocumentAncestors", 1000)); |
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
private static final int EXTENDED_XMP_GUID_LENGTH = 32; |
| 77 |
private static final int EXTENDED_XMP_INT_LENGTH = 4; |
| 78 |
|
| 79 |
@NotNull |
| 80 |
public Iterable<JpegSegmentType> getSegmentTypes() |
| 81 |
{ |
| 82 |
return Collections.singletonList(JpegSegmentType.APP1); |
| 83 |
} |
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
public void readJpegSegments(@NotNull Iterable<byte[]> segments, @NotNull Metadata metadata, @NotNull JpegSegmentType segmentType) |
| 94 |
{ |
| 95 |
final int preambleLength = XMP_JPEG_PREAMBLE.length(); |
| 96 |
final int extensionPreambleLength = XMP_EXTENSION_JPEG_PREAMBLE.length(); |
| 97 |
String extendedXMPGUID = null; |
| 98 |
byte[] extendedXMPBuffer = null; |
| 99 |
|
| 100 |
for (byte[] segmentBytes : segments) { |
| 101 |
|
| 102 |
if (segmentBytes.length >= preambleLength) { |
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
if (XMP_JPEG_PREAMBLE.equalsIgnoreCase(new String(segmentBytes, 0, preambleLength)) || |
| 107 |
"XMP".equalsIgnoreCase(new String(segmentBytes, 0, 3))) { |
| 108 |
|
| 109 |
byte[] xmlBytes = new byte[segmentBytes.length - preambleLength]; |
| 110 |
System.arraycopy(segmentBytes, preambleLength, xmlBytes, 0, xmlBytes.length); |
| 111 |
extract(xmlBytes, metadata); |
| 112 |
|
| 113 |
extendedXMPGUID = getExtendedXMPGUID(metadata); |
| 114 |
continue; |
| 115 |
} |
| 116 |
} |
| 117 |
|
| 118 |
|
| 119 |
if (extendedXMPGUID != null && |
| 120 |
segmentBytes.length >= extensionPreambleLength && |
| 121 |
XMP_EXTENSION_JPEG_PREAMBLE.equalsIgnoreCase(new String(segmentBytes, 0, extensionPreambleLength))) { |
| 122 |
|
| 123 |
extendedXMPBuffer = processExtendedXMPChunk(metadata, segmentBytes, extendedXMPGUID, extendedXMPBuffer); |
| 124 |
} |
| 125 |
} |
| 126 |
|
| 127 |
|
| 128 |
if (extendedXMPBuffer != null) { |
| 129 |
extract(extendedXMPBuffer, metadata); |
| 130 |
} |
| 131 |
} |
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
public void extract(@NotNull final byte[] xmpBytes, @NotNull Metadata metadata) |
| 139 |
{ |
| 140 |
extract(xmpBytes, metadata, null); |
| 141 |
} |
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
public void extract(@NotNull final byte[] xmpBytes, @NotNull Metadata metadata, @Nullable Directory parentDirectory) |
| 149 |
{ |
| 150 |
extract(xmpBytes, 0, xmpBytes.length, metadata, parentDirectory); |
| 151 |
} |
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
public void extract(@NotNull final byte[] xmpBytes, int offset, int length, @NotNull Metadata metadata, @Nullable Directory parentDirectory) |
| 159 |
{ |
| 160 |
XmpDirectory directory = new XmpDirectory(); |
| 161 |
|
| 162 |
if (parentDirectory != null) |
| 163 |
directory.setParent(parentDirectory); |
| 164 |
|
| 165 |
try { |
| 166 |
XMPMeta xmpMeta; |
| 167 |
|
| 168 |
|
| 169 |
if (offset == 0 && length == xmpBytes.length) { |
| 170 |
xmpMeta = XMPMetaFactory.parseFromBuffer(xmpBytes, PARSE_OPTIONS); |
| 171 |
} else { |
| 172 |
ByteBuffer buffer = new ByteBuffer(xmpBytes, offset, length); |
| 173 |
xmpMeta = XMPMetaFactory.parse(buffer.getByteStream(), PARSE_OPTIONS); |
| 174 |
} |
| 175 |
|
| 176 |
directory.setXMPMeta(xmpMeta); |
| 177 |
} catch (XMPException e) { |
| 178 |
directory.addError("Error processing XMP data: " + e.getMessage()); |
| 179 |
} |
| 180 |
|
| 181 |
if (!directory.isEmpty()) |
| 182 |
metadata.addDirectory(directory); |
| 183 |
} |
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
public void extract(@NotNull final String xmpString, @NotNull Metadata metadata) |
| 191 |
{ |
| 192 |
extract(xmpString, metadata, null); |
| 193 |
} |
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
public void extract(@NotNull final StringValue xmpString, @NotNull Metadata metadata) |
| 201 |
{ |
| 202 |
extract(xmpString.getBytes(), metadata, null); |
| 203 |
} |
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
public void extract(@NotNull final String xmpString, @NotNull Metadata metadata, @Nullable Directory parentDirectory) |
| 211 |
{ |
| 212 |
XmpDirectory directory = new XmpDirectory(); |
| 213 |
|
| 214 |
if (parentDirectory != null) |
| 215 |
directory.setParent(parentDirectory); |
| 216 |
|
| 217 |
try { |
| 218 |
XMPMeta xmpMeta = XMPMetaFactory.parseFromString(xmpString, PARSE_OPTIONS); |
| 219 |
directory.setXMPMeta(xmpMeta); |
| 220 |
} catch (XMPException e) { |
| 221 |
directory.addError("Error processing XMP data: " + e.getMessage()); |
| 222 |
} |
| 223 |
|
| 224 |
if (!directory.isEmpty()) |
| 225 |
metadata.addDirectory(directory); |
| 226 |
} |
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
@Nullable |
| 233 |
private static String getExtendedXMPGUID(@NotNull Metadata metadata) |
| 234 |
{ |
| 235 |
final Collection<XmpDirectory> xmpDirectories = metadata.getDirectoriesOfType(XmpDirectory.class); |
| 236 |
|
| 237 |
for (XmpDirectory directory : xmpDirectories) { |
| 238 |
final XMPMeta xmpMeta = directory.getXMPMeta(); |
| 239 |
|
| 240 |
try { |
| 241 |
final XMPIterator itr = xmpMeta.iterator(SCHEMA_XMP_NOTES, null, null); |
| 242 |
if (itr == null) |
| 243 |
continue; |
| 244 |
|
| 245 |
while (itr.hasNext()) { |
| 246 |
final XMPPropertyInfo pi = (XMPPropertyInfo) itr.next(); |
| 247 |
if (ATTRIBUTE_EXTENDED_XMP.equals(pi.getPath())) { |
| 248 |
return pi.getValue(); |
| 249 |
} |
| 250 |
} |
| 251 |
} catch (XMPException e) { |
| 252 |
|
| 253 |
} |
| 254 |
} |
| 255 |
|
| 256 |
return null; |
| 257 |
} |
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
@Nullable |
| 267 |
private static byte[] processExtendedXMPChunk(@NotNull Metadata metadata, @NotNull byte[] segmentBytes, @NotNull String extendedXMPGUID, @Nullable byte[] extendedXMPBuffer) |
| 268 |
{ |
| 269 |
final int extensionPreambleLength = XMP_EXTENSION_JPEG_PREAMBLE.length(); |
| 270 |
final int segmentLength = segmentBytes.length; |
| 271 |
final int totalOffset = extensionPreambleLength + EXTENDED_XMP_GUID_LENGTH + EXTENDED_XMP_INT_LENGTH + EXTENDED_XMP_INT_LENGTH; |
| 272 |
|
| 273 |
if (segmentLength >= totalOffset) { |
| 274 |
try { |
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
|
| 284 |
final SequentialReader reader = new SequentialByteArrayReader(segmentBytes); |
| 285 |
reader.skip(extensionPreambleLength); |
| 286 |
final String segmentGUID = reader.getString(EXTENDED_XMP_GUID_LENGTH); |
| 287 |
|
| 288 |
if (extendedXMPGUID.equals(segmentGUID)) { |
| 289 |
final int fullLength = (int)reader.getUInt32(); |
| 290 |
final int chunkOffset = (int)reader.getUInt32(); |
| 291 |
|
| 292 |
if (extendedXMPBuffer == null) |
| 293 |
extendedXMPBuffer = new byte[fullLength]; |
| 294 |
|
| 295 |
if (extendedXMPBuffer.length == fullLength) { |
| 296 |
System.arraycopy(segmentBytes, totalOffset, extendedXMPBuffer, chunkOffset, segmentLength - totalOffset); |
| 297 |
} else { |
| 298 |
XmpDirectory directory = new XmpDirectory(); |
| 299 |
directory.addError(String.format("Inconsistent length for the Extended XMP buffer: %d instead of %d", fullLength, extendedXMPBuffer.length)); |
| 300 |
metadata.addDirectory(directory); |
| 301 |
} |
| 302 |
} |
| 303 |
} catch (IOException ex) { |
| 304 |
XmpDirectory directory = new XmpDirectory(); |
| 305 |
directory.addError(ex.getMessage()); |
| 306 |
metadata.addDirectory(directory); |
| 307 |
} |
| 308 |
} |
| 309 |
|
| 310 |
return extendedXMPBuffer; |
| 311 |
} |
| 312 |
} |