Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/*
* Copyright 2014-2015 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.jprop;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Properties;
import java.util.Set;
import org.dbflute.helper.jprop.exception.JavaPropertiesImplicitOverrideException;
import org.dbflute.helper.jprop.exception.JavaPropertiesLonelyOverrideException;
import org.dbflute.helper.jprop.exception.JavaPropertiesReadFailureException;
import org.dbflute.helper.jprop.exception.JavaPropertiesStreamNotFoundException;
import org.dbflute.helper.message.ExceptionMessageBuilder;
import org.dbflute.util.DfCollectionUtil;
import org.dbflute.util.DfCollectionUtil.AccordingToOrderIdExtractor;
import org.dbflute.util.DfCollectionUtil.AccordingToOrderResource;
import org.dbflute.util.DfReflectionUtil;
import org.dbflute.util.Srl;
import org.dbflute.util.Srl.ScopeInfo;
/**
* @author jflute
* @since 1.0.1 (2012/12/15 Saturday)
*/
public class JavaPropertiesReader {
// ===================================================================================
// Definition
// ==========
public static final String OVERRIDE_ANNOTATION = "@Override";
public static final String SECURE_ANNOTATION = "@Secure";
// ===================================================================================
// Attribute
// =========
// -----------------------------------------------------
// Basic
// -----
protected final String _title;
protected final JavaPropertiesStreamProvider _streamProvider;
// -----------------------------------------------------
// Option
// ------
protected final Map _extendsProviderMap = newLinkedHashMapSized(4);
protected boolean _checkImplicitOverride;
protected String _streamEncoding; // used if set
// -----------------------------------------------------
// Reflection
// ----------
protected Method _convertMethod; // cached
protected boolean _convertMethodNotFound;
protected final Properties _reflectionProperties = new Properties();
// ===================================================================================
// Constructor
// ===========
public JavaPropertiesReader(String title, JavaPropertiesStreamProvider streamProvider) {
_title = title;
_streamProvider = streamProvider;
}
// -----------------------------------------------------
// Option
// ------
public JavaPropertiesReader extendsProperties(String title, JavaPropertiesStreamProvider noArgLambda) {
if (_extendsProviderMap.containsKey(title)) {
String msg = "The argument 'title' has already been registered:";
msg = msg + " title=" + title + " registered=" + _extendsProviderMap.keySet();
throw new IllegalArgumentException(msg);
}
_extendsProviderMap.put(title, noArgLambda);
return this;
}
public JavaPropertiesReader checkImplicitOverride() {
_checkImplicitOverride = true;
return this;
}
public JavaPropertiesReader encodeAsUTF8() {
_streamEncoding = "UTF-8";
return this;
}
public JavaPropertiesReader encodeAs(String encoding) {
_streamEncoding = encoding;
return this;
}
// ===================================================================================
// Read
// ====
public JavaPropertiesResult read() {
final List propertyList = newArrayList();
final List duplicateKeyList = newArrayList();
final Map keyCommentMap = readKeyCommentMap(duplicateKeyList);
final Properties prop = readPlainProperties();
final List keyList = orderKeyList(prop, keyCommentMap);
for (String key : keyList) {
final String value = prop.getProperty(key);
final String comment = keyCommentMap.get(key);
final JavaPropertiesProperty property = new JavaPropertiesProperty(key, value);
final String defName = Srl.replace(key, ".", "_").toUpperCase();
property.setDefName(defName);
final String camelizedName = Srl.camelize(defName);
property.setCamelizedName(camelizedName);
property.setCapCamelName(Srl.initCap(camelizedName));
property.setUncapCamelName(Srl.initUncap(camelizedName));
final List variableScopeList = newArrayList();
{
final List scopeList;
if (Srl.is_NotNull_and_NotTrimmedEmpty(value)) {
scopeList = Srl.extractScopeList(value, "{", "}"); // e.g. {0} is for {1}.
} else {
scopeList = DfCollectionUtil.emptyList();
}
for (ScopeInfo scopeInfo : scopeList) {
final String content = scopeInfo.getContent();
try {
Integer.valueOf(content);
variableScopeList.add(scopeInfo);
} catch (NumberFormatException ignored) { // e.g. {A} is for {B}
}
}
}
final List variableNumberList = DfCollectionUtil.newArrayList();
for (ScopeInfo scopeInfo : variableScopeList) {
variableNumberList.add(valueOfVariableNumber(key, scopeInfo.getContent()));
}
property.setVariableArgDef(buildVariableArgDef(variableNumberList));
property.setVariableArgSet(buildVariableArgSet(variableNumberList));
property.setVariableNumberList(variableNumberList);
property.setComment(comment);
if (containsSecureAnnotation(property)) {
property.toBeSecure();
}
propertyList.add(property);
}
return prepareResult(prop, propertyList, duplicateKeyList);
}
protected boolean containsSecureAnnotation(JavaPropertiesProperty property) {
final String comment = property.getComment();
return comment != null && Srl.containsIgnoreCase(comment, SECURE_ANNOTATION);
}
// -----------------------------------------------------
// Order Key
// ---------
protected List orderKeyList(Properties prop, final Map keyCommentMap) {
final List