|
| 1 | +package com.cherkashyn.vitaliy.bpmn.diagram_scaner; |
| 2 | + |
| 3 | +import java.io.IOException; |
| 4 | +import java.io.InputStream; |
| 5 | +import java.io.UnsupportedEncodingException; |
| 6 | +import java.text.MessageFormat; |
| 7 | +import java.util.List; |
| 8 | + |
| 9 | +import javax.xml.parsers.ParserConfigurationException; |
| 10 | + |
| 11 | +import org.activiti.engine.RepositoryService; |
| 12 | +import org.activiti.engine.repository.DeploymentBuilder; |
| 13 | +import org.apache.commons.io.IOUtils; |
| 14 | +import org.apache.commons.lang3.StringUtils; |
| 15 | +import org.apache.commons.lang3.tuple.Pair; |
| 16 | +import org.apache.log4j.Logger; |
| 17 | +import org.springframework.beans.factory.annotation.Autowired; |
| 18 | +import org.w3c.dom.Document; |
| 19 | +import org.xml.sax.SAXException; |
| 20 | + |
| 21 | +import com.cherkashyn.vitaliy.bpmn.utility.InputStreamIterator; |
| 22 | +import com.cherkashyn.vitaliy.bpmn.utility.XmlUtility; |
| 23 | +import com.cherkashyn.vitaliy.bpmn.xml_extender.XmlProcessor; |
| 24 | + |
| 25 | +public class DiagramLoader { |
| 26 | + private static final Logger logger=Logger.getLogger(DiagramLoader.class); |
| 27 | + @Autowired(required=true) |
| 28 | + RepositoryService repositoryService; |
| 29 | + |
| 30 | + @Autowired(required=true) |
| 31 | + InputStreamIterator inputStreamIterator; |
| 32 | + |
| 33 | + private final static String magicStringForActiviti="bpmn20.xml"; |
| 34 | + |
| 35 | + public void init() throws Exception, IOException, SAXException, ParserConfigurationException { |
| 36 | + DeploymentBuilder builder= repositoryService.createDeployment(); |
| 37 | + for(Pair<String, InputStream> eachFile:inputStreamIterator){ |
| 38 | + logger.debug(MessageFormat.format("Load data from file: {0}", eachFile.getKey())); |
| 39 | + Document document=convertStringToXml(getStringFromInputStream(eachFile.getValue())); |
| 40 | + String key=StringUtils.removeEnd(getFileName(eachFile.getKey()),"."); |
| 41 | + document=filterIntercepter(document, key); |
| 42 | + logger.debug(getStringFromXml(document)); |
| 43 | + builder.addInputStream(key+magicStringForActiviti, |
| 44 | + IOUtils.toInputStream(getStringFromXml(document))); |
| 45 | + } |
| 46 | + builder.deploy(); |
| 47 | + } |
| 48 | + |
| 49 | + private String getStringFromXml(Document document) { |
| 50 | + return this.xmlUtility.getStringFromXml(document); |
| 51 | + } |
| 52 | + |
| 53 | + private String getFileName(String fileName) { |
| 54 | + return StringUtils.substringBeforeLast(fileName, "bpmn20.xml"); |
| 55 | + } |
| 56 | + |
| 57 | + private XmlProcessor[] xmlProcessor; |
| 58 | + public void setXmlProcessors(XmlProcessor[] xmlProcessors){ |
| 59 | + this.xmlProcessor=xmlProcessors; |
| 60 | + } |
| 61 | + |
| 62 | + private Document filterIntercepter(Document document, String fileName) throws Exception{ |
| 63 | + if(xmlProcessor!=null && xmlProcessor.length>0){ |
| 64 | + for(XmlProcessor processor:xmlProcessor){ |
| 65 | + document=processor.processDocument(document, fileName); |
| 66 | + } |
| 67 | + } |
| 68 | + return document; |
| 69 | + } |
| 70 | + |
| 71 | + private XmlUtility xmlUtility=new XmlUtility(); |
| 72 | + |
| 73 | + private Document convertStringToXml(String xmlText) throws UnsupportedEncodingException, SAXException, IOException, ParserConfigurationException{ |
| 74 | + return xmlUtility.getDocumentFromString(xmlText); |
| 75 | + } |
| 76 | + |
| 77 | + private String getStringFromInputStream(InputStream inputStream) throws IOException{ |
| 78 | + return linesToString(IOUtils.readLines(inputStream)); |
| 79 | + } |
| 80 | + |
| 81 | + private String linesToString(List<String> list){ |
| 82 | + StringBuilder returnValue=new StringBuilder(); |
| 83 | + if(list!=null && list.size()>0){ |
| 84 | + for(String line:list){ |
| 85 | + returnValue.append(line); |
| 86 | + } |
| 87 | + } |
| 88 | + return returnValue.toString(); |
| 89 | + } |
| 90 | + |
| 91 | +} |
0 commit comments