Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
Copyright (c) 2004-2010 Oracle Corporation. All rights reserved. This program and the accompanying materials are made available under the terms of the Eclipse Public License v1.0 which accompanies this distribution, and is available at Contributors: Kohsuke Kawaguchi /
 package org.kohsuke.stapler.export;
 import java.util.Map;
 import java.util.Set;
Generates XML Schema that describes the XML representation of exported beans.

Kohsuke Kawaguchi
 public class SchemaGenerator {
     private final Stack<Modelqueue = new Stack<Model>();
     private final Set<Modelwritten = new HashSet<Model>();
Enumerations to be generated.
     private final Set<Classenums = new HashSet<Class>();
     private final ModelBuilder builder;
     private final Model<?> top;
     public SchemaGenerator(Model<?> m) {
         this. = m.parent;
         this. = m;

Generates the complete schema to the specified result.
     public void generateSchema(Result r) {
         Schema s = TXW.create(Schema.class, ResultFactory.createSerializer(r));
         // element decl for the root element
         // write all beans
             writeBean(s, (Model<?>) .pop());
         // then enums
         for (Class e : )
     private void writeEnum(Schema sClass e) {
         XSD.Restriction facets = s.simpleType().name(getXmlToken(e)).restriction().base(..);
         for (Object constant : e.getEnumConstants()) {
     private void writeBean(Schema sModel<?> m) {
         ComplexType ct = s.complexType().name(getXmlToken(m.type));
         final ContentModel cm;
             cm = ct.sequence();
         else {
             cm = ct.complexContent().extension().base(getXmlTypeName(m.superModel.type)).sequence();
         for (Property p : m.getProperties()) {
             Class t = p.getType();
            final boolean isCollection;
            final Class itemType;
            if(t.isArray()) {
                isCollection = true;
                itemType = t.getComponentType();
            } else
            if(Collection.class.isAssignableFrom(t)) {
                isCollection = true;
                itemType = TypeUtil.erasure(
            } else {
                isCollection = false;
                itemType = t;
            Element e = cm.element()

Annotates the schema element by javadoc, if that exists.
    private void annotate(Annotated eString javadoc) {
        if(javadoc==null)   return;

Adds the schema for the XML representation of the given class.
    public void add(Class<?> c) {
    private void addToQueue(Model m) {
        if (.add(m))
    public QName getXmlTypeName(Class<?> t) {
        if(..contains(t))       return ..;
        if(t==Boolean.class || t==boolean.class)    return ..;
        if(t==Integer.class || t==int.class)        return ..;
        if(t==Long.class || t==long.class)          return ..;
        if(Map.class.isAssignableFrom(t))           return ..;
        if(Calendar.class.isAssignableFrom(t))      return ..;
        if(Enum.class.isAssignableFrom(t)) {
            return new QName(getXmlToken(t));
        // otherwise bean
        try {
        } catch (IllegalArgumentException e) {
            // not an exposed bean by itself
            return ..;
        return new QName(getXmlToken(t));
    private String getXmlToken(Class<?> t) {
        return t.getName().replace('$','-');
    private String getXmlElementName(Class<?> type) {
        return getXmlToken(type);
New to GrepCode? Check out our FAQ X