jbpm4.0流程设计器中文乱码解决方案

jbpm4的流程设计器对中文的支持还是不理想,在研究了插件源码后,做了如下下修改解决中文乱码问题

1.在jbpm设置界面增加一个编码属性设置修改org.jboss.tools.jbpm.common插件中的JbpmLocationsPage类添加一个Text encode用来设置编码方式
添加的代码内容如下

  private Text encode;

  protected Control createContents(Composite parent) {
    Composite clientArea = createClientArea(parent);
    createLabels(clientArea);
    createJbpmHomeListTable(clientArea);
    createButtons(clientArea);
    //加入下面代码
    createEncodeContent(parent);
    return null;
  }

  private void createEncodeContent(Composite parent) {
    Composite clientArea = new Composite(parent, SWT.NONE);
    GridLayout layout = new GridLayout();
    layout.numColumns = 2;
    clientArea.setLayout(layout);
    Label encodeLabel = new Label(clientArea, SWT.NONE);
    encodeLabel.setText("encode:");
    GridData gridData = new GridData(GridData.FILL_HORIZONTAL);
    encode = new Text(clientArea, SWT.SINGLE | SWT.BORDER);
    encode.setLayoutData(gridData);
    initEncode();
  }

  private void initEncode(){
    String encodeValue = plugin.getPreferenceStore().getString("encode");
    if(encodeValue == null || encodeValue == "") encodeValue = "UTF-8";
    encode.setText(encodeValue);
  }

  public boolean performOk() {
    String name = "";
    PreferencesManager inputManager = (PreferencesManager) tableViewer
        .getInput();
    inputManager.saveInstallations();
    TableItem item = getCheckedItem(tableViewer);
    if (item != null) {
      name = item.getText(0);
    }
    plugin.getPreferenceStore().setValue(Constants.JBPM_NAME, name);
    //加入下面代码
    plugin.getPreferenceStore().setValue("encode", encode.getText());
    return true;
  }

  public void performDefaults() {
    plugin.getPreferenceStore().setToDefault(Constants.JBPM_NAME);
    PreferencesManager inputManager = (PreferencesManager) tableViewer
        .getInput();
    inputManager.getJbpmInstallationMap().clear();
    tableViewer.setInput(inputManager);
    checkItemToCheck(tableViewer);
    setMessage("");
    updateButtons();
    setValid(true);
    //加入下面代码
    initEncode();
  }

2.修改org.jboss.tools.flow.jpdl4插件中JpdlSerializer,ProcessSerializer,NewJpdl4FileWizardPage 3个类中有关设置文件编码的地方

JpdlSerializer类中serialize方法修改后代码为
  public static void serialize(Wrapper wrapper, OutputStream os) throws IOException {
    String encodeValue = Activator.getDefault().getPreferenceStore().getString("encode");
    if(encodeValue == null || encodeValue == "") encodeValue = "UTF-8";
    StringBuffer buffer = new StringBuffer();
    serialize(wrapper, buffer, 0);
    Writer writer = new OutputStreamWriter(os, encodeValue);
    writer.write(buffer.toString());
    writer.close();
  }
根据1.中的配置编码来设置文件内容编码,默认编码为UTF-8

ProcessSerializer中appendOpening方法修改后代码为
  public void appendOpening(StringBuffer buffer, Wrapper wrapper, int level) {
    String encodeValue = Activator.getDefault().getPreferenceStore().getString("encode");
    if(encodeValue == null || encodeValue == "") encodeValue = "UTF-8";
buffer.append("<?xml version=\"1.0\" encoding=\"").append(encodeValue).append("\"?>\n\n");
    buffer.append("<" + Registry.getXmlNodeName(wrapper.getElement()));
appendAttributes(buffer, wrapper, level);
}
NewJpdl4FileWizardPage中getInitialContentsAsString方法修改后代码为
    private String getInitialContentsAsString() {
      String encodeValue = Activator.getDefault().getPreferenceStore().getString("encode");
      if(encodeValue == null || encodeValue == "") encodeValue = "UTF-8";
    StringBuffer buffer = new StringBuffer();
      buffer.append("<?xml version=\"1.0\" encoding=\"").append(encodeValue).append("\"?>\n\n");
    buffer.append("<process xmlns=\"http://jbpm.org/4.0/jpdl\" name=\"");
    buffer.append(getFileName().substring(0, getFileName().lastIndexOf(getFileExtension()) - 1));
    buffer.append("\">\n</process>");
    return buffer.toString();
    }

你可能感兴趣的:(xml,jboss,jbpm,OS)