    001/*
    002 * Licensed to the Apache Software Foundation (ASF) under one or more
    003 * contributor license agreements.  See the NOTICE file distributed with
    004 * this work for additional information regarding copyright ownership.
    005 * The ASF licenses this file to You under the Apache License, Version 2.0
    006 * (the "License"); you may not use this file except in compliance with
    007 * the License.  You may obtain a copy of the License at
    008 *
    009 *      http://www.apache.org/licenses/LICENSE-2.0
    010 *
    011 * Unless required by applicable law or agreed to in writing, software
    012 * distributed under the License is distributed on an "AS IS" BASIS,
    013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    014 * See the License for the specific language governing permissions and
    015 * limitations under the License.
    016 */
    017
    018package org.apache.commons.imaging.formats.tiff.taginfos;
    019
    020import java.nio.ByteOrder;
    021import java.util.ArrayList;
    022import java.util.Arrays;
    023import java.util.Collections;
    024import java.util.List;
    025
    026import org.apache.commons.imaging.ImagingException;
    027import org.apache.commons.imaging.formats.tiff.TiffField;
    028import org.apache.commons.imaging.formats.tiff.constants.TiffDirectoryType;
    029import org.apache.commons.imaging.formats.tiff.fieldtypes.AbstractFieldType;
    030
    031public class TagInfo {
    032    public static final int LENGTH_UNKNOWN = -1;
    033    public final String name;
    034    public final int tag;
    035    public final List<AbstractFieldType> dataTypes;
    036    public final int length;
    037    public final TiffDirectoryType directoryType;
    038    private final boolean isOffset;
    039
    040    public TagInfo(final String name, final int tag, final AbstractFieldType dataType) {
    041        this(name, tag, dataType, LENGTH_UNKNOWN, TiffDirectoryType.EXIF_DIRECTORY_UNKNOWN);
    042    }
    043
    044    public TagInfo(final String name, final int tag, final AbstractFieldType dataType, final int length) {
    045        this(name, tag, Arrays.asList(dataType), length, TiffDirectoryType.EXIF_DIRECTORY_UNKNOWN);
    046    }
    047
    048    public TagInfo(final String name, final int tag, final AbstractFieldType dataType, final int length, final TiffDirectoryType exifDirectory) {
    049        this(name, tag, Arrays.asList(dataType), length, exifDirectory);
    050    }
    051
    052    public TagInfo(final String name, final int tag, final AbstractFieldType dataType, final int length, final TiffDirectoryType exifDirectory,
    053            final boolean isOffset) {
    054        this(name, tag, Arrays.asList(dataType), length, exifDirectory, isOffset);
    055    }
    056
    057    public TagInfo(final String name, final int tag, final List<AbstractFieldType> dataTypes, final int length, final TiffDirectoryType exifDirectory) {
    058        this(name, tag, dataTypes, length, exifDirectory, false);
    059    }
    060
    061    public TagInfo(final String name, final int tag, final List<AbstractFieldType> dataTypes, final int length, final TiffDirectoryType exifDirectory,
    062            final boolean isOffset) {
    063        this.name = name;
    064        this.tag = tag;
    065        this.dataTypes = Collections.unmodifiableList(new ArrayList<>(dataTypes));
    066        this.length = length;
    067        this.directoryType = exifDirectory;
    068        this.isOffset = isOffset;
    069    }
    070
    071    public byte[] encodeValue(final AbstractFieldType abstractFieldType, final Object value, final ByteOrder byteOrder) throws ImagingException {
    072        return abstractFieldType.writeData(value, byteOrder);
    073    }
    074
    075    public String getDescription() {
    076        return tag + " (0x" + Integer.toHexString(tag) + ": " + name + "): ";
    077    }
    078
    079    /**
    080     *
    081     * @param entry the TIFF field whose value to return
    082     * @return the value of the TIFF field
    083     * @throws ImagingException thrown by subclasses
    084     */
    085    public Object getValue(final TiffField entry) throws ImagingException {
    086        return entry.getFieldType().getValue(entry);
    087    }
    088
    089    public boolean isOffset() {
    090        return isOffset;
    091    }
    092
    093    public boolean isText() {
    094        return false;
    095    }
    096
    097    @Override
    098    public String toString() {
    099        return "[TagInfo. tag: " + tag + " (0x" + Integer.toHexString(tag) + ", name: " + name + "]";
    100    }
    101}