Author: nickarls
Date: 2009-03-13 17:45:50 -0400 (Fri, 13 Mar 2009)
New Revision: 1984
Modified:
ri/trunk/impl/src/main/java/org/jboss/webbeans/xsd/PackageSchemaGenerator.java
ri/trunk/impl/src/main/java/org/jboss/webbeans/xsd/helpers/DataSetter.java
ri/trunk/impl/src/main/java/org/jboss/webbeans/xsd/helpers/XSDHelper.java
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
ri/trunk/impl/src/main/java/org/jboss/webbeans/xsd/model/NamedModel.java
ri/trunk/impl/src/main/java/org/jboss/webbeans/xsd/model/ParameterModel.java
Log:
docs for xsd
Modified: ri/trunk/impl/src/main/java/org/jboss/webbeans/xsd/PackageSchemaGenerator.java
===================================================================
---
ri/trunk/impl/src/main/java/org/jboss/webbeans/xsd/PackageSchemaGenerator.java 2009-03-13
18:12:03 UTC (rev 1983)
+++
ri/trunk/impl/src/main/java/org/jboss/webbeans/xsd/PackageSchemaGenerator.java 2009-03-13
21:45:50 UTC (rev 1984)
@@ -1,3 +1,20 @@
+/*
+ * 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;
@@ -20,10 +37,18 @@
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
@@ -37,45 +62,62 @@
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 packge XSDs for the files changed
helper.updatePackageXSDs(workingSet);
+ // And flush the changes to disk
helper.flushPackageXSDs();
}
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);
}
- helper.setCachedClassModel(classModel.getName(), classModel);
+ // Place the new class model in the cache
+ helper.cacheClassModel(classModel);
return classModel;
}
-
}
Modified: ri/trunk/impl/src/main/java/org/jboss/webbeans/xsd/helpers/DataSetter.java
===================================================================
--- ri/trunk/impl/src/main/java/org/jboss/webbeans/xsd/helpers/DataSetter.java 2009-03-13
18:12:03 UTC (rev 1983)
+++ ri/trunk/impl/src/main/java/org/jboss/webbeans/xsd/helpers/DataSetter.java 2009-03-13
21:45:50 UTC (rev 1984)
@@ -1,3 +1,20 @@
+/*
+ * 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;
@@ -11,14 +28,33 @@
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;
@@ -26,6 +62,12 @@
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))
@@ -37,6 +79,12 @@
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))
@@ -56,6 +104,7 @@
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);
Modified: ri/trunk/impl/src/main/java/org/jboss/webbeans/xsd/helpers/XSDHelper.java
===================================================================
--- ri/trunk/impl/src/main/java/org/jboss/webbeans/xsd/helpers/XSDHelper.java 2009-03-13
18:12:03 UTC (rev 1983)
+++ ri/trunk/impl/src/main/java/org/jboss/webbeans/xsd/helpers/XSDHelper.java 2009-03-13
21:45:50 UTC (rev 1984)
@@ -1,3 +1,20 @@
+/*
+ * 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;
@@ -23,17 +40,36 @@
import org.jboss.webbeans.xsd.model.ClassModel;
import org.xml.sax.SAXException;
+/**
+ * Helper for XSD related operations
+ *
+ * @author Nicklas Karlsson
+ *
+ */
public class XSDHelper
{
+ // The annotation processing environment
private ProcessingEnvironment processingEnvironment;
+ // 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, Document> packageXSDs = new HashMap<String,
Document>();
+ /**
+ * Creates a new helper
+ *
+ * @param processingEnvironment The processing environment
+ */
public XSDHelper(ProcessingEnvironment processingEnvironment)
{
this.processingEnvironment = processingEnvironment;
}
+ /**
+ * Updates the XSD for the affected packages
+ *
+ * @param classModels The list of class models in the batch
+ */
public void updatePackageXSDs(List<ClassModel> classModels)
{
for (ClassModel classModel : classModels)
@@ -43,6 +79,9 @@
}
}
+ /**
+ * Writes the XSD documents back to disk
+ */
public void flushPackageXSDs()
{
for (Entry<String, Document> entry : packageXSDs.entrySet())
@@ -62,13 +101,22 @@
}
}
+ /**
+ * Gets the XSD document for a package
+ *
+ * @param packageName The package name of the XSD
+ * @return The document
+ */
private Document getPackageXSD(String packageName)
{
+ // Tries to get the document from the cache
Document packageXSD = packageXSDs.get(packageName);
if (packageXSD == null)
{
+ // If this is the first modification to a package
try
{
+ // Read it from disk
packageXSD = readPackageXSD(packageName);
}
catch (IOException e)
@@ -79,25 +127,45 @@
{
throw new RuntimeException("Could not parse schema for package " +
packageName);
}
+ // If it was not on disk
if (packageXSD == null)
{
+ // Create a new document
packageXSD = createPackageXSD(packageName);
}
+ // And cache it
packageXSDs.put(packageName, packageXSD);
}
return packageXSD;
}
+ /**
+ * Updates a package XSD with XSD from a file model
+ *
+ * @param packageXSD The package XSD
+ * @param classModel The class model
+ */
private void updateClass(Document packageXSD, ClassModel classModel)
{
Node oldClassModel = packageXSD.selectSingleNode("//" +
classModel.getSimpleName());
if (oldClassModel != null)
{
+ // Remove the old class definition
packageXSD.getRootElement().remove(oldClassModel);
}
+ // Create a new one
packageXSD.getRootElement().addElement(classModel.getSimpleName());
}
+ /**
+ * Read the package XSD for a package
+ *
+ * @param packageName The package name
+ *
+ * @return The document
+ * @throws IOException If a file could not be read
+ * @throws DocumentException If a document could not be parsed
+ */
private Document readPackageXSD(String packageName) throws IOException,
DocumentException
{
InputStream in = null;
@@ -119,6 +187,12 @@
}
}
+ /**
+ * Creates a new XSD document for a package
+ *
+ * @param packageName The name of the package
+ * @return The document
+ */
private Document createPackageXSD(String packageName)
{
Document packageXSD = DocumentHelper.createDocument();
@@ -127,12 +201,26 @@
return packageXSD;
}
+ /**
+ * 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);
}
+ /**
+ * Writes a package XSD back to disk
+ *
+ * @param packageName The name of the package
+ * @param packageXSD The document
+ * @throws IOException If the file could not be written
+ * @throws SAXException If the document was badly formatted
+ */
private void writePackageXSD(String packageName, Document packageXSD) throws
IOException, SAXException
{
OutputStream out = null;
@@ -154,13 +242,24 @@
}
}
- public ClassModel getCachedClassModel(String packageName)
+ /**
+ * 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(packageName);
+ return classModelCache.get(FQN);
}
- public void setCachedClassModel(String packageName, ClassModel classModel)
+ /**
+ * Puts a class model in the cache
+ *
+ * @param classModel The class model
+ */
+ public void cacheClassModel(ClassModel classModel)
{
- classModelCache.put(packageName, classModel);
+ classModelCache.put(classModel.getName(), classModel);
}
}
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
18:12:03 UTC (rev 1983)
+++ ri/trunk/impl/src/main/java/org/jboss/webbeans/xsd/model/ClassModel.java 2009-03-13
21:45:50 UTC (rev 1984)
@@ -1,3 +1,20 @@
+/*
+ * 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;
@@ -5,28 +22,56 @@
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);
@@ -43,38 +88,74 @@
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);
@@ -87,11 +168,21 @@
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);
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
18:12:03 UTC (rev 1983)
+++ ri/trunk/impl/src/main/java/org/jboss/webbeans/xsd/model/FieldModel.java 2009-03-13
21:45:50 UTC (rev 1984)
@@ -1,5 +1,28 @@
+/*
+ * 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;
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
18:12:03 UTC (rev 1983)
+++ ri/trunk/impl/src/main/java/org/jboss/webbeans/xsd/model/MethodModel.java 2009-03-13
21:45:50 UTC (rev 1984)
@@ -1,3 +1,20 @@
+/*
+ * 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;
@@ -3,4 +20,10 @@
import java.util.List;
+/**
+ * The model of a method
+ *
+ * @author Nicklas Karlsson
+ *
+ */
public class MethodModel extends NamedModel
{
Modified: 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 2009-03-13
18:12:03 UTC (rev 1983)
+++ ri/trunk/impl/src/main/java/org/jboss/webbeans/xsd/model/NamedModel.java 2009-03-13
21:45:50 UTC (rev 1984)
@@ -1,5 +1,28 @@
+/*
+ * 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;
Modified: ri/trunk/impl/src/main/java/org/jboss/webbeans/xsd/model/ParameterModel.java
===================================================================
---
ri/trunk/impl/src/main/java/org/jboss/webbeans/xsd/model/ParameterModel.java 2009-03-13
18:12:03 UTC (rev 1983)
+++
ri/trunk/impl/src/main/java/org/jboss/webbeans/xsd/model/ParameterModel.java 2009-03-13
21:45:50 UTC (rev 1984)
@@ -1,5 +1,28 @@
+/*
+ * 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
{