Author: nickarls
Date: 2009-03-23 03:09:01 -0400 (Mon, 23 Mar 2009)
New Revision: 2138
Added:
extensions/trunk/xsd/src/main/java/org/jboss/webbeans/xsd/model/FieldModel.java
Modified:
extensions/trunk/xsd/src/main/java/org/jboss/webbeans/xsd/NamespaceHandler.java
extensions/trunk/xsd/src/main/java/org/jboss/webbeans/xsd/Schema.java
extensions/trunk/xsd/src/main/java/org/jboss/webbeans/xsd/helpers/DataSetter.java
extensions/trunk/xsd/src/main/java/org/jboss/webbeans/xsd/helpers/XSDHelper.java
extensions/trunk/xsd/src/main/java/org/jboss/webbeans/xsd/model/ClassModel.java
extensions/trunk/xsd/src/main/java/org/jboss/webbeans/xsd/model/MethodModel.java
extensions/trunk/xsd/src/main/java/org/jboss/webbeans/xsd/model/TypedModel.java
Log:
Some refactorings
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
05:25:18 UTC (rev 2137)
+++
extensions/trunk/xsd/src/main/java/org/jboss/webbeans/xsd/NamespaceHandler.java 2009-03-23
07:09:01 UTC (rev 2138)
@@ -18,11 +18,14 @@
package org.jboss.webbeans.xsd;
import java.util.Arrays;
+import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
+import org.dom4j.Namespace;
+
/**
* Helper for generating and keeping track of namespaces in a schema
*
@@ -31,91 +34,28 @@
*/
public class NamespaceHandler
{
- // The set of reserved EE packages
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 local package of the scema
private String localPackage;
+ private Map<String, Namespace> namespaces = new HashMap<String,
Namespace>();
- // Duplicate shortname counters
- private Map<String, Integer> counters = new HashMap<String, Integer>();
- // Namespace infos
- private Map<String, SchemaNamespace> schemaNamespaces = new HashMap<String,
SchemaNamespace>();
-
- public Map<String, SchemaNamespace> getSchemaNamespaces()
+ public Collection<Namespace> getNamespaces()
{
- return schemaNamespaces;
+ return namespaces.values();
}
- /**
- * Creats a new namespace generator
- *
- * @param localPackage The local package
- */
public NamespaceHandler(String localPackage)
{
this.localPackage = localPackage;
- addPackage(localPackage);
+ addNamespace(new Namespace("", localPackage));
}
- /**
- * Data for a package namespace
- *
- * @author Nicklas Karlsson
- *
- */
- public class SchemaNamespace
+ public String getPrefix(String packageName)
{
- // The package name
- String packageName;
- // The full namespace
- String shortNamespace;
- // The urn
- String urn;
- // Is this a EE reserved package?
- boolean ee;
-
- public SchemaNamespace(String packageName, String shortNamespace, boolean ee)
- {
- this.packageName = packageName;
- this.shortNamespace = shortNamespace;
- this.ee = ee;
- this.urn = "urn:java:" + (ee ? "ee" : packageName);
- }
-
- @Override
- public String toString()
- {
- return shortNamespace + "=" + urn;
- }
- }
-
- /**
- * Gets all used namespaces for the schema
- *
- * @return The used namespaces
- */
- // public Set<String> getUsedNamespaces()
- // {
- // Set<String> usedNamespaces = new HashSet<String>();
- // for (SchemaNamespace schemaNamespace : schemaNamespaces.values())
- // {
- // usedNamespaces.add(schemaNamespace.getFullNamespace());
- // }
- // return usedNamespaces;
- // }
- /**
- * Gets a namespace abbreviation for a package
- *
- * @param packageName The name of the package
- * @return The namespace abbreviation
- */
- public String getShortNamespace(String packageName)
- {
String shortName = getPackageName(packageName);
- if (schemaNamespaces.containsKey(shortName))
+ if (namespaces.containsKey(shortName))
{
- return schemaNamespaces.get(shortName).shortNamespace;
+ return namespaces.get(shortName).getPrefix();
}
else
{
@@ -123,59 +63,91 @@
}
}
- /**
- * Gets the short name (last part) of a package
- *
- * @param packageName The package name to parse
- * @return The short name
- */
- private String getShortName(String packageName)
+ private String getPackageLastPart(String packageName)
{
int lastDot = packageName.lastIndexOf(".");
return lastDot < 0 ? packageName : packageName.substring(lastDot + 1);
}
- private String getPackageName(String FQN)
+ private String getPackageName(String FQCN)
{
- int lastDot = FQN.lastIndexOf(".");
- return lastDot < 0 ? "nopak" : FQN.substring(0, lastDot);
+ int lastDot = FQCN.lastIndexOf(".");
+ return lastDot < 0 ? "nopak" : FQCN.substring(0, lastDot);
}
public void addPackage(String packageName)
{
- if (schemaNamespaces.containsKey(packageName))
+ if (namespaces.containsKey(packageName))
{
return;
}
- String shortNamespace = "";
- boolean ee = false;
+ String prefix = null;
+ String URI = "java:urn:" + packageName;
if (localPackage.equals(packageName))
{
- // Nothing to do but want to hit this case first for performance
+ prefix = "";
}
else if (URN_JAVA_EE.contains(packageName))
{
- shortNamespace = "ee";
- ee = true;
+ prefix = "ee";
+ URI = "java:urn:ee";
}
else
{
- String shortName = getShortName(packageName);
- Integer count = counters.get(shortName);
- String countString = "";
- if (count == null)
+ prefix = getAvailablePrefix(packageName);
+ }
+ namespaces.put(packageName, new Namespace(prefix, URI));
+ }
+
+ private String getAvailablePrefix(String packageName)
+ {
+ int suffix = 1;
+ boolean found = false;
+ while (true)
+ {
+ String prefix = getPackageLastPart(packageName) + (suffix == 1 ? "" :
String.valueOf(suffix));
+ for (Namespace namespace : namespaces.values())
{
- count = new Integer(1);
- counters.put(shortName, count);
+ if (namespace.getPrefix().equals(prefix))
+ {
+ found = true;
+ break;
+ }
}
+ if (!found)
+ {
+ return prefix;
+ }
else
{
- count++;
- countString = String.valueOf(count);
+ suffix++;
+ found = false;
}
- shortNamespace = getShortName(packageName) + countString;
}
- schemaNamespaces.put(packageName, new SchemaNamespace(packageName, shortNamespace,
ee));
}
+ public void addNamespace(Namespace namespace)
+ {
+ namespaces.put(getNamespacePackage(namespace), namespace);
+ }
+
+ private String getNamespacePackage(Namespace namespace)
+ {
+ int urnJava = namespace.getURI().indexOf("urn:java:");
+ if (urnJava >= 0)
+ {
+ return namespace.getURI().substring(urnJava);
+ }
+ else
+ {
+ return namespace.getURI();
+ }
+ }
+
+ @Override
+ public String toString()
+ {
+ return namespaces.toString();
+ }
+
}
Modified: extensions/trunk/xsd/src/main/java/org/jboss/webbeans/xsd/Schema.java
===================================================================
--- extensions/trunk/xsd/src/main/java/org/jboss/webbeans/xsd/Schema.java 2009-03-23
05:25:18 UTC (rev 2137)
+++ extensions/trunk/xsd/src/main/java/org/jboss/webbeans/xsd/Schema.java 2009-03-23
07:09:01 UTC (rev 2138)
@@ -29,7 +29,6 @@
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.Namespace;
-import org.jboss.webbeans.xsd.NamespaceHandler.SchemaNamespace;
import org.jboss.webbeans.xsd.model.ClassModel;
import org.jboss.webbeans.xsd.model.TypedModel;
@@ -41,6 +40,14 @@
*/
public class Schema
{
+ public static final List<Namespace> defaultNamespaces = new
ArrayList<Namespace>();
+
+ static
+ {
+ defaultNamespaces.add(new Namespace("wb",
"http://seamframework.org/WebBeans"));
+ defaultNamespaces.add(new Namespace("xsi",
"http://www.w3.org/2001/XMLSchema-instance"));
+ };
+
// The name of the package
private String packageName;
// The XSD document
@@ -50,7 +57,7 @@
// The set of classes to update
private Set<ClassModel> classModels;
private PackageElement packageElement;
-
+
/**
* Creates a new package
*
@@ -58,10 +65,10 @@
*/
public Schema(String packageName, PackageElement packageElement)
{
- this.packageName = packageName;
- namespaceHandler = new NamespaceHandler(packageName);
classModels = new HashSet<ClassModel>();
+ this.packageName = packageName;
this.packageElement = packageElement;
+ namespaceHandler = new NamespaceHandler(packageName);
}
/**
@@ -75,7 +82,8 @@
}
/**
- * Adds a class model to the working set and adds the referenced types to the
namespace handler
+ * Adds a class model to the working set and adds the referenced types to the
+ * namespace handler
*
* @param classModel The class model
*/
@@ -106,34 +114,16 @@
public void setDocument(Document document)
{
this.document = document;
+ init();
}
/**
- * Checks if a class is still in the package
- *
- * @param packageElement The package abstraction
- * @param FQN The full name of the class
- * @return True if present, false otherwise
+ * Cleans out namespaces and XSD for files that are no longer present in the
+ * package
*/
- private boolean isClassInPackage(String FQN)
+ private void init()
{
- for (javax.lang.model.element.Element classElement :
packageElement.getEnclosedElements())
- {
- TypeElement typeElement = (TypeElement) classElement;
- if (typeElement.getQualifiedName().toString().equals(FQN))
- {
- return true;
- }
- }
- return false;
- }
-
- /**
- * Cleans out XSD for files that are no longer present in the package
- * @param packageElement
- */
- private void cleanRemovedClasses()
- {
+ // Removes elements that are no longer in the package
for (Object xsdClass : document.selectNodes("/xs:schema/xs:element"))
{
String FQN = packageName + "." + ((Element)
xsdClass).attributeValue("name");
@@ -142,51 +132,48 @@
((Element) xsdClass).detach();
}
}
- }
- /**
- * Updates the namespaces of the XSD document
- */
- private void updateNamespaces()
- {
- // Collects the prefixes of the namspaces referenced in the classes
- List<String> referencedNamespaces = new ArrayList<String>();
- for (Object attribute : document.getRootElement().selectNodes("//@ref"))
+ Set<Namespace> referencedNamespaces = new
HashSet<Namespace>(defaultNamespaces);
+ for (Object attribute :
document.getRootElement().selectNodes("//@type"))
{
String ref = ((Attribute) attribute).getValue();
int colon = ref.indexOf(":");
- String namespace = colon < 0 ? "" : ref.substring(0, colon);
- referencedNamespaces.add(namespace);
+ String prefix = colon < 0 ? "" : ref.substring(0, colon);
+
referencedNamespaces.add(document.getRootElement().getNamespaceForPrefix(prefix));
}
-
- // Collects the prefixes of the namespaces in the schema document
- List<String> currentNamespaces = new ArrayList<String>();
+
for (Object item : document.getRootElement().additionalNamespaces())
{
Namespace namespace = (Namespace) item;
- String prefix = namespace.getPrefix();
- if (!("".equals(prefix) || "xsi".equals(prefix)))
+ if (referencedNamespaces.contains(namespace))
{
- currentNamespaces.add(namespace.getPrefix());
+ namespaceHandler.addNamespace(namespace);
}
+ else
+ {
+ document.getRootElement().remove(namespace);
+ }
}
-
- // Removes the namespaces that are no longer referenced
- currentNamespaces.removeAll(referencedNamespaces);
- for (String obsolete : currentNamespaces)
+ }
+
+ /**
+ * Checks if a class is still in the package
+ *
+ * @param packageElement The package abstraction
+ * @param FQN The full name of the class
+ * @return True if present, false otherwise
+ */
+ private boolean isClassInPackage(String FQN)
+ {
+ for (javax.lang.model.element.Element classElement :
packageElement.getEnclosedElements())
{
-
document.getRootElement().remove(document.getRootElement().getNamespaceForPrefix(obsolete));
- }
-
- // Adds new namespaces if they are not already present
- for (SchemaNamespace schemaNamespace :
namespaceHandler.getSchemaNamespaces().values())
- {
- if
(document.getRootElement().getNamespaceForPrefix(schemaNamespace.shortNamespace) == null)
+ TypeElement typeElement = (TypeElement) classElement;
+ if (typeElement.getQualifiedName().toString().equals(FQN))
{
- document.getRootElement().addNamespace(schemaNamespace.shortNamespace,
schemaNamespace.urn);
+ return true;
}
}
-
+ return false;
}
/**
@@ -194,10 +181,16 @@
*
* @param packageElement The package abstraction
*/
- public void rebuild()
+ public Schema rebuild()
{
- cleanRemovedClasses();
- updateNamespaces();
+ // Adds new namespaces if they are not already present
+ for (Namespace namespace : namespaceHandler.getNamespaces())
+ {
+ if (document.getRootElement().getNamespaceForPrefix(namespace.getPrefix()) ==
null)
+ {
+ document.getRootElement().add(namespace);
+ }
+ }
for (ClassModel classModel : classModels)
{
@@ -208,6 +201,7 @@
}
document.getRootElement().add(classModel.toXSD(namespaceHandler));
}
+ return this;
}
@Override
@@ -216,9 +210,9 @@
StringBuilder buffer = new StringBuilder();
buffer.append("Package: " + packageName + "\n");
buffer.append("Used namespaces\n");
- for (SchemaNamespace schemaNamespace :
namespaceHandler.getSchemaNamespaces().values())
+ for (Namespace namespace : namespaceHandler.getNamespaces())
{
- buffer.append(" " + schemaNamespace + "\n");
+ buffer.append(" " + namespace + "\n");
}
buffer.append("Contained classes:\n");
for (ClassModel classModel : classModels)
Modified:
extensions/trunk/xsd/src/main/java/org/jboss/webbeans/xsd/helpers/DataSetter.java
===================================================================
---
extensions/trunk/xsd/src/main/java/org/jboss/webbeans/xsd/helpers/DataSetter.java 2009-03-23
05:25:18 UTC (rev 2137)
+++
extensions/trunk/xsd/src/main/java/org/jboss/webbeans/xsd/helpers/DataSetter.java 2009-03-23
07:09:01 UTC (rev 2138)
@@ -23,8 +23,8 @@
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;
-import org.jboss.webbeans.xsd.model.NamedModel;
/**
* Helper for examining classes and members and populating the model
@@ -58,7 +58,7 @@
{
return;
}
- NamedModel field = NamedModel.of(element.getSimpleName().toString());
+ FieldModel field = FieldModel.of(element.getSimpleName().toString());
classModel.addField(field);
}
Modified:
extensions/trunk/xsd/src/main/java/org/jboss/webbeans/xsd/helpers/XSDHelper.java
===================================================================
---
extensions/trunk/xsd/src/main/java/org/jboss/webbeans/xsd/helpers/XSDHelper.java 2009-03-23
05:25:18 UTC (rev 2137)
+++
extensions/trunk/xsd/src/main/java/org/jboss/webbeans/xsd/helpers/XSDHelper.java 2009-03-23
07:09:01 UTC (rev 2138)
@@ -33,6 +33,7 @@
import org.dom4j.DocumentFactory;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
+import org.dom4j.Namespace;
import org.dom4j.QName;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
@@ -85,8 +86,10 @@
rootElement.addAttribute("elementFormDefault",
"qualified");
rootElement.addAttribute("targetNamespace", "urn:java:" +
packageName);
rootElement.addAttribute("elementFormDefault",
"qualified");
- rootElement.addNamespace("xsi",
"http://www.w3.org/2001/XMLSchema-instance");
- rootElement.addAttribute("xsi:schemaLocation",
"http://www.w3.org/2001/XMLSchema
http://www.w3.org/2001/XMLSchema.xsd");
+ for (Namespace namespace : Schema.defaultNamespaces)
+ {
+ rootElement.add(namespace);
+ }
document.setRootElement(rootElement);
}
schema.setDocument(document);
@@ -184,7 +187,6 @@
for (Schema schema : schemaMap.values())
{
schema.rebuild();
- System.out.println(schema.getDocument().asXML());
try
{
writeSchema(schema);
Modified: extensions/trunk/xsd/src/main/java/org/jboss/webbeans/xsd/model/ClassModel.java
===================================================================
---
extensions/trunk/xsd/src/main/java/org/jboss/webbeans/xsd/model/ClassModel.java 2009-03-23
05:25:18 UTC (rev 2137)
+++
extensions/trunk/xsd/src/main/java/org/jboss/webbeans/xsd/model/ClassModel.java 2009-03-23
07:09:01 UTC (rev 2138)
@@ -43,7 +43,7 @@
// The parent (or null if top-level)
private ClassModel parent;
// The fields of the class
- private List<NamedModel> fields = new ArrayList<NamedModel>();
+ private List<FieldModel> fields = new ArrayList<FieldModel>();
// The methods of the class
private List<MethodModel> methods = new ArrayList<MethodModel>();
// The constructors of the class
@@ -68,7 +68,7 @@
*
* @param field The field to add
*/
- public void addField(NamedModel field)
+ public void addField(FieldModel field)
{
fields.add(field);
}
@@ -129,7 +129,7 @@
*
* @return The public fields
*/
- public List<NamedModel> getFields()
+ public List<FieldModel> getFields()
{
return fields;
}
@@ -252,10 +252,7 @@
{
Element classElement =
DocumentFactory.getInstance().createElement("xs:element");
classElement.addAttribute("name", getSimpleName());
- Element complexElement =
DocumentFactory.getInstance().createElement("xs:complexType");
- Element anyElement =
DocumentFactory.getInstance().createElement("xs:any");
- complexElement.add(anyElement);
- classElement.add(complexElement);
+ classElement.addAttribute("type", "wb:bean");
Element choice =
DocumentFactory.getInstance().createElement("xs:choice");
for (ConstructorModel constructor : getMergedConstructors())
@@ -265,16 +262,16 @@
choice.add(constructor.toXSD(namespaceHandler));
}
}
- anyElement.add(choice);
+ classElement.add(choice);
for (MethodModel method : getMergedMethods())
{
- anyElement.add(method.toXSD(namespaceHandler));
+ classElement.add(method.toXSD(namespaceHandler));
}
for (NamedModel field : getMergedFields())
{
- anyElement.add(field.toXSD(namespaceHandler));
+ classElement.add(field.toXSD(namespaceHandler));
}
return classElement;
Added: 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
(rev 0)
+++
extensions/trunk/xsd/src/main/java/org/jboss/webbeans/xsd/model/FieldModel.java 2009-03-23
07:09:01 UTC (rev 2138)
@@ -0,0 +1,27 @@
+package org.jboss.webbeans.xsd.model;
+
+import org.dom4j.Element;
+import org.jboss.webbeans.xsd.NamespaceHandler;
+
+public class FieldModel extends NamedModel
+{
+
+ protected FieldModel(String name)
+ {
+ super(name);
+ }
+
+ public static FieldModel of(String name)
+ {
+ return new FieldModel(name);
+ }
+
+ @Override
+ public Element toXSD(NamespaceHandler namespaceHandler)
+ {
+ Element field = super.toXSD(namespaceHandler);
+ field.addAttribute("type", "wb:field");
+ return field;
+ }
+
+}
Modified:
extensions/trunk/xsd/src/main/java/org/jboss/webbeans/xsd/model/MethodModel.java
===================================================================
---
extensions/trunk/xsd/src/main/java/org/jboss/webbeans/xsd/model/MethodModel.java 2009-03-23
05:25:18 UTC (rev 2137)
+++
extensions/trunk/xsd/src/main/java/org/jboss/webbeans/xsd/model/MethodModel.java 2009-03-23
07:09:01 UTC (rev 2138)
@@ -96,6 +96,7 @@
public Element toXSD(NamespaceHandler namespaceHandler)
{
Element method = super.toXSD(namespaceHandler);
+ method.addAttribute("type", "wb:method");
for (TypedModel parameter : parameters)
{
method.add(parameter.toXSD(namespaceHandler));
Modified: extensions/trunk/xsd/src/main/java/org/jboss/webbeans/xsd/model/TypedModel.java
===================================================================
---
extensions/trunk/xsd/src/main/java/org/jboss/webbeans/xsd/model/TypedModel.java 2009-03-23
05:25:18 UTC (rev 2137)
+++
extensions/trunk/xsd/src/main/java/org/jboss/webbeans/xsd/model/TypedModel.java 2009-03-23
07:09:01 UTC (rev 2138)
@@ -100,10 +100,9 @@
} else {
}
- String typeOrRef = isPrimitive() ? ("xs:" + type) :
(namespaceHandler.getShortNamespace(type) + ":" + getTypeSimpleName());
- String attrName = isPrimitive() ? "type" : "ref";
+ String typeValue = isPrimitive() ? ("xs:" + type) :
(namespaceHandler.getPrefix(type) + ":" + getTypeSimpleName());
Element item =
DocumentFactory.getInstance().createElement("xs:element");
- item.addAttribute(attrName, typeOrRef);
+ item.addAttribute("type", typeValue);
return item;
}