/* * Copyright 2002-2019 Drew Noakes and contributors * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * * More information about this project is available at: * * https://drewnoakes.com/code/exif/ * https://github.com/drewnoakes/metadata-extractor */ package com.drew.metadata.xmp; import com.adobe.internal.xmp.XMPException; import com.adobe.internal.xmp.XMPIterator; import com.adobe.internal.xmp.XMPMeta; import com.adobe.internal.xmp.XMPMetaFactory; import com.adobe.internal.xmp.impl.ByteBuffer; import com.adobe.internal.xmp.options.ParseOptions; import com.adobe.internal.xmp.properties.XMPPropertyInfo; import com.drew.imaging.jpeg.JpegSegmentMetadataReader; import com.drew.imaging.jpeg.JpegSegmentType; import com.drew.lang.SequentialByteArrayReader; import com.drew.lang.SequentialReader; import com.drew.metadata.Directory; import com.drew.lang.annotations.NotNull; import com.drew.lang.annotations.Nullable; import com.drew.metadata.Metadata; import com.drew.metadata.StringValue; import java.io.IOException; import java.util.Collection; import java.util.Collections; /** * Extracts XMP data from JPEG APP1 segments. *
* Note that XMP uses a namespace and path format for identifying values, which does not map to metadata-extractor's * integer based tag identifiers. Therefore, XMP data is extracted and exposed via {@link XmpDirectory#getXMPMeta()} * which returns an instance of Adobe's {@link XMPMeta} which exposes the full XMP data set. *
* The extraction is done with Adobe's XmpCore-Library (XMP-Toolkit)
* Copyright (c) 1999 - 2007, Adobe Systems Incorporated All rights reserved.
*
* @author Torsten Skadell
* @author Drew Noakes https://drewnoakes.com
* @author https://github.com/bezineb5
*/
public class XmpReader implements JpegSegmentMetadataReader
{
@NotNull
private static final String XMP_JPEG_PREAMBLE = "http://ns.adobe.com/xap/1.0/\0";
@NotNull
private static final String XMP_EXTENSION_JPEG_PREAMBLE = "http://ns.adobe.com/xmp/extension/\0";
@NotNull
private static final String SCHEMA_XMP_NOTES = "http://ns.adobe.com/xmp/note/";
@NotNull
private static final String ATTRIBUTE_EXTENDED_XMP = "xmpNote:HasExtendedXMP";
// Limit photoshop:DocumentAncestors node as it can reach over 100000 items and make parsing extremely slow.
// This is not a typical value but it may happen https://forums.adobe.com/thread/2081839
@NotNull
private static final ParseOptions PARSE_OPTIONS = new ParseOptions().setXMPNodesToLimit(Collections.singletonMap("photoshop:DocumentAncestors", 1000));
/**
* Extended XMP constants
*/
private static final int EXTENDED_XMP_GUID_LENGTH = 32;
private static final int EXTENDED_XMP_INT_LENGTH = 4;
@NotNull
public Iterable
* The extraction is done with Adobe's XMPCore library.
*/
public void extract(@NotNull final byte[] xmpBytes, @NotNull Metadata metadata)
{
extract(xmpBytes, metadata, null);
}
/**
* Performs the XMP data extraction, adding found values to the specified instance of {@link Metadata}.
*
* The extraction is done with Adobe's XMPCore library.
*/
public void extract(@NotNull final byte[] xmpBytes, @NotNull Metadata metadata, @Nullable Directory parentDirectory)
{
extract(xmpBytes, 0, xmpBytes.length, metadata, parentDirectory);
}
/**
* Performs the XMP data extraction, adding found values to the specified instance of {@link Metadata}.
*
* The extraction is done with Adobe's XMPCore library.
*/
public void extract(@NotNull final byte[] xmpBytes, int offset, int length, @NotNull Metadata metadata, @Nullable Directory parentDirectory)
{
XmpDirectory directory = new XmpDirectory();
if (parentDirectory != null)
directory.setParent(parentDirectory);
try {
XMPMeta xmpMeta;
// If all xmpBytes are requested, no need to make a new ByteBuffer
if (offset == 0 && length == xmpBytes.length) {
xmpMeta = XMPMetaFactory.parseFromBuffer(xmpBytes, PARSE_OPTIONS);
} else {
ByteBuffer buffer = new ByteBuffer(xmpBytes, offset, length);
xmpMeta = XMPMetaFactory.parse(buffer.getByteStream(), PARSE_OPTIONS);
}
directory.setXMPMeta(xmpMeta);
} catch (XMPException e) {
directory.addError("Error processing XMP data: " + e.getMessage());
}
if (!directory.isEmpty())
metadata.addDirectory(directory);
}
/**
* Performs the XMP data extraction, adding found values to the specified instance of {@link Metadata}.
*
* The extraction is done with Adobe's XMPCore library.
*/
public void extract(@NotNull final String xmpString, @NotNull Metadata metadata)
{
extract(xmpString, metadata, null);
}
/**
* Performs the XMP data extraction, adding found values to the specified instance of {@link Metadata}.
*
* The extraction is done with Adobe's XMPCore library.
*/
public void extract(@NotNull final StringValue xmpString, @NotNull Metadata metadata)
{
extract(xmpString.getBytes(), metadata, null);
}
/**
* Performs the XMP data extraction, adding found values to the specified instance of {@link Metadata}.
*
* The extraction is done with Adobe's XMPCore library.
*/
public void extract(@NotNull final String xmpString, @NotNull Metadata metadata, @Nullable Directory parentDirectory)
{
XmpDirectory directory = new XmpDirectory();
if (parentDirectory != null)
directory.setParent(parentDirectory);
try {
XMPMeta xmpMeta = XMPMetaFactory.parseFromString(xmpString, PARSE_OPTIONS);
directory.setXMPMeta(xmpMeta);
} catch (XMPException e) {
directory.addError("Error processing XMP data: " + e.getMessage());
}
if (!directory.isEmpty())
metadata.addDirectory(directory);
}
/**
* Determine if there is an extended XMP section based on the standard XMP part.
* The xmpNote:HasExtendedXMP attribute contains the GUID of the Extended XMP chunks.
*/
@Nullable
private static String getExtendedXMPGUID(@NotNull Metadata metadata)
{
final Collection