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 2019, Continual.io
*
* 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 io.continual.util.data.exprEval;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import io.continual.util.data.TypeConvertor;
import io.continual.util.data.json.JsonVisitor;
import io.continual.util.data.json.JsonVisitor.ArrayVisitor;
import io.continual.util.data.json.JsonVisitor.ObjectVisitor;
public class ExpressionEvaluator
{
public ExpressionEvaluator ( ExprDataSource... srcs )
{
fSources = srcs;
}
public Object evaluateSymbol ( String expr )
{
return evaluateSymbol ( expr, fSources );
}
public String evaluateText ( String expr )
{
return evaluateText ( expr, fSources );
}
public int evaluateTextToInt ( Object value, int defaultValue )
{
return evaluateTextToInt ( value, defaultValue, fSources );
}
public long evaluateTextToLong ( Object value, long defaultValue )
{
return evaluateTextToLong ( value, defaultValue, fSources );
}
public boolean evaluateTextToBoolean ( Object value, boolean defaultValue )
{
return evaluateTextToBoolean ( value, defaultValue, fSources );
}
public JSONObject evaluateJsonObject ( JSONObject value )
{
return evaluateJsonObject ( value, fSources );
}
public JSONArray evaluateJsonArray ( JSONArray value )
{
return evaluateJsonArray ( value, fSources );
}
/**
* Evaluate the given expression against the given data sources and return
* an object. If no source can resolve the symbol, null is returned.
* @param symbol the symbol to evaluate
* @param srcs a set of data sources, evaluated in order
* @return an object if found
*/
public static Object evaluateSymbol ( String symbol, ExprDataSource... srcs )
{
for ( ExprDataSource src : srcs )
{
final Object result = src.eval ( symbol );
if ( result != null ) return result;
}
return null;
}
/**
* substitute any occurrence of ${<expr>} with the evaluation of that expression
* @param sourceString the original string
* @param srcs a set of data sources, evaluated in order
* @return a string
*/
public static String evaluateText ( String sourceString, ExprDataSource... srcs )
{
if ( sourceString == null ) return null;
final StringBuffer sb = new StringBuffer ();
do
{
final int open = sourceString.indexOf ( "${" );
if ( open < 0 )
{
// just straight text left
sb.append ( sourceString );
sourceString = "";
}
else
{
// read to "}", use the content as a key into the json
final int closer = sourceString.indexOf ( '}', open );
if ( closer < 0 )
{
// not found. just treat it like plain text
sb.append ( sourceString );
sourceString = "";
}
else
{
sb.append ( sourceString.substring ( 0, open ) );
String key = sourceString.substring ( open+2, closer ).trim ();
String defval = null;
// allow a default value in the key expression via vertical bar separator
final int vertBar = key.indexOf ( '|' );
if ( vertBar > -1 )
{
defval = key.substring ( vertBar + 1 ).trim ();
key = key.substring ( 0, vertBar ).trim ();
}
ExprDataSource[] allSrcs = srcs;
if ( defval != null )
{
final String finalDefVal = defval;
allSrcs = new ExprDataSource [ srcs.length + 1 ];
System.arraycopy ( srcs, 0, allSrcs, 0, srcs.length );
allSrcs [ srcs.length ] = new ExprDataSource ()
{
@Override
public Object eval ( String label ) { return finalDefVal; }
};
}
final Object symval = evaluateSymbol ( key, allSrcs );
sb.append ( symval == null ? "" : symval.toString () );
sourceString = sourceString.substring ( closer + 1 );
}
}
}
while ( sourceString.length () > 0 );
return sb.toString ();
}
/**
* Evaluate all values in the given object.
* @param data an object
* @param srcs a set of data sources, evaluated in order
* @return a new JSON object
*/
public static JSONObject evaluateJsonObject ( JSONObject data, ExprDataSource... srcs )
{
if ( data == null ) return null;
final JSONObject result = new JSONObject ();
JsonVisitor.forEachElement ( data, new ObjectVisitor