+49 228 5552576-0


info@predic8.com

Parsing Schema with Java

The Membrane SOA Model makes it easy to parse a schema. Listing 1 is an example of parsing a schema with almost all the information of it. Over and above that, you can get much more details using many other methods and features of SOA Model. For more information see the java api doc or discuss in SOA Model Forum.

package samples.schema;

import com.predic8.schema.Appinfo;
import com.predic8.schema.Attribute;
import com.predic8.schema.ComplexContent;
import com.predic8.schema.ComplexType;
import com.predic8.schema.Derivation;
import com.predic8.schema.Documentation;
import com.predic8.schema.Element;
import com.predic8.schema.Import;
import com.predic8.schema.Include;
import com.predic8.schema.ModelGroup;
import com.predic8.schema.Schema;
import com.predic8.schema.SchemaComponent;
import com.predic8.schema.SchemaParser;
import com.predic8.schema.SimpleType;

public class FullSchemaParser {

	public static void main(String[] args) {
		SchemaParser parser = new SchemaParser();
		Schema schema = parser.parse("samples/xsd/human-resources.xsd");

		out("-------------- Schema Information --------------");
		out("  Schema TargetNamespace: " + schema.getTargetNamespace());
		out("  AttributeFormDefault: " + schema.getAttributeFormDefault());
		out("  ElementFormDefault: " + schema.getElementFormDefault());
		out("");

		if (schema.getImports().size() > 0) {
			out("  Schema Imports: ");
			for (Import imp : schema.getImports()) {
				out("    Import Namespace: " + imp.getNamespace());
				out("    Import Location: " + imp.getSchemaLocation());
			}
			out("");
		}

		if (schema.getIncludes().size() > 0) {
			out("  Schema Includes: ");
			for (Include inc : schema.getIncludes()) {
				out("    Include Location: " + inc.getSchemaLocation());
			}
			out("");
		}

		out("  Schema Elements: ");
		for (Element e : schema.getAllElements()) {
			out("    Element Name: " + e.getName());
			if (e.getType() != null) {
				/*
				 * schema.getType() delivers a TypeDefinition (SimpleType orComplexType)
				 * object.
				 */
				out("    Element Type Name: " + schema.getType(e.getType()).getName());
				out("    Element minoccurs: " + e.getMinOccurs());
				out("    Element maxoccurs: " + e.getMaxOccurs());
				if (e.getAnnotation() != null)
					annotationOut(e);
			}
		}
		out("");

		out("  Schema ComplexTypes: ");
		for (ComplexType ct : schema.getComplexTypes()) {
			out("    ComplexType Name: " + ct.getName());
			if (ct.getAnnotation() != null)
				annotationOut(ct);
			if (ct.getAttributes().size() > 0) {
				out("    ComplexType Attributes: ");
				/*
				 * If available, attributeGroup could be read as same as attribute in
				 * the following.
				 */
				for (Attribute attr : ct.getAttributes()) {
					out("      Attribute Name: " + attr.getName());
					out("      Attribute Form: " + attr.getForm());
					out("      Attribute ID: " + attr.getId());
					out("      Attribute Use: " + attr.getUse());
					out("      Attribute FixedValue: " + attr.getFixedValue());
					out("      Attribute DefaultValue: " + attr.getDefaultValue());
				}
			}
			/*
			 * ct.getModel() delivers the child element used in complexType. In case
			 * of 'sequence' you can also use the getSequence() method.
			 */
			out("    ComplexType Model: " + ct.getModel().getClass().getSimpleName());
			if (ct.getModel() instanceof ModelGroup) {
				out("    Model Particles: ");
				for (SchemaComponent sc : ((ModelGroup) ct.getModel()).getParticles()) {
					out("      Particle Kind: " + sc.getClass().getSimpleName());
					out("      Particle Name: " + sc.getName() + "\n");
				}
			}

			if (ct.getModel() instanceof ComplexContent) {
				Derivation der = ((ComplexContent) ct.getModel()).getDerivation();
				out("      ComplexConten Derivation: " + der.getClass().getSimpleName());
				out("      Derivation Base: " + der.getBase());
			}

			if (ct.getAbstractAttr() != null)
				out("    ComplexType AbstractAttribute: " + ct.getAbstractAttr());
			if (ct.getAnyAttribute() != null)
				out("    ComplexType AnyAttribute: " + ct.getAnyAttribute());

			out("");
		}

		if (schema.getSimpleTypes().size() > 0) {
			out("  Schema SimpleTypes: ");
			for (SimpleType st : schema.getSimpleTypes()) {
				out("    SimpleType Name: " + st.getName());
				out("    SimpleType Restriction: " + st.getRestriction());
				out("    SimpleType Union: " + st.getUnion());
				out("    SimpleType List: " + st.getList());
			}
		}
	}

	private static void annotationOut(SchemaComponent sc) {
		if (sc.getAnnotation().getAppinfos().size() > 0) {
			System.out
			    .print("    Annotation (appinfos) available with the content: ");
			for (Appinfo appinfo : sc.getAnnotation().getAppinfos()) {
				out(appinfo.getContent());
			}
		} else {
			System.out
			    .print("    Annotation (documentation) available with the content: ");
			for (Documentation doc : sc.getAnnotation().getDocumentations()) {
				out(doc.getContent());
			}
		}
	}

	private static void out(String str) {
		System.out.println(str);
	}
}
          
Listing 1: FullSchemaParser.java

In listing 2 you can see the output.

-------------- Schema Information --------------
  Schema TargetNamespace: http://predic8.com/human-resources/
  AttributeFormDefault: unqualified
  ElementFormDefault: unqualified

  Schema Imports: 
    Import Namespace: http://predic8.com/common-types/
    Import Location: common-types.xsd

  Schema Elements: 

  Schema ComplexTypes: 
    ComplexType Name: PersonType
    ComplexType Model: Sequence
    Model Particles: 
      Particle Kind: Element
      Particle Name: id

      Particle Kind: Element
      Particle Name: firstName

      Particle Kind: Element
      Particle Name: lastName

      Particle Kind: Element
      Particle Name: address


    ComplexType Name: AddressType
    ComplexType Model: Sequence
    Model Particles: 
      Particle Kind: Element
      Particle Name: street

      Particle Kind: Element
      Particle Name: city

      Particle Kind: Element
      Particle Name: postCode

      Particle Kind: Element
      Particle Name: country


    ComplexType Name: EmployeeType
    ComplexType Model: ComplexContent
      ComplexConten Derivation: Extension
      Derivation Base: {http://predic8.com/human-resources/}PersonType

    ComplexType Name: EmployeeListType
    ComplexType Model: Sequence
    Model Particles: 
      Particle Kind: Element
      Particle Name: employee
      
Listing 2: The List of the operation in the WSDL Document