com.alogic.xscript.plugins.RepeatObj Maven / Gradle / Ivy
package com.alogic.xscript.plugins;
import com.alogic.xscript.ExecuteWatcher;
import com.alogic.xscript.Logiclet;
import com.alogic.xscript.LogicletContext;
import com.alogic.xscript.doc.XsObject;
import com.alogic.xscript.doc.json.JsonObject;
import com.anysoft.util.BaseException;
import com.anysoft.util.Properties;
import com.anysoft.util.PropertiesConstants;
import java.util.Iterator;
import java.util.Map;
/**
* 对json对象的子对象进行遍历
*
* @since 1.6.12.50 [20191202 duanyy]
*/
public class RepeatObj extends Segment{
protected String key = "$key";
public RepeatObj(String tag, Logiclet p) {
super(tag, p);
}
public void configure(Properties p){
super.configure(p);
key = PropertiesConstants.getString(p,"key",key,true);
}
@Override
protected void onExecute(XsObject root,XsObject current, LogicletContext ctx, ExecuteWatcher watcher) {
if (current instanceof JsonObject){
Map jsonMap = (Map) current.getContent();
Iterator> iter = jsonMap.entrySet().iterator();
while (iter.hasNext()){
Map.Entry entry = iter.next();
Object val = entry.getValue();
if (val instanceof Map,?>){
ctx.SetValue(key, entry.getKey());
super.onExecute(root, new JsonObject("current",(Map)val), ctx, watcher);
}
}
}else{
throw new BaseException("core.e1000",
String.format("Tag %s does not support protocol %s",this.getXmlTag(),root.getClass().getName()));
}
}
}