net.nicoll.boot.metadata.CsvMetadataFormatter.java Source code

Java tutorial

Introduction

Here is the source code for net.nicoll.boot.metadata.CsvMetadataFormatter.java

Source

/*
 * Copyright 2012-2014 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package net.nicoll.boot.metadata;

import java.io.IOException;
import java.io.StringWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.supercsv.cellprocessor.Optional;
import org.supercsv.cellprocessor.constraint.NotNull;
import org.supercsv.cellprocessor.ift.CellProcessor;
import org.supercsv.io.CsvMapWriter;
import org.supercsv.prefs.CsvPreference;

import org.springframework.configurationmetadata.ConfigurationMetadataGroup;
import org.springframework.configurationmetadata.ConfigurationMetadataProperty;
import org.springframework.configurationmetadata.ConfigurationMetadataRepository;
import org.springframework.configurationmetadata.ConfigurationMetadataSource;

/**
 *
 * @author Stephane Nicoll
 */
public class CsvMetadataFormatter extends AbstractMetadataFormatter {

    static final CellProcessor[] processors = new CellProcessor[] { new NotNull(), // id
            new Optional(), // description
            new Optional(), // default value
            new Optional(), // type
    };

    static final String[] header = new String[] { "id", "description", "defaultValue", "type" };

    @Override
    public String formatMetadata(ConfigurationMetadataRepository repository) throws IOException {
        StringWriter out = new StringWriter();
        CsvMapWriter writer = new CsvMapWriter(out, CsvPreference.STANDARD_PREFERENCE);
        try {
            List<ConfigurationMetadataGroup> groups = sortGroups(repository.getAllGroups().values());
            Map<String, Object> content = new HashMap<String, Object>();
            for (ConfigurationMetadataGroup group : groups) {
                content.clear();
                StringBuilder groupSb = new StringBuilder(group.getId()).append(" - ");
                for (ConfigurationMetadataSource source : group.getSources().values()) {
                    groupSb.append(source.getType()).append(" ");
                }
                content.put("id", groupSb.toString());
                writer.write(content, header, processors);

                List<ConfigurationMetadataProperty> properties = sortProperties(group.getProperties().values());
                for (ConfigurationMetadataProperty property : properties) {
                    content.clear();
                    content.put("id", property.getId());
                    content.put("description", extractTagLine(property, null));
                    content.put("defaultValue", property.getDefaultValue());
                    content.put("type", property.getType());
                    writer.write(content, header, processors);
                }
            }
        } finally {
            writer.close();
        }
        return out.toString();
    }

}