Skip to content

Commit

Permalink
split out xsd
Browse files Browse the repository at this point in the history
git-svn-id: http://anonsvn.jboss.org/repos/weld/ri/trunk@2004 1c488680-804c-0410-94cd-c6b725194a0e
  • Loading branch information
pmuir committed Mar 15, 2009
1 parent 471cdb5 commit df52117
Show file tree
Hide file tree
Showing 12 changed files with 1,110 additions and 0 deletions.
1 change: 1 addition & 0 deletions pom.xml
Expand Up @@ -67,6 +67,7 @@
<module>api</module>
<module>spi</module>
<module>impl</module>
<module>xsd</module>
<module>porting-package</module>
<module>jboss-tck-runner</module>
</modules>
Expand Down
50 changes: 50 additions & 0 deletions xsd/pom.xml
@@ -0,0 +1,50 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<parent>
<artifactId>webbeans-parent</artifactId>
<groupId>org.jboss.webbeans</groupId>
<version>1.0.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<groupId>org.jboss.webbeans</groupId>
<artifactId>webbeans-xsd-generator</artifactId>
<version>1.0.0-SNAPSHOT</version>
<name>Web Beans XSD generator</name>
<dependencies>

<dependency>
<groupId>org.jboss.webbeans</groupId>
<artifactId>jsr299-api</artifactId>
</dependency>

<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<scope>test</scope>
<classifier>jdk15</classifier>
<exclusions>
<exclusion>
<artifactId>junit</artifactId>
<groupId>junit</groupId>
</exclusion>
</exclusions>
</dependency>

<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
</dependency>

<dependency>
<groupId>dom4j</groupId>
<artifactId>dom4j</artifactId>
</dependency>

</dependencies>

<build>
<defaultGoal>install</defaultGoal>
<plugins>
</plugins>
</build>

</project>
48 changes: 48 additions & 0 deletions xsd/src/main/java/org/jboss/webbeans/xsd/PackageInfo.java
@@ -0,0 +1,48 @@
package org.jboss.webbeans.xsd;

import java.util.List;

import org.dom4j.Document;

public class PackageInfo
{
private List<String> namespaces;
private Document schema;
private String packageName;

public PackageInfo(String packageName)
{
this.packageName = packageName;
}

public List<String> getNamespaces()
{
return namespaces;
}

public void setNamespaces(List<String> namespaces)
{
this.namespaces = namespaces;
}

public Document getSchema()
{
return schema;
}

public void setSchema(Document schema)
{
this.schema = schema;
}

public String getPackageName()
{
return packageName;
}

public void setPackageName(String packageName)
{
this.packageName = packageName;
}

}
124 changes: 124 additions & 0 deletions xsd/src/main/java/org/jboss/webbeans/xsd/PackageSchemaGenerator.java
@@ -0,0 +1,124 @@
/*
* JBoss, Home of Professional Open Source
* Copyright 2008, Red Hat Middleware LLC, and individual contributors
* by the @authors tag. See the copyright.txt in the distribution for a
* full listing of individual contributors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.jboss.webbeans.xsd;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import javax.annotation.processing.AbstractProcessor;
import javax.annotation.processing.ProcessingEnvironment;
import javax.annotation.processing.RoundEnvironment;
import javax.annotation.processing.SupportedAnnotationTypes;
import javax.annotation.processing.SupportedSourceVersion;
import javax.lang.model.SourceVersion;
import javax.lang.model.element.Element;
import javax.lang.model.element.TypeElement;
import javax.lang.model.type.DeclaredType;
import javax.lang.model.type.TypeKind;
import javax.lang.model.util.ElementFilter;

import org.jboss.webbeans.xsd.helpers.DataSetter;
import org.jboss.webbeans.xsd.helpers.XSDHelper;
import org.jboss.webbeans.xsd.model.ClassModel;

/**
* An annotation processor that updates the package-level XSD for the packages
* that have had their files compiled.
*
* @author Nicklas Karlsson
*
*/
@SupportedSourceVersion(SourceVersion.RELEASE_6)
@SupportedAnnotationTypes("*")
public class PackageSchemaGenerator extends AbstractProcessor
{
// A helper for the XSD operations
XSDHelper helper;

@Override
public synchronized void init(ProcessingEnvironment processingEnv)
{
super.init(processingEnv);
helper = new XSDHelper(processingEnv.getFiler());
}

@Override
public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv)
{
List<ClassModel> workingSet = new ArrayList<ClassModel>();
// Iterates over the classes compiled, creates a model of the classes and
// add them to a working set
for (Element element : roundEnv.getRootElements())
{
workingSet.add(inspectClass(element));
}
if (!roundEnv.processingOver())
{
// Update the package XSDs for the files changed
helper.updateSchemas(workingSet);
// And flush the changes to disk
helper.writeSchemas();
}
return false;
}

/**
* Creates a class model from a class element
*
* @param element The element to analyze
* @return The class model
*/
private ClassModel inspectClass(Element element)
{
TypeElement typeElement = (TypeElement) element;
ClassModel classModel = new ClassModel();

// If the class has superclass's, scan them recursively
if (typeElement.getSuperclass().getKind() != TypeKind.NONE)
{
inspectClass(((DeclaredType) typeElement.getSuperclass()).asElement());
}

// Gets the parent from the cache. We know it's there since we has scanned
// the
// hierarchy already
ClassModel parent = helper.getCachedClassModel(typeElement.getSuperclass().toString());
// Populate the class level info (name, parent etc)
DataSetter.populateClassModel(classModel, element, parent);
// Filter out the fields and populate the model
for (Element field : ElementFilter.fieldsIn(element.getEnclosedElements()))
{
DataSetter.populateFieldModel(classModel, field);
}
// Filter out the methods and populate the model
for (Element method : ElementFilter.methodsIn(element.getEnclosedElements()))
{
DataSetter.populateMethodModel(classModel, method);
}
// Filter out the constructors and populate the model
for (Element constructor : ElementFilter.constructorsIn(element.getEnclosedElements()))
{
DataSetter.populateMethodModel(classModel, constructor);
}
// Place the new class model in the cache
helper.cacheClassModel(classModel);
return classModel;
}

}
118 changes: 118 additions & 0 deletions xsd/src/main/java/org/jboss/webbeans/xsd/helpers/DataSetter.java
@@ -0,0 +1,118 @@
/*
* JBoss, Home of Professional Open Source
* Copyright 2008, Red Hat Middleware LLC, and individual contributors
* by the @authors tag. See the copyright.txt in the distribution for a
* full listing of individual contributors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.jboss.webbeans.xsd.helpers;

import javax.lang.model.element.Element;
import javax.lang.model.element.ExecutableElement;
import javax.lang.model.element.Modifier;
import javax.lang.model.element.TypeElement;
import javax.lang.model.element.VariableElement;

import org.jboss.webbeans.xsd.model.ClassModel;
import org.jboss.webbeans.xsd.model.FieldModel;
import org.jboss.webbeans.xsd.model.MethodModel;
import org.jboss.webbeans.xsd.model.ParameterModel;

/**
* Helper for examining classes and members and populating the model
*
* @author Nicklas Karlsson
*
*/
public class DataSetter
{

/**
* Checks if an element is public
*
* @param element The element to check
* @return True if public, false otherwise
*/
private static boolean isPublic(Element element)
{
return element.getModifiers().contains(Modifier.PUBLIC);
}

/**
* Inspects a type element and populates a class model
*
* @param classModel The class model to populate
* @param element The element to inspect
* @param parent The parent of the class
*/
public static void populateClassModel(ClassModel classModel, Element element, ClassModel parent)
{
TypeElement typeElement = (TypeElement) element;
classModel.setName(typeElement.getQualifiedName().toString());
classModel.setParent(parent);
}

/**
* Inspects a field element and populates a class model
*
* @param classModel The class model to populate
* @param element The element to inspect
*/
public static void populateFieldModel(ClassModel classModel, Element element)
{
if (!isPublic(element))
{
return;
}
String name = element.getSimpleName().toString();
String type = element.asType().toString();
classModel.addField(new FieldModel(name, type));
}

/**
* Inspects a method or constructor and populates a class model
*
* @param classModel The class model to populate
* @param element The element to inspect
*/
public static void populateMethodModel(ClassModel classModel, Element element)
{
if (!isPublic(element))
{
return;
}
ExecutableElement executableElement = (ExecutableElement) element;

String name = element.getSimpleName().toString();
String returnType = executableElement.getReturnType().toString();
MethodModel method = new MethodModel(name, returnType);

for (VariableElement parameterElement : executableElement.getParameters())
{
String paramName = parameterElement.getSimpleName().toString();
String paramType = parameterElement.asType().toString();
ParameterModel parameter = new ParameterModel(paramName, paramType);
method.addParameter(parameter);
}
// OK, checting a little with a common model for methods and constructors
if ("<init>".equals(name))
{
classModel.addConstructor(method);
}
else
{
classModel.addMethod(method);
}
}

}

0 comments on commit df52117

Please sign in to comment.