org.springframework.util.MessageSourceUtils Maven / Gradle / Ivy
package org.springframework.util;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Collections;
import java.util.Enumeration;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Properties;
import org.dbunit.dataset.Column;
import org.dbunit.dataset.DataSetException;
import org.dbunit.dataset.IDataSet;
import org.dbunit.dataset.ITable;
import org.dbunit.dataset.excel.XlsDataSet;
import org.springframework.core.io.Resource;
import org.springframework.util.comparator.NullSafeComparator;
/**
*
public static MessageSource getMessageSource() {
if (messageSource == null) {
RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
if (requestAttributes instanceof ServletRequestAttributes) {
HttpServletRequest servletRequest = ((ServletRequestAttributes) requestAttributes).getRequest();
if (servletRequest instanceof HttpServletRequest) {
messageSource = WebApplicationContextUtils.getRequiredWebApplicationContext(servletRequest.getServletContext()).getBean(MessageSource.class);
}
}
}
return messageSource;
}
*
*/
public final class MessageSourceUtils {
public static void write(Properties source, Resource target, Locale locale) {
Assert.notNull(source, "'source' must not be null");
Assert.notNull(target, "'target' must not be null");
try {
File directory = target.getFile().getCanonicalFile();
Assert.isTrue(directory != null && (directory.mkdirs() || directory.isDirectory()), "Create Directory fail..." + directory);
String filename = locale == null ? directory.getName() + ".properties" : directory.getName() + '_' + locale + ".properties";
try (OutputStream outputStream = new FileOutputStream(new File(directory, filename))) {
getProperties(source).store(outputStream, null);
}
}
catch (IOException e) {
throw new RuntimeException(e);
}
}
/**
* @see java.util.Map#getOrDefault(Object, Object)
* @see java.util.Map#putIfAbsent(Object, Object)
*/
public static void write(Resource source, Resource target) {
Assert.notNull(source, "'source' must not be null");
Assert.notNull(target, "'target' must not be null");
try (InputStream inputStream = source.getInputStream()) {
IDataSet dataSet = new XlsDataSet(inputStream);
Map> map = new LinkedHashMap>();
for (String tableName : dataSet.getTableNames()) {
ITable table = dataSet.getTable(tableName);
String keyColumnName = null;
for (Column column : table.getTableMetaData().getColumns()) {
if (keyColumnName == null && (keyColumnName = column.getColumnName()) != null) {
continue;
}
String columnName = column.getColumnName();
for (int row = 0; row < table.getRowCount(); row++) {
Object key = table.getValue(row, keyColumnName);
Object value = table.getValue(row, columnName);
if (key == null || value == null) {
continue;
}
Map locale = map.getOrDefault(tableName, new LinkedHashMap());
map.put(tableName, locale);
Properties properties = locale.getOrDefault(columnName, new Properties());
locale.put(columnName, properties);
properties.put(key, value);
}
}
}
File directory = target.getFile().getCanonicalFile();
Assert.isTrue(directory != null && (directory.mkdirs() || directory.isDirectory()), "Create Directory fail..." + directory);
for (Iterator>> iterator = map.entrySet().iterator(); iterator.hasNext();) {
Entry> next = iterator.next();
String key = next.getKey();
boolean first = true;
for (Entry entry : next.getValue().entrySet()) {
String locale = entry.getKey();
Properties properties = entry.getValue();
if (first) {
try (OutputStream outputStream = new FileOutputStream(new File(directory, key + ".properties"))) {
getProperties(properties).store(outputStream, null);
first = false;
}
}
try (OutputStream outputStream = new FileOutputStream(new File(directory, key + '_' + locale + ".properties"))) {
getProperties(properties).store(outputStream, null);
}
}
}
}
catch (IOException | DataSetException e) {
throw new RuntimeException(e);
}
}
private static Properties getProperties(Map map) {
Properties properties = new Properties() {
private static final long serialVersionUID = 7155231228035463990L;
/**
* @see java.util.TreeSet#TreeSet(java.util.Collection)
*/
@SuppressWarnings("unchecked")
@Override
public synchronized Enumeration