为什么这个jibx绑定没有像binding.xml中指定的那样解组?

时间:2015-11-16 10:34:41

标签: java xml gradle jibx

我有这些gradle任务来破坏jibx-bind和jibx-bindgen:

dependencies {
  compile 'org.jibx:jibx-run:1.2.+'

  jibx 'org.jibx:jibx-bind:1.2.+'
  jibx 'org.jibx:jibx-run:1.2.+'
  jibx 'xpp3:xpp3:1.1.3.4-RC8'
  jibx 'org.apache.bcel:bcel:6.0-SNAPSHOT'
}

task bindGen(type: JavaExec) {
  main = 'org.jibx.binding.BindingGenerator'
  classpath configurations.jibx
  classpath sourceSets.main.runtimeClasspath
  args 'com.rwe.amm.server.profile.AlgoProfiles'
  args 'com.rwe.amm.server.profile.AlgoProperties'
  args 'com.rwe.amm.server.profile.AlgoProperty'
  args 'com.rwe.amm.server.profile.ClientPosition'
  args 'com.rwe.amm.server.profile.ClientPanels'
  args 'com.rwe.amm.shared.model.AlgoGenericContractPanel'
  args 'com.rwe.amm.shared.model.Property'
}

task bind(type: JavaExec) {
  classpath configurations.jibx
  classpath sourceSets.main.runtimeClasspath
  main = 'org.jibx.binding.Compile'
  args projectDir.path + '/binding.xml'
}

bindGen创建此binding.xml:

<?xml version="1.0" encoding="UTF-8"?>
<binding value-style="attribute">
  <mapping class="com.rwe.amm.server.profile.AlgoProfiles" name="algo-profiles">
    <collection field="profiles" usage="optional" factory="org.jibx.runtime.Utility.arrayListFactory"/>
    <collection field="thresholdProperties" usage="optional" factory="org.jibx.runtime.Utility.arrayListFactory"/>
    <collection field="fieldDefaultProperties" usage="optional" factory="org.jibx.runtime.Utility.arrayListFactory"/>
    <structure field="clientPosition" usage="optional"/>
    <structure field="clientPanels" usage="optional"/>
  </mapping>
  <mapping class="com.rwe.amm.server.profile.AlgoProperties" name="algo-properties">
    <value style="element" name="contract" field="contract" usage="optional"/>
    <collection field="algoProperties" usage="optional" factory="org.jibx.runtime.Utility.arrayListFactory"/>
  </mapping>
  <mapping class="com.rwe.amm.server.profile.AlgoProperty" name="algo-property">
    <value style="element" name="key" field="key" usage="optional"/>
    <value style="element" name="value" field="value" usage="optional"/>
  </mapping>
  <mapping class="com.rwe.amm.server.profile.ClientPosition" name="client-position">
    <value name="xpos" field="xpos"/>
    <value name="ypos" field="ypos"/>
    <value name="width" field="width"/>
    <value name="height" field="height"/>
  </mapping>
  <mapping class="com.rwe.amm.server.profile.ClientPanels" name="client-panels">
    <value name="main-quoting-panel-collapsed" field="mainQuotingPanelCollapsed"/>
    <value name="spreads-panel-collapsed" field="spreadsPanelCollapsed"/>
    <value name="manual-spreads-swaps-panel-collapsed" field="manualSpreadsSwapsPanelCollapsed"/>
    <collection field="genericContractPanels" usage="optional" factory="org.jibx.runtime.Utility.arrayListFactory"/>
  </mapping>
  <mapping class="com.rwe.amm.shared.model.AlgoGenericContractPanel" name="algo-generic-contract-panel">
    <value style="element" name="contract" field="contract" usage="optional"/>
    <value style="element" name="contract-panel-id" field="contractPanelId" usage="optional"/>
    <value style="element" name="algo-type" field="algoType" usage="optional"/>
    <value name="skew-value" field="skewValue" usage="optional"/>
    <value name="manual-value" field="manualValue" usage="optional"/>
    <value style="element" name="benchmark-contract" field="benchmarkContract" usage="optional"/>
  </mapping>
  <mapping class="com.rwe.amm.shared.model.Property" name="property">
    <value style="element" name="name" field="name" usage="optional"/>
    <value style="element" name="value" field="value" usage="optional"/>
    <value style="element" name="description" field="description" usage="optional"/>
  </mapping>
</binding>

bind创建了jibx * .classes。

使用此文件并运行此java代码会产生意外结果:

<algo-profiles>
  <algo-properties>
    <contract>PEAK YEAR 4,PEAK YEAR 4</contract>
    <algo-property>
      <key>ARBITRAGE_MANUAL</key>
      <value>0.000</value>
    </algo-property>
    <algo-property>
      <key>TIME_MANUAL</key>
      <value>50.180</value>
    </algo-property>
    <algo-property>
      <key>DE Mid</key>
      <value>n/a</value>
    </algo-property>
    <algo-property>
      <key>SETBASPREAD</key>
      <value>MM</value>
    </algo-property>
    <algo-property>
      <key>PRICING</key>
      <value>0.000</value>
    </algo-property>
  </algo-properties>
  <client-panels main-quoting-panel-collapsed="true" spreads-panel-collapsed="true" manual-spreads-swaps-panel-collapsed="true"/>
</algo-profiles>
public class AlgoProfiles {
    public List<AlgoProperties> profiles; 
    public List<Property> thresholdProperties;
    public List<Property> fieldDefaultProperties;
    public ClientPosition clientPosition;
    public ClientPanels clientPanels;
}

public boolean read() throws JiBXException {
    IBindingFactory bfact = BindingDirectory.getFactory(com.rwe.amm.server.profile.AlgoProfiles.class);
    IUnmarshallingContext uctx = bfact.createUnmarshallingContext();

    try {
        FileInputStream in = new FileInputStream(_profilesLocation + _user + ".xml");
        Object obj = uctx.unmarshalDocument(in, null);
        setAlgoProfiles((AlgoProfiles)obj);

        if ( getAlgoProfiles() == null ) {
            logger.warn("... );
            return false;
        }

        if ( getAlgoProfiles() != null && getAlgoProfiles().profiles != null ) {
            for ( AlgoProperties algoProperties : getAlgoProfiles().profiles ) {
                algoProperties.contract = RollingContractFacade.adapt(algoProperties.contract).getContract();
            }
        }
    } catch ( FileNotFoundException fnfe ) {
        logger.warn("... );
    }
    return ( getAlgoProfiles() != null );   
}

我遇到的问题是,未编组的对象可以转换为AlgoProfiles,但字段配置文件有一个AlgoProperties对象,但也有一个ClientPanels对象。

ClientPanels对象应设置为AlgoProfiles对象上的字段,而不是配置文件集合中的成员。

这适用于java 1.7和较旧的jibx版本,但我必须转向Java 8,并且需要更新的jibx版本。

我不知道为什么会这样。

更新

当我使用正确的类型将item-type属性添加到集合标记时,则解组可以正常工作。

是否可以告诉BindingGenerator添加这些属性?

1 个答案:

答案 0 :(得分:0)

通过将我的任务改为此来解决我的所有问题:

dependencies {
  jibx 'org.jibx:jibx-tools:1.2.+'
}

task bindGen(type: JavaExec) {
  main = 'org.jibx.binding.generator.BindGen'
  classpath configurations.jibx
  classpath sourceSets.main.runtimeClasspath
  args '-m'
  args 'com.rwe.amm.server.profile.AlgoProfiles'
}