码迷,mamicode.com
首页 > 系统相关 > 详细

打印org.eclipse.xsd.XSDSchema对象

时间:2014-11-16 00:36:10      阅读:172      评论:0      收藏:0      [点我收藏+]

标签:eclipse xsd   xsdschema   

因为网上关于Eclipse XSD的中文资料比较少,但是有的时候,我们需要使用Eclipse XSD的API去构造或者修改一个XSD文件。

那么当我们创建了org.eclipse.xsd.XSDSchema的对象,并已经在里面添加或者修改许多的元素类型等信息后,我们想知道我们的添加或者修改是否有效。

那么这个时候我们应该怎么办呢?有两种方式,我们把生成的org.eclipse.xsd.XSDSchema的对象,写到一个文件里面去,另外一种方式就是直接把XSDSchema对象

转成一个字符串,然后把XSDSchema代码的XSD打印出来。其代码代码方法如下:

import org.eclipse.xsd.XSDImport;
import org.eclipse.xsd.XSDInclude;
import org.eclipse.xsd.XSDRedefine;
import org.eclipse.xsd.XSDSchema;
import org.eclipse.xsd.XSDSchemaDirective;
import org.eclipse.xsd.util.XSDResourceImpl;
import org.w3c.dom.Element;

public class SchemaPrintService {

	public static void printSchema(XSDSchema xsdSchema){
	 System.out.println("<!-- ===== Schema Composition =====");
	 printDirectives("  ", xsdSchema);
        System.out.println("-->");
        
        System.out.println("<!-- [ " + xsdSchema.getSchemaLocation() + " ] -->");
        xsdSchema.updateElement();
        Element element = xsdSchema.getElement();
        if (element != null){
          // Print the serialization of the model.
          XSDResourceImpl.serialize(System.out, element);
        }
	}
	
	private static void printSchemaStart(XSDSchema xsdSchema) {
		System.out.print("<schema targetNamespace=\"");
		if (xsdSchema.getTargetNamespace() != null) {
			System.out.print(xsdSchema.getTargetNamespace());
		}
		System.out.print("\" schemaLocation=\"");
		if (xsdSchema.getSchemaLocation() != null) {
			System.out.print(xsdSchema.getSchemaLocation());
		}
		System.out.print("\">");
	}
	
	private static void printDirectives(String indent, XSDSchema xsdSchema) {
		System.out.print(indent);
		printSchemaStart(xsdSchema);
		System.out.println();

		if (!xsdSchema.getReferencingDirectives().isEmpty()) {
			System.out.println(indent + "  <referencingDirectives>");
			for (XSDSchemaDirective xsdSchemaDirective : xsdSchema.getReferencingDirectives()) {
				XSDSchema referencingSchema = xsdSchemaDirective.getSchema();
				System.out.print(indent + "    ");
				printSchemaStart(referencingSchema);
				System.out.println();
				System.out.print(indent + "      ");
				if (xsdSchemaDirective instanceof XSDImport) {
					XSDImport xsdImport = (XSDImport) xsdSchemaDirective;
					System.out.print("<import namespace=\"");
					if (xsdImport.getNamespace() != null) {
						System.out.print(xsdImport.getNamespace());
					}
					System.out.print("\" schemaLocation=\"");
				} else if (xsdSchemaDirective instanceof XSDRedefine) {
					System.out.print("<redefine schemaLocation=\"");
				} else if (xsdSchemaDirective instanceof XSDInclude) {
					System.out.print("<include schemaLocation=\"");
				}
				if (xsdSchemaDirective.getSchemaLocation() != null) {
					System.out.print(xsdSchemaDirective.getSchemaLocation());
				}
				System.out.println("\"/>");
				System.out.println(indent + "    </schema>");
			}
			System.out.println(indent + "  </referencingDirectives>");
		}

		if (!xsdSchema.getIncorporatedVersions().isEmpty()) {
			System.out.println(indent + "  <incorporatedVersions>");
			for (XSDSchema incorporatedVersion : xsdSchema
					.getIncorporatedVersions()) {
				printDirectives(indent + "    ", incorporatedVersion);
			}
			System.out.println(indent + "  </incorporatedVersions>");
		}

		System.out.println(indent + "</schema>");
	}

	
}


打印org.eclipse.xsd.XSDSchema对象

标签:eclipse xsd   xsdschema   

原文地址:http://blog.csdn.net/chancein007/article/details/41156783

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!