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