[webbeans-commits] Webbeans SVN: r2102 - in extensions/trunk/xsd/src/main/java/org/jboss/webbeans/xsd: helpers and 1 other directories.

webbeans-commits at lists.jboss.org webbeans-commits at lists.jboss.org
Wed Mar 18 18:14:29 EDT 2009


Author: nickarls
Date: 2009-03-18 18:14:29 -0400 (Wed, 18 Mar 2009)
New Revision: 2102

Modified:
   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/model/TypedModel.java
Log:
cleanup of document for obsolete classes/namespaces

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-18 21:31:15 UTC (rev 2101)
+++ extensions/trunk/xsd/src/main/java/org/jboss/webbeans/xsd/Schema.java	2009-03-18 22:14:29 UTC (rev 2102)
@@ -17,31 +17,44 @@
 
 package org.jboss.webbeans.xsd;
 
+import java.util.ArrayList;
 import java.util.HashSet;
+import java.util.List;
 import java.util.Set;
 
 import javax.lang.model.element.PackageElement;
 import javax.lang.model.element.TypeElement;
 
+import org.dom4j.Attribute;
 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;
 
 /**
- * Package information
+ * A schema representation
  * 
  * @author Nicklas Karlsson
  * 
  */
 public class Schema
 {
+   // The name of the package
    private String packageName;
+   // The XSD document
    private Document document;
+   // The namespace handler
    private NamespaceHandler namespaceHandler;
+   // The set of classes to update
    private Set<ClassModel> classModels;
 
+   /**
+    * Creates a new package
+    * 
+    * @param packageName The name of the package
+    */
    public Schema(String packageName)
    {
       this.packageName = packageName;
@@ -49,11 +62,21 @@
       classModels = new HashSet<ClassModel>();
    }
 
+   /**
+    * Gets the name of the package
+    * 
+    * @return The package name
+    */
    public String getPackageName()
    {
       return packageName;
    }
 
+   /**
+    * Adds a class model to the working set and adds the referenced types to the namespace handler
+    * 
+    * @param classModel The class model
+    */
    public void addClass(ClassModel classModel)
    {
       classModels.add(classModel);
@@ -63,16 +86,33 @@
       }
    }
 
+   /**
+    * Gets the XSD document
+    * 
+    * @return The XSD document
+    */
    public Document getDocument()
    {
       return document;
    }
 
+   /**
+    * Sets the XSD document
+    * 
+    * @param document The XSD document
+    */
    public void setDocument(Document document)
    {
       this.document = document;
    }
 
+   /**
+    * 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(PackageElement packageElement, String FQN)
    {
       for (javax.lang.model.element.Element classElement : packageElement.getEnclosedElements())
@@ -86,47 +126,86 @@
       return false;
    }
 
-   public void rebuild(PackageElement packageElement)
+   /**
+    * Cleans out XSD for files that are no longer present in the package
+    * @param packageElement
+    */
+   private void cleanRemovedClasses(PackageElement packageElement)
    {
-      for (SchemaNamespace schemaNamespace : namespaceHandler.getSchemaNamespaces().values())
+      for (Object xsdClass : document.selectNodes("/xs:schema/xs:element"))
       {
-         document.getRootElement().addNamespace(schemaNamespace.shortNamespace, schemaNamespace.urn);
+         String FQN = packageName + "." + ((Element) xsdClass).attributeValue("name");
+         if (!isClassInPackage(packageElement, FQN))
+         {
+            ((Element) xsdClass).detach();
+         }
       }
+   }
 
-      for (Object xsdClass : document.selectNodes("//xs:schema//xs:element"))
+   /**
+    * 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"))
       {
-         String className = ((Element) xsdClass).attributeValue("name");
-         if (!isClassInPackage(packageElement, packageName + "." + className))
+         String ref = ((Attribute) attribute).getValue();
+         int colon = ref.indexOf(":");
+         String namespace = colon < 0 ? "" : ref.substring(0, colon);
+         referencedNamespaces.add(namespace);
+      }
+      
+      // 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)))
          {
-            ((Element) xsdClass).detach();
+            currentNamespaces.add(namespace.getPrefix());
          }
       }
+      
+      // Removes the namespaces that are no longer referenced
+      currentNamespaces.removeAll(referencedNamespaces);
+      for (String obsolete : currentNamespaces)
+      {
+         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)
+         {
+            document.getRootElement().addNamespace(schemaNamespace.shortNamespace, schemaNamespace.urn);
+         }
+      }
 
+   }
+
+   /**
+    * Rebuilds the schema document
+    * 
+    * @param packageElement The package abstraction
+    */
+   public void rebuild(PackageElement packageElement)
+   {
+      cleanRemovedClasses(packageElement);
+      updateNamespaces();
+
       for (ClassModel classModel : classModels)
       {
          // Remove old version of class xsd (if present)
-         for (Object previousClass : document.selectNodes("//xs:schema//xs:element[@name=\"" + classModel.getSimpleName() + "\"]"))
+         for (Object previousClass : document.selectNodes("/xs:schema/xs:element[@name=\"" + classModel.getSimpleName() + "\"]"))
          {
             ((Element) previousClass).detach();
          }
          document.getRootElement().add(classModel.toXSD(namespaceHandler));
       }
-
-      /**
-       * XSD: Foo Bar Tar
-       * 
-       * ClassModels: Foo
-       * 
-       * Package: Foo Bar
-       * 
-       * => update Foo, remove Tar
-       */
-
-      // System.out.println("Current contents of package " + packageName);
-      // for (Element e : packageElement.getEnclosedElements())
-      // {
-      // System.out.println(e.asType().toString());
-      // }
    }
 
    @Override

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-18 21:31:15 UTC (rev 2101)
+++ extensions/trunk/xsd/src/main/java/org/jboss/webbeans/xsd/helpers/DataSetter.java	2009-03-18 22:14:29 UTC (rev 2102)
@@ -38,7 +38,7 @@
  */
 public class DataSetter
 {
-   public static Map<TypedModel, TypedModel> typeSubstitutions = new HashMap<TypedModel, TypedModel>()
+   private static Map<TypedModel, TypedModel> typeSubstitutions = new HashMap<TypedModel, TypedModel>()
    { 
       private static final long serialVersionUID = 8092480390430415094L;
    {

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-18 21:31:15 UTC (rev 2101)
+++ extensions/trunk/xsd/src/main/java/org/jboss/webbeans/xsd/model/TypedModel.java	2009-03-18 22:14:29 UTC (rev 2102)
@@ -95,9 +95,15 @@
 
    public Element toXSD(NamespaceHandler namespaceHandler)
    {
-      String namespace = isPrimitive() ? ("xs:" + type) : (namespaceHandler.getShortNamespace(type) + ":" + getTypeSimpleName());
+      if (isPrimitive()) {
+         
+      } else {
+         
+      }
+      String typeOrRef = isPrimitive() ? ("xs:" + type) : (namespaceHandler.getShortNamespace(type) + ":" + getTypeSimpleName());
+      String attrName = isPrimitive() ? "type" : "ref";
       Element item = DocumentFactory.getInstance().createElement("xs:element");
-      item.addAttribute("type", namespace);
+      item.addAttribute(attrName, typeOrRef);
       return item;
    }
 




More information about the weld-commits mailing list