Author: nickarls
Date: 2009-03-13 09:45:34 -0400 (Fri, 13 Mar 2009)
New Revision: 1968
Added:
ri/trunk/impl/src/main/java/org/jboss/webbeans/xsd/PackageSchemaGenerator.java
ri/trunk/impl/src/main/java/org/jboss/webbeans/xsd/helpers/
ri/trunk/impl/src/main/java/org/jboss/webbeans/xsd/helpers/DataSetter.java
Removed:
ri/trunk/impl/src/main/java/org/jboss/webbeans/xsd/AnnotationProcessor.java
ri/trunk/impl/src/main/java/org/jboss/webbeans/xsd/DataSetter.java
Modified:
ri/trunk/impl/src/main/java/org/jboss/webbeans/xsd/model/ClassModel.java
ri/trunk/impl/src/main/java/org/jboss/webbeans/xsd/model/FieldModel.java
ri/trunk/impl/src/main/java/org/jboss/webbeans/xsd/model/MethodModel.java
Log:
Some more xsd stuff
Deleted: ri/trunk/impl/src/main/java/org/jboss/webbeans/xsd/AnnotationProcessor.java
===================================================================
--- ri/trunk/impl/src/main/java/org/jboss/webbeans/xsd/AnnotationProcessor.java 2009-03-13
12:19:49 UTC (rev 1967)
+++ ri/trunk/impl/src/main/java/org/jboss/webbeans/xsd/AnnotationProcessor.java 2009-03-13
13:45:34 UTC (rev 1968)
@@ -1,119 +0,0 @@
-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;
- }
-
-}
Deleted: ri/trunk/impl/src/main/java/org/jboss/webbeans/xsd/DataSetter.java
===================================================================
--- ri/trunk/impl/src/main/java/org/jboss/webbeans/xsd/DataSetter.java 2009-03-13 12:19:49
UTC (rev 1967)
+++ ri/trunk/impl/src/main/java/org/jboss/webbeans/xsd/DataSetter.java 2009-03-13 13:45:34
UTC (rev 1968)
@@ -1,69 +0,0 @@
-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.FieldModel;
-import org.jboss.webbeans.xsd.model.MethodModel;
-import org.jboss.webbeans.xsd.model.ParameterModel;
-
-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 FieldModel(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();
- ParameterModel parameter = new ParameterModel(paramName, paramType);
- method.addParameter(parameter);
- }
- if ("<init>".equals(name))
- {
- classModel.addConstructor(method);
- }
- else
- {
- classModel.addMethod(method);
- }
- }
-
-}
Copied: ri/trunk/impl/src/main/java/org/jboss/webbeans/xsd/PackageSchemaGenerator.java
(from rev 1962,
ri/trunk/impl/src/main/java/org/jboss/webbeans/xsd/AnnotationProcessor.java)
===================================================================
--- ri/trunk/impl/src/main/java/org/jboss/webbeans/xsd/PackageSchemaGenerator.java
(rev 0)
+++
ri/trunk/impl/src/main/java/org/jboss/webbeans/xsd/PackageSchemaGenerator.java 2009-03-13
13:45:34 UTC (rev 1968)
@@ -0,0 +1,209 @@
+package org.jboss.webbeans.xsd;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+import java.util.Map.Entry;
+
+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 javax.tools.StandardLocation;
+
+import org.dom4j.Document;
+import org.dom4j.DocumentException;
+import org.dom4j.DocumentHelper;
+import org.dom4j.Namespace;
+import org.dom4j.Node;
+import org.dom4j.QName;
+import org.dom4j.io.OutputFormat;
+import org.dom4j.io.SAXReader;
+import org.dom4j.io.XMLWriter;
+import org.jboss.webbeans.xsd.helpers.DataSetter;
+import org.jboss.webbeans.xsd.model.ClassModel;
+import org.xml.sax.SAXException;
+
+(a)SupportedSourceVersion(SourceVersion.RELEASE_6)
+@SupportedAnnotationTypes("*")
+public class PackageSchemaGenerator extends AbstractProcessor
+{
+ private Map<String, ClassModel> classModelCache = new HashMap<String,
ClassModel>();
+ private Map<String, Document> packageXSDs = new HashMap<String,
Document>();
+
+ @Override
+ public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment
roundEnv)
+ {
+ List<ClassModel> workingSet = new ArrayList<ClassModel>();
+ for (Element element : roundEnv.getRootElements())
+ {
+ workingSet.add(inspectClass(element));
+ }
+ if (!roundEnv.processingOver())
+ {
+ updatePackageXSDs(workingSet);
+ flushPackageXSDs();
+ }
+ return false;
+ }
+
+ private Document getPackageXSD(String packageName)
+ {
+ Document packageXSD = packageXSDs.get(packageName);
+ if (packageXSD == null)
+ {
+ try
+ {
+ packageXSD = readPackageXSD(packageName);
+ }
+ catch (IOException e)
+ {
+ throw new RuntimeException("Could not read schema for package " +
packageName);
+ }
+ catch (DocumentException e)
+ {
+ throw new RuntimeException("Could not parse schema for package " +
packageName);
+ }
+ if (packageXSD == null)
+ {
+ packageXSD = createPackageXSD(packageName);
+ }
+ packageXSDs.put(packageName, packageXSD);
+ }
+ return packageXSD;
+ }
+
+ private Document readPackageXSD(String packageName) throws IOException,
DocumentException
+ {
+ InputStream in = null;
+ try
+ {
+ in = processingEnv.getFiler().getResource(StandardLocation.CLASS_OUTPUT,
packageName, "schema.xsd").openInputStream();
+ return new SAXReader().read(in);
+ }
+ catch (IOException e)
+ {
+ return null;
+ }
+ finally
+ {
+ if (in != null)
+ {
+ in.close();
+ }
+ }
+ }
+
+ private void flushPackageXSDs()
+ {
+ for (Entry<String, Document> entry : packageXSDs.entrySet())
+ {
+ try
+ {
+ writePackageXSD(entry.getKey(), entry.getValue());
+ }
+ catch (IOException e)
+ {
+ throw new RuntimeException("Could not flush XSD for " +
entry.getKey());
+ }
+ catch (SAXException e)
+ {
+ throw new RuntimeException("Could not parse XSD when flushing for "
+ entry.getKey());
+ }
+ }
+ }
+
+ private void writePackageXSD(String packageName, Document packageXSD) throws
IOException, SAXException
+ {
+ OutputStream out = null;
+ try
+ {
+ OutputFormat format = OutputFormat.createPrettyPrint();
+ out = processingEnv.getFiler().createResource(StandardLocation.CLASS_OUTPUT,
packageName, "schema.xsd").openOutputStream();
+ XMLWriter writer = new XMLWriter(out, format);
+ writer.write(packageXSD);
+ writer.flush();
+ writer.close();
+ }
+ finally
+ {
+ if (out != null)
+ {
+ out.close();
+ }
+ }
+ }
+
+ private String getShortName(String packageName)
+ {
+ int lastDot = packageName.lastIndexOf(".");
+ return lastDot < 0 ? packageName : packageName.substring(lastDot + 1);
+ }
+
+ private Document createPackageXSD(String packageName)
+ {
+ Document packageXSD = DocumentHelper.createDocument();
+
+ packageXSD.addElement(new QName("Package", new
Namespace(getShortName(packageName), "urn:java:" + packageName)));
+ return packageXSD;
+ }
+
+ private void updatePackageXSDs(List<ClassModel> classModels)
+ {
+ for (ClassModel classModel : classModels)
+ {
+ Document packageXSD = getPackageXSD(classModel.getPackage());
+ updateClass(packageXSD, classModel);
+ }
+ }
+
+ private void updateClass(Document packageXSD, ClassModel classModel)
+ {
+ Node oldClassModel = packageXSD.selectSingleNode("//" +
classModel.getSimpleName());
+ if (oldClassModel != null)
+ {
+ packageXSD.getRootElement().remove(oldClassModel);
+ }
+ 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;
+ }
+
+}
Property changes on:
ri/trunk/impl/src/main/java/org/jboss/webbeans/xsd/PackageSchemaGenerator.java
___________________________________________________________________
Name: svn:mergeinfo
+
Copied: ri/trunk/impl/src/main/java/org/jboss/webbeans/xsd/helpers/DataSetter.java (from
rev 1965, ri/trunk/impl/src/main/java/org/jboss/webbeans/xsd/DataSetter.java)
===================================================================
--- ri/trunk/impl/src/main/java/org/jboss/webbeans/xsd/helpers/DataSetter.java
(rev 0)
+++ ri/trunk/impl/src/main/java/org/jboss/webbeans/xsd/helpers/DataSetter.java 2009-03-13
13:45:34 UTC (rev 1968)
@@ -0,0 +1,69 @@
+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;
+
+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 FieldModel(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();
+ ParameterModel parameter = new ParameterModel(paramName, paramType);
+ method.addParameter(parameter);
+ }
+ if ("<init>".equals(name))
+ {
+ classModel.addConstructor(method);
+ }
+ else
+ {
+ classModel.addMethod(method);
+ }
+ }
+
+}
Property changes on:
ri/trunk/impl/src/main/java/org/jboss/webbeans/xsd/helpers/DataSetter.java
___________________________________________________________________
Name: svn:mergeinfo
+
Modified: 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 2009-03-13
12:19:49 UTC (rev 1967)
+++ ri/trunk/impl/src/main/java/org/jboss/webbeans/xsd/model/ClassModel.java 2009-03-13
13:45:34 UTC (rev 1968)
@@ -37,9 +37,9 @@
{
StringBuilder buffer = new StringBuilder();
buffer.append("Name: " + name + "\n");
- buffer.append("Constructors: " + constructors + "\n");
- buffer.append("Methods: " + methods + "\n");
- buffer.append("Fields: " + fields + "\n");
+ buffer.append("Constructors: " + getMergedConstructors() +
"\n");
+ buffer.append("Methods: " + getMergedMethods() + "\n");
+ buffer.append("Fields: " + getMergedFields() + "\n");
return buffer.toString();
}
Modified: ri/trunk/impl/src/main/java/org/jboss/webbeans/xsd/model/FieldModel.java
===================================================================
--- ri/trunk/impl/src/main/java/org/jboss/webbeans/xsd/model/FieldModel.java 2009-03-13
12:19:49 UTC (rev 1967)
+++ ri/trunk/impl/src/main/java/org/jboss/webbeans/xsd/model/FieldModel.java 2009-03-13
13:45:34 UTC (rev 1968)
@@ -28,4 +28,10 @@
return name.hashCode() + type.hashCode();
}
+ @Override
+ public String toString()
+ {
+ return type + " " + name;
+ }
+
}
Modified: 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 2009-03-13
12:19:49 UTC (rev 1967)
+++ ri/trunk/impl/src/main/java/org/jboss/webbeans/xsd/model/MethodModel.java 2009-03-13
13:45:34 UTC (rev 1968)
@@ -41,5 +41,11 @@
{
return name.hashCode() + returnType.hashCode() + parameters.hashCode();
}
+
+ @Override
+ public String toString()
+ {
+ return returnType + " " + name + "(" + (parameters.isEmpty() ?
"" : parameters) + ")";
+ }
}