| | | | | | | [文章信息] | | | 作者: | 键者天行 | | 时间: | 2004-10-13 | | 出处: | csdn | | 责任编辑: | 方舟 | |
| [文章导读] | | | 从 XML schema 创建 Java 类。该类必须准确表示数据约束,并提供 Java 应用程序将使用的简单读方法和写方法 | |
| |
|
| | | |
|
|
|
|
|
组装 SchemaMapper 类框架
要做的第一件事就是为要生成的代码设置一些基本存储器。必须能够从每个执行映射的 XML schema 生成多个接口和实现。Java HashMap 正好满足要求。键是接口或实现名称以及映射表中的值,该值是将要输出到新 Java 程序文件的实际代码。还需要存储每对接口/实现的属性(属性是在这两种类之间共享的)。这里,我再次使用 HashMap。其中,键是接口名称。但是,由于每个接口可能有多个属性,因此该值是另一个具有属性及其类型的 HashMap。最后,必须存储 XML schema 的名称空间,因为 JDOM 将使用这个名称空间来访问 XML schema 中的结构。所有这些具体信息都足以初步勾画出新类的框架,新类在清单 2 中。
还请注意在清单 2 中已添加了两个需要使用的基本方法:其中一个方法需要使用 XML schema 的 URL 来执行生成(允许它在网络可访问 schema 以及本地 schema 下运行),另一个方法将类输出到指定的目录中。最后,简单的 main 方法将 XML schema 看作一个变量,然后执行生成。
清单 2. SchemaMapper 类的框架 package org.enhydra.xml.binding;
import java.io.File; import java.io.FileNotFoundException; import java.io.FileWriter; import java.io.IOException; import java.net.URL; import java.util.HashMap; import java.util.Map; import java.util.Iterator; import java.util.List;
// JDOM classes used for document representation import org.jdom.Document; import org.jdom.Element; import org.jdom.JDOMException; import org.jdom.Namespace; import org.jdom.NoSuchAttributeException; import org.jdom.NoSuchChildException; import org.jdom.input.SAXBuilder;
/** * <p> * <code>SchemaMapper</code> handles generation of Java interfaces and classes * from an XML schema, essentially allowing data contracts to be set up * for the binding of XML instance documents to Java objects. * </p> * * @author Brett McLaughlin */ public class SchemaMapper {
/** Storage for code for interfaces */ private Map interfaces;
/** Storage for code for implementations */ private Map implementations;
/** Properties that accessor/mutators should be created for */ protected Map properties;
/** XML Schema Namespace */ private Namespace schemaNamespace;
/** XML Schema Namespace URI */ private static final String SCHEMA_NAMESPACE_URI = "http://www.w3.org/1999/xmlSchema";
/** * <p> * Allocate storage and set up defaults. * </p> */ public SchemaMapper() { interfaces = new HashMap(); implementations = new HashMap(); properties = new HashMap(); schemaNamespace = Namespace.getNamespace(SCHEMA_NAMESPACE_URI); }
/** * <p> * This is the "entry point" for generation of Java classes from an XML * Schema. It allows a schema to be supplied, via <code>URL</code>, * and that schema is used for input to generation. * </p> * * @param schemaURL <code>URL</code> at which XML Schema is located. * @throws <code>IOException</code> - when problems in generation occur. */ public void generateClasses(URL schemaURL) throws IOException { // Perform generation }
/** * <p> * This will write out the generated classes to the supplied stream. * </p> * * @param directory <code>File</code> to write to (should be a directory). * @throws <code>IOException</code> - when output errors occur. */ public void writeClasses(File dir) throws IOException { // Perform output to files }
/** * <p> * This provides a static entry point for class generation from * XML Schemas. * </p> * * @param args <code>String[]</code> list of files to parse. */ public static void main(String[] args) { SchemaMapper mapper = new SchemaMapper(); try { for (int i=0; i<args.length; i++) { File file = new File(args[i]); mapper.generateClasses(file.toURL()); mapper.writeClasses(new File(".")); } } catch (FileNotFoundException e) { System.out.println("Could not locate XML Schema: "); e.printStackTrace(); } catch (IOException e) { System.out.println("Java class generation failed: "); e.printStackTrace(); } } } | In 清单 2 中,可以看到对于每个作为自变量传递的 XML schema,main 方法都调用生成过程。首先,方法会生成类。将文件名转换为 URL,并传递到 generateClasses(URL schemaURL)。然后,通过 writeClasses(File dir) 方法将类写到当前目录中(转换成 Java File: new File("."))。
任何其它 Java 类都可以在运行时进行相同的调用,并生成类。例如,一个定制类装入器也许能发现需要打包,确定仍要生成的接口和实现,并使用 SchemaMapper 类来执行该任务。所有这一切都在运行时完成。因为 generateClasses() 方法需要一个 URL,所以在网络上使用这个类非常简单。例如,可以使用它来请求从 HTTP 上公开可用的 XML schema 生成类。
由于对如何使用类做了尽量少的假设,因此它是一个普通类;程序可以同时在本地和远程使用它。并且这个类可以当作一组 Java 语言和 XML 实用程序类的一部分,而不是必须以某种特殊形式使用的专用类。这种可重用性原则对 XML 特别关键,因为在不同系统上进行网络访问和通信是 XML 的基本前提。
|
|
|
|
|
|
|
|
|