org.dbflute.helper.mapstring.MapListFile Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of dbflute-runtime Show documentation
Show all versions of dbflute-runtime Show documentation
The runtime library of DBFlute
/*
* Copyright 2014-2023 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 org.dbflute.helper.mapstring;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
/**
* @author jflute
* @since 0.9.7.1 (2010/06/06 Sunday)
* @deprecated use DfMapFile
*/
public class MapListFile {
// ===================================================================================
// Definition
// ==========
private static final String UTF8_ENCODING = "UTF-8";
// ===================================================================================
// Attribute
// =========
protected final String _fileEncoding;
protected final String _lineCommentMark;
protected boolean _skipLineSeparator;
protected boolean _checkDuplicateEntry;
// ===================================================================================
// Constructor
// ===========
public MapListFile() { // as default
_fileEncoding = UTF8_ENCODING;
_lineCommentMark = "#";
}
// ===================================================================================
// Map
// ===
// -----------------------------------------------------
// Read
// ----
/**
* Read the map string file.
* If the type of values is various type, this method is available.
* A trimmed line that starts with '#' is treated as line comment.
* This is the most basic method in the map-handling methods.
*
* map:{
* ; key1 = string-value1
* ; key2 = list:{element1 ; element2 }
* ; key3 = map:{key1 = value1 ; key2 = value2 }
* ; ... = ...
* }
*
* @param ins The input stream for DBFlute property file, which is closed here. (NotNull)
* @return The read map. (NotNull)
* @throws IOException When it fails to handle the IO.
*/
public Map readMap(InputStream ins) throws IOException {
final String mapString = readString(ins);
if (mapString.trim().length() == 0) {
return newLinkedHashMap();
}
final MapListString mapListString = createMapListString();
return mapListString.generateMap(mapString);
}
/**
* Read the map string file as string value.
* If the type of all values is string type, this method is available.
* A trimmed line that starts with '#' is treated as line comment.
*
* e.g.
* map:{
* ; key1 = string-value1
* ; key2 = string-value2
* ; ... = ...
* }
*
* @param ins The input stream for DBFlute property file, which is closed here. (NotNull)
* @return The read map whose values is string. (NotNull)
* @throws IOException When it fails to handle the IO.
*/
public Map readMapAsStringValue(InputStream ins) throws IOException {
final Map resultMap = new LinkedHashMap();
final Map map = readMap(ins);
final Set> entrySet = map.entrySet();
for (Entry entry : entrySet) {
resultMap.put(entry.getKey(), (String) entry.getValue());
}
return resultMap;
}
/**
* Read the map string file as string list value.
* If the type of all values is string list type, this method is available.
* A trimmed line that starts with '#' is treated as line comment.
*
* e.g.
* map:{
* ; key1 = list:{string-element1 ; string-element2 ; ...}
* ; key2 = list:{string-element1 ; string-element2 ; ...}
* ; ... = list:{...}
* }
*
* @param ins The input stream for DBFlute property file, which is closed here. (NotNull)
* @return The read map whose values is string list. (NotNull)
* @throws IOException When it fails to handle the IO.
*/
@SuppressWarnings("unchecked")
public Map> readMapAsStringListValue(InputStream ins) throws IOException {
final Map> resultMap = newLinkedHashMap();
final Map map = readMap(ins);
final Set> entrySet = map.entrySet();
for (Entry entry : entrySet) {
resultMap.put(entry.getKey(), (List) entry.getValue());
}
return resultMap;
}
/**
* Read the map string file as string map value.
* If the type of all values is string map type, this method is available.
* A trimmed line that starts with '#' is treated as line comment.
*
* e.g.
* map:{
* ; key1 = map:{string-key1 = string-value1 ; string-key2 = string-value2 }
* ; key2 = map:{string-key1 = string-value1 ; string-key2 = string-value2 }
* ; ... = map:{...}
* }
*
* @param ins The input stream for DBFlute property file, which is closed here. (NotNull)
* @return The read map whose values is string map. (NotNull)
* @throws IOException When it fails to handle the IO.
*/
@SuppressWarnings("unchecked")
public Map> readMapAsStringMapValue(InputStream ins) throws IOException {
final Map> resultMap = newLinkedHashMap();
final Map map = readMap(ins);
final Set> entrySet = map.entrySet();
for (Entry entry : entrySet) {
resultMap.put(entry.getKey(), (Map) entry.getValue());
}
return resultMap;
}
// -----------------------------------------------------
// Write
// -----
public void writeMap(OutputStream ous, Map map) throws IOException {
final MapListString mapListString = createMapListString();
final String mapString = mapListString.buildMapString(map);
writeString(ous, mapString);
}
// ===================================================================================
// List
// ====
// -----------------------------------------------------
// Read
// ----
/**
* Read the list string file.
* If the type of values is various type, this method is available.
* A trimmed line that starts with '#' is treated as line comment.
*
* list:{
* ; element1
* ; list:{element2-1 ; element2-2 }
* ; map:{key3-1 = value3-1 ; key3-2 = value3-2 }
* ; ... = ...
* }
*
* @param ins The input stream for DBFlute property file, which is closed here. (NotNull)
* @return The read list. (NotNull)
* @throws IOException When it fails to handle the IO.
*/
public List
© 2015 - 2025 Weber Informatics LLC | Privacy Policy