【问题标题】:Why is this jibx binding not unmarshaled like specified in the binding.xml?为什么这个 jibx 绑定没有像 binding.xml 中指定的那样解组?
【发布时间】:2015-11-16 10:34:41
【问题描述】:

我有这些 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 添加这些属性?

【问题讨论】:

    标签: java xml gradle jibx


    【解决方案1】:

    通过改变我的任务解决了我所有的问题:

    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'
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-03-28
      • 2016-04-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多