2012-08-06 69 views
1

問題出於某種原因。 xsd不能/不能定義除基本屬性和setter和getter之外的所有邏輯變量,所以我們試圖通過xsd定義來「注入代碼」,這是其他人偶爾討論過的。我用'簡單的java方法''簡單的注入'沒有問題,它不需要在類def上面的任何'import'聲明。通過XSD將邏輯代碼插入生成的JAXB java文件def

不知怎的,如果我們想使用它。它看起來對我來說,我們無法取得或導入除setter或getter之外的任何軟件包。 ,詳見下文

  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碼-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, ','); 
         } 
    

因此,我們知道jdk抱怨編譯錯誤爲Apache公用程序中的StringUtils(或其他第三方util應用程序工具,如google集合以幫助其他場景)不會導入生成的文件中。明白有一些谷歌項目使用jaxb插件插入或調用方法到生成的java文件。只是想花一天左右的時間來看看我們是否可以通過xsd本身只有沒有任何插件。任何想法將不勝感激。

回答

1

你可能要注入的代碼,例如:

return org.apache.commons.lang.StringUtils.join(locationids, ','); 
+0

丹尼,大內指定完全分類的類別名稱。這是在某些情況下解決此問題的一種方法。在這種情況下,它應該可以正常工作。 – 2012-08-07 00:30:07