工具软件   办公软件   操作系统   网络安全   设计在线   程序开发   教程宝典   软件下载   软件论坛
您的位置:软件 > 开发者网络 > 微软开发专栏 > 技术专题 > 正文
从XML到Java代码的数据绑定之创建类
[文章信息]
作者:键者天行
时间:2004-10-13
出处:csdn
责任编辑:方舟
[文章导读]
从 XML schema 创建 Java 类。该类必须准确表示数据约束,并提供 Java 应用程序将使用的简单读方法和写方法
advertisement
热点推荐
· Java加密和数字签名编程快速入门
· 在VB6中用命令行为模式控制GUI动作
· Excel图表向导详解
· 浅析各种计算机病毒应对方法
· 2月16日软件精选 春节文章精彩回顾
[正文]

上一页  1 2 3 4  下一页

  生成类

  构建好类的框架后,就可以添加类的主体了。

  我已经提到过生成过程具有递归性质。请记住这一点,需要填充 generateClasses() 方法才能开始。可以使用 JDOM 读取 XML schema,然后从 schema 中抽取每个 complexType 元素。对于这些元素中的每一个,如清单 3 所示,递归进程从 handleComplexType() 调用处开始(以后将进一步讨论)。

  清单 3. The generateClasses() 方法

public void generateClasses(URL schemaURL) throws IOException {
 /**
 * Create builder to generate JDOM representation of XML Schema,
 * without validation and using Apache Xerces.
 */
 SAXBuilder builder = new SAXBuilder();
 
 try {
  Document schemaDoc = builder.build(schemaURL);

  // Handle complex types
  List complexTypes = schemaDoc.getRootElement().getChildren("complexType", schemaNamespace);
  for (Iterator i = complexTypes.iterator(); i.hasNext(); ) {
   // Iterate and handle
   Element complexType = (Element)i.next();
   handleComplexType(complexType);
  }

 } catch (JDOMException e) {
  throw new IOException(e.getMessage());
 }
}

  为简便起见,将强调一些重点,而不是详细阐述将 schema 转换为 Java 类的整个过程。可以联机查看完整的 SchemaMapper 类,或者可以下载它。

  生成器必须确定在 XML schema 中找到的每个 complexType 元素是显式的(具有“类型”属性),还是隐式的(没有“类型”属性)。如果类型是显式的,则类型将成为接口名称,并且首字母大写。如果类型是隐式的,那么将根据特性名称构造接口名称。清单 4 中显示了处理这个逻辑的代码段。(如要了解更多数据绑定的定义,请参阅侧栏,术语解释。)

  清单 4. 确定接口名称 // Determine if this is an explict or implicit type

String type = null;
// Handle extension, if needed
String baseType = null;

try {
 // Assume that we are dealing with an explicit type
 type = complexType.getAttribute("name").getValue();
} catch (NoSuchAttributeException e) {
 /*
 * It is safe with an implicit type to assume that the parent
 * is of type "element", has no "type" attribute, and that we
 * can derive the type as the value of the element's "name"
 * attribute with the word "Type" appended to it.
 */
 try {
  type = new StringBuffer().append(BindingUtils.initialCaps(complexType.getParent()
   .getAttribute("name")
   .getValue()))
   .append("Type")
   .toString();
 } catch (NoSuchAttributeException nsae) {
  // Shouldn't happen in schema-valid documents
  throw new IOException("All elements must at have a name.");
 }
}

  因此,根据代码中的命名约定,具有 ServiceConfiguration 类型的元素将生成名为 ServiceConfiguration 的 Java 接口。名为 port 但没有显式类型的元素将生成叫做 PortType 的 Java 接口。它采用元素名称 (port),将首字母转成大写 (Port),再加上单词 Type,就得到了 PortType。

  同样,所有实现类都使用接口名称,然后添加缩写 Impl。所以,最终实现类是 ServiceConfigurationImpl 和 PortTypeImpl。

  使用这些命名约定,您可以很容易地确定将数据约束映射到 Java 接口会得到哪些 Java 类。如果设置了应用程序在运行时装入类,那么类装入器或其它实用程序可以迅速确定是否已装入了所需的类。类装入器或实用程序只要从 XML schema 中找出生成的类名称,然后尝试装入它们就可以了。命名逻辑是事先确定的,因此检查起来非常方便。

  一旦确定了名称,就可以生成接口和实现类的框架(请参阅清单 5)。

  清单 5. 生成代码

StringBuffer interfaceCode = new StringBuffer();
StringBuffer implementationCode = new StringBuffer();

/*
* Start writing out the interface and implementation class
* definitions.
*/
interfaceCode.append("public interface ")
.append(interfaceName);

// Add in extension if appropriate
if (baseType != null) {
interfaceCode.append(" extends ")
.append(baseType);
}

interfaceCode.append(" {\n");

implementationCode.append("public class ")
.append(implementationName);

// Add in extension if appropriate
if (baseType != null) {
implementationCode.append(" extends ")
.append(baseType)
.append("Impl");
}

implementationCode.append(" implements ")
.append(interfaceName)
.append(" {\n");

// Add in properties and methods

// Close up interface and implementation classes
interfaceCode.append("}");
implementationCode.append("}");

  实际上,生成属性和方法是相当简单的。将接口和相应实现的名称添加到类的存储器中,然后是右花括号,它们的作用是结束类。像这样成对生成类,而不是单独生成类,将使同时在接口和实现反映出该过程变得简单。检查源代码(请参阅参考资料),就可以得到足够的解释。

  清单 5 中的粗体注释表示源列表中的多行代码。在这里精简代码是为了保持简洁。对于正在创建的 XML schema 的每个特性(由 schema attribute 表示),都会将读方法和写方法添加到接口和实现(实现还有执行方法逻辑的代码)。同时,将为实现类的代码添加变量。

  最终结果就是本系列第一部分中生成的类。可以在这里查看它们,或者与本文中的其余代码一起下载(请参阅参考资料):

   ServiceConfiguration.java

   ServiceConfigurationImpl.java

   PortType.java

   PortTypeImpl.java

   DocumentType.java

   DocumentTypeImpl.java

   WebServiceConfiguration.java

   WebServiceConfigurationImpl.java

  有两个辅助程序类也将参与类生成:

  BindingUtils,将首字母变成大写。虽然,可以将这个方法添加到生成器类,但我打算以后在打包和解包类时再使用该方法,所以我将它归到这个辅助程序类中。可以联机查看 BindingUtils,或者可以下载它。

  DataMapping,SchemaMapper 类用来转换数据类型。可以联机查看源码或者下载源码。


上一页  1 2 3 4  下一页

天极社区邀请您:写博客日记  上传相片   论坛聊天  订阅电子杂志  推荐网摘   免费图铃工具
笔名:   请您注意:

 遵守国家有关法律、法规,尊重网上道德,承担一切因您的行为而直接或间接引起的法律责任。

 天极网拥有管理笔名和留言的一切权利。
评论:
 
发表评论推荐给朋友我想参加相关培训打印我对此感兴趣订阅电子杂志
相关内容焦点新闻
  • 中兴携手阿尔卡特 全球逐鹿CDMA
  • 用友总裁王文京:誓将ERP变成“大众消费”
  • 香港消费者委员会:数码相机最贵未必最好
  • 外电称中兴正评估西门子手机业务 或能并购
  • 国信办督战八大行业灾难备份 将出台国家标准
  • 中国IT企业走进欧盟 搭第六框架计划直通车
  • 美邦银行用户注意 警惕新型“钓鱼”邮件
  • 微软三年时间磨出新IE 具有更强大安全性能
  • Advertisement