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 http://www.eclipse.org/legal/epl-v10.html 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.

Author(s):
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));
         s._namespace(.,"xsd");
 
         .clear();
         .clear();
 
         // element decl for the root element
 
         // write all beans
         while(!.isEmpty())
             writeBean(s, (Model<?>) .pop());
 
         // then enums
         for (Class e : )
             writeEnum(s,e);
 
         s.commit();
     }
 
     private void writeEnum(Schema sClass e) {
         XSD.Restriction facets = s.simpleType().name(getXmlToken(e)).restriction().base(..);
         for (Object constant : e.getEnumConstants()) {
             facets.enumeration().value(constant.toString());
         }
     }
 
     private void writeBean(Schema sModel<?> m) {
         ComplexType ct = s.complexType().name(getXmlToken(m.type));
         final ContentModel cm;
 
         if(m.superModel==null)
             cm = ct.sequence();
         else {
             addToQueue(m.superModel);
             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(
                    TypeUtil.getTypeArgument(TypeUtil.getBaseClass(p.getGenericType(),Collection.class),0));
            } else {
                isCollection = false;
                itemType = t;
            }
            Element e = cm.element()
                .name(isCollection?XMLDataWriter.toSingular(p.name):p.name)
                .type(getXmlTypeName(itemType));
            if(!t.isPrimitive())
                e.minOccurs(0);
            if(isCollection)
                e.maxOccurs("unbounded");
            annotate(e,p.getJavadoc());
        }
    }

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

    
Adds the schema for the XML representation of the given class.
    public void add(Class<?> c) {
        addToQueue(.get(c));
    }
    private void addToQueue(Model m) {
        if (.add(m))
            .push(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)) {
            .add(t);
            return new QName(getXmlToken(t));
        }
        // otherwise bean
        try {
            addToQueue(.get(t));
        } 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