Author: nickarls
Date: 2009-03-23 18:01:57 -0400 (Mon, 23 Mar 2009)
New Revision: 2159
Added:
extensions/trunk/xsd/src/main/java/org/jboss/webbeans/xsd/test/test/test/Foo.java
Removed:
extensions/trunk/xsd/src/main/java/org/jboss/webbeans/xsd/helpers/
extensions/trunk/xsd/src/main/java/org/jboss/webbeans/xsd/test/test/test/Tar.java
Modified:
extensions/trunk/xsd/src/main/java/org/jboss/webbeans/xsd/NamespaceHandler.java
extensions/trunk/xsd/src/main/java/org/jboss/webbeans/xsd/PackageSchemaGenerator.java
extensions/trunk/xsd/src/main/java/org/jboss/webbeans/xsd/model/FieldModel.java
extensions/trunk/xsd/src/main/java/org/jboss/webbeans/xsd/test/Bar.java
extensions/trunk/xsd/src/main/java/org/jboss/webbeans/xsd/test/Foo.java
Log:
refactor
Modified: extensions/trunk/xsd/src/main/java/org/jboss/webbeans/xsd/NamespaceHandler.java
===================================================================
---
extensions/trunk/xsd/src/main/java/org/jboss/webbeans/xsd/NamespaceHandler.java 2009-03-23
20:09:09 UTC (rev 2158)
+++
extensions/trunk/xsd/src/main/java/org/jboss/webbeans/xsd/NamespaceHandler.java 2009-03-23
22:01:57 UTC (rev 2159)
@@ -34,22 +34,41 @@
*/
public class NamespaceHandler
{
+ // The packages that belong to urn:java:ee
private 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 package of the schema
private String localPackage;
+ // The namespaces currently in use mapped by package name
private Map<String, Namespace> namespaces = new HashMap<String,
Namespace>();
- public Collection<Namespace> getNamespaces()
- {
- return namespaces.values();
- }
-
+ /**
+ * Creates a new namespace handler
+ *
+ * @param localPackage The local package
+ */
public NamespaceHandler(String localPackage)
{
this.localPackage = localPackage;
addNamespace(new Namespace("", localPackage));
}
+
+ /**
+ * Gets the used namespaces
+ *
+ * @return The namespaces
+ */
+ public Collection<Namespace> getNamespaces()
+ {
+ return namespaces.values();
+ }
+ /**
+ * Gets the prefix for a package
+ *
+ * @param packageName The package name
+ * @return The prefix
+ */
public String getPrefix(String packageName)
{
String shortName = getPackageName(packageName);
@@ -63,35 +82,56 @@
}
}
+ /**
+ * Gets the last part of a package name
+ *
+ * @param packageName The package name
+ * @return The part after the last dot
+ */
private String getPackageLastPart(String packageName)
{
int lastDot = packageName.lastIndexOf(".");
return lastDot < 0 ? packageName : packageName.substring(lastDot + 1);
}
+ /**
+ * Gets the package name of a FQCN
+ *
+ * @param FQCN The FQCN
+ * @return The package part
+ */
private String getPackageName(String FQCN)
{
int lastDot = FQCN.lastIndexOf(".");
return lastDot < 0 ? "nopak" : FQCN.substring(0, lastDot);
}
+ /**
+ * Adds a package to the namespace handler
+ *
+ * @param packageName The package
+ */
public void addPackage(String packageName)
{
+ // Already handled, exit early
if (namespaces.containsKey(packageName))
{
return;
}
String prefix = null;
String URI = "java:urn:" + packageName;
+ // The local package, prefix is blank and uri is package name
if (localPackage.equals(packageName))
{
prefix = "";
}
+ // EE stuff, prefix is ee and urn is ee
else if (URN_JAVA_EE.contains(packageName))
{
prefix = "ee";
URI = "java:urn:ee";
}
+ // Another package, get available prefix
else
{
prefix = getAvailablePrefix(packageName);
@@ -99,10 +139,20 @@
namespaces.put(packageName, new Namespace(prefix, URI));
}
+ /**
+ * Gets an available namespace prefix for a package name
+ *
+ * @param packageName The package name to search prefix for
+ * @return An available prefix
+ */
private String getAvailablePrefix(String packageName)
{
int suffix = 1;
boolean found = false;
+ /**
+ * If we search for a prefix for com.acme.foo, we iterate over the namespaces and
see
+ * if "foo" is present, if so, we try with foo2 etc until it found to be
available
+ */
while (true)
{
String prefix = getPackageLastPart(packageName) + (suffix == 1 ? "" :
String.valueOf(suffix));
@@ -126,11 +176,22 @@
}
}
+ /**
+ * Adds a namespace to the map of known namespaces
+ *
+ * @param namespace The namespace to add
+ */
public void addNamespace(Namespace namespace)
{
namespaces.put(getNamespacePackage(namespace), namespace);
}
+ /**
+ * Gets the package from a namespace
+ *
+ * @param namespace The namespace to examine
+ * @return The package name
+ */
private String getNamespacePackage(Namespace namespace)
{
int urnJava = namespace.getURI().indexOf("urn:java:");
@@ -150,4 +211,12 @@
return namespaces.toString();
}
+ public static void main(String[] x) {
+ NamespaceHandler nh = new NamespaceHandler("foo");
+ nh.addPackage("org.jboss.webbeans.xsd.test.test.test");
+ nh.addPackage("org.jboss.webbeans.xsd.test.test.test");
+ nh.addPackage("org.jboss.webbeans.xsd.test.test");
+ nh.addPackage("org.jboss.webbeans.xsd.test.test");
+ }
+
}
Modified:
extensions/trunk/xsd/src/main/java/org/jboss/webbeans/xsd/PackageSchemaGenerator.java
===================================================================
---
extensions/trunk/xsd/src/main/java/org/jboss/webbeans/xsd/PackageSchemaGenerator.java 2009-03-23
20:09:09 UTC (rev 2158)
+++
extensions/trunk/xsd/src/main/java/org/jboss/webbeans/xsd/PackageSchemaGenerator.java 2009-03-23
22:01:57 UTC (rev 2159)
@@ -30,6 +30,8 @@
import javax.lang.model.SourceVersion;
import javax.lang.model.element.Element;
import javax.lang.model.element.ElementKind;
+import javax.lang.model.element.ExecutableElement;
+import javax.lang.model.element.Modifier;
import javax.lang.model.element.TypeElement;
import javax.lang.model.type.DeclaredType;
import javax.lang.model.type.TypeKind;
@@ -38,8 +40,10 @@
import org.dom4j.DocumentException;
import org.jboss.webbeans.log.LogProvider;
import org.jboss.webbeans.log.Logging;
-import org.jboss.webbeans.xsd.helpers.DataSetter;
import org.jboss.webbeans.xsd.model.ClassModel;
+import org.jboss.webbeans.xsd.model.ConstructorModel;
+import org.jboss.webbeans.xsd.model.FieldModel;
+import org.jboss.webbeans.xsd.model.MethodModel;
/**
* An annotation processor that updates the package-level XSD for the packages
@@ -53,7 +57,7 @@
public class PackageSchemaGenerator extends AbstractProcessor
{
private static LogProvider log =
Logging.getLogProvider(PackageSchemaGenerator.class);
-
+
private Map<String, ClassModel> classModelCache;
private Map<String, Schema> schemas;
@@ -97,7 +101,7 @@
}
}
}
- return false;
+ return true;
}
private void addClassToSchema(ClassModel classModel) throws DocumentException
@@ -112,6 +116,11 @@
schema.addClass(classModel);
}
+ private boolean isPublic(Element element)
+ {
+ return element.getModifiers().contains(Modifier.PUBLIC);
+ }
+
/**
* Creates a class model from a class element
*
@@ -135,17 +144,29 @@
// Filter out the fields and populate the model
for (Element field : ElementFilter.fieldsIn(element.getEnclosedElements()))
{
- DataSetter.populateFieldModel(classModel, field);
+ if (!isPublic(field))
+ {
+ continue;
+ }
+ classModel.addField(FieldModel.of(field));
}
// Filter out the methods and populate the model
- for (Element method : ElementFilter.methodsIn(element.getEnclosedElements()))
+ for (ExecutableElement method :
ElementFilter.methodsIn(element.getEnclosedElements()))
{
- DataSetter.populateMethodModel(classModel, method);
+ if (!isPublic(method))
+ {
+ continue;
+ }
+ classModel.addMethod(MethodModel.of(method));
}
// Filter out the constructors and populate the model
- for (Element constructor :
ElementFilter.constructorsIn(element.getEnclosedElements()))
+ for (ExecutableElement constructor :
ElementFilter.constructorsIn(element.getEnclosedElements()))
{
- DataSetter.populateConstructorModel(classModel, constructor);
+ if (!isPublic(constructor))
+ {
+ continue;
+ }
+ classModel.addConstructor(ConstructorModel.of(constructor));
}
// Place the new class model in the cache
classModelCache.put(classModel.getName(), classModel);
Modified: extensions/trunk/xsd/src/main/java/org/jboss/webbeans/xsd/model/FieldModel.java
===================================================================
---
extensions/trunk/xsd/src/main/java/org/jboss/webbeans/xsd/model/FieldModel.java 2009-03-23
20:09:09 UTC (rev 2158)
+++
extensions/trunk/xsd/src/main/java/org/jboss/webbeans/xsd/model/FieldModel.java 2009-03-23
22:01:57 UTC (rev 2159)
@@ -17,7 +17,8 @@
package org.jboss.webbeans.xsd.model;
-import org.dom4j.Element;
+import javax.lang.model.element.Element;
+
import org.jboss.webbeans.xsd.NamespaceHandler;
/**
@@ -34,15 +35,15 @@
super(name);
}
- public static FieldModel of(String name)
+ public static FieldModel of(Element element)
{
- return new FieldModel(name);
+ return new FieldModel(element.getSimpleName().toString());
}
@Override
- public Element toXSD(NamespaceHandler namespaceHandler)
+ public org.dom4j.Element toXSD(NamespaceHandler namespaceHandler)
{
- Element field = super.toXSD(namespaceHandler);
+ org.dom4j.Element field = super.toXSD(namespaceHandler);
field.addAttribute("type", "wb:field");
return field;
}
Modified: extensions/trunk/xsd/src/main/java/org/jboss/webbeans/xsd/test/Bar.java
===================================================================
--- extensions/trunk/xsd/src/main/java/org/jboss/webbeans/xsd/test/Bar.java 2009-03-23
20:09:09 UTC (rev 2158)
+++ extensions/trunk/xsd/src/main/java/org/jboss/webbeans/xsd/test/Bar.java 2009-03-23
22:01:57 UTC (rev 2159)
@@ -2,9 +2,8 @@
public class Bar
{
- public int poo;
- public void testy() {
-
+ public org.jboss.webbeans.xsd.test.test.Foo
pong(org.jboss.webbeans.xsd.test.test.test.Foo foo)
+ {
+ return null;
}
-
}
Modified: extensions/trunk/xsd/src/main/java/org/jboss/webbeans/xsd/test/Foo.java
===================================================================
--- extensions/trunk/xsd/src/main/java/org/jboss/webbeans/xsd/test/Foo.java 2009-03-23
20:09:09 UTC (rev 2158)
+++ extensions/trunk/xsd/src/main/java/org/jboss/webbeans/xsd/test/Foo.java 2009-03-23
22:01:57 UTC (rev 2159)
@@ -1,33 +1,15 @@
package org.jboss.webbeans.xsd.test;
-import javax.inject.Current;
-import javax.inject.Initializer;
-import org.jboss.webbeans.xsd.test.test.test.Tar;
-
-@Current
public class Foo extends Bar
{
- @Current
public String foo;
public int poo;
public Bar bar;
public org.jboss.webbeans.xsd.test.test.Foo foo2;
- public org.jboss.webbeans.xsd.test.test.test.Tar tar2;
- public Tar tar;
+ public org.jboss.webbeans.xsd.test.test.test.Foo foo3;
- @Initializer
- public Foo(String foo)
- {
- }
-
- @Current
- public String foo(@Current String foo)
- {
- return foo;
- }
-
- public void testy(org.jboss.webbeans.xsd.test.test.Foo a,
org.jboss.webbeans.xsd.test.test.test.Tar b) {
+ public void testy(org.jboss.webbeans.xsd.test.test.Foo a,
org.jboss.webbeans.xsd.test.test.test.Foo b) {
}
Added: extensions/trunk/xsd/src/main/java/org/jboss/webbeans/xsd/test/test/test/Foo.java
===================================================================
--- extensions/trunk/xsd/src/main/java/org/jboss/webbeans/xsd/test/test/test/Foo.java
(rev 0)
+++
extensions/trunk/xsd/src/main/java/org/jboss/webbeans/xsd/test/test/test/Foo.java 2009-03-23
22:01:57 UTC (rev 2159)
@@ -0,0 +1,6 @@
+package org.jboss.webbeans.xsd.test.test.test;
+
+public class Foo
+{
+
+}
Deleted:
extensions/trunk/xsd/src/main/java/org/jboss/webbeans/xsd/test/test/test/Tar.java
===================================================================
---
extensions/trunk/xsd/src/main/java/org/jboss/webbeans/xsd/test/test/test/Tar.java 2009-03-23
20:09:09 UTC (rev 2158)
+++
extensions/trunk/xsd/src/main/java/org/jboss/webbeans/xsd/test/test/test/Tar.java 2009-03-23
22:01:57 UTC (rev 2159)
@@ -1,6 +0,0 @@
-package org.jboss.webbeans.xsd.test.test.test;
-
-public class Tar
-{
-
-}