Print the org. eclipse. xsd. XSDSchema object, eclipsexsd

Source: Internet
Author: User

Print the org. eclipse. xsd. XSDSchema object, eclipsexsd

Because there are few Chinese documents on Eclipse XSD on the Internet, but sometimes we need to use the Eclipse xsd api to construct or modify an XSD file. When org. eclipse. xsd. after the XSDSchema object has added or modified many element types and other information in it, we want to know whether our addition or modification is valid. What should we do at this time?

There are two ways,

(1) We will write the generated org. eclipse. xsd. XSDSchema object into a file.

(2) Another way is to directly convert the XSDSchema object into a string and print the xsd of the XSDSchema code.

In our code debugging process, of course, the second method is more convenient and convenient. The Code is as follows:

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>");}}


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.