Author: nickarls
Date: 2009-03-13 05:58:55 -0400 (Fri, 13 Mar 2009)
New Revision: 1962
Added:
ri/trunk/impl/src/main/java/org/jboss/webbeans/xsd/
ri/trunk/impl/src/main/java/org/jboss/webbeans/xsd/AnnotationProcessor.java
ri/trunk/impl/src/main/java/org/jboss/webbeans/xsd/AnnotationTest2.java
ri/trunk/impl/src/main/java/org/jboss/webbeans/xsd/DataSetter.java
ri/trunk/impl/src/main/java/org/jboss/webbeans/xsd/model/
ri/trunk/impl/src/main/java/org/jboss/webbeans/xsd/model/ClassModel.java
ri/trunk/impl/src/main/java/org/jboss/webbeans/xsd/model/MethodModel.java
ri/trunk/impl/src/main/java/org/jboss/webbeans/xsd/model/NamedModel.java
ri/trunk/impl/src/main/java/org/jboss/webbeans/xsd/model/ParameterFieldModel.java
Modified:
ri/trunk/impl/pom.xml
ri/trunk/impl/src/main/java/org/jboss/webbeans/jsf/PhaseHelper.java
Log:
Start work on package XSD generator
Modified: ri/trunk/impl/pom.xml
===================================================================
--- ri/trunk/impl/pom.xml 2009-03-13 08:33:15 UTC (rev 1961)
+++ ri/trunk/impl/pom.xml 2009-03-13 09:58:55 UTC (rev 1962)
@@ -89,6 +89,11 @@
</dependency>
<dependency>
+ <groupId>dom4j</groupId>
+ <artifactId>dom4j</artifactId>
+ </dependency>
+
+ <dependency>
<groupId>javax.el</groupId>
<artifactId>el-api</artifactId>
<optional>true</optional>
Modified: ri/trunk/impl/src/main/java/org/jboss/webbeans/jsf/PhaseHelper.java
===================================================================
--- ri/trunk/impl/src/main/java/org/jboss/webbeans/jsf/PhaseHelper.java 2009-03-13
08:33:15 UTC (rev 1961)
+++ ri/trunk/impl/src/main/java/org/jboss/webbeans/jsf/PhaseHelper.java 2009-03-13
09:58:55 UTC (rev 1962)
@@ -35,7 +35,7 @@
{
private static LogProvider log = Logging.getLogProvider(PhaseHelper.class);
- private static final String CONVERSATION_PROPAGATION_KEY =
"webbeans_conversation_propagationz";
+ private static final String CONVERSATION_PROPAGATION_KEY =
"webbeans_conversation_propagation";
/**
* Gets a FacesContext instance
Added: ri/trunk/impl/src/main/java/org/jboss/webbeans/xsd/AnnotationProcessor.java
===================================================================
--- ri/trunk/impl/src/main/java/org/jboss/webbeans/xsd/AnnotationProcessor.java
(rev 0)
+++ ri/trunk/impl/src/main/java/org/jboss/webbeans/xsd/AnnotationProcessor.java 2009-03-13
09:58:55 UTC (rev 1962)
@@ -0,0 +1,119 @@
+package org.jboss.webbeans.xsd;
+
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+
+import javax.annotation.processing.AbstractProcessor;
+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.dom4j.Document;
+import org.dom4j.DocumentHelper;
+import org.dom4j.Namespace;
+import org.dom4j.QName;
+import org.jboss.webbeans.xsd.model.ClassModel;
+
+(a)SupportedSourceVersion(SourceVersion.RELEASE_6)
+@SupportedAnnotationTypes("*")
+public class AnnotationProcessor extends AbstractProcessor
+{
+ private Map<String, ClassModel> classModelCache = new HashMap<String,
ClassModel>();
+ private Map<String, Document> packageXSDs = new HashMap<String,
Document>();
+ private List<ClassModel> workingSet = new ArrayList<ClassModel>();
+
+ @Override
+ public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment
roundEnv)
+ {
+ for (Element element : roundEnv.getRootElements())
+ {
+ workingSet.add(inspectClass(element));
+ }
+ if (roundEnv.processingOver())
+ {
+ writeXSD();
+ }
+ return true;
+ }
+
+ private Document getPackageXSD(String packageName)
+ {
+ Document packageXSD = packageXSDs.get(packageName);
+ if (packageXSD == null)
+ {
+ packageXSD = initPackageXSD(packageName);
+ packageXSDs.put(packageName, packageXSD);
+ }
+ return packageXSD;
+ }
+
+ private String getShortName(String packageName)
+ {
+ int lastDot = packageName.lastIndexOf(".");
+ return lastDot < 0 ? packageName : packageName.substring(lastDot + 1);
+ }
+
+ private Document initPackageXSD(String packageName)
+ {
+ Document packageXSD = DocumentHelper.createDocument();
+
+ packageXSD.addElement(new QName("Package", new
Namespace(getShortName(packageName), "urn:java:" + packageName)));
+ return packageXSD;
+ }
+
+ private void writeXSD()
+ {
+ for (ClassModel classModel : workingSet)
+ {
+ Document packageXSD = getPackageXSD(classModel.getPackage());
+ addClass(packageXSD, classModel);
+ }
+ for (Document document : packageXSDs.values())
+ {
+ System.out.println(document.asXML());
+ }
+ }
+
+ private void addClass(Document packageXSD, ClassModel classModel)
+ {
+ packageXSD.getRootElement().addElement(classModel.getSimpleName());
+ }
+
+ private ClassModel inspectClass(Element element)
+ {
+ TypeElement typeElement = (TypeElement) element;
+ ClassModel classModel = new ClassModel();
+
+ if (typeElement.getSuperclass().getKind() != TypeKind.NONE)
+ {
+ inspectClass(((DeclaredType) typeElement.getSuperclass()).asElement());
+ }
+
+ ClassModel parent = classModelCache.get(typeElement.getSuperclass().toString());
+ DataSetter.populateClassModel(classModel, element, parent);
+ for (Element field : ElementFilter.fieldsIn(element.getEnclosedElements()))
+ {
+ DataSetter.populateFieldModel(classModel, field);
+ }
+ for (Element method : ElementFilter.methodsIn(element.getEnclosedElements()))
+ {
+ DataSetter.populateMethodModel(classModel, method);
+ }
+ for (Element constructor :
ElementFilter.constructorsIn(element.getEnclosedElements()))
+ {
+ DataSetter.populateMethodModel(classModel, constructor);
+ }
+ classModelCache.put(classModel.getName(), classModel);
+ return classModel;
+ }
+
+}
Added: ri/trunk/impl/src/main/java/org/jboss/webbeans/xsd/AnnotationTest2.java
===================================================================
--- ri/trunk/impl/src/main/java/org/jboss/webbeans/xsd/AnnotationTest2.java
(rev 0)
+++ ri/trunk/impl/src/main/java/org/jboss/webbeans/xsd/AnnotationTest2.java 2009-03-13
09:58:55 UTC (rev 1962)
@@ -0,0 +1,30 @@
+package org.jboss.webbeans.xsd;
+
+public class AnnotationTest2 extends AnnotationTest
+{
+ public String tar;
+ private String xar;
+
+ private void hidden() {
+ }
+
+ public AnnotationTest2(String foo)
+ {
+ }
+
+ public AnnotationTest2() {
+ }
+
+ public void ping()
+ {
+ }
+
+ public void ping(String foo)
+ {
+ }
+
+ public String foo(String bar)
+ {
+ return bar;
+ }
+}
Added: ri/trunk/impl/src/main/java/org/jboss/webbeans/xsd/DataSetter.java
===================================================================
--- ri/trunk/impl/src/main/java/org/jboss/webbeans/xsd/DataSetter.java
(rev 0)
+++ ri/trunk/impl/src/main/java/org/jboss/webbeans/xsd/DataSetter.java 2009-03-13 09:58:55
UTC (rev 1962)
@@ -0,0 +1,74 @@
+package org.jboss.webbeans.xsd;
+
+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.MethodModel;
+import org.jboss.webbeans.xsd.model.ParameterFieldModel;
+
+public class DataSetter
+{
+
+ private static boolean isPublic(Element element)
+ {
+ return element.getModifiers().contains(Modifier.PUBLIC);
+ }
+
+ public static void populateClassModel(ClassModel classModel, Element element,
ClassModel parent)
+ {
+ TypeElement typeElement = (TypeElement) element;
+ classModel.setName(typeElement.getQualifiedName().toString());
+ classModel.setParent(parent);
+ }
+
+ 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 ParameterFieldModel(name, type));
+ }
+
+ 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();
+ ParameterFieldModel parameter = new ParameterFieldModel(paramName, paramType);
+ method.addParameter(parameter);
+ }
+ if ("<init>".equals(name))
+ {
+ classModel.addConstructor(method);
+ }
+ else
+ {
+ classModel.addMethod(method);
+ }
+ }
+
+ public static String getSimpleNameFromQualifiedName(String qualifiedName)
+ {
+ int lastDot = qualifiedName.lastIndexOf(".");
+ return lastDot < 0 ? qualifiedName : qualifiedName.substring(lastDot);
+ }
+
+}
Added: ri/trunk/impl/src/main/java/org/jboss/webbeans/xsd/model/ClassModel.java
===================================================================
--- ri/trunk/impl/src/main/java/org/jboss/webbeans/xsd/model/ClassModel.java
(rev 0)
+++ ri/trunk/impl/src/main/java/org/jboss/webbeans/xsd/model/ClassModel.java 2009-03-13
09:58:55 UTC (rev 1962)
@@ -0,0 +1,66 @@
+package org.jboss.webbeans.xsd.model;
+
+import java.util.ArrayList;
+import java.util.List;
+
+public class ClassModel extends NamedModel
+{
+ private ClassModel parent;
+
+ private List<ParameterFieldModel> fields = new
ArrayList<ParameterFieldModel>();
+ private List<MethodModel> methods = new ArrayList<MethodModel>();
+ private List<MethodModel> constructors = new ArrayList<MethodModel>();
+
+ public ClassModel()
+ {
+ }
+
+ public void addField(ParameterFieldModel fieldModel)
+ {
+ fields.add(fieldModel);
+ }
+
+ public void addConstructor(MethodModel constructorModel)
+ {
+ constructors.add(constructorModel);
+ }
+
+ public void addMethod(MethodModel methodModel)
+ {
+ methods.add(methodModel);
+ }
+
+ @Override
+ public String toString()
+ {
+ StringBuilder buffer = new StringBuilder();
+ buffer.append("Name: " + name + "\n");
+ buffer.append("Constructors: " + constructors + "\n");
+ buffer.append("Methods: " + methods + "\n");
+ buffer.append("Fields: " + fields + "\n");
+ return buffer.toString();
+ }
+
+ public ClassModel getParent()
+ {
+ return parent;
+ }
+
+ public void setParent(ClassModel parent)
+ {
+ this.parent = parent;
+ }
+
+ public String getPackage()
+ {
+ int lastDot = name.lastIndexOf(".");
+ return lastDot < 0 ? name : name.substring(0, lastDot);
+ }
+
+ public String getSimpleName()
+ {
+ int lastDot = name.lastIndexOf(".");
+ return lastDot < 0 ? name : name.substring(lastDot + 1);
+ }
+
+}
Added: ri/trunk/impl/src/main/java/org/jboss/webbeans/xsd/model/MethodModel.java
===================================================================
--- ri/trunk/impl/src/main/java/org/jboss/webbeans/xsd/model/MethodModel.java
(rev 0)
+++ ri/trunk/impl/src/main/java/org/jboss/webbeans/xsd/model/MethodModel.java 2009-03-13
09:58:55 UTC (rev 1962)
@@ -0,0 +1,32 @@
+package org.jboss.webbeans.xsd.model;
+
+import java.util.ArrayList;
+import java.util.List;
+
+public class MethodModel extends NamedModel
+{
+ private String returnType;
+ private List<ParameterFieldModel> parameters = new
ArrayList<ParameterFieldModel>();
+
+ public MethodModel(String name, String returnType)
+ {
+ super(name);
+ this.returnType = returnType;
+ }
+
+ public void addParameter(ParameterFieldModel parameter)
+ {
+ parameters.add(parameter);
+ }
+
+ public List<ParameterFieldModel> getParameters()
+ {
+ return parameters;
+ }
+
+ public String getReturnType()
+ {
+ return returnType;
+ }
+
+}
Added: ri/trunk/impl/src/main/java/org/jboss/webbeans/xsd/model/NamedModel.java
===================================================================
--- ri/trunk/impl/src/main/java/org/jboss/webbeans/xsd/model/NamedModel.java
(rev 0)
+++ ri/trunk/impl/src/main/java/org/jboss/webbeans/xsd/model/NamedModel.java 2009-03-13
09:58:55 UTC (rev 1962)
@@ -0,0 +1,25 @@
+package org.jboss.webbeans.xsd.model;
+
+public class NamedModel
+{
+ protected String name;
+
+ public NamedModel()
+ {
+ }
+
+ public NamedModel(String name)
+ {
+ this.name = name;
+ }
+
+ public String getName()
+ {
+ return name;
+ }
+
+ public void setName(String name)
+ {
+ this.name = name;
+ }
+}
Added: ri/trunk/impl/src/main/java/org/jboss/webbeans/xsd/model/ParameterFieldModel.java
===================================================================
--- ri/trunk/impl/src/main/java/org/jboss/webbeans/xsd/model/ParameterFieldModel.java
(rev 0)
+++
ri/trunk/impl/src/main/java/org/jboss/webbeans/xsd/model/ParameterFieldModel.java 2009-03-13
09:58:55 UTC (rev 1962)
@@ -0,0 +1,18 @@
+package org.jboss.webbeans.xsd.model;
+
+public class ParameterFieldModel extends NamedModel
+{
+ private String type;
+
+ public ParameterFieldModel(String name, String type)
+ {
+ super(name);
+ this.type = type;
+ }
+
+ public String getType()
+ {
+ return type;
+ }
+
+}