简体   繁体   English

通过XSD def将逻辑代码插入生成的JAXB Java文件中

[英]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. 除了基本属性,设置器和获取器之外,xsd不能/不能定义所有逻辑变量,因此我们尝试通过xsd定义“注入代码”,而其他人实际上已经讨论了两次。 I have no problem with 'simple injection' with 'simple java method' which won't need any 'import' statement on top of class def. 我对带有“简单java方法”的“简单注入”没有问题,在类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. 在我看来,除了setter或getters之外,我们无法获取或导入任何软件包。 , see below for details ,请参阅下面的详细信息

  1. xsd definition test.xsd 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. run jaxb ri command : xjc.bat test.xsd -Xinject-code -extension 运行jaxb ri命令:xjc.bat test.xsd -Xinject-code -extension

  3. observe below code snippet in the Client.java successfully 成功观察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, ','); } 

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. 因此,我们知道jdk抱怨编译错误,因为Apache Commons中的StringUtils(或其他第3部分实用工具,例如google集合,以在其他情况下提供帮助)未导入生成的文件中。 understand there are some google projects which use jaxb plugin to insert or invoke method into generated java files. 了解有些Google项目使用jaxb插件在生成的Java文件中插入或调用方法。 just want to spend day or so to see if we could make it by xsd itself only without any plugin. 只是想花一天左右的时间,看看我们是否可以仅使用xsd本身而无需任何插件。 any idea would be appreciated . 任何想法将不胜感激。

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM