Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
2.0k views
in Technique[技术] by (71.8m points)

xml - Insert Logic code into generated JAXB java files by XSD 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.

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

             <?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. run jaxb ri command : xjc.bat test.xsd -Xinject-code -extension

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

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 .

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...