打印org.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>");
	}

}
时间: 2024-08-13 22:13:27

打印org.eclipse.xsd.XSDSchema对象的相关文章

使用org.eclipse.xsd.XSDEnumerationFacet生成枚举类型的Schema

因为网上关于Eclipse XSD的中文资料比较少,而且关于Eclipse XSD的范例代码也凤毛麟角,但是有的时候我们需要生成一个带枚举限定的简单类型的XSD Schema,比如下面的格式, <?xml version="1.0" encoding="UTF-8"?><schema xmlns="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www

【2017-05-03】winform打印控件、事件对象和事件数据、MDI窗体容器

一.打印控件 第一步先把打印对象搞出来. - printDocument    打印对象(将要打印的内容放到该对象里,从该对象里取内容打印) 设置他的PrintPage事件(对于要打印的每一页触发一次) - pageSetupDialog 打印设置对话框 - printPreviewDialog 打印预览对话框 - printPreviewControl  打印预览控件. 只需要把预览对象给他设置上就好 - printDialog  打印对话框 打印界面里面是否禁用页面范围.打印到文件等从Pri

发现日志文件和打印在eclipse控制台中的编码不一致

发现日志文件和打印在eclipse控制台中的编码不一致,正好相反. 日志文件是用notepad打开的,notepad有自己的编码方式,查询编码为utf-8,日志文件汉字等等显示正常. 但是在eclipse控制台打印的是乱码. 为什么? 原因:其实日志文件和控制台打印的本身是一致的,只是设置显示的编码方式不同. 控制台显示也有自己的编码方式,设置如下: http://www.cnblogs.com/nannan0226/p/8421757.html 原文地址:https://www.cnblogs

java之如何在eclipse中新建对象时自动补全

比如我们在输入: new Employee(); 这时按下ctrl+1, 选择第一项即可: Employee employee = new Employee(); 原文地址:https://www.cnblogs.com/xiximayou/p/12230554.html

如何用Eclipse API 提供的 org.eclipse.wst.wsdl 去解析读取WSDL文件?

相对来说,Eclipse API的中文资料比较少,但是Eclipse的API提供了非常多的,非常强大的功能. 比如,eclipse的Eclipse API 提供的 org.eclipse.wst.wsdl包,里面提供了很多的类去解析WSDL文件. 总体来说,其提供的API简单易懂,而且其API是和专业术语对应起来的,比如, 一个WSDL文档通常包含7个重要的元素,即types.import.message.portType.operation.binding. service元素. 这些元素嵌套

空对象模式(Null Object Pattern)

空对象模式:用一个空对象来取代null实例的检查,空对象实现一个不做任何动作的关系.(消除如if(Object == null) 这样的检查null实例代码) 例子: public abstract class Duck { public String name; public abstract void getName(); } public class ConcreteDuck extends Duck { public ConcreteDuck(String name) { this.na

IO【转换流,打印流,序列化】

编码: 把看的懂,变成看不懂的 String str = "中国"; byte[] bytes = str.getBytes(); System.out.println(Arrays.toString(bytes));解码: 把看不懂的内容,变成能看懂的 String s = new String(bytes); System.out.println(s); java.io.OutputStreamWriter extends Writer OutputStreamWriter:转换流

对象转换为json格式,类似中间层API

<一头扎进SpringMvc视频教程\<一头扎进SpringMvc>第四讲 源码\> 对象自动转换为json格式要在 spring-mvc.xml添加一个东西 ,和对应的命名空间引用和规范,和对应的jar包 <!-- 支持对象与json的转换. --> <mvc:annotation-driven/>  //注解驱动 提到目前已经引用了一堆很乱的jar包,maven时就简单了. <beans xmlns="http://www.springf

C# 打印和打印预览

C#打印和C#打印预览的实现的三大部分:页面设置.打印预览.打印具体用到的类和属性事件都是什么内容呢?那么本文就向你介绍C#打印和C#打印预览的实现具体事宜. 我们谈到C#打印和C#打印预览的实现其中主要就是包括:页面设置.打印预览.打印三大部分.打印的原理是:生成mdi文件,系统碰到mdi的时候会自动以打印的方式处理.所以,不管用什么模板,什么方式:能在PrintPage事件处理中,生成一张要打印内容的图片就OK了!主要的支持类都包括在命名空间System.Drawing.Printing中,