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

com.alogic.remote.xscript.request.ByJson Maven / Gradle / Ivy

There is a newer version: 1.6.16
Show newest version
package com.alogic.remote.xscript.request;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.alogic.json.JsonFactory;
import com.anysoft.util.Settings;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import com.alogic.remote.Request;
import com.alogic.remote.xscript.RequestHandler;
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.Properties;
import com.anysoft.util.XmlElementProperties;

/**
 * 通过构造的JSON文档发送消息体
 * 
 * @author yyduan
 * @since 1.6.10.3
 */
public class ByJson extends RequestHandler {
	protected JsonFactory jsonFactory = Settings.getToolkit(JsonFactory.class);

	/**
	 * 子节点
	 */
	protected List children = new ArrayList(); // NOSONAR
	
	public ByJson(String tag, Logiclet p) {
		super(tag, p);
	}

	@Override
	public void configure(Element element, Properties props) {
		XmlElementProperties p = new XmlElementProperties(element, props);
		NodeList nodeList = element.getChildNodes();
		
		for (int i = 0 ; i < nodeList.getLength() ; i ++){
			Node n = nodeList.item(i);
			
			if (n.getNodeType() != Node.ELEMENT_NODE){
				//只处理Element节点
				continue;
			}
			
			Element e = (Element)n;
			String xmlTag = e.getNodeName();		
			Logiclet statement = createLogiclet(xmlTag, this);
			
			if (statement != null){
				statement.configure(e, p);
				children.add(statement);
			}
		}
		
		configure(p);
	}		
	
	@Override
	protected void onExecute(final Request req, final XsObject root,final XsObject current, final LogicletContext ctx,
			final ExecuteWatcher watcher) {
		Map jsonData = new HashMap();
		XsObject doc = new JsonObject("root",jsonData);
		for (Logiclet child:children){
			child.execute(doc, doc, ctx, watcher);
		}
		
		String text = jsonFactory.toJsonString(jsonData);
		
		req.setBody(text);
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy