⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 bindinghandler.java

📁 一个java写的business process management系统
💻 JAVA
字号:
/*
 * Copyright (c) 2003-2004, Alexander Greif
 * All rights reserved.
 * 
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 * 
 *     * Redistributions of source code must retain the above copyright
 *       notice, this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above copyright
 *       notice, this list of conditions and the following disclaimer in the
 *       documentation and/or other materials provided with the distribution.
 *     * Neither the name of the Flow4J-Eclipse project nor the names of its
 *       contributors may be used to endorse or promote products derived from
 *       this software without specific prior written permission.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
 * THE POSSIBILITY OF SUCH DAMAGE.
 */

package net.orthanc.flow4j.model.bind;

import java.io.BufferedWriter;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;

import net.orthanc.flow4j.model.bind.converters.BendpointConverter;
import net.orthanc.flow4j.model.bind.converters.BooleanTransitionConverter;
import net.orthanc.flow4j.model.bind.converters.CallFlowletConverter;
import net.orthanc.flow4j.model.bind.converters.DecisionFlowletConverter;
import net.orthanc.flow4j.model.bind.converters.EndFlowletConverter;
import net.orthanc.flow4j.model.bind.converters.FlowModelConverter;
import net.orthanc.flow4j.model.bind.converters.FlowletLabelConverter;
import net.orthanc.flow4j.model.bind.converters.JavaTaskFlowletConverter;
import net.orthanc.flow4j.model.bind.converters.JoinFlowletConverter;
import net.orthanc.flow4j.model.bind.converters.JumpFlowletConverter;
import net.orthanc.flow4j.model.bind.converters.PropertyConverter;
import net.orthanc.flow4j.model.bind.converters.ScriptTaskFlowletConverter;
import net.orthanc.flow4j.model.bind.converters.StartFlowletConverter;
import net.orthanc.flow4j.model.bind.converters.TemplateFlowletConverter;
import net.orthanc.flow4j.model.bind.converters.TransitionConverter;
import net.orthanc.flow4j.model.bind.converters.TransitionSourceFlowletConverter;
import net.orthanc.flow4j.model.bind.converters.TransitionTargetFlowletConverter;

import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.io.xml.DomDriver;

/**
 * @author greifa
 *
 * To change the template for this generated type comment go to
 * Window>Preferences>Java>Code Generation>Code and Comments
 */
public class BindingHandler {

	private static BindingHandler instance;
	private XStream xstream;

	private BindingHandler() {
		xstream = new XStream(new DomDriver());
		// does not require XPP3 library

		xstream.alias("flowmodel", FlowModelBind.class);
		xstream.registerConverter(new FlowModelConverter());

		//xstream.alias("flowletlabel", FlowletLabelBind.class);
		xstream.registerConverter(new FlowletLabelConverter());

		xstream.alias("startflowlet", StartFlowletBind.class);
		xstream.registerConverter(new StartFlowletConverter());

		xstream.alias("javataskflowlet", JavaTaskFlowletBind.class);
		xstream.registerConverter(new JavaTaskFlowletConverter());

		xstream.alias("scripttaskflowlet", ScriptTaskFlowletBind.class);
		xstream.registerConverter(new ScriptTaskFlowletConverter());

		xstream.alias("decisionflowlet", DecisionFlowletBind.class);
		xstream.registerConverter(new DecisionFlowletConverter());

		xstream.alias("joinflowlet", JoinFlowletBind.class);
		xstream.registerConverter(new JoinFlowletConverter());

		xstream.alias("callflowlet", CallFlowletBind.class);
		xstream.registerConverter(new CallFlowletConverter());

		xstream.alias("jumpflowlet", JumpFlowletBind.class);
		xstream.registerConverter(new JumpFlowletConverter());

		xstream.alias("templateflowlet", TemplateFlowletBind.class);
		xstream.registerConverter(new TemplateFlowletConverter());

		xstream.alias("endflowlet", EndFlowletBind.class);
		xstream.registerConverter(new EndFlowletConverter());

		xstream.alias("property", PropertyBind.class);
		xstream.registerConverter(new PropertyConverter());

		//	connections
		xstream.alias("transition", TransitionBind.class);
		xstream.registerConverter(new TransitionConverter());

		xstream.alias("booleantransition", BooleanTransitionBind.class);
		xstream.registerConverter(new BooleanTransitionConverter());

		xstream.alias("sourceflowlet", TransitionSourceFlowletBind.class);
		xstream.registerConverter(new TransitionSourceFlowletConverter());

		xstream.alias("targetflowlet", TransitionTargetFlowletBind.class);
		xstream.registerConverter(new TransitionTargetFlowletConverter());

		xstream.alias("bendpoint", BendpointBind.class);
		xstream.registerConverter(new BendpointConverter());
	}

	public static BindingHandler getInstance() {
		if (instance == null)
			instance = new BindingHandler();

		return instance;
	}

	public FlowModelBind loadFlowModel(File file) throws IOException {
		return loadFlowModel(file, null);
	}

	public FlowModelBind loadFlowModel(
		File file,
		ClassLoader taskFlowletClassLoader)
		throws IOException {
		//	set the task class loader for property verification
		JavaTaskFlowletBind.setTaskClassLoader(taskFlowletClassLoader);

		FileReader fr = null;
		FlowModelBind flowModelBind = null;
		try {
			fr = new FileReader(file);
			flowModelBind = (FlowModelBind) xstream.fromXML(fr);
		} catch (FileNotFoundException e) {
			throw e;
		} finally {
			if (fr != null)
				fr.close();
		}

		return flowModelBind;
	}


	public void saveFlowModel(FlowModelBind flowModelBind, OutputStream out) throws IOException {
		OutputStreamWriter osw = new OutputStreamWriter(out, "UTF-8");
		BufferedWriter bw = new BufferedWriter(osw);
		bw.write("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
		bw.newLine();
		xstream.toXML(flowModelBind, bw);
		bw.flush();
	}


	public ByteArrayInputStream saveFlowModel(FlowModelBind flowModelBind) throws IOException {
		ByteArrayOutputStream bout = new ByteArrayOutputStream();
		saveFlowModel(flowModelBind, bout);
		bout.close();
		
		return new ByteArrayInputStream(bout.toByteArray());
	}






}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -