📄 dom4jfpdlparser.java.svn-base
字号:
/**
* Copyright 2003-2008 陈乜云(非也,Chen Nieyun)
* All rights reserved.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation。
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see http://www.gnu.org/licenses. *
*/
package org.fireflow.model.io;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentFactory;
import org.dom4j.Element;
import org.dom4j.io.DOMReader;
import org.dom4j.io.SAXReader;
import org.fireflow.model.DataField;
import org.fireflow.model.Duration;
import org.fireflow.model.EventListener;
import org.fireflow.model.IWFElement;
import org.fireflow.model.Task;
import org.fireflow.model.WorkflowProcess;
import org.fireflow.model.net.Activity;
import org.fireflow.model.net.EndNode;
import org.fireflow.model.net.Node;
import org.fireflow.model.net.StartNode;
import org.fireflow.model.net.Synchronizer;
import org.fireflow.model.net.Transition;
import org.fireflow.model.reference.Application;
import org.fireflow.model.reference.Form;
import org.fireflow.model.reference.Participant;
import org.fireflow.model.reference.SubWorkflowProcess;
import org.xml.sax.EntityResolver;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
public class Dom4JFPDLParser implements IFPDLParser {
String encoding = "utf-8";
/** Construct a new Dom4JFPDLParser. */
public Dom4JFPDLParser() {
}
public void setEncoding(String ecd) {
this.encoding = ecd;
}
public WorkflowProcess parse(InputStream in) throws IOException,
FPDLParserException {
return parse(new InputStreamReader(in));
}
public WorkflowProcess parse(Document document) throws FPDLParserException {
Element workflowProcessElement = document.getRootElement();
WorkflowProcess wp = new WorkflowProcess(workflowProcessElement.attributeValue(NAME));
wp.setDescription(Util4Parser.elementAsString(workflowProcessElement,
DESCRIPTION));
wp.setDisplayName(workflowProcessElement.attributeValue(DISPLAY_NAME));
wp.setResourceFile(workflowProcessElement.attributeValue(RESOURCE_FILE));
wp.setResourceManager(workflowProcessElement.attributeValue(RESOURCE_MANAGER));
this.loadDataFields(wp, wp.getDataFields(), Util4Parser.child(
workflowProcessElement, this.DATA_FIELDS));
loadStartNode(wp, Util4Parser.child(workflowProcessElement, START_NODE));
loadActivities(wp, wp.getActivities(), Util4Parser.child(
workflowProcessElement, ACTIVITIES));
loadSynchronizers(wp, wp.getSynchronizers(), Util4Parser.child(
workflowProcessElement, SYNCHRONIZERS));
loadEndNodes(wp, wp.getEndNodes(), Util4Parser.child(
workflowProcessElement, END_NODES));
loadTransitions(wp, Util4Parser.child(workflowProcessElement,
TRANSITIONS));
loadEventListeners(wp.getEventListeners(), Util4Parser.child(workflowProcessElement, EVENT_LISTENERS));
Map<String, String> extAttrs = wp.getExtendedAttributes();
loadExtendedAttributes(extAttrs, Util4Parser.child(
workflowProcessElement, EXTENDED_ATTRIBUTES));
return wp;
}
public WorkflowProcess parse(InputSource in) throws IOException, FPDLParserException {
String oldSigletonClassName = System.getProperty("org.dom4j.DocumentFactory.singleton.strategy");
try {
// java.util.logging.LogManager.getLogManager().getLogger(Dom4JFPDLSerializer.class.getName()).log(Level.WARNING, "=======================检查是否是新版本");
System.setProperty("org.dom4j.DocumentFactory.singleton.strategy", "org.fireflow.model.io.MySigleton4DocumentFactory");
SAXReader reader = new SAXReader(new DocumentFactory());
reader.setEntityResolver(new EntityResolver() {
String emptyDtd = "";
ByteArrayInputStream bytels = new ByteArrayInputStream(emptyDtd.getBytes());
public InputSource resolveEntity(String publicId,
String systemId) throws SAXException, IOException {
return new InputSource(bytels);
}
});
reader.setEncoding(encoding);
Document document = reader.read(in);
WorkflowProcess wp = parse(document);
return wp;
} catch (DocumentException e) {
e.printStackTrace();
throw new FPDLParserException("Error parsing document.", e);
} finally {
if (oldSigletonClassName != null && !oldSigletonClassName.equals("")) {
System.setProperty("org.dom4j.DocumentFactory.singleton.strategy", oldSigletonClassName);
}
}
}
public WorkflowProcess parse(Reader in) throws IOException,
FPDLParserException {
String oldSigletonClassName = System.getProperty("org.dom4j.DocumentFactory.singleton.strategy");
try {
// java.util.logging.LogManager.getLogManager().getLogger(Dom4JFPDLSerializer.class.getName()).log(Level.WARNING, "=======================检查是否是新版本");
System.setProperty("org.dom4j.DocumentFactory.singleton.strategy", "org.fireflow.model.io.MySigleton4DocumentFactory");
SAXReader reader = new SAXReader(new DocumentFactory());
reader.setEntityResolver(new EntityResolver() {
String emptyDtd = "";
ByteArrayInputStream bytels = new ByteArrayInputStream(emptyDtd.getBytes());
public InputSource resolveEntity(String publicId,
String systemId) throws SAXException, IOException {
return new InputSource(bytels);
}
});
reader.setEncoding(encoding);
Document document = reader.read(in);
WorkflowProcess wp = parse(document);
return wp;
} catch (DocumentException e) {
e.printStackTrace();
throw new FPDLParserException("Error parsing document.", e);
} finally {
if (oldSigletonClassName != null && !oldSigletonClassName.equals("")) {
System.setProperty("org.dom4j.DocumentFactory.singleton.strategy", oldSigletonClassName);
}
}
}
protected void loadEventListeners(List listeners, Element element) {
listeners.clear();
if (element == null) {
return;
}
if (element == null) {
return;
}
List listenerElms = Util4Parser.children(element, EVENT_LISTENER);
Iterator iter = listenerElms.iterator();
while (iter.hasNext()) {
Element elm = (Element) iter.next();
EventListener listener = new EventListener();
listener.setClassName(elm.attributeValue(CLASS_NAME));
listeners.add(listener);
}
}
protected void loadStartNode(WorkflowProcess wp, Element element)
throws FPDLParserException {
if (element == null) {
return;
}
StartNode startNode = new StartNode(wp);
startNode.setSn(UUID.randomUUID().toString());
startNode.setDescription(Util4Parser.elementAsString(element,
DESCRIPTION));
startNode.setDisplayName(element.attributeValue(DISPLAY_NAME));
loadExtendedAttributes(startNode.getExtendedAttributes(), Util4Parser.child(element, EXTENDED_ATTRIBUTES));
wp.setStartNode(startNode);
}
protected void loadEndNodes(WorkflowProcess wp, List<EndNode> endNodes,
Element element) throws FPDLParserException {
endNodes.clear();
if (element == null) {
return;
}
List endNodesElms = Util4Parser.children(element, END_NODE);
Iterator iter = endNodesElms.iterator();
while (iter.hasNext()) {
Element elm = (Element) iter.next();
EndNode endNode = new EndNode(wp, elm.attributeValue(NAME));
endNode.setSn(UUID.randomUUID().toString());
endNode.setDescription(Util4Parser.elementAsString(element,
DESCRIPTION));
endNode.setDisplayName(element.attributeValue(DISPLAY_NAME));
loadExtendedAttributes(endNode.getExtendedAttributes(), Util4Parser.child(elm, EXTENDED_ATTRIBUTES));
endNodes.add(endNode);
}
}
protected void loadSynchronizers(WorkflowProcess wp, List<Synchronizer> synchronizers,
Element element) throws FPDLParserException {
synchronizers.clear();
if (element == null) {
return;
}
List synchronizerElms = Util4Parser.children(element, SYNCHRONIZER);
Iterator iter = synchronizerElms.iterator();
while (iter.hasNext()) {
Element elm = (Element) iter.next();
Synchronizer synchronizer = new Synchronizer(wp, elm.attributeValue(NAME));
synchronizer.setSn(UUID.randomUUID().toString());
synchronizer.setDescription(Util4Parser.elementAsString(element,
DESCRIPTION));
synchronizer.setDisplayName(element.attributeValue(DISPLAY_NAME));
loadExtendedAttributes(synchronizer.getExtendedAttributes(),
Util4Parser.child(elm, EXTENDED_ATTRIBUTES));
synchronizers.add(synchronizer);
}
}
protected void loadActivities(WorkflowProcess wp, List<Activity> activities,
Element element) throws FPDLParserException {
if (element == null) {
// log.debug("Activites element was null");
return;
}
List activitElements = Util4Parser.children(element, ACTIVITY);
activities.clear();
Iterator iter = activitElements.iterator();
while (iter.hasNext()) {
Element activityElement = (Element) iter.next();
Activity activity = new Activity(wp, activityElement.attributeValue(NAME));
activity.setSn(UUID.randomUUID().toString());
activity.setDisplayName(activityElement.attributeValue(DISPLAY_NAME));
activity.setDescription(Util4Parser.elementAsString(
activityElement, DESCRIPTION));
activity.setCompletionStrategy(activityElement.attributeValue(COMPLETION_STRATEGY));
loadEventListeners(activity.getEventListeners(), Util4Parser.child(activityElement, EVENT_LISTENERS));
loadExtendedAttributes(activity.getExtendedAttributes(),
Util4Parser.child(activityElement, EXTENDED_ATTRIBUTES));
loadTasks(activity, activity.getTasks(), Util4Parser.child(
activityElement, TASKS));
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -