【问题标题】:Insert Logic code into generated JAXB java files by XSD def通过 XSD def 将逻辑代码插入到生成的 JAXB java 文件中
【发布时间】:2012-08-06 19:24:20
【问题描述】:

问题是,出于某种原因。 xsd 不能/不能定义除基本属性和 setter 和 getter 之外的所有逻辑变量,因此我们尝试通过 xsd 定义“注入代码”,这实际上已被其他人讨论过几次。我对使用“简单 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-code -extension

  3. 在Client.java中成功观察到下面的代码sn-p

           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 commons 中的 StringUtils(或其他第 3 部分实用工具,如 google 集合以在其他情况下提供帮助)未导入生成的文件中。了解有一些谷歌项目使用 jaxb 插件将方法插入或调用到生成的 java 文件中。只是想花一天左右的时间看看我们是否可以在没有任何插件的情况下仅通过 xsd 本身制作它。任何想法将不胜感激。

【问题讨论】:

    标签: java xml xsd jaxb


    【解决方案1】:

    您可以在要注入的代码中指定完全分类的类名,例如:

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

    【讨论】:

    • 丹尼,太好了。在某些情况下,这是解决此问题的一种方法。在这种情况下它应该可以正常工作。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多