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

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

There is a newer version: 1.6.17
Show newest version
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 org.apache.commons.lang3.StringUtils;
import org.yaml.snakeyaml.Yaml;

import java.util.Iterator;
import java.util.Map;

/**
 * 从yaml中加载文档
 *
 * @since 1.6.12.27 [20190403 duanyy] 
*/ public class FromYaml extends Segment{ protected String tag = "data"; protected String content = ""; protected boolean extend = false; protected static ThreadLocal yamlLocal = new ThreadLocal(){ protected Yaml initialValue() { return new Yaml(); }; }; public FromYaml(String tag, Logiclet p) { super(tag, p); } @Override public void configure(Properties p){ super.configure(p); tag = PropertiesConstants.getRaw(p,"tag",tag); content = PropertiesConstants.getRaw(p,"content",""); extend = PropertiesConstants.getBoolean(p,"extend",extend); } @SuppressWarnings("unchecked") @Override protected void onExecute(XsObject root, XsObject current, LogicletContext ctx, ExecuteWatcher watcher) { if (current instanceof JsonObject){ Map jsonCurrent = (Map) current.getContent(); String v = ctx.transform(content); if (StringUtils.isNotEmpty(v)){ Object template = yamlLocal.get().load(v); if (template instanceof Map){ if (extend){ Map data = (Map)template; Iterator> iter = data.entrySet().iterator(); while (iter.hasNext()){ Map.Entry entry = iter.next(); jsonCurrent.put(entry.getKey(), entry.getValue()); } super.onExecute(root, current, ctx, watcher); }else{ String tagValue = ctx.transform(tag); if (StringUtils.isNotEmpty(tagValue)){ jsonCurrent.put(tagValue, template); JsonObject newCurrent = new JsonObject(tagValue,(Map)template); super.onExecute(root, newCurrent, ctx, watcher); } } }else{ String tagValue = ctx.transform(tag); if (StringUtils.isNotEmpty(tagValue)){ jsonCurrent.put(tagValue, template); } } } }else{ throw new BaseException("core.e1000", String.format("Tag %s does not support protocol %s", getXmlTag(),root.getClass().getName())); } } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy