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.common;
018
019import java.util.ArrayList;
020import java.util.List;
021
022public class GenericImageMetadata implements ImageMetadata {
023    private static final String NEWLINE = System.getProperty("line.separator");
024    private final List<ImageMetadataItem> items = new ArrayList<>();
025
026    public void add(final String keyword, final String text) {
027        add(new GenericImageMetadataItem(keyword, text));
028    }
029
030    public void add(final ImageMetadataItem item) {
031        items.add(item);
032    }
033
034    @Override
035    public List<? extends ImageMetadataItem> getItems() {
036        return new ArrayList<>(items);
037    }
038
039    @Override
040    public String toString() {
041        return toString(null);
042    }
043
044    @Override
045    public String toString(String prefix) {
046        if (null == prefix) {
047            prefix = "";
048        }
049
050        final StringBuilder result = new StringBuilder();
051        for (int i = 0; i < items.size(); i++) {
052            if (i > 0) {
053                result.append(NEWLINE);
054            }
055            // if (null != prefix)
056            // result.append(prefix);
057
058            final ImageMetadataItem item = items.get(i);
059            result.append(item.toString(prefix + "\t"));
060
061            // Debug.debug("prefix", prefix);
062            // Debug.debug("item", items.get(i));
063            // Debug.debug();
064        }
065        return result.toString();
066    }
067
068    public static class GenericImageMetadataItem implements ImageMetadataItem {
069        private final String keyword;
070        private final String text;
071
072        public GenericImageMetadataItem(final String keyword, final String text) {
073            this.keyword = keyword;
074            this.text = text;
075        }
076
077        public String getKeyword() {
078            return keyword;
079        }
080
081        public String getText() {
082            return text;
083        }
084
085        @Override
086        public String toString() {
087            return toString(null);
088        }
089
090        @Override
091        public String toString(final String prefix) {
092            final String result = keyword + ": " + text;
093            if (null != prefix) {
094                return prefix + result;
095            } else {
096                return result;
097            }
098        }
099    }
100
101}