📄 humantaskxmlconfigloader.java
字号:
package com.ibm.gbsc.cte.sample;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import javax.faces.context.FacesContext;
import javax.faces.model.SelectItem;
import javax.servlet.http.HttpServletRequest;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
public class HumanTaskXMLConfigLoader {
public static HumanTaskInstruction getTaskInstruction(String name)
{
for (int i=0; i<humanTaskList.length; i++)
{
if (humanTaskList[i].getTaskType().equalsIgnoreCase(name) )
{
return humanTaskList[i];
}
}
return null;
}
private static HumanTaskInstruction[] humanTaskList = null;
private static String fileURL = ((HttpServletRequest) FacesContext
.getCurrentInstance().getExternalContext().getRequest())
.getRealPath("/article/HumanTaskConfig.xml");
static
{
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db;
try {
db = dbf.newDocumentBuilder();
Document doc = db.parse(fileURL);
doc.normalize();
NodeList tasklist = doc.getElementsByTagName("HumanTask");
int len = tasklist.getLength();
humanTaskList = new HumanTaskInstruction[len];
for (int i=0; i<len; i++)
{
Node item = tasklist.item(i);
humanTaskList[i] = new HumanTaskInstruction();
for (int j=0; j<item.getChildNodes().getLength(); j++)
{
Node field = item.getChildNodes().item(j);
if (field == null)
{
continue;
}
if (field.getNodeName().equalsIgnoreCase("Type"))
{
humanTaskList[i].setTaskType(field.getFirstChild().getNodeValue());
}
if (field.getNodeName().equalsIgnoreCase("Name"))
{
humanTaskList[i].setTaskName(field.getFirstChild().getNodeValue());
}
else if (field.getNodeName().equalsIgnoreCase("Instruction"))
{
humanTaskList[i].setInstruction(field.getFirstChild().getNodeValue());
}
else if (field.getNodeName().equalsIgnoreCase("Routing"))
{
LoadRouting(field, i);
}
else if (field.getNodeName().equalsIgnoreCase("Variables"))
{
LoadVariables(field, i);
}
else
{
System.out.println(field.getNodeName() + "::: " + field.getNodeValue());
// ParserConfigurationException e = new ParserConfigurationException("Invalid element appeared");
// throw e;
}
}
}
System.out.println("success");
}catch (SAXException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ParserConfigurationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
private static void LoadVariables(Node field, int i) throws ParserConfigurationException {
HumanTaskInstruction task = humanTaskList[i];
NodeList variables = field.getChildNodes();
List vars = new ArrayList();
for (int k = 0; k<variables.getLength(); k++)
{
Node varNode = variables.item(k);
if (varNode.getNodeName().equalsIgnoreCase("Variable"))
{
NodeList attList = varNode.getChildNodes();
Variable var = new Variable();
for(int m=0; m<attList.getLength(); m++)
{
Node att = attList.item(m);
if (att.getNodeName().equalsIgnoreCase("Name"))
{
var.setName(att.getFirstChild().getNodeValue());
}
else if(att.getNodeName().equalsIgnoreCase("Type"))
{
String value = att.getFirstChild().getNodeValue();
if(value.equalsIgnoreCase("Read-Only"))
{
var.setType(Constants.READ_ONLY);
}
else if (value.equalsIgnoreCase("Writable"))
{
var.setType(Constants.WRITEABLE);
}
else
{
System.out.println(field.getNodeName() + "::: " + field.getNodeValue());
// ParserConfigurationException e = new ParserConfigurationException("Wrong value of variable type");
// throw e;
}
}
else if (att.getNodeName().equalsIgnoreCase("Source"))
{
String value = att.getFirstChild().getNodeValue();
if (value.equalsIgnoreCase("Global"))
{
var.setSource(Constants.GLOBAL);
}
else
{
System.out.println(field.getNodeName() + "::: " + field.getNodeValue());
// ParserConfigurationException e = new ParserConfigurationException("Wrong value of variable source");
// throw e;
}
}
else
{
System.out.println(field.getNodeName() + "::: " + field.getNodeValue());
// ParserConfigurationException e = new ParserConfigurationException("Wrong element under Variable");
// throw e;
}
}
vars.add(var);
}
else
{
System.out.println(field.getNodeName() + "::: " + field.getNodeValue());
// ParserConfigurationException e = new ParserConfigurationException("Wrong element under Variables");
// throw e;
}
}
task.setVars(vars);
}
private static void LoadRouting(Node field, int i) throws ParserConfigurationException {
HumanTaskInstruction task = humanTaskList[i];
NodeList routeNode = field.getChildNodes();
Routing routing = new Routing();
List choices = new ArrayList();
for (int j=0; j<routeNode.getLength(); j++)
{
Node item = routeNode.item(j);
if(item.getNodeName().equalsIgnoreCase("Name"))
{
routing.setName(item.getFirstChild().getNodeValue());
}
else if (item.getNodeName().equalsIgnoreCase("Choice"))
{
// chocies.add(item.getFirstChild().getNodeValue());
SelectItem selectitem = new SelectItem();
selectitem.setLabel(item.getChildNodes().item(1).getFirstChild().getNodeValue());
selectitem.setValue(item.getChildNodes().item(3).getFirstChild().getNodeValue());
choices.add(selectitem);
}
else
{
System.out.println(field.getNodeName() + "::: " + field.getNodeValue());
// ParserConfigurationException e = new ParserConfigurationException("Wrong element under Routing");
// throw e;
}
}
routing.setChocies(choices);
//mark for show routingv
if (choices.size() > 1)
task.setShowRoute(true);
task.setRoute(routing);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -