activiti 发布时 字符集(UTF8,UTF-8)问题

Underlying stream encoding 'UTF8' and input paramter for writeStartDocument() method 'UTF-8' do not match

 

问题在于

 

org.activiti.bpmn.converter.BpmnXMLConverter类的convertToXML方法

 

         ByteArrayOutputStream outputStream;
         outputStream = new ByteArrayOutputStream();
         XMLOutputFactory xof = XMLOutputFactory.newInstance();
        

         //OutputStreamWriter out = new OutputStreamWriter(outputStream, encoding);  //这里获得的OutputStreamWriter对象字符集有问题
         //XMLStreamWriter writer = xof.createXMLStreamWriter(out);


         XMLStreamWriter writer = xof.createXMLStreamWriter(outputStream,encoding); //使用这个方法就可以了!

解决方案: 自己重载这个类, 之后再发布model  或 将model 转换为xml  时使用这个类!        

 

package com.mf.component.workflow.util;

import java.io.ByteArrayOutputStream;
import java.util.Iterator;

import javax.xml.stream.XMLOutputFactory;
import javax.xml.stream.XMLStreamWriter;

import org.activiti.bpmn.converter.BaseBpmnXMLConverter;
import org.activiti.bpmn.converter.BpmnXMLConverter;
import org.activiti.bpmn.converter.IndentingXMLStreamWriter;
import org.activiti.bpmn.converter.export.ActivitiListenerExport;
import org.activiti.bpmn.converter.export.BPMNDIExport;
import org.activiti.bpmn.converter.export.DefinitionsRootExport;
import org.activiti.bpmn.converter.export.MultiInstanceExport;
import org.activiti.bpmn.converter.export.PoolExport;
import org.activiti.bpmn.converter.export.ProcessExport;
import org.activiti.bpmn.converter.export.SignalAndMessageDefinitionExport;
import org.activiti.bpmn.exceptions.XMLException;
import org.activiti.bpmn.model.Artifact;
import org.activiti.bpmn.model.BpmnModel;
import org.activiti.bpmn.model.EventSubProcess;
import org.activiti.bpmn.model.FlowElement;
import org.activiti.bpmn.model.Process;
import org.activiti.bpmn.model.SubProcess;
import org.apache.commons.lang3.StringUtils;

public class WFBpmnXMLConverter extends BpmnXMLConverter {
	
	public byte[] convertToXML(BpmnModel model, String encoding)
    {
		
		try{
	        ByteArrayOutputStream outputStream;
	        outputStream = new ByteArrayOutputStream();
	        XMLOutputFactory xof = XMLOutputFactory.newInstance();
	        //wangzhe
	        //OutputStreamWriter out = new OutputStreamWriter(outputStream, encoding);
	        //XMLStreamWriter writer = xof.createXMLStreamWriter(out);
	        XMLStreamWriter writer = xof.createXMLStreamWriter(outputStream,encoding);
	        //wangzhe end
	        XMLStreamWriter xtw = new IndentingXMLStreamWriter(writer);
	        DefinitionsRootExport.writeRootElement(model, xtw, encoding);
	        SignalAndMessageDefinitionExport.writeSignalsAndMessages(model, xtw);
	        PoolExport.writePools(model, xtw);
	        Iterator<Process> ip = model.getProcesses().iterator();
	        do
	        {
	            if(!ip.hasNext())  break;
	            Process process = ip.next();
	            if(process.getFlowElements().size() != 0 || process.getLanes().size() != 0)
	            {
	                ProcessExport.writeProcess(process, xtw);
	                FlowElement flowElement;
	                Iterator<FlowElement> ita = process.getFlowElements().iterator();
	                for(; ita.hasNext(); ){
	                	flowElement = ita.next();
	                	createXML(flowElement, model, xtw);
	                }
	                    
	
	                Artifact artifact;
	                Iterator<Artifact> itb = process.getArtifacts().iterator();
	                for( ; itb.hasNext(); ){
	                	artifact = itb.next();
	                	createXML(artifact, model, xtw);
	                }
	                    
	
	                xtw.writeEndElement();
	            }
	        } while(true);
	        
	        BPMNDIExport.writeBPMNDI(model, xtw);
	        xtw.writeEndElement();
	        xtw.writeEndDocument();
	        xtw.flush();
	        outputStream.close();
	        xtw.close();
	        return outputStream.toByteArray();
		}catch(Exception e){
             LOGGER.error("Error writing BPMN XML", e);
             throw new XMLException("Error writing BPMN XML", e);
        }
    }
	
	private void createXML(FlowElement flowElement, BpmnModel model, XMLStreamWriter xtw)
	        throws Exception
	    {
	        if(flowElement instanceof SubProcess)
	        {
	            SubProcess subProcess = (SubProcess)flowElement;
	            xtw.writeStartElement("subProcess");
	            xtw.writeAttribute("id", subProcess.getId());
	            if(StringUtils.isNotEmpty(subProcess.getName()))
	                xtw.writeAttribute("name", subProcess.getName());
	            else
	                xtw.writeAttribute("name", "subProcess");
	            if(subProcess instanceof EventSubProcess)
	                xtw.writeAttribute("triggeredByEvent", "true");
	            if(StringUtils.isNotEmpty(subProcess.getDocumentation()))
	            {
	                xtw.writeStartElement("documentation");
	                xtw.writeCharacters(subProcess.getDocumentation());
	                xtw.writeEndElement();
	            }
	            boolean wroteListener = ActivitiListenerExport.writeListeners(subProcess, false, xtw);
	            if(wroteListener)
	                xtw.writeEndElement();
	            MultiInstanceExport.writeMultiInstance(subProcess, xtw);
	            FlowElement subElement;
	            Iterator<FlowElement> ie = subProcess.getFlowElements().iterator();
	            for(; ie.hasNext(); ){
	            	subElement = ie.next();
	            	createXML(subElement, model, xtw);
	            }
	            Artifact artifact;
	            Iterator<Artifact> ia = subProcess.getArtifacts().iterator();
	            for(; ia.hasNext(); ){
	            	 artifact = ia.next();
	            	createXML(artifact, model, xtw);
	            }
	            xtw.writeEndElement();
	        } else
	        {
	            Class converter = (Class)convertersToXMLMap.get(flowElement.getClass());
	            if(converter == null)
	                throw new XMLException((new StringBuilder()).append("No converter for ").append(flowElement.getClass()).append(" found").toString());
	            ((BaseBpmnXMLConverter)converter.newInstance()).convertToXML(xtw, flowElement, model);
	        }
	    }
	
	private void createXML(Artifact artifact, BpmnModel model, XMLStreamWriter xtw)
	        throws Exception
	    {
	        Class converter = (Class)convertersToXMLMap.get(artifact.getClass());
	        if(converter == null)
	        {
	            throw new XMLException((new StringBuilder()).append("No converter for ").append(artifact.getClass()).append(" found").toString());
	        } else
	        {
	            ((BaseBpmnXMLConverter)converter.newInstance()).convertToXML(xtw, artifact, model);
	            return;
	        }
	    }
}


 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值