[webbeans-commits] Webbeans SVN: r2082 - 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 04:27:05 EDT 2009


Author: nickarls
Date: 2009-03-18 04:27:05 -0400 (Wed, 18 Mar 2009)
New Revision: 2082

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/Schema.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/NamedModel.java
   extensions/trunk/xsd/src/main/java/org/jboss/webbeans/xsd/model/TypedModel.java
Log:
xsd stuff

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-18 06:31:05 UTC (rev 2081)
+++ extensions/trunk/xsd/src/main/java/org/jboss/webbeans/xsd/NamespaceHandler.java	2009-03-18 08:27:05 UTC (rev 2082)
@@ -42,6 +42,11 @@
    // Namespace infos
    private Map<String, SchemaNamespace> schemaNamespaces = new HashMap<String, SchemaNamespace>();
 
+   public Map<String, SchemaNamespace> getSchemaNamespaces()
+   {
+      return schemaNamespaces;
+   }
+
    /**
     * Creats a new namespace generator
     * 
@@ -59,14 +64,14 @@
     * @author Nicklas Karlsson
     * 
     */
-   private class SchemaNamespace
+   public class SchemaNamespace
    {
       // The package name
       String packageName;
       // The full namespace
-      String namespace;
-      // The namespace abbreviation
       String shortNamespace;
+      // The urn
+      String urn;
       // Is this a EE reserved package?
       boolean ee;
 
@@ -74,13 +79,15 @@
       {
          this.packageName = packageName;
          this.shortNamespace = shortNamespace;
-         // Skip ":" for default namespace
-         String colon = "".equals(shortNamespace) ? "" : ":";
-         // Hardcode "ee" for EE reserved packages
-         String url = ee ? "ee" : packageName;
-         this.namespace = "xmlns" + colon + shortNamespace + "=\"urn:java:" + url + "\"";
          this.ee = ee;
+         this.urn = "urn:java:" + (ee ? "ee" : packageName);
       }
+
+      @Override
+      public String toString()
+      {
+         return shortNamespace + "=" + urn;
+      }
    }
 
    /**
@@ -88,16 +95,15 @@
     * 
     * @return The used namespaces
     */
-   public Set<String> getUsedNamespaces()
-   {
-      Set<String> usedNamespaces = new HashSet<String>();
-      for (SchemaNamespace schemaNamespace : schemaNamespaces.values())
-      {
-         usedNamespaces.add(schemaNamespace.namespace);
-      }
-      return usedNamespaces;
-   }
-
+   // 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
     * 
@@ -106,9 +112,10 @@
     */
    public String getShortNamespace(String packageName)
    {
-      if (schemaNamespaces.containsKey(packageName))
+      String shortName = getPackageName(packageName);
+      if (schemaNamespaces.containsKey(shortName))
       {
-         return schemaNamespaces.get(packageName).shortNamespace;
+         return schemaNamespaces.get(shortName).shortNamespace;
       }
       else
       {
@@ -128,6 +135,12 @@
       return lastDot < 0 ? packageName : packageName.substring(lastDot + 1);
    }
 
+   private String getPackageName(String FQN)
+   {
+      int lastDot = FQN.lastIndexOf(".");
+      return lastDot < 0 ? "nopak" : FQN.substring(0, lastDot);
+   }
+
    public void addPackage(String packageName)
    {
       if (schemaNamespaces.containsKey(packageName))

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-18 06:31:05 UTC (rev 2081)
+++ extensions/trunk/xsd/src/main/java/org/jboss/webbeans/xsd/PackageSchemaGenerator.java	2009-03-18 08:27:05 UTC (rev 2082)
@@ -94,7 +94,7 @@
    private ClassModel inspectClass(Element element)
    {
       TypeElement typeElement = (TypeElement) element;
-      ClassModel classModel = new ClassModel(typeElement.getQualifiedName().toString());
+      ClassModel classModel = ClassModel.of(typeElement);
 
       // If the class has superclass's, scan them recursively
       if (typeElement.getSuperclass().getKind() != TypeKind.NONE)

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 06:31:05 UTC (rev 2081)
+++ extensions/trunk/xsd/src/main/java/org/jboss/webbeans/xsd/Schema.java	2009-03-18 08:27:05 UTC (rev 2082)
@@ -24,6 +24,7 @@
 
 import org.dom4j.Document;
 import org.dom4j.DocumentFactory;
+import org.jboss.webbeans.xsd.NamespaceHandler.SchemaNamespace;
 import org.jboss.webbeans.xsd.model.ClassModel;
 import org.jboss.webbeans.xsd.model.TypedModel;
 
@@ -76,22 +77,20 @@
       this.packageName = packageName;
    }
 
-   public Set<String> getNamespaces()
-   {
-      return namespaceHandler.getUsedNamespaces();
-   }
+//   public Set<String> getNamespaces()
+//   {
+//      return namespaceHandler.getUsedNamespaces();
+//   }
 
    public void rebuild(PackageElement packageElement)
    {
-      for (String namespace : namespaceHandler.getUsedNamespaces())
+      for (SchemaNamespace schemaNamespace : namespaceHandler.getSchemaNamespaces().values())
       {
-         document.getRootElement().addNamespace("x", namespace);
+         document.getRootElement().addNamespace(schemaNamespace.shortNamespace, schemaNamespace.urn);
       }
       for (ClassModel classModel : classModels)
       {
-         org.dom4j.Element classElement = DocumentFactory.getInstance().createElement("element");
-         classElement.addAttribute("name", classModel.getSimpleName());
-         document.getRootElement().add(classElement);
+         document.getRootElement().add(classModel.toXSD(namespaceHandler));
       }
       // System.out.println("Current contents of package " + packageName);
       // for (Element e : packageElement.getEnclosedElements())
@@ -106,9 +105,9 @@
       StringBuilder buffer = new StringBuilder();
       buffer.append("Package: " + packageName + "\n");
       buffer.append("Used namespaces\n");
-      for (String namespace : namespaceHandler.getUsedNamespaces())
+      for (SchemaNamespace schemaNamespace : namespaceHandler.getSchemaNamespaces().values())
       {
-         buffer.append("  " + namespace + "\n");
+         buffer.append("  " + schemaNamespace + "\n");
       }
       buffer.append("Contained classes:\n");
       for (ClassModel classModel : classModels)

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-18 06:31:05 UTC (rev 2081)
+++ extensions/trunk/xsd/src/main/java/org/jboss/webbeans/xsd/helpers/XSDHelper.java	2009-03-18 08:27:05 UTC (rev 2082)
@@ -162,11 +162,11 @@
             }
             catch (DocumentException e)
             {
-               throw new RuntimeException("Could not parse schema for package " + packageName);
+               throw new RuntimeException("Could not parse schema for package " + packageName, e);
             }
             catch (IOException e)
             {
-               throw new RuntimeException("Could not read schema for package " + packageName);
+               throw new RuntimeException("Could not read schema for package " + packageName, e);
             }
             schemaMap.put(packageName, schema);
          }
@@ -187,11 +187,14 @@
          System.out.println(schema.getDocument().asXML());
          try
          {
-            writeSchema(schema);
+            if (1 == 2)
+            {
+               writeSchema(schema);
+            }
          }
          catch (IOException e)
          {
-            throw new RuntimeException("Could not write schema for " + schema.getPackageName());
+            throw new RuntimeException("Could not write schema for " + schema.getPackageName(), e);
          }
       }
    }

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-18 06:31:05 UTC (rev 2081)
+++ extensions/trunk/xsd/src/main/java/org/jboss/webbeans/xsd/model/ClassModel.java	2009-03-18 08:27:05 UTC (rev 2082)
@@ -22,6 +22,12 @@
 import java.util.List;
 import java.util.Set;
 
+import javax.lang.model.element.TypeElement;
+
+import org.dom4j.DocumentFactory;
+import org.dom4j.Element;
+import org.jboss.webbeans.xsd.NamespaceHandler;
+
 /**
  * A model of a class
  * 
@@ -40,7 +46,7 @@
    // The constructors of the class
    private List<MethodModel> constructors = new ArrayList<MethodModel>();
 
-   public ClassModel(String name)
+   protected ClassModel(String name)
    {
       super(name);
    }
@@ -224,4 +230,28 @@
       return buffer.toString();
    }
 
+   public static ClassModel of(TypeElement typeElement)
+   {
+      return new ClassModel(typeElement.getQualifiedName().toString());
+   }
+
+   public Element toXSD(NamespaceHandler namespaceHandler)
+   {
+      Element classElement = DocumentFactory.getInstance().createElement("element");
+      classElement.addAttribute("name", getSimpleName());
+      for (MethodModel constructor : getMergedConstructors())
+      {
+         classElement.add(constructor.toXSD(namespaceHandler));
+      }
+      for (NamedModel field : getMergedFields())
+      {
+         classElement.add(field.toXSD(namespaceHandler));
+      }
+      for (MethodModel method : getMergedMethods())
+      {
+         classElement.add(method.toXSD(namespaceHandler));
+      }
+      return classElement;
+   }
+
 }

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-18 06:31:05 UTC (rev 2081)
+++ extensions/trunk/xsd/src/main/java/org/jboss/webbeans/xsd/model/MethodModel.java	2009-03-18 08:27:05 UTC (rev 2082)
@@ -20,6 +20,9 @@
 import java.util.ArrayList;
 import java.util.List;
 
+import org.dom4j.Element;
+import org.jboss.webbeans.xsd.NamespaceHandler;
+
 /**
  * The model of a method
  * 
@@ -64,4 +67,15 @@
       return "\n  " + name + "(" + (parameters.isEmpty() ? "" : parameters) + ")";
    }
 
+   @Override
+   public Element toXSD(NamespaceHandler namespaceHandler)
+   {
+      Element method = super.toXSD(namespaceHandler);
+      for (TypedModel parameter : parameters)
+      {
+         method.add(parameter.toXSD(namespaceHandler));
+      }
+      return method;
+   }
+
 }

Modified: extensions/trunk/xsd/src/main/java/org/jboss/webbeans/xsd/model/NamedModel.java
===================================================================
--- extensions/trunk/xsd/src/main/java/org/jboss/webbeans/xsd/model/NamedModel.java	2009-03-18 06:31:05 UTC (rev 2081)
+++ extensions/trunk/xsd/src/main/java/org/jboss/webbeans/xsd/model/NamedModel.java	2009-03-18 08:27:05 UTC (rev 2082)
@@ -17,6 +17,10 @@
 
 package org.jboss.webbeans.xsd.model;
 
+import org.dom4j.DocumentFactory;
+import org.dom4j.Element;
+import org.jboss.webbeans.xsd.NamespaceHandler;
+
 /**
  * A superclass for named models
  * 
@@ -55,4 +59,11 @@
    {
       return name.hashCode();
    }
+
+   public Element toXSD(NamespaceHandler namespaceHandler)
+   {
+      Element item = DocumentFactory.getInstance().createElement("element");
+      item.addAttribute("name", name);
+      return item;
+   }
 }

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 06:31:05 UTC (rev 2081)
+++ extensions/trunk/xsd/src/main/java/org/jboss/webbeans/xsd/model/TypedModel.java	2009-03-18 08:27:05 UTC (rev 2082)
@@ -17,6 +17,10 @@
 
 package org.jboss.webbeans.xsd.model;
 
+import org.dom4j.DocumentFactory;
+import org.dom4j.Element;
+import org.jboss.webbeans.xsd.NamespaceHandler;
+
 /**
  * The model of a typed member
  * 
@@ -88,4 +92,12 @@
       return type.hashCode();
    }
 
+   public Element toXSD(NamespaceHandler namespaceHandler)
+   {
+      String namespace = isPrimitive() ? "" : namespaceHandler.getShortNamespace(type);
+      Element item = DocumentFactory.getInstance().createElement("element");
+      item.addAttribute("type", type);
+      return item;
+   }
+
 }




More information about the weld-commits mailing list