![JAR search and dependency download from the Maven repository](/logo.png)
com.tsc9526.monalisa.tools.template.VarTemplate Maven / Gradle / Ivy
/*******************************************************************************************
* Copyright (c) 2016, zzg.zhou([email protected])
*
* Monalisa is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see .
*******************************************************************************************/
package com.tsc9526.monalisa.tools.template;
import java.util.HashMap;
import java.util.LinkedHashSet;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
import com.tsc9526.monalisa.tools.logger.Logger;
import com.tsc9526.monalisa.tools.string.MelpString;
/**
*
* @author zzg.zhou([email protected])
*/
public class VarTemplate {
static Logger logger=Logger.getLogger(VarTemplate.class);
private Map variables;
private String nullVar ="";
private boolean throwExceptionOnVarNotFound=false;
public VarTemplate(Map variables){
this.variables=variables;
}
public VarTemplate(Properties variables){
this.variables=new HashMap();
for(Object key:variables.keySet()){
this.variables.put((String)key, (String)variables.get(key));
}
}
public String getValue(String templateString){
if(templateString==null){
return null;
}
if(templateString.length()==0){
return "";
}
Set missingVars=new LinkedHashSet();
StringBuilder sb=new StringBuilder();
for(int i=0;i0){
String var=templateString.substring(i+2,x2);
String value=variables.get(var);
if(value!=null){
sb.append(value);
}else{
sb.append(nullVar);
missingVars.add(var);
}
i=x2;
}else{
sb.append(c1);
}
}else if( (c2>='a' && c2<='z') || (c2>='A' && c2<='Z') || c1=='_'){//$var_abc
StringBuilder var=new StringBuilder();
var.append(c2);
i+=2;
while(i='a' && c3<='z') || (c3>='A' && c3<='Z') || (c3>='0' && c3<='9') || c3=='_'){
var.append(c3);
}else{
i--;
break;
}
i++;
}
String value=variables.get(var.toString());
if(value!=null){
sb.append(value);
}else{
sb.append(nullVar);
missingVars.add(var.toString());
}
}else{//$ 单独一个
sb.append("$");
}
}else{
sb.append(c1);
}
}
StringBuilder msg=new StringBuilder();
if(missingVars.size()>0){
msg.append("Missing variables: ");
msg.append(MelpString.toString(missingVars));
msg.append(", template: "+templateString+", exist variable: "+MelpString.toString(variables));
}
if(throwExceptionOnVarNotFound && missingVars.size()>0){
throw new RuntimeException(msg.toString());
}else{
if(msg.length()>0){
logger.warn(msg.toString());
}
return sb.toString();
}
}
public String getNullVar() {
return nullVar;
}
public VarTemplate setNullVar(String nullVar) {
this.nullVar = nullVar;
return this;
}
public boolean isThrowExceptionOnVarNotFound() {
return throwExceptionOnVarNotFound;
}
public VarTemplate setThrowExceptionOnVarNotFound(boolean throwVarNotFound) {
this.throwExceptionOnVarNotFound = throwVarNotFound;
return this;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy