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 */
017package org.apache.commons.imaging.formats.png;
018
019import java.util.ArrayList;
020import java.util.List;
021import java.util.Objects;
022
023import org.apache.commons.imaging.common.ImageMetadata;
024import org.apache.commons.imaging.formats.tiff.TiffImageMetadata;
025import org.apache.commons.imaging.internal.Debug;
026
027/**
028 * @since 1.0-alpha6
029 */
030public class PngImageMetadata implements ImageMetadata {
031
032    private static final String NEWLINE = System.lineSeparator();
033
034    private final ImageMetadata textualInformation;
035    private final TiffImageMetadata exif;
036
037    public PngImageMetadata(final ImageMetadata textualInformation) {
038        this(textualInformation, null);
039    }
040
041    public PngImageMetadata(final ImageMetadata textualInformation, final TiffImageMetadata exif) {
042        this.textualInformation = Objects.requireNonNull(textualInformation);
043        this.exif = exif;
044    }
045
046    public void dump() {
047        Debug.debug(this.toString());
048    }
049
050    public TiffImageMetadata getExif() {
051        return exif;
052    }
053
054    @Override
055    public List<? extends ImageMetadataItem> getItems() {
056        if (exif == null) {
057            return textualInformation.getItems();
058        }
059
060        final ArrayList<ImageMetadataItem> result = new ArrayList<>(textualInformation.getItems());
061        result.addAll(exif.getItems());
062        return result;
063    }
064
065    public ImageMetadata getTextualInformation() {
066        return textualInformation;
067    }
068
069    @Override
070    public String toString() {
071        return toString(null);
072    }
073
074    @Override
075    public String toString(String prefix) {
076        if (prefix == null) {
077            prefix = "";
078        }
079
080        final StringBuilder result = new StringBuilder();
081
082        result.append(prefix);
083        result.append("Textual information:");
084        result.append(NEWLINE);
085        result.append(textualInformation.toString("\t"));
086
087        if (exif != null) {
088            result.append(NEWLINE);
089            result.append(prefix);
090            result.append("Exif metadata:");
091            result.append(NEWLINE);
092            result.append(exif.toString("\t"));
093        }
094
095        return result.toString();
096    }
097}