通过XSD def将逻辑代码插入到生成的JAXB java文件中 [英] Insert Logic code into generated JAXB java files by XSD def

查看:62
本文介绍了通过XSD def将逻辑代码插入到生成的JAXB java文件中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题是出于某种原因。 xsd没有/不能定义除基本属性和setter和getter之外的所有逻辑变量,所以我们试图通过xsd定义注入代码,这些代码实际上已被其他人多次讨论过。使用'简单java方法'进行'简单注入'没有问题,它不需要在类def之上使用任何'import'语句。

Question is , for some reason. the xsd doesn't/can't define all logic vars other than basic properties and setters and getters, so we've tried to 'inject code' by xsd definition which are actually discussed by other folks couple of times. I have no problem with 'simple injection' with 'simple java method' which won't need any 'import' statement on top of class def.

但是如果我们想要使用它的话。在我看来,我们无法接受或导入除了setter或getter之外的任何包。 ,详见下文

yet somehow if we want to use it. it looks to me there is no way we could take or import any packages other than setter or getters. , see below for details


  1. xsd definition test.xsd

  1. xsd definition 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>


  • 运行jaxb ri命令:
    xjc.bat test.xsd -Xinject- code -extension

  • run jaxb ri command : xjc.bat test.xsd -Xinject-code -extension

    成功查看Client.java中的以下代码片段

    observe below code snippet in the Client.java successfully

           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(或google集合中的其他第三部分util工具,以帮助其他方案)不会导入生成的文件中。了解有一些谷歌项目使用jaxb插件插入或调用生成的java文件的方法。只是想花一天左右的时间来看看我们是否可以通过xsd本身来制作它而不需要任何插件。任何想法都将不胜感激。

    As a consequence, we know jdk complains the compilation error as StringUtils in Apache commons ( or other 3rd part util tools like google collections to help in other scenarios) are not imported in generated file. understand there are some google projects which use jaxb plugin to insert or invoke method into generated java files. just want to spend day or so to see if we could make it by xsd itself only without any plugin. any idea would be appreciated .

    推荐答案

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

    You could specify the fully classified class name within the code that you want to inject, e.g.:

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

    这篇关于通过XSD def将逻辑代码插入到生成的JAXB java文件中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆