Author: pete.muir(a)jboss.org
Date: 2009-03-15 13:25:15 -0400 (Sun, 15 Mar 2009)
New Revision: 2004
Added:
ri/trunk/xsd/
ri/trunk/xsd/pom.xml
ri/trunk/xsd/src/
ri/trunk/xsd/src/main/
ri/trunk/xsd/src/main/java/
ri/trunk/xsd/src/main/java/org/
ri/trunk/xsd/src/main/java/org/jboss/
ri/trunk/xsd/src/main/java/org/jboss/webbeans/
ri/trunk/xsd/src/main/java/org/jboss/webbeans/xsd/
ri/trunk/xsd/src/main/java/org/jboss/webbeans/xsd/PackageInfo.java
ri/trunk/xsd/src/main/java/org/jboss/webbeans/xsd/PackageSchemaGenerator.java
ri/trunk/xsd/src/main/java/org/jboss/webbeans/xsd/helpers/
ri/trunk/xsd/src/main/java/org/jboss/webbeans/xsd/helpers/DataSetter.java
ri/trunk/xsd/src/main/java/org/jboss/webbeans/xsd/helpers/XSDHelper.java
ri/trunk/xsd/src/main/java/org/jboss/webbeans/xsd/model/
ri/trunk/xsd/src/main/java/org/jboss/webbeans/xsd/model/ClassModel.java
ri/trunk/xsd/src/main/java/org/jboss/webbeans/xsd/model/FieldModel.java
ri/trunk/xsd/src/main/java/org/jboss/webbeans/xsd/model/MethodModel.java
ri/trunk/xsd/src/main/java/org/jboss/webbeans/xsd/model/NamedModel.java
ri/trunk/xsd/src/main/java/org/jboss/webbeans/xsd/model/ParameterModel.java
ri/trunk/xsd/src/main/java/org/jboss/webbeans/xsd/sample-schema.xsd
ri/trunk/xsd/src/main/resources/
ri/trunk/xsd/src/test/
ri/trunk/xsd/src/test/java/
ri/trunk/xsd/src/test/resources/
Modified:
ri/trunk/pom.xml
Log:
split out xsd
Modified: ri/trunk/pom.xml
===================================================================
--- ri/trunk/pom.xml 2009-03-15 15:53:42 UTC (rev 2003)
+++ ri/trunk/pom.xml 2009-03-15 17:25:15 UTC (rev 2004)
@@ -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>
Added: ri/trunk/xsd/pom.xml
===================================================================
--- ri/trunk/xsd/pom.xml (rev 0)
+++ ri/trunk/xsd/pom.xml 2009-03-15 17:25:15 UTC (rev 2004)
@@ -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>
Added: ri/trunk/xsd/src/main/java/org/jboss/webbeans/xsd/PackageInfo.java
===================================================================
--- ri/trunk/xsd/src/main/java/org/jboss/webbeans/xsd/PackageInfo.java
(rev 0)
+++ ri/trunk/xsd/src/main/java/org/jboss/webbeans/xsd/PackageInfo.java 2009-03-15 17:25:15
UTC (rev 2004)
@@ -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;
+ }
+
+}
Added: ri/trunk/xsd/src/main/java/org/jboss/webbeans/xsd/PackageSchemaGenerator.java
===================================================================
--- ri/trunk/xsd/src/main/java/org/jboss/webbeans/xsd/PackageSchemaGenerator.java
(rev 0)
+++
ri/trunk/xsd/src/main/java/org/jboss/webbeans/xsd/PackageSchemaGenerator.java 2009-03-15
17:25:15 UTC (rev 2004)
@@ -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
+ *
+ */
+(a)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;
+ }
+
+}
Added: ri/trunk/xsd/src/main/java/org/jboss/webbeans/xsd/helpers/DataSetter.java
===================================================================
--- ri/trunk/xsd/src/main/java/org/jboss/webbeans/xsd/helpers/DataSetter.java
(rev 0)
+++ ri/trunk/xsd/src/main/java/org/jboss/webbeans/xsd/helpers/DataSetter.java 2009-03-15
17:25:15 UTC (rev 2004)
@@ -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);
+ }
+ }
+
+}
Added: ri/trunk/xsd/src/main/java/org/jboss/webbeans/xsd/helpers/XSDHelper.java
===================================================================
--- ri/trunk/xsd/src/main/java/org/jboss/webbeans/xsd/helpers/XSDHelper.java
(rev 0)
+++ ri/trunk/xsd/src/main/java/org/jboss/webbeans/xsd/helpers/XSDHelper.java 2009-03-15
17:25:15 UTC (rev 2004)
@@ -0,0 +1,289 @@
+/*
+ * 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 java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+
+import javax.annotation.processing.Filer;
+import javax.tools.StandardLocation;
+
+import org.dom4j.Document;
+import org.dom4j.DocumentException;
+import org.dom4j.DocumentHelper;
+import org.dom4j.Node;
+import org.dom4j.io.OutputFormat;
+import org.dom4j.io.SAXReader;
+import org.dom4j.io.XMLWriter;
+import org.jboss.webbeans.xsd.PackageInfo;
+import org.jboss.webbeans.xsd.model.ClassModel;
+
+/**
+ * Helper for XSD related operations
+ *
+ * @author Nicklas Karlsson
+ *
+ */
+public class XSDHelper
+{
+ public static final Set<String> URN_JAVA_EE = new
HashSet<String>(Arrays.asList("java.lang", "java.util",
"javax.annotation", "javax.inject", "javax.context",
"javax.interceptor", "javax.decorator", "javax.event",
"javax.ejb", "javax.persistence", "javax.xml.ws",
"javax.jms", "javax.sql"));
+
+ // The filed of the annotation processing environment
+ private Filer filer;
+ // The cache of already processed classes
+ private Map<String, ClassModel> classModelCache = new HashMap<String,
ClassModel>();
+ // The XSD documents of the affected packages
+ private Map<String, PackageInfo> packageInfoMap = new HashMap<String,
PackageInfo>();
+
+ /**
+ * Creates a new helper
+ *
+ * @param filer The filer of the processing environment
+ */
+ public XSDHelper(Filer filer)
+ {
+ this.filer = filer;
+ }
+
+ /**
+ * Reads package info
+ *
+ * @param packageName The package name
+ * @return The package info of the package
+ * @throws DocumentException If the schema could not be parsed
+ * @throws IOException If the schema could not be read
+ */
+ private PackageInfo readPackageInfo(String packageName) throws DocumentException,
IOException
+ {
+ PackageInfo packageInfo = new PackageInfo(packageName);
+ packageInfo.setNamespaces(readNamespaces(packageName));
+ Document schema = readSchema(packageName);
+ packageInfo.setSchema(schema != null ? schema : createSchema(packageName));
+ return packageInfo;
+ }
+
+ /**
+ * Reads the namespaces for a package
+ *
+ * @param packageName The name of the package
+ * @return The namespaces
+ */
+ private List<String> readNamespaces(String packageName)
+ {
+ // TODO dummy
+ return new ArrayList<String>();
+ }
+
+ /**
+ * Creates a new schema document
+ *
+ * @param packageName The package name of the schema
+ * @return The document
+ */
+ private Document createSchema(String packageName)
+ {
+ Document packageXSD = DocumentHelper.createDocument();
+ packageXSD.addElement("Package");
+ return packageXSD;
+ }
+
+ /**
+ * Reads a schema for a package
+ *
+ * @param packageName The package name
+ * @return The schema document
+ * @throws DocumentException If the document could not be parsed
+ * @throws IOException If the document could not be read
+ */
+ private Document readSchema(String packageName) throws DocumentException, IOException
+ {
+ InputStream in = null;
+ try
+ {
+ in = filer.getResource(StandardLocation.CLASS_OUTPUT, packageName,
"schema.xsd").openInputStream();
+ return new SAXReader().read(in);
+ }
+ catch (IOException e)
+ {
+ return null;
+ }
+ finally
+ {
+ if (in != null)
+ {
+ in.close();
+ }
+ }
+ }
+
+ /**
+ * Writes package info to the disk
+ *
+ * @param packageInfo The package info to store
+ */
+ private void writePackageInfo(PackageInfo packageInfo)
+ {
+ try
+ {
+ writeSchema(packageInfo.getPackageName(), packageInfo.getSchema());
+ }
+ catch (IOException e)
+ {
+ throw new RuntimeException("Could not write schema for " +
packageInfo.getPackageName());
+ }
+ writeNamespaces(packageInfo.getPackageName(), packageInfo.getNamespaces());
+ }
+
+ /**
+ * Writes the namespaces to disk
+ *
+ * @param packageName The package name
+ * @param namespaces The namespaces
+ */
+ private void writeNamespaces(String packageName, List<String> namespaces)
+ {
+ // TODO dummy
+ }
+
+ /**
+ * Writes a schema to disk
+ *
+ * @param packageName The package name
+ * @param schema The schema
+ * @throws IOException If the file could not be written
+ */
+ private void writeSchema(String packageName, Document schema) throws IOException
+ {
+ OutputStream out = null;
+ try
+ {
+ OutputFormat format = OutputFormat.createPrettyPrint();
+ out = filer.createResource(StandardLocation.CLASS_OUTPUT, packageName,
"schema.xsd").openOutputStream();
+ XMLWriter writer = new XMLWriter(out, format);
+ writer.write(schema);
+ writer.flush();
+ writer.close();
+ }
+ finally
+ {
+ if (out != null)
+ {
+ out.close();
+ }
+ }
+ }
+
+ /**
+ * Updates the schemas for the affected packages
+ *
+ * @param classModels The list of class models in the batch
+ */
+ public void updateSchemas(List<ClassModel> classModels)
+ {
+ for (ClassModel classModel : classModels)
+ {
+ String packageName = classModel.getPackage();
+ PackageInfo packageInfo = packageInfoMap.get(packageName);
+ if (packageInfo == null) {
+ try
+ {
+ packageInfo = readPackageInfo(packageName);
+ }
+ catch (DocumentException e)
+ {
+ throw new RuntimeException("Could not parse schema for package "
+ packageName);
+ }
+ catch (IOException e)
+ {
+ throw new RuntimeException("Could not read schema for package "
+ packageName);
+ }
+ packageInfoMap.put(packageName, packageInfo);
+ }
+ updateClassInSchema(classModel, packageInfo.getSchema());
+ }
+ }
+
+ /**
+ * Writes the schemas back to disk
+ */
+ public void writeSchemas()
+ {
+ for (PackageInfo packageInfo : packageInfoMap.values()) {
+ writePackageInfo(packageInfo);
+ }
+ }
+
+ /**
+ * Updates a schema with XSD from a file model
+ *
+ * @param schema The schema
+ * @param classModel The class model
+ */
+ private void updateClassInSchema(ClassModel classModel, Document schema)
+ {
+ Node oldClassModel = schema.selectSingleNode("//" +
classModel.getSimpleName());
+ if (oldClassModel != null)
+ {
+ // Remove the old class definition
+ schema.getRootElement().remove(oldClassModel);
+ }
+ // Create a new one
+ schema.getRootElement().addElement(classModel.getSimpleName());
+ }
+
+ /**
+ * Gets the short name of a package (the last part)
+ *
+ * @param packageName The package name
+ * @return A short name
+ */
+ private String getShortName(String packageName)
+ {
+ int lastDot = packageName.lastIndexOf(".");
+ return lastDot < 0 ? packageName : packageName.substring(lastDot + 1);
+ }
+
+ /**
+ * Gets a cached class model
+ *
+ * @param FQN The FQN of the class
+ * @return The class model (or null if not cached)
+ */
+ public ClassModel getCachedClassModel(String FQN)
+ {
+ return classModelCache.get(FQN);
+ }
+
+ /**
+ * Puts a class model in the cache
+ *
+ * @param classModel The class model
+ */
+ public void cacheClassModel(ClassModel classModel)
+ {
+ classModelCache.put(classModel.getName(), classModel);
+ }
+}
Added: ri/trunk/xsd/src/main/java/org/jboss/webbeans/xsd/model/ClassModel.java
===================================================================
--- ri/trunk/xsd/src/main/java/org/jboss/webbeans/xsd/model/ClassModel.java
(rev 0)
+++ ri/trunk/xsd/src/main/java/org/jboss/webbeans/xsd/model/ClassModel.java 2009-03-15
17:25:15 UTC (rev 2004)
@@ -0,0 +1,223 @@
+/*
+ * 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.model;
+
+import java.util.ArrayList;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Set;
+
+/**
+ * A model of a class
+ *
+ * @author Nicklas Karlsson
+ *
+ */
+public class ClassModel extends NamedModel
+{
+ // The parent (or null if top-level)
+ private ClassModel parent;
+
+ // The fields of the class
+ private List<FieldModel> fields = new ArrayList<FieldModel>();
+ // The methods of the class
+ private List<MethodModel> methods = new ArrayList<MethodModel>();
+ // The constructors of the class
+ private List<MethodModel> constructors = new ArrayList<MethodModel>();
+
+ /**
+ * Creates a new class model
+ */
+ public ClassModel()
+ {
+ }
+
+ /**
+ * Adds a field to the class model
+ *
+ * @param fieldModel The field to add
+ */
+ public void addField(FieldModel fieldModel)
+ {
+ fields.add(fieldModel);
+ }
+
+ /**
+ * Adds a constructor to the model
+ *
+ * @param constructorModel The constructor to add
+ */
+ public void addConstructor(MethodModel constructorModel)
+ {
+ constructors.add(constructorModel);
+ }
+
+ /**
+ * Adds a method to the model
+ *
+ * @param methodModel The method to add
+ */
+ public void addMethod(MethodModel methodModel)
+ {
+ methods.add(methodModel);
+ }
+
+ @Override
+ public String toString()
+ {
+ StringBuilder buffer = new StringBuilder();
+ buffer.append("Name: " + name + "\n");
+ buffer.append("Constructors: " + getMergedConstructors() +
"\n");
+ buffer.append("Methods: " + getMergedMethods() + "\n");
+ buffer.append("Fields: " + getMergedFields() + "\n");
+ return buffer.toString();
+ }
+
+ /**
+ * Gets the parent class model of the class
+ *
+ * @return The parent or null if none present
+ */
+ public ClassModel getParent()
+ {
+ return parent;
+ }
+
+ /**
+ * Sets the parent
+ *
+ * @param parent The new parent class model
+ */
+ public void setParent(ClassModel parent)
+ {
+ this.parent = parent;
+ }
+
+ /**
+ * Gets the package of the file
+ *
+ * @return The package
+ */
+ public String getPackage()
+ {
+ int lastDot = name.lastIndexOf(".");
+ return lastDot < 0 ? name : name.substring(0, lastDot);
+ }
+
+ /**
+ * Gets the simple name of the class
+ *
+ * @return The simple name
+ */
+ public String getSimpleName()
+ {
+ int lastDot = name.lastIndexOf(".");
+ return lastDot < 0 ? name : name.substring(lastDot + 1);
+ }
+
+ /**
+ * Gets the merged hierarchy of available constructors. Returns the
+ * constructors of this class since constructors aren't inherited
+ *
+ * @return The set of constructors available
+ */
+ public Set<MethodModel> getMergedConstructors()
+ {
+ return new HashSet<MethodModel>(constructors);
+ }
+
+ /**
+ * Gets the public field of the class
+ *
+ * @return The public fields
+ */
+ public List<FieldModel> getFields()
+ {
+ return fields;
+ }
+
+ /**
+ * Gets the merged hierarchy of available fields.
+ *
+ * @return The set of public fields available
+ */
+ public Set<FieldModel> getMergedFields()
+ {
+ Set<FieldModel> mergedFields = new HashSet<FieldModel>(fields);
+ ClassModel currentParent = parent;
+ while (currentParent != null)
+ {
+ mergedFields.addAll(currentParent.getFields());
+ currentParent = currentParent.getParent();
+ }
+ return mergedFields;
+ }
+
+ /**
+ * Gets the public methods of the class
+ *
+ * @return The public methods
+ */
+ public List<MethodModel> getMethods()
+ {
+ return methods;
+ }
+
+ /**
+ * Gets the merged hierarchy of available fields.
+ *
+ * @return The set of public fields available
+ */
+ public Set<MethodModel> getMergedMethods()
+ {
+ Set<MethodModel> mergedMethods = new HashSet<MethodModel>(methods);
+ ClassModel currentParent = parent;
+ while (currentParent != null)
+ {
+ mergedMethods.addAll(currentParent.getMethods());
+ currentParent = currentParent.getParent();
+ }
+ return mergedMethods;
+ }
+
+ public Set<String> getReferencedTypes()
+ {
+ Set<String> types = new HashSet<String>();
+ for (FieldModel field : getMergedFields())
+ {
+ types.add(field.getType());
+ }
+ for (MethodModel method : getMergedMethods())
+ {
+ types.add(method.getReturnType());
+ for (ParameterModel parameter : method.getParameters())
+ {
+ types.add(parameter.getType());
+ }
+ }
+ for (MethodModel constructor : getMergedConstructors())
+ {
+ for (ParameterModel parameter : constructor.getParameters())
+ {
+ types.add(parameter.getType());
+ }
+ }
+ return types;
+ }
+
+}
Added: ri/trunk/xsd/src/main/java/org/jboss/webbeans/xsd/model/FieldModel.java
===================================================================
--- ri/trunk/xsd/src/main/java/org/jboss/webbeans/xsd/model/FieldModel.java
(rev 0)
+++ ri/trunk/xsd/src/main/java/org/jboss/webbeans/xsd/model/FieldModel.java 2009-03-15
17:25:15 UTC (rev 2004)
@@ -0,0 +1,60 @@
+/*
+ * 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.model;
+
+/**
+ * A model of a field
+ *
+ * @author Nicklas Karlsosn
+ *
+ */
+public class FieldModel extends NamedModel
+{
+ protected String type;
+
+ public FieldModel(String name, String type)
+ {
+ super(name);
+ this.type = type;
+ }
+
+ public String getType()
+ {
+ return type;
+ }
+
+ @Override
+ public boolean equals(Object other)
+ {
+ FieldModel otherModel = (FieldModel) other;
+ return name.equals(otherModel.getName()) &&
type.equals(otherModel.getType());
+ }
+
+ @Override
+ public int hashCode()
+ {
+ return name.hashCode() + type.hashCode();
+ }
+
+ @Override
+ public String toString()
+ {
+ return type + " " + name;
+ }
+
+}
Added: ri/trunk/xsd/src/main/java/org/jboss/webbeans/xsd/model/MethodModel.java
===================================================================
--- ri/trunk/xsd/src/main/java/org/jboss/webbeans/xsd/model/MethodModel.java
(rev 0)
+++ ri/trunk/xsd/src/main/java/org/jboss/webbeans/xsd/model/MethodModel.java 2009-03-15
17:25:15 UTC (rev 2004)
@@ -0,0 +1,74 @@
+/*
+ * 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.model;
+
+import java.util.ArrayList;
+import java.util.List;
+
+/**
+ * The model of a method
+ *
+ * @author Nicklas Karlsson
+ *
+ */
+public class MethodModel extends NamedModel
+{
+ private String returnType;
+ private List<ParameterModel> parameters = new
ArrayList<ParameterModel>();
+
+ public MethodModel(String name, String returnType)
+ {
+ super(name);
+ this.returnType = returnType;
+ }
+
+ public void addParameter(ParameterModel parameter)
+ {
+ parameters.add(parameter);
+ }
+
+ public List<ParameterModel> getParameters()
+ {
+ return parameters;
+ }
+
+ public String getReturnType()
+ {
+ return returnType;
+ }
+
+ @Override
+ public boolean equals(Object other)
+ {
+ MethodModel otherModel = (MethodModel) other;
+ return name.equals(otherModel.getName()) &&
returnType.equals(otherModel.getReturnType()) &&
parameters.equals(otherModel.getParameters());
+ }
+
+ @Override
+ public int hashCode()
+ {
+ return name.hashCode() + returnType.hashCode() + parameters.hashCode();
+ }
+
+ @Override
+ public String toString()
+ {
+ return returnType + " " + name + "(" + (parameters.isEmpty() ?
"" : parameters) + ")";
+ }
+
+}
Added: ri/trunk/xsd/src/main/java/org/jboss/webbeans/xsd/model/NamedModel.java
===================================================================
--- ri/trunk/xsd/src/main/java/org/jboss/webbeans/xsd/model/NamedModel.java
(rev 0)
+++ ri/trunk/xsd/src/main/java/org/jboss/webbeans/xsd/model/NamedModel.java 2009-03-15
17:25:15 UTC (rev 2004)
@@ -0,0 +1,48 @@
+/*
+ * 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.model;
+
+/**
+ * A superclass for named models
+ *
+ * @author Nicklas Karlsson
+ *
+ */
+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/xsd/src/main/java/org/jboss/webbeans/xsd/model/ParameterModel.java
===================================================================
--- ri/trunk/xsd/src/main/java/org/jboss/webbeans/xsd/model/ParameterModel.java
(rev 0)
+++ ri/trunk/xsd/src/main/java/org/jboss/webbeans/xsd/model/ParameterModel.java 2009-03-15
17:25:15 UTC (rev 2004)
@@ -0,0 +1,47 @@
+/*
+ * 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.model;
+
+/**
+ * The model of a method or constrcutor parameter
+ *
+ * @author Nicklas Karlsson
+ *
+ */
+public class ParameterModel extends FieldModel
+{
+
+ public ParameterModel(String name, String type)
+ {
+ super(name, type);
+ }
+
+ @Override
+ public boolean equals(Object other)
+ {
+ ParameterModel otherModel = (ParameterModel) other;
+ return type.equals(otherModel.getType());
+ }
+
+ @Override
+ public int hashCode()
+ {
+ return type.hashCode();
+ }
+
+}
Added: ri/trunk/xsd/src/main/java/org/jboss/webbeans/xsd/sample-schema.xsd
===================================================================
--- ri/trunk/xsd/src/main/java/org/jboss/webbeans/xsd/sample-schema.xsd
(rev 0)
+++ ri/trunk/xsd/src/main/java/org/jboss/webbeans/xsd/sample-schema.xsd 2009-03-15
17:25:15 UTC (rev 2004)
@@ -0,0 +1,28 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<xs:schema
xmlns:xs="http://www.w3.org/2001/XMLSchema"
+ elementFormDefault="qualified"
+ targetNamespace="urn:java:com.acme.foo"
+ xmlns:foo="urn:java:com.acme.foo"
+ xmlns:ee="urn:java:ee"
+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+
xsi:schemaLocation="http://www.w3.org/2001/XMLSchema
http://www.w3.org/2001/XMLSchema.xsd"
+ >
+
+ <xs:element name="Foo">
+ <xs:complexType>
+ <xs:choice>
+ <!-- Options to allow people to declare this class a binding type or
stereotype - TODO only if an annotation -->
+ <xs:element ref="ee:BindingType"/>
+ <xs:element ref="ee:StereotypType"/>
+ <!-- Somehow import all binding types known etc. to give people that option
-->
+ <xs:any /> <!-- Can't guess all types people want to put here
-->
+ </xs:choice>
+ </xs:complexType>
+ </xs:element>
+
+ <xs:element name="Bar">
+
+ </xs:element>
+
+
+</xs:schema>
\ No newline at end of file