|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | + * contributor license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright ownership. |
| 5 | + * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | + * (the "License"); you may not use this file except in compliance with |
| 7 | + * the License. You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | +package org.apache.commons.imaging.formats.png; |
| 18 | + |
| 19 | +import org.apache.commons.imaging.common.ImageMetadata; |
| 20 | +import org.apache.commons.imaging.formats.tiff.TiffImageMetadata; |
| 21 | +import org.apache.commons.imaging.internal.Debug; |
| 22 | + |
| 23 | +import java.util.ArrayList; |
| 24 | +import java.util.List; |
| 25 | +import java.util.Objects; |
| 26 | + |
| 27 | +public class PngImageMetadata implements ImageMetadata { |
| 28 | + |
| 29 | + private static final String NEWLINE = System.getProperty("line.separator"); |
| 30 | + |
| 31 | + private final ImageMetadata textualInformation; |
| 32 | + private final TiffImageMetadata exif; |
| 33 | + |
| 34 | + public PngImageMetadata(ImageMetadata textualInformation) { |
| 35 | + this(textualInformation, null); |
| 36 | + } |
| 37 | + |
| 38 | + public PngImageMetadata(ImageMetadata textualInformation, TiffImageMetadata exif) { |
| 39 | + this.textualInformation = Objects.requireNonNull(textualInformation); |
| 40 | + this.exif = exif; |
| 41 | + } |
| 42 | + |
| 43 | + public ImageMetadata getTextualInformation() { |
| 44 | + return textualInformation; |
| 45 | + } |
| 46 | + |
| 47 | + public TiffImageMetadata getExif() { |
| 48 | + return exif; |
| 49 | + } |
| 50 | + |
| 51 | + @Override |
| 52 | + public String toString() { |
| 53 | + return toString(null); |
| 54 | + } |
| 55 | + |
| 56 | + @Override |
| 57 | + public String toString(String prefix) { |
| 58 | + if (prefix == null) { |
| 59 | + prefix = ""; |
| 60 | + } |
| 61 | + |
| 62 | + final StringBuilder result = new StringBuilder(); |
| 63 | + |
| 64 | + result.append(prefix); |
| 65 | + result.append("Textual information:"); |
| 66 | + result.append(NEWLINE); |
| 67 | + result.append(textualInformation.toString("\t")); |
| 68 | + |
| 69 | + if (exif != null) { |
| 70 | + result.append(NEWLINE); |
| 71 | + result.append(prefix); |
| 72 | + result.append("Exif metadata:"); |
| 73 | + result.append(NEWLINE); |
| 74 | + result.append(exif.toString("\t")); |
| 75 | + } |
| 76 | + |
| 77 | + return result.toString(); |
| 78 | + } |
| 79 | + |
| 80 | + @Override |
| 81 | + public List<? extends ImageMetadataItem> getItems() { |
| 82 | + if (exif == null) { |
| 83 | + return textualInformation.getItems(); |
| 84 | + } |
| 85 | + |
| 86 | + ArrayList<ImageMetadataItem> result = new ArrayList<>(); |
| 87 | + result.addAll(textualInformation.getItems()); |
| 88 | + result.addAll(exif.getItems()); |
| 89 | + return result; |
| 90 | + } |
| 91 | + |
| 92 | + public void dump() { |
| 93 | + Debug.debug(this.toString()); |
| 94 | + } |
| 95 | +} |
0 commit comments