Java tutorial
/** * Copyright 1996-2014 FoxBPM ORG. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * * @author ych */ package org.foxbpm.rest.service.api.config; import java.io.IOException; import java.io.InputStream; import java.util.Iterator; import java.util.zip.ZipEntry; import java.util.zip.ZipOutputStream; import org.dom4j.Document; import org.dom4j.Element; import org.dom4j.io.SAXReader; import org.dom4j.io.XMLWriter; import org.foxbpm.engine.ProcessEngineManagement; import org.foxbpm.engine.exception.FoxBPMException; import org.foxbpm.engine.impl.ProcessEngineConfigurationImpl; import org.foxbpm.engine.impl.util.ReflectUtil; import org.slf4j.Logger; import org.slf4j.LoggerFactory; /** * zip? * ?foxbpm.cfg.xml????java * flowResouce/connector/flowconnector * ??connector? * @author ych * */ public class ConnectorGenerator implements IZipGenerator { Logger log = LoggerFactory.getLogger(ConnectorGenerator.class); private static final int SIZE = 1024; @SuppressWarnings("unchecked") private void parseNode(Element nodeElement, ZipOutputStream out, String type, String folderName) throws Exception { if (nodeElement != null) { Iterator<Element> iterator = nodeElement.elements("node").iterator(); while (iterator.hasNext()) { Element nodeTmpElement = iterator.next(); parseNode(nodeTmpElement, out, type, folderName); } iterator = nodeElement.elements("connector").iterator(); while (iterator.hasNext()) { Element tmp = iterator.next(); parseConnector(tmp, out, type, folderName); } } } private void parseConnector(Element tmp, ZipOutputStream out, String type, String folderName) throws Exception { String id = tmp.attributeValue("id"); String packageName = tmp.attributeValue("package"); String connectorXmlName = ""; if (type.equals("flowConnector")) { connectorXmlName = "FlowConnector.xml"; } else if (type.equals("actorConnector")) { connectorXmlName = "ActorConnector.xml"; } else { throw new FoxBPMException("??" + type); } String xmlFileName = packageName + "/" + connectorXmlName; String pngFileName = packageName + "/" + id + ".png"; String xmlEntryName = folderName + "/" + id + "/" + connectorXmlName; String pngEntryName = folderName + "/" + id + "/" + id + ".png"; InputStream xmlInputStream = ReflectUtil.getResourceAsStream(xmlFileName); if (xmlInputStream == null) { log.error("{}?,?????", xmlFileName); return; } else { generZip(xmlInputStream, xmlEntryName, out); } InputStream pngInputStream = ReflectUtil.getResourceAsStream(pngFileName); if (pngInputStream == null) { pngFileName = packageName + "/" + id + ".jpg"; pngInputStream = ReflectUtil.getResourceAsStream(pngFileName); } if (pngInputStream == null) { log.error("{}?,?????", pngFileName); } else { generZip(pngInputStream, pngEntryName, out); } } private void generZip(InputStream stream, String entryName, ZipOutputStream out) throws Exception { ZipEntry zipEntry = new ZipEntry(entryName); zipEntry.setMethod(ZipEntry.DEFLATED);// ?? out.putNextEntry(zipEntry); int n = 0; byte b[] = new byte[SIZE]; while ((n = stream.read(b)) != -1) { out.write(b, 0, n); } out.closeEntry(); stream.close(); } private void generatorConnector(String prefix, InputStream stream, ZipOutputStream out) throws Exception { SAXReader reader = null; reader = new SAXReader(); Document doc = reader.read(stream); ZipEntry zipEntry = new ZipEntry(prefix + "/ConnectorMenu.xml"); zipEntry.setMethod(ZipEntry.DEFLATED);// ?? out.putNextEntry(zipEntry); XMLWriter xmlWriter = new XMLWriter(out); xmlWriter.setEscapeText(false); xmlWriter.write(doc); out.closeEntry(); Element flowConnectorElement = doc.getRootElement().element("flowConnector"); if (flowConnectorElement != null) { parseNode(flowConnectorElement, out, "flowConnector", prefix + "/flowConnector"); } Element actorConnectorElement = doc.getRootElement().element("actorConnector"); if (actorConnectorElement != null) { parseNode(actorConnectorElement, out, "actorConnector", prefix + "/actorConnector"); } } public void generate(ZipOutputStream out) { ProcessEngineConfigurationImpl processEngineConfigurationImpl = ProcessEngineManagement .getDefaultProcessEngine().getProcessEngineConfiguration(); InputStream stream = ReflectUtil.getResourceAsStream("org/foxbpm/connector/ConnectorMenu.xml"); String connectorMenuPath = processEngineConfigurationImpl.getConnectorMenuPath(); InputStream connectorStream = null; if (connectorMenuPath != null) { connectorStream = ReflectUtil.getResourceAsStream(connectorMenuPath); } try { if (stream != null) { generatorConnector("connector/defaultConnector", stream, out); } if (connectorStream != null) { generatorConnector("connector/custormConnector", connectorStream, out); } } catch (Exception e) { log.error("" + e.getMessage(), e); throw new FoxBPMException("" + e.getMessage(), e); } finally { try { if (stream != null) { stream.close(); } if (connectorStream != null) { stream.close(); } } catch (IOException e) { log.error("??" + e.getMessage(), e); } } } }