2

質問は、何らかの理由でです。xsdは、基本的なプロパティとセッターおよびゲッター以外のすべてのロジック変数を定義できない/できないため、他の人々によって実際に数回議論されているxsd定義によって「コードを挿入」しようとしました。クラスdefの上に「import」ステートメントを必要としない「simplejavamethod」を使用した「simpleinjection」に問題はありません。

それでもどういうわけかそれを使いたいのなら。セッターまたはゲッター以外のパッケージを取得またはインポートする方法はないように思われます。、詳細については以下を参照してください

  1. xsd定義test.xsd

             <?xml version="1.0" encoding="UTF-8"?>
            <xs:schema targetNamespace="http://company.com/schema/response"
        xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:test="http://company.com/schema/response"
        xmlns:jaxb="http://java.sun.com/xml/ns/jaxb" jaxb:version="2.1"
        xmlns:ci="http://jaxb.dev.java.net/plugin/code-injector"
        jaxb:extensionBindingPrefixes="ci">
        <xs:element name="client">
            <xs:complexType>
                <xs:annotation>
                    <xs:appinfo>
                        <ci:code>
                            <![CDATA[
                    private String str;
                    public String returnStr() {
                        Locations locationCls =this.getLocations();
                        List<String> locationids = new ArrayList<String>();
    
                        // get a list of locationid into locationids (list)
                        List<Location> locationList = locationCls.getLocation();
                        for (Location loc : locationList) {
                            locationids.add(String.valueOf(loc.getId()));
                        }
                        // return string like loc1,loc2,loc3
                        return StringUtils.join(locationids, ','); 
                    }
                            ]]>
                        </ci:code>
                    </xs:appinfo>
                </xs:annotation>
                <xs:sequence>
                    <xs:element name="name" type="xs:NCName" />
                    <xs:element name="pass" type="xs:NCName" />
                    <xs:element ref="test:locations" />
                </xs:sequence>
            </xs:complexType>
        </xs:element>
        <xs:element name="locations">
            <xs:complexType>
                <xs:sequence>
                    <xs:element maxOccurs="unbounded" ref="test:location" />
                </xs:sequence>
            </xs:complexType>
        </xs:element>
        <xs:element name="location">
            <xs:complexType>
                <xs:attribute name="id" use="required" type="xs:string" />
                <xs:attribute name="address" use="required" type="xs:string" />
                <xs:attribute name="biz" type="xs:string" />
            </xs:complexType>
        </xs:element>
        </xs:schema>
    
  2. jaxb riコマンドを実行します:xjc.bat test.xsd -Xinject-code -extension

  3. Client.javaの以下のコードスニペットを正常に確認してください

           private String str;
           public String returnStr() {
            Locations locationCls =this.getLocations();
            List<String> locationids = new ArrayList<String>();
    
            // get a list of locationid into locationids (list)
            List<Location> locationList = locationCls.getLocation();
            for (Location loc : locationList) {
                locationids.add(String.valueOf(loc.getId()));
            }
            // return string like loc1,loc2,loc3
            return StringUtils.join(locationids, ','); 
           }
    

結果として、ApacheコモンズのStringUtils(または他のシナリオで役立つgoogleコレクションのような他のサードパーティのutilツール)が生成されたファイルにインポートされないため、jdkがコンパイルエラーを訴えることがわかっています。生成されたJavaファイルにメソッドを挿入または呼び出すためにjaxbプラグインを使用するいくつかのGoogleプロジェクトがあることを理解してください。プラグインなしでxsd自体だけでそれを実現できるかどうかを確認するために1日かそこらを費やしたいだけです。任意のアイデアをいただければ幸いです。

4

1 に答える 1

2

挿入するコード内で完全に分類されたクラス名を指定できます。例:

return org.apache.commons.lang.StringUtils.join(locationids, ',');
于 2012-08-06T20:27:29.300 に答える