All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.alogic.xscript.plugins.Array Maven / Gradle / Ivy

There is a newer version: 1.6.17
Show newest version
package com.alogic.xscript.plugins;

import com.alogic.xscript.doc.json.JsonArray;
import org.apache.commons.lang3.StringUtils;
import com.alogic.xscript.ExecuteWatcher;
import com.alogic.xscript.Logiclet;
import com.alogic.xscript.LogicletContext;
import com.alogic.xscript.doc.XsArray;
import com.alogic.xscript.doc.XsObject;
import com.anysoft.util.Properties;
import com.anysoft.util.PropertiesConstants;
import java.util.ArrayList;
import java.util.List;

/**
 * 在当前文档增加一个数组
 * 
 * @author duanyy
 * @version 1.6.8.14 [20170509 duanyy] 
* - 增加xscript的中间文档模型,以便支持多种报文协议
* * @version 1.6.9.1 [20170516 duanyy]
* - 修复部分插件由于使用新的文档模型产生的兼容性问题
* * @version 1.6.12.28 [20190407]
* - 增加子数组的插件实现
*/ public class Array extends Segment { protected String tag = "data"; protected String id = "$array"; public Array(String tag, Logiclet p) { super(tag, p); } @Override public void configure(Properties p){ super.configure(p); id = PropertiesConstants.getString(p,"id",id,true); tag = PropertiesConstants.getRaw(p,"tag", tag); } protected void onExecute(XsObject root,XsObject current, LogicletContext ctx, ExecuteWatcher watcher) { String tagValue = ctx.transform(tag); if (StringUtils.isNotEmpty(tagValue)){ XsArray array = current.getArrayChild(tagValue, true); try { ctx.setObject(id, array); super.onExecute(root, current, ctx, watcher); }finally{ ctx.removeObject(id); } } } /** * 子数组 */ public static class Child extends Segment{ protected String id = "$array"; protected String cid = "$array-child"; public Child(String tag, Logiclet p) { super(tag, p); } @Override public void configure(Properties p){ super.configure(p); id = PropertiesConstants.getString(p,"id",id,true); cid = PropertiesConstants.getString(p,"cid",id,true); } @SuppressWarnings("unchecked") protected void onExecute(XsObject root,XsObject current, LogicletContext ctx, ExecuteWatcher watcher) { XsArray list = ctx.getObject(id); if (list != null && list instanceof JsonArray){ List child = new ArrayList(); JsonArray array = new JsonArray(child); try { ctx.setObject(cid,array); super.onExecute(root,current,ctx,watcher); }finally{ ctx.removeObject(cid); if (!child.isEmpty()){ ((List)((JsonArray)list).getContent()).add(array.getContent()); } } } } } }