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

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

问题描述

问题是,出于某种原因.xsd 不/不能定义除基本属性、setter 和 getter 之外的所有逻辑变量,因此我们尝试通过 xsd 定义注入代码",其他人实际上讨论过几次.我对带有简单java方法"的简单注入"没有问题,它不需要在类def之上的任何导入"语句.

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 定义 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(或其他 3rd part util 工具,如 google collections 在其他情况下提供帮助)未导入到生成的文件中.了解有一些谷歌项目使用 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天全站免登陆