3

非常に簡単な質問があります:

次のように定義されたモデルクラスがあるとします。

public class Test{

   private String testAttribute;

   public Test(){

   }

   public String getFormattedTestAttribute(){
      return testAttribute + "A nice formatted thingy"; //right, this is just an example
   }

   public void setTestAttribute(String value){
      testAttribute = value;
   }

}

testPropertyの標準セッターがありますが、ゲッターの名前はgetFormattedTestProperty()であることがわかります。

Jaxb / Moxyで、特定のプロパティに使用するゲッターを指定することはできますか?

外部メタデータバインディングファイルでMOXy実装を使用しています。私が取り組んでいるプロジェクトは、Castorを使用しています。Castorのマッピングファイルに、次のように使用するゲッター/セッターを指定できます。

   <field name="testAttribute"
      get-method="getFormattedTestAttribute">
      <bind-xml name="test-attribute" node="attribute"/>
   </field>

moxyの外部メタデータでも同じようなことが可能ですか?

そのようなカスタマイズがサポートされていない場合、フィールドを読み取り専用としてマークし、別のフィールドを書き込み専用としてマークすることは可能ですか?したがって、「formattedTestAttribute」という名前の読み取り専用プロパティと「testAttribute」という名前の書き込み専用プロパティをメタデータバインディングファイルに宣言できますか?

<!-- read only property -->
<xml-element java-attribute="formattedTestAttribute" xml-path="@test-attribute" />

<!-- write only property -->
<xml-element java-attribute="testAttribute" xml-path="@test-attribute" /> 

モデルクラスの制御は非常に限られていることに注意してください。

よろしくお願いします。

4

1 に答える 1

3

これは、 EclipseLink JAXB (MOXy)の外部マッピング ドキュメントで次のように表すことができます。

<?xml version="1.0"?>
<xml-bindings
    xmlns="http://www.eclipse.org/eclipselink/xsds/persistence/oxm"
    package-name="forum8834871">
    <java-types>
        <java-type name="Test" xml-accessor-type="PUBLIC_MEMBER">
            <xml-root-element/>
            <java-attributes>
                <xml-element 
                    java-attribute="testAttribute" 
                    name="test-attribute">
                    <xml-access-methods 
                        get-method="getFormattedTestAttribute" 
                        set-method="setTestAttribute"/>
                </xml-element>
                <xml-transient java-attribute="formattedTestAttribute"/>
            </java-attributes>
        </java-type>
    </java-types>
</xml-bindings>

テスト

Testget/set メソッドにいくつかのロジックを入れるために、クラスを変更しました。

package forum8834871;

public class Test{

    private String testAttribute;

    public Test(){
    }

    public String getFormattedTestAttribute(){
       return "APPENDED_ON_GET " + testAttribute;
    }

    public void setTestAttribute(String value){
       testAttribute = "APPENDED_ON_SET " + value;
    }

}

デモ

package forum8834871;

import java.io.File;
import java.util.*;
import javax.xml.bind.*;
import org.eclipse.persistence.jaxb.JAXBContextFactory;

public class Demo {

    public static void main(String[] args) throws Exception {
        Map<String, Object> properties = new HashMap<String, Object>(1);
        properties.put(JAXBContextFactory.ECLIPSELINK_OXM_XML_KEY, "forum8834871/oxm.xml");
        JAXBContext jc = JAXBContext.newInstance(new Class[] {Test.class}, properties);

        File xml = new File("src/forum8834871/input.xml");
        Unmarshaller unmarshaller = jc.createUnmarshaller();
        Test test = (Test) unmarshaller.unmarshal(xml);

        Marshaller marshaller = jc.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        marshaller.marshal(test, System.out);
    }

}

入力.xml

<?xml version="1.0" encoding="UTF-8"?>
<test>
    <test-attribute>ORIGINAL</test-attribute>
</test>

出力

<?xml version="1.0" encoding="UTF-8"?>
<test>
   <test-attribute>APPENDED_ON_GET APPENDED_ON_SET ORIGINAL</test-attribute>
</test>
于 2012-01-12T14:15:56.357 に答える