View Javadoc
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 java.util.ArrayList;
20  import java.util.List;
21  import java.util.Objects;
22  
23  import org.apache.commons.imaging.common.ImageMetadata;
24  import org.apache.commons.imaging.formats.tiff.TiffImageMetadata;
25  import org.apache.commons.imaging.internal.Debug;
26  
27  /**
28   * @since 1.0-alpha6
29   */
30  public class PngImageMetadata implements ImageMetadata {
31  
32      private static final String NEWLINE = System.lineSeparator();
33  
34      private final ImageMetadata textualInformation;
35      private final TiffImageMetadata exif;
36  
37      public PngImageMetadata(final ImageMetadata textualInformation) {
38          this(textualInformation, null);
39      }
40  
41      public PngImageMetadata(final ImageMetadata textualInformation, final TiffImageMetadata exif) {
42          this.textualInformation = Objects.requireNonNull(textualInformation);
43          this.exif = exif;
44      }
45  
46      public void dump() {
47          Debug.debug(this.toString());
48      }
49  
50      public TiffImageMetadata getExif() {
51          return exif;
52      }
53  
54      @Override
55      public List<? extends ImageMetadataItem> getItems() {
56          if (exif == null) {
57              return textualInformation.getItems();
58          }
59  
60          final ArrayList<ImageMetadataItem> result = new ArrayList<>(textualInformation.getItems());
61          result.addAll(exif.getItems());
62          return result;
63      }
64  
65      public ImageMetadata getTextualInformation() {
66          return textualInformation;
67      }
68  
69      @Override
70      public String toString() {
71          return toString(null);
72      }
73  
74      @Override
75      public String toString(String prefix) {
76          if (prefix == null) {
77              prefix = "";
78          }
79  
80          final StringBuilder result = new StringBuilder();
81  
82          result.append(prefix);
83          result.append("Textual information:");
84          result.append(NEWLINE);
85          result.append(textualInformation.toString("\t"));
86  
87          if (exif != null) {
88              result.append(NEWLINE);
89              result.append(prefix);
90              result.append("Exif metadata:");
91              result.append(NEWLINE);
92              result.append(exif.toString("\t"));
93          }
94  
95          return result.toString();
96      }
97  }