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.common;
18  
19  import java.util.ArrayList;
20  import java.util.List;
21  
22  public class GenericImageMetadata implements ImageMetadata {
23  
24      public static class GenericImageMetadataItem implements ImageMetadataItem {
25  
26          private final String keyword;
27          private final String text;
28  
29          public GenericImageMetadataItem(final String keyword, final String text) {
30              this.keyword = keyword;
31              this.text = text;
32          }
33  
34          public String getKeyword() {
35              return keyword;
36          }
37  
38          public String getText() {
39              return text;
40          }
41  
42          @Override
43          public String toString() {
44              return toString(null);
45          }
46  
47          @Override
48          public String toString(final String prefix) {
49              final String result = keyword + ": " + text;
50              if (null != prefix) {
51                  return prefix + result;
52              }
53              return result;
54          }
55      }
56  
57      private static final String NEWLINE = System.lineSeparator();
58  
59      private final List<ImageMetadataItem> items = new ArrayList<>();
60  
61      public void add(final ImageMetadataItem item) {
62          items.add(item);
63      }
64  
65      public void add(final String keyword, final String text) {
66          add(new GenericImageMetadataItem(keyword, text));
67      }
68  
69      @Override
70      public List<? extends ImageMetadataItem> getItems() {
71          return new ArrayList<>(items);
72      }
73  
74      @Override
75      public String toString() {
76          return toString(null);
77      }
78  
79      @Override
80      public String toString(String prefix) {
81          if (null == prefix) {
82              prefix = "";
83          }
84  
85          final StringBuilder result = new StringBuilder();
86          for (int i = 0; i < items.size(); i++) {
87              if (i > 0) {
88                  result.append(NEWLINE);
89              }
90              // if (null != prefix)
91              // result.append(prefix);
92  
93              final ImageMetadataItem item = items.get(i);
94              result.append(item.toString(prefix + "\t"));
95  
96              // Debug.debug("prefix", prefix);
97              // Debug.debug("item", items.get(i));
98              // Debug.debug();
99          }
100         return result.toString();
101     }
102 
103 }