生成类
构建好类的框架后,就可以添加类的主体了。
我已经提到过生成过程具有递归性质。请记住这一点,需要填充 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 类用来转换数据类型。可以联机查看源码或者下载源码。
|
|