[weld-commits] Weld SVN: r3903 - in extensions/trunk/xsd/src/main/java/org/jboss: webbeans/xsd and 8 other directories.

weld-commits at lists.jboss.org weld-commits at lists.jboss.org
Thu Oct 8 10:22:11 EDT 2009


Author: shane.bryzak at jboss.com
Date: 2009-10-08 10:22:10 -0400 (Thu, 08 Oct 2009)
New Revision: 3903

Added:
   extensions/trunk/xsd/src/main/java/org/jboss/weld/
   extensions/trunk/xsd/src/main/java/org/jboss/weld/xsd/
   extensions/trunk/xsd/src/main/java/org/jboss/weld/xsd/NamespaceHandler.java
   extensions/trunk/xsd/src/main/java/org/jboss/weld/xsd/PackageSchemaGenerator.java
   extensions/trunk/xsd/src/main/java/org/jboss/weld/xsd/Schema.java
   extensions/trunk/xsd/src/main/java/org/jboss/weld/xsd/model/
   extensions/trunk/xsd/src/main/java/org/jboss/weld/xsd/sample-schema.xsd
   extensions/trunk/xsd/src/main/java/org/jboss/weld/xsd/test/
   extensions/trunk/xsd/src/main/java/org/jboss/weld/xsd/test/Bar.java
   extensions/trunk/xsd/src/main/java/org/jboss/weld/xsd/test/Foo.java
   extensions/trunk/xsd/src/main/java/org/jboss/weld/xsd/test/Test.java
   extensions/trunk/xsd/src/main/java/org/jboss/weld/xsd/test/test/
   extensions/trunk/xsd/src/main/java/org/jboss/weld/xsd/test/test/Foo.java
   extensions/trunk/xsd/src/main/java/org/jboss/weld/xsd/test/test/test/
Removed:
   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/model/
   extensions/trunk/xsd/src/main/java/org/jboss/webbeans/xsd/sample-schema.xsd
   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
   extensions/trunk/xsd/src/main/java/org/jboss/webbeans/xsd/test/Test.java
   extensions/trunk/xsd/src/main/java/org/jboss/webbeans/xsd/test/test/Foo.java
   extensions/trunk/xsd/src/main/java/org/jboss/webbeans/xsd/test/test/test/
Modified:
   extensions/trunk/xsd/src/main/java/org/jboss/weld/xsd/model/ClassModel.java
   extensions/trunk/xsd/src/main/java/org/jboss/weld/xsd/model/ConstructorModel.java
   extensions/trunk/xsd/src/main/java/org/jboss/weld/xsd/model/FieldModel.java
   extensions/trunk/xsd/src/main/java/org/jboss/weld/xsd/model/MethodModel.java
   extensions/trunk/xsd/src/main/java/org/jboss/weld/xsd/model/NamedModel.java
   extensions/trunk/xsd/src/main/java/org/jboss/weld/xsd/model/TypedModel.java
   extensions/trunk/xsd/src/main/java/org/jboss/weld/xsd/test/test/test/Foo.java
Log:
refactor package naming, fix compiler errors


Deleted: 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-10-08 13:44:49 UTC (rev 3902)
+++ extensions/trunk/xsd/src/main/java/org/jboss/webbeans/xsd/NamespaceHandler.java	2009-10-08 14:22:10 UTC (rev 3903)
@@ -1,222 +0,0 @@
-/*
- * 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.Arrays;
-import java.util.Collection;
-import java.util.HashMap;
-import java.util.HashSet;
-import java.util.Map;
-import java.util.Set;
-
-import javax.xml.stream.events.Namespace;
-
-/**
- * Helper for generating and keeping track of namespaces in a schema
- * 
- * @author Nicklas Karlsson
- * 
- */
-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>();
-
-   /**
-    * 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);
-      if (namespaces.containsKey(shortName))
-      {
-         return namespaces.get(shortName).getPrefix();
-      }
-      else
-      {
-         throw new IllegalArgumentException("Package name " + packageName + " is not known to namespace handler of package " + localPackage);
-      }
-   }
-
-   /**
-    * 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);
-      }
-      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));
-         for (Namespace namespace : namespaces.values())
-         {
-            if (namespace.getPrefix().equals(prefix))
-            {
-               found = true;
-               break;
-            }
-         }
-         if (!found)
-         {
-            return prefix;
-         }
-         else
-         {
-            suffix++;
-            found = false;
-         }
-      }
-   }
-
-   /**
-    * 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:");
-      if (urnJava >= 0)
-      {
-         return namespace.getURI().substring(urnJava);
-      }
-      else
-      {
-         return namespace.getURI();
-      }
-   }
-
-   @Override
-   public String toString()
-   {
-      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");
-   }
-
-}

Deleted: 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-10-08 13:44:49 UTC (rev 3902)
+++ extensions/trunk/xsd/src/main/java/org/jboss/webbeans/xsd/PackageSchemaGenerator.java	2009-10-08 14:22:10 UTC (rev 3903)
@@ -1,173 +0,0 @@
-/*
- * 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.io.IOException;
-import java.util.HashMap;
-import java.util.Map;
-import java.util.Set;
-
-import javax.annotation.processing.AbstractProcessor;
-import javax.annotation.processing.ProcessingEnvironment;
-import javax.annotation.processing.RoundEnvironment;
-import javax.annotation.processing.SupportedAnnotationTypes;
-import javax.annotation.processing.SupportedSourceVersion;
-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;
-import javax.lang.model.util.ElementFilter;
-
-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
- * that have had their files compiled.
- * 
- * @author Nicklas Karlsson
- * 
- */
- at SupportedSourceVersion(SourceVersion.RELEASE_6)
- at SupportedAnnotationTypes("*")
-public class PackageSchemaGenerator extends AbstractProcessor
-{
-   private static LogProvider log = Logging.getLogProvider(PackageSchemaGenerator.class);
-
-   private Map<String, ClassModel> classModelCache;
-   private Map<String, Schema> schemas;
-
-   @Override
-   public synchronized void init(ProcessingEnvironment processingEnvironment)
-   {
-      super.init(processingEnvironment);
-      classModelCache = new HashMap<String, ClassModel>();
-      schemas = new HashMap<String, Schema>();
-   }
-
-   @Override
-   public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnvironment)
-   {
-      for (Element element : roundEnvironment.getRootElements())
-      {
-         if (ElementKind.CLASS.equals(element.getKind()) || ElementKind.ANNOTATION_TYPE.equals(element.getKind()))
-         {
-            ClassModel classModel = inspectClass(element);
-            try
-            {
-               addClassToSchema(classModel);
-            }
-            catch (DocumentException e)
-            {
-               log.error("Could not read or create schema for package " + classModel.getPackage());
-            }
-         }
-      }
-      if (!roundEnvironment.processingOver())
-      {
-         for (Schema schema : schemas.values())
-         {
-            try
-            {
-               schema.rebuild().write(processingEnv.getFiler());
-            }
-            catch (IOException e)
-            {
-               log.error("Could not write schema.xsd for " + schema, e);
-            }
-         }
-      }
-      return true;
-   }
-
-   private void addClassToSchema(ClassModel classModel) throws DocumentException
-   {
-      String packageName = classModel.getPackage();
-      Schema schema = schemas.get(packageName);
-      if (schema == null)
-      {
-         schema = Schema.of(packageName, classModel.getPackageElement(), processingEnv.getFiler());
-         schemas.put(packageName, schema);
-      }
-      schema.addClass(classModel);
-   }
-
-   private boolean isPublic(Element element)
-   {
-      return element.getModifiers().contains(Modifier.PUBLIC);
-   }
-
-   /**
-    * 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 = ClassModel.of(typeElement, processingEnv.getElementUtils().getPackageOf(typeElement));
-
-      // 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.setParent(classModelCache.get(typeElement.getSuperclass().toString()));
-      // Filter out the fields and populate the model
-      for (Element field : ElementFilter.fieldsIn(element.getEnclosedElements()))
-      {
-         if (!isPublic(field))
-         {
-            continue;
-         }
-         classModel.addField(FieldModel.of(field));
-      }
-      // Filter out the methods and populate the model
-      for (ExecutableElement method : ElementFilter.methodsIn(element.getEnclosedElements()))
-      {
-         if (!isPublic(method))
-         {
-            continue;
-         }
-         classModel.addMethod(MethodModel.of(method));
-      }
-      // Filter out the constructors and populate the model
-      for (ExecutableElement constructor : ElementFilter.constructorsIn(element.getEnclosedElements()))
-      {
-         if (!isPublic(constructor))
-         {
-            continue;
-         }
-         classModel.addConstructor(ConstructorModel.of(constructor));
-      }
-      // Place the new class model in the cache
-      classModelCache.put(classModel.getName(), classModel);
-      return classModel;
-   }
-
-}

Deleted: 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-10-08 13:44:49 UTC (rev 3902)
+++ extensions/trunk/xsd/src/main/java/org/jboss/webbeans/xsd/Schema.java	2009-10-08 14:22:10 UTC (rev 3903)
@@ -1,286 +0,0 @@
-/*
- * 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.io.IOException;
-import java.io.InputStream;
-import java.io.OutputStream;
-import java.util.ArrayList;
-import java.util.HashSet;
-import java.util.List;
-import java.util.Set;
-
-import javax.annotation.processing.Filer;
-import javax.lang.model.element.PackageElement;
-import javax.lang.model.element.TypeElement;
-import javax.tools.StandardLocation;
-import javax.xml.stream.events.Namespace;
-
-import org.jboss.webbeans.xsd.model.ClassModel;
-import org.jboss.webbeans.xsd.model.TypedModel;
-
-import com.sun.org.apache.xml.internal.serialize.OutputFormat;
-
-/**
- * A schema representation
- * 
- * @author Nicklas Karlsson
- * 
- */
-public class Schema
-{
-   // The default namespace of the 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
-   private Document document;
-   // The namespace handler
-   private NamespaceHandler namespaceHandler;
-   // The set of classes to update
-   private Set<ClassModel> classModels;
-   private PackageElement packageElement;
-
-   /**
-    * Creates a new package
-    * 
-    * @param packageName The name of the package
-    * @throws DocumentException 
-    */
-   public Schema(String packageName, PackageElement packageElement, Filer filer) throws DocumentException
-   {
-      this.packageName = packageName;
-      this.packageElement = packageElement;
-      classModels = new HashSet<ClassModel>();
-      namespaceHandler = new NamespaceHandler(packageName);
-      try
-      {
-         document = readDocument(filer);
-      } catch (IOException e) {
-         // schema not found, safe to proceed
-      }
-      if (document == null)
-      {
-         document = createDocument();
-      }
-      init();
-   }
-
-   /**
-    * Creates a new schema document
-    * 
-    * @return The document
-    */
-   private Document createDocument()
-   {
-      Document document = DocumentHelper.createDocument();
-      QName rootQName = DocumentFactory.getInstance().createQName("schema", "xs", "http://www.w3.org/2001/XMLSchema");
-      Element rootElement = DocumentFactory.getInstance().createElement(rootQName);
-      rootElement.addAttribute("elementFormDefault", "qualified");
-      rootElement.addAttribute("targetNamespace", "urn:java:" + packageName);
-      rootElement.addAttribute("elementFormDefault", "qualified");
-      rootElement.addAttribute("xsi:schemaLocation", "http://www.w3.org/2001/XMLSchema http://www.w3.org/2001/XMLSchema.xsd");
-      document.setRootElement(rootElement);
-      for (Namespace namespace : defaultNamespaces)
-      {
-         rootElement.add(namespace);
-      }
-      return document;
-   }
-
-   /**
-    * Static factory method
-    * 
-    * @param packageName The package name
-    * @param packageElement The package content representation
-    * @param filer The filer for resources
-    * 
-    * @return A new schema instance
-    * 
-    * @throws DocumentException If an existing schema could not be parsed
-    */
-   public static Schema of(String packageName, PackageElement packageElement, Filer filer) throws DocumentException
-   {
-      return new Schema(packageName, packageElement, filer);
-   }
-
-   /**
-    * Reads a schema document
-    * 
-    * @param filer The filer to be used for resource reading
-    * 
-    * @return The schema documetn
-    * 
-    * @throws IOException If the file could not be find or there was an error reading it
-    * @throws DocumentException If the read document could not be parsed
-    */
-   private Document readDocument(Filer filer) throws IOException, DocumentException
-   {
-      InputStream in = filer.getResource(StandardLocation.CLASS_OUTPUT, packageName, "schema.xsd").openInputStream();
-      return new SAXReader().read(in);
-   }
-
-   /**
-    * Writes a schema back to disk
-    * 
-    * @param filer The filer to be used for resource writing
-    * 
-    * @throws IOException If the file could not be written
-    */
-   public void write(Filer filer) throws IOException
-   {
-      OutputFormat format = OutputFormat.createPrettyPrint();
-      OutputStream out = filer.createResource(StandardLocation.CLASS_OUTPUT, packageName, "schema.xsd").openOutputStream();
-      XMLWriter writer = new XMLWriter(out, format);
-      writer.write(document);
-      writer.flush();
-      writer.close();
-   }
-
-   /**
-    * 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);
-      for (TypedModel reference : classModel.getTypeReferences())
-      {
-         namespaceHandler.addPackage(reference.getTypePackage());
-      }
-   }
-
-   /**
-    * Cleans out namespaces and XSD for files that are no longer present in the
-    * package
-    */
-   private void init()
-   {
-      // 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");
-         if (!isClassInPackage(FQN))
-         {
-            ((Element) xsdClass).detach();
-         }
-      }
-
-      // Collect namespaces that are references in the document
-      Set<Namespace> referencedNamespaces = new HashSet<Namespace>(defaultNamespaces);
-      for (Object attribute : document.getRootElement().selectNodes("//@type"))
-      {
-         String ref = ((Attribute) attribute).getValue();
-         int colon = ref.indexOf(":");
-         String prefix = colon < 0 ? "" : ref.substring(0, colon);
-         referencedNamespaces.add(document.getRootElement().getNamespaceForPrefix(prefix));
-      }
-
-      // Register the namespaces with the namespace handler if they are present, otherwise
-      // remove them from the document
-      for (Object item : document.getRootElement().additionalNamespaces())
-      {
-         Namespace namespace = (Namespace) item;
-         if (referencedNamespaces.contains(namespace))
-         {
-            namespaceHandler.addNamespace(namespace);
-         }
-         else
-         {
-            document.getRootElement().remove(namespace);
-         }
-      }
-   }
-
-   /**
-    * 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())
-      {
-         TypeElement typeElement = (TypeElement) classElement;
-         if (typeElement.getQualifiedName().toString().equals(FQN))
-         {
-            return true;
-         }
-      }
-      return false;
-   }
-
-   /**
-    * Rebuilds the schema document
-    * 
-    * @param packageElement The package abstraction
-    * @return the schema for fluent interface
-    */
-   public Schema rebuild()
-   {
-      // 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);
-         }
-      }
-
-      // Remove old version of class xsd (if present) and add the fresh version
-      for (ClassModel classModel : classModels)
-      {
-         for (Object previousClass : document.selectNodes("/xs:schema/xs:element[@name=\"" + classModel.getSimpleName() + "\"]"))
-         {
-            ((Element) previousClass).detach();
-         }
-         document.getRootElement().add(classModel.toXSD(namespaceHandler));
-      }
-      return this;
-   }
-
-   @Override
-   public String toString()
-   {
-      StringBuilder buffer = new StringBuilder();
-      buffer.append("Package: " + packageName + "\n");
-      buffer.append("Used namespaces\n");
-      for (Namespace namespace : namespaceHandler.getNamespaces())
-      {
-         buffer.append("  " + namespace + "\n");
-      }
-      buffer.append("Contained classes:\n");
-      for (ClassModel classModel : classModels)
-      {
-         buffer.append(classModel + "\n");
-      }
-      return buffer.toString();
-   }
-
-}

Deleted: extensions/trunk/xsd/src/main/java/org/jboss/webbeans/xsd/sample-schema.xsd
===================================================================
--- extensions/trunk/xsd/src/main/java/org/jboss/webbeans/xsd/sample-schema.xsd	2009-10-08 13:44:49 UTC (rev 3902)
+++ extensions/trunk/xsd/src/main/java/org/jboss/webbeans/xsd/sample-schema.xsd	2009-10-08 14:22:10 UTC (rev 3903)
@@ -1,28 +0,0 @@
-<?xml version="1.0" encoding="UTF-8"?>
-<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" 
-           elementFormDefault="qualified"
-           targetNamespace="urn:java:com.acme.foo"
-           xmlns:foo="urn:java:com.acme.foo"
-           xmlns:ee="urn:java:ee"
-           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
-           xsi:schemaLocation="http://www.w3.org/2001/XMLSchema http://www.w3.org/2001/XMLSchema.xsd"
-           >
-
-  <xs:element name="Foo">
-     <xs:complexType>
-        <xs:choice>
-           <!-- Options to allow people to declare this class a binding type or stereotype - TODO only if an annotation -->
-           <xs:element ref="ee:BindingType"/>
-           <xs:element ref="ee:StereotypType"/>
-           <!-- Somehow import all binding types known etc. to give people that option -->
-           <xs:any /> <!-- Can't guess all types people want to put here -->
-        </xs:choice>
-     </xs:complexType>
-  </xs:element>
-  
-  <xs:element name="Bar">
-  
-  </xs:element>
-
-  
-</xs:schema>
\ No newline at end of file

Deleted: 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-10-08 13:44:49 UTC (rev 3902)
+++ extensions/trunk/xsd/src/main/java/org/jboss/webbeans/xsd/test/Bar.java	2009-10-08 14:22:10 UTC (rev 3903)
@@ -1,9 +0,0 @@
-package org.jboss.webbeans.xsd.test;
-
-public class Bar
-{
-   public org.jboss.webbeans.xsd.test.test.Foo pong(org.jboss.webbeans.xsd.test.test.test.Foo foo)
-   {
-      return null;
-   }
-}

Deleted: 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-10-08 13:44:49 UTC (rev 3902)
+++ extensions/trunk/xsd/src/main/java/org/jboss/webbeans/xsd/test/Foo.java	2009-10-08 14:22:10 UTC (rev 3903)
@@ -1,16 +0,0 @@
-package org.jboss.webbeans.xsd.test;
-
-
-public class Foo extends Bar
-{
-   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.Foo foo3;
-
-   public void testy(org.jboss.webbeans.xsd.test.test.Foo a, org.jboss.webbeans.xsd.test.test.test.Foo b) {
-      
-   }
-   
-}

Deleted: extensions/trunk/xsd/src/main/java/org/jboss/webbeans/xsd/test/Test.java
===================================================================
--- extensions/trunk/xsd/src/main/java/org/jboss/webbeans/xsd/test/Test.java	2009-10-08 13:44:49 UTC (rev 3902)
+++ extensions/trunk/xsd/src/main/java/org/jboss/webbeans/xsd/test/Test.java	2009-10-08 14:22:10 UTC (rev 3903)
@@ -1,30 +0,0 @@
-package org.jboss.webbeans.xsd.test;
-
-import java.util.ArrayList;
-import java.util.List;
-
-import javax.annotation.processing.AbstractProcessor;
-import javax.tools.JavaCompiler;
-import javax.tools.JavaFileObject;
-import javax.tools.StandardJavaFileManager;
-import javax.tools.ToolProvider;
-import javax.tools.JavaCompiler.CompilationTask;
-
-import org.jboss.webbeans.xsd.PackageSchemaGenerator;
-
-public class Test
-{
-
-   public static void main(String[] args)
-   {
-      JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
-      StandardJavaFileManager fileManager = compiler.getStandardFileManager(null, null, null);
-      Iterable<? extends JavaFileObject> compilationUnits = fileManager.getJavaFileObjects(args);
-      CompilationTask task = compiler.getTask(null, fileManager, null, null, null, compilationUnits);
-      List<AbstractProcessor> processors = new ArrayList<AbstractProcessor>();
-      processors.add(new PackageSchemaGenerator());
-      task.setProcessors(processors);
-      task.call();
-   }
-
-}

Deleted: extensions/trunk/xsd/src/main/java/org/jboss/webbeans/xsd/test/test/Foo.java
===================================================================
--- extensions/trunk/xsd/src/main/java/org/jboss/webbeans/xsd/test/test/Foo.java	2009-10-08 13:44:49 UTC (rev 3902)
+++ extensions/trunk/xsd/src/main/java/org/jboss/webbeans/xsd/test/test/Foo.java	2009-10-08 14:22:10 UTC (rev 3903)
@@ -1,6 +0,0 @@
-package org.jboss.webbeans.xsd.test.test;
-
-public class Foo
-{
-
-}

Copied: extensions/trunk/xsd/src/main/java/org/jboss/weld/xsd/NamespaceHandler.java (from rev 3901, extensions/trunk/xsd/src/main/java/org/jboss/webbeans/xsd/NamespaceHandler.java)
===================================================================
--- extensions/trunk/xsd/src/main/java/org/jboss/weld/xsd/NamespaceHandler.java	                        (rev 0)
+++ extensions/trunk/xsd/src/main/java/org/jboss/weld/xsd/NamespaceHandler.java	2009-10-08 14:22:10 UTC (rev 3903)
@@ -0,0 +1,222 @@
+/*
+ * 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.weld.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
+ * 
+ * @author Nicklas Karlsson
+ * 
+ */
+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>();
+
+   /**
+    * 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);
+      if (namespaces.containsKey(shortName))
+      {
+         return namespaces.get(shortName).getPrefix();
+      }
+      else
+      {
+         throw new IllegalArgumentException("Package name " + packageName + " is not known to namespace handler of package " + localPackage);
+      }
+   }
+
+   /**
+    * 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);
+      }
+      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));
+         for (Namespace namespace : namespaces.values())
+         {
+            if (namespace.getPrefix().equals(prefix))
+            {
+               found = true;
+               break;
+            }
+         }
+         if (!found)
+         {
+            return prefix;
+         }
+         else
+         {
+            suffix++;
+            found = false;
+         }
+      }
+   }
+
+   /**
+    * 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:");
+      if (urnJava >= 0)
+      {
+         return namespace.getURI().substring(urnJava);
+      }
+      else
+      {
+         return namespace.getURI();
+      }
+   }
+
+   @Override
+   public String toString()
+   {
+      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");
+   }
+
+}

Copied: extensions/trunk/xsd/src/main/java/org/jboss/weld/xsd/PackageSchemaGenerator.java (from rev 3901, extensions/trunk/xsd/src/main/java/org/jboss/webbeans/xsd/PackageSchemaGenerator.java)
===================================================================
--- extensions/trunk/xsd/src/main/java/org/jboss/weld/xsd/PackageSchemaGenerator.java	                        (rev 0)
+++ extensions/trunk/xsd/src/main/java/org/jboss/weld/xsd/PackageSchemaGenerator.java	2009-10-08 14:22:10 UTC (rev 3903)
@@ -0,0 +1,176 @@
+/*
+ * 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.weld.xsd;
+
+import java.io.IOException;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.Set;
+
+import javax.annotation.processing.AbstractProcessor;
+import javax.annotation.processing.ProcessingEnvironment;
+import javax.annotation.processing.RoundEnvironment;
+import javax.annotation.processing.SupportedAnnotationTypes;
+import javax.annotation.processing.SupportedSourceVersion;
+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;
+import javax.lang.model.util.ElementFilter;
+
+import org.dom4j.DocumentException;
+import org.jboss.weld.log.LogProvider;
+import org.jboss.weld.log.Logging;
+import org.jboss.weld.xsd.model.ClassModel;
+import org.jboss.weld.xsd.model.ConstructorModel;
+import org.jboss.weld.xsd.model.FieldModel;
+import org.jboss.weld.xsd.model.MethodModel;
+
+/**
+ * An annotation processor that updates the package-level XSD for the packages
+ * that have had their files compiled.
+ * 
+ * @author Nicklas Karlsson
+ * 
+ */
+ at SupportedSourceVersion(SourceVersion.RELEASE_6)
+ at SupportedAnnotationTypes("*")
+public class PackageSchemaGenerator extends AbstractProcessor
+{
+   private static LogProvider log = Logging.getLogProvider(PackageSchemaGenerator.class);
+
+   private Map<String, ClassModel> classModelCache;
+   private Map<String, Schema> schemas;
+
+   @Override
+   public synchronized void init(ProcessingEnvironment processingEnvironment)
+   {
+      super.init(processingEnvironment);
+      classModelCache = new HashMap<String, ClassModel>();
+      schemas = new HashMap<String, Schema>();
+   }
+
+   @Override
+   public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnvironment)
+   {
+      for (Element element : roundEnvironment.getRootElements())
+      {
+         if (ElementKind.CLASS.equals(element.getKind()) || ElementKind.ANNOTATION_TYPE.equals(element.getKind()))
+         {
+            ClassModel classModel = inspectClass(element);
+            try
+            {
+               addClassToSchema(classModel);
+            }
+            catch (DocumentException e)
+            {
+               log.error("Could not read or create schema for package " + classModel.getPackage());
+            }
+         }
+      }
+      if (!roundEnvironment.processingOver())
+      {
+         for (Schema schema : schemas.values())
+         {
+            try
+            {
+               schema.rebuild().write(processingEnv.getFiler());
+            }
+            catch (IOException e)
+            {
+               log.error("Could not write schema.xsd for " + schema, e);
+            }
+         }
+      }
+      return true;
+   }
+
+   private void addClassToSchema(ClassModel classModel) throws DocumentException
+   {
+      String packageName = classModel.getPackage();
+      Schema schema = schemas.get(packageName);
+      if (schema == null)
+      {
+         schema = Schema.of(packageName, classModel.getPackageElement(), processingEnv.getFiler());
+         schemas.put(packageName, schema);
+      }
+      schema.addClass(classModel);
+   }
+
+   private boolean isPublic(Element element)
+   {
+      return element.getModifiers().contains(Modifier.PUBLIC);
+   }
+
+   /**
+    * 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 = ClassModel.of(typeElement, processingEnv.getElementUtils().getPackageOf(typeElement));
+
+      // 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.setParent(classModelCache.get(typeElement.getSuperclass().toString()));
+      // Filter out the fields and populate the model
+      for (Element field : ElementFilter.fieldsIn(element.getEnclosedElements()))
+      {
+         if (!isPublic(field))
+         {
+            continue;
+         }
+         classModel.addField(FieldModel.of(field));
+      }
+      // Filter out the methods and populate the model
+      for (ExecutableElement method : ElementFilter.methodsIn(element.getEnclosedElements()))
+      {
+         if (!isPublic(method))
+         {
+            continue;
+         }
+         classModel.addMethod(MethodModel.of(method));
+      }
+      // Filter out the constructors and populate the model
+      for (ExecutableElement constructor : ElementFilter.constructorsIn(element.getEnclosedElements()))
+      {
+         if (!isPublic(constructor))
+         {
+            continue;
+         }
+         classModel.addConstructor(ConstructorModel.of(constructor));
+      }
+      // Place the new class model in the cache
+      classModelCache.put(classModel.getName(), classModel);
+      return classModel;
+   }
+
+}

Copied: extensions/trunk/xsd/src/main/java/org/jboss/weld/xsd/Schema.java (from rev 3901, extensions/trunk/xsd/src/main/java/org/jboss/webbeans/xsd/Schema.java)
===================================================================
--- extensions/trunk/xsd/src/main/java/org/jboss/weld/xsd/Schema.java	                        (rev 0)
+++ extensions/trunk/xsd/src/main/java/org/jboss/weld/xsd/Schema.java	2009-10-08 14:22:10 UTC (rev 3903)
@@ -0,0 +1,294 @@
+/*
+ * 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.weld.xsd;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
+import java.util.ArrayList;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Set;
+
+import javax.annotation.processing.Filer;
+import javax.lang.model.element.PackageElement;
+import javax.lang.model.element.TypeElement;
+import javax.tools.StandardLocation;
+
+import org.dom4j.Attribute;
+import org.dom4j.Document;
+import org.dom4j.DocumentException;
+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;
+import org.dom4j.io.XMLWriter;
+import org.jboss.weld.xsd.model.ClassModel;
+import org.jboss.weld.xsd.model.TypedModel;
+
+/**
+ * A schema representation
+ * 
+ * @author Nicklas Karlsson
+ * 
+ */
+public class Schema
+{
+   // The default namespace of the 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
+   private Document document;
+   // The namespace handler
+   private NamespaceHandler namespaceHandler;
+   // The set of classes to update
+   private Set<ClassModel> classModels;
+   private PackageElement packageElement;
+
+   /**
+    * Creates a new package
+    * 
+    * @param packageName The name of the package
+    * @throws DocumentException 
+    */
+   public Schema(String packageName, PackageElement packageElement, Filer filer) throws DocumentException
+   {
+      this.packageName = packageName;
+      this.packageElement = packageElement;
+      classModels = new HashSet<ClassModel>();
+      namespaceHandler = new NamespaceHandler(packageName);
+      try
+      {
+         document = readDocument(filer);
+      } catch (IOException e) {
+         // schema not found, safe to proceed
+      }
+      if (document == null)
+      {
+         document = createDocument();
+      }
+      init();
+   }
+
+   /**
+    * Creates a new schema document
+    * 
+    * @return The document
+    */
+   private Document createDocument()
+   {
+      Document document = DocumentHelper.createDocument();
+      QName rootQName = DocumentFactory.getInstance().createQName("schema", "xs", "http://www.w3.org/2001/XMLSchema");
+      Element rootElement = DocumentFactory.getInstance().createElement(rootQName);
+      rootElement.addAttribute("elementFormDefault", "qualified");
+      rootElement.addAttribute("targetNamespace", "urn:java:" + packageName);
+      rootElement.addAttribute("elementFormDefault", "qualified");
+      rootElement.addAttribute("xsi:schemaLocation", "http://www.w3.org/2001/XMLSchema http://www.w3.org/2001/XMLSchema.xsd");
+      document.setRootElement(rootElement);
+      for (Namespace namespace : defaultNamespaces)
+      {
+         rootElement.add(namespace);
+      }
+      return document;
+   }
+
+   /**
+    * Static factory method
+    * 
+    * @param packageName The package name
+    * @param packageElement The package content representation
+    * @param filer The filer for resources
+    * 
+    * @return A new schema instance
+    * 
+    * @throws DocumentException If an existing schema could not be parsed
+    */
+   public static Schema of(String packageName, PackageElement packageElement, Filer filer) throws DocumentException
+   {
+      return new Schema(packageName, packageElement, filer);
+   }
+
+   /**
+    * Reads a schema document
+    * 
+    * @param filer The filer to be used for resource reading
+    * 
+    * @return The schema documetn
+    * 
+    * @throws IOException If the file could not be find or there was an error reading it
+    * @throws DocumentException If the read document could not be parsed
+    */
+   private Document readDocument(Filer filer) throws IOException, DocumentException
+   {
+      InputStream in = filer.getResource(StandardLocation.CLASS_OUTPUT, packageName, "schema.xsd").openInputStream();
+      return new SAXReader().read(in);
+   }
+
+   /**
+    * Writes a schema back to disk
+    * 
+    * @param filer The filer to be used for resource writing
+    * 
+    * @throws IOException If the file could not be written
+    */
+   public void write(Filer filer) throws IOException
+   {
+      OutputFormat format = OutputFormat.createPrettyPrint();
+      OutputStream out = filer.createResource(StandardLocation.CLASS_OUTPUT, packageName, "schema.xsd").openOutputStream();
+      XMLWriter writer = new XMLWriter(out, format);
+      writer.write(document);
+      writer.flush();
+      writer.close();
+   }
+
+   /**
+    * 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);
+      for (TypedModel reference : classModel.getTypeReferences())
+      {
+         namespaceHandler.addPackage(reference.getTypePackage());
+      }
+   }
+
+   /**
+    * Cleans out namespaces and XSD for files that are no longer present in the
+    * package
+    */
+   private void init()
+   {
+      // 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");
+         if (!isClassInPackage(FQN))
+         {
+            ((Element) xsdClass).detach();
+         }
+      }
+
+      // Collect namespaces that are references in the document
+      Set<Namespace> referencedNamespaces = new HashSet<Namespace>(defaultNamespaces);
+      for (Object attribute : document.getRootElement().selectNodes("//@type"))
+      {
+         String ref = ((Attribute) attribute).getValue();
+         int colon = ref.indexOf(":");
+         String prefix = colon < 0 ? "" : ref.substring(0, colon);
+         referencedNamespaces.add(document.getRootElement().getNamespaceForPrefix(prefix));
+      }
+
+      // Register the namespaces with the namespace handler if they are present, otherwise
+      // remove them from the document
+      for (Object item : document.getRootElement().additionalNamespaces())
+      {
+         Namespace namespace = (Namespace) item;
+         if (referencedNamespaces.contains(namespace))
+         {
+            namespaceHandler.addNamespace(namespace);
+         }
+         else
+         {
+            document.getRootElement().remove(namespace);
+         }
+      }
+   }
+
+   /**
+    * 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())
+      {
+         TypeElement typeElement = (TypeElement) classElement;
+         if (typeElement.getQualifiedName().toString().equals(FQN))
+         {
+            return true;
+         }
+      }
+      return false;
+   }
+
+   /**
+    * Rebuilds the schema document
+    * 
+    * @param packageElement The package abstraction
+    * @return the schema for fluent interface
+    */
+   public Schema rebuild()
+   {
+      // 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);
+         }
+      }
+
+      // Remove old version of class xsd (if present) and add the fresh version
+      for (ClassModel classModel : classModels)
+      {
+         for (Object previousClass : document.selectNodes("/xs:schema/xs:element[@name=\"" + classModel.getSimpleName() + "\"]"))
+         {
+            ((Element) previousClass).detach();
+         }
+         document.getRootElement().add(classModel.toXSD(namespaceHandler));
+      }
+      return this;
+   }
+
+   @Override
+   public String toString()
+   {
+      StringBuilder buffer = new StringBuilder();
+      buffer.append("Package: " + packageName + "\n");
+      buffer.append("Used namespaces\n");
+      for (Namespace namespace : namespaceHandler.getNamespaces())
+      {
+         buffer.append("  " + namespace + "\n");
+      }
+      buffer.append("Contained classes:\n");
+      for (ClassModel classModel : classModels)
+      {
+         buffer.append(classModel + "\n");
+      }
+      return buffer.toString();
+   }
+
+}

Copied: extensions/trunk/xsd/src/main/java/org/jboss/weld/xsd/model (from rev 3901, extensions/trunk/xsd/src/main/java/org/jboss/webbeans/xsd/model)

Modified: extensions/trunk/xsd/src/main/java/org/jboss/weld/xsd/model/ClassModel.java
===================================================================
--- extensions/trunk/xsd/src/main/java/org/jboss/webbeans/xsd/model/ClassModel.java	2009-10-08 10:48:12 UTC (rev 3901)
+++ extensions/trunk/xsd/src/main/java/org/jboss/weld/xsd/model/ClassModel.java	2009-10-08 14:22:10 UTC (rev 3903)
@@ -15,7 +15,7 @@
  * limitations under the License.
  */
 
-package org.jboss.webbeans.xsd.model;
+package org.jboss.weld.xsd.model;
 
 import java.util.ArrayList;
 import java.util.HashSet;
@@ -26,7 +26,9 @@
 import javax.lang.model.element.PackageElement;
 import javax.lang.model.element.TypeElement;
 
-import org.jboss.webbeans.xsd.NamespaceHandler;
+import org.dom4j.DocumentFactory;
+import org.dom4j.Element;
+import org.jboss.weld.xsd.NamespaceHandler;
 
 /**
  * A model of a class

Modified: extensions/trunk/xsd/src/main/java/org/jboss/weld/xsd/model/ConstructorModel.java
===================================================================
--- extensions/trunk/xsd/src/main/java/org/jboss/webbeans/xsd/model/ConstructorModel.java	2009-10-08 10:48:12 UTC (rev 3901)
+++ extensions/trunk/xsd/src/main/java/org/jboss/weld/xsd/model/ConstructorModel.java	2009-10-08 14:22:10 UTC (rev 3903)
@@ -15,11 +15,13 @@
  * limitations under the License.
  */
 
-package org.jboss.webbeans.xsd.model;
+package org.jboss.weld.xsd.model;
 
 import javax.lang.model.element.ExecutableElement;
 
-import org.jboss.webbeans.xsd.NamespaceHandler;
+import org.dom4j.DocumentFactory;
+import org.dom4j.Element;
+import org.jboss.weld.xsd.NamespaceHandler;
 
 /**
  * The model of a method

Modified: extensions/trunk/xsd/src/main/java/org/jboss/weld/xsd/model/FieldModel.java
===================================================================
--- extensions/trunk/xsd/src/main/java/org/jboss/webbeans/xsd/model/FieldModel.java	2009-10-08 10:48:12 UTC (rev 3901)
+++ extensions/trunk/xsd/src/main/java/org/jboss/weld/xsd/model/FieldModel.java	2009-10-08 14:22:10 UTC (rev 3903)
@@ -15,11 +15,11 @@
  * limitations under the License.
  */
 
-package org.jboss.webbeans.xsd.model;
+package org.jboss.weld.xsd.model;
 
 import javax.lang.model.element.Element;
 
-import org.jboss.webbeans.xsd.NamespaceHandler;
+import org.jboss.weld.xsd.NamespaceHandler;
 
 /**
  * The model of a field

Modified: extensions/trunk/xsd/src/main/java/org/jboss/weld/xsd/model/MethodModel.java
===================================================================
--- extensions/trunk/xsd/src/main/java/org/jboss/webbeans/xsd/model/MethodModel.java	2009-10-08 10:48:12 UTC (rev 3901)
+++ extensions/trunk/xsd/src/main/java/org/jboss/weld/xsd/model/MethodModel.java	2009-10-08 14:22:10 UTC (rev 3903)
@@ -15,7 +15,7 @@
  * limitations under the License.
  */
 
-package org.jboss.webbeans.xsd.model;
+package org.jboss.weld.xsd.model;
 
 import java.util.ArrayList;
 import java.util.HashMap;
@@ -25,7 +25,8 @@
 import javax.lang.model.element.ExecutableElement;
 import javax.lang.model.element.VariableElement;
 
-import org.jboss.webbeans.xsd.NamespaceHandler;
+import org.dom4j.Element;
+import org.jboss.weld.xsd.NamespaceHandler;
 
 /**
  * The model of a method

Modified: extensions/trunk/xsd/src/main/java/org/jboss/weld/xsd/model/NamedModel.java
===================================================================
--- extensions/trunk/xsd/src/main/java/org/jboss/webbeans/xsd/model/NamedModel.java	2009-10-08 10:48:12 UTC (rev 3901)
+++ extensions/trunk/xsd/src/main/java/org/jboss/weld/xsd/model/NamedModel.java	2009-10-08 14:22:10 UTC (rev 3903)
@@ -15,9 +15,11 @@
  * limitations under the License.
  */
 
-package org.jboss.webbeans.xsd.model;
+package org.jboss.weld.xsd.model;
 
-import org.jboss.webbeans.xsd.NamespaceHandler;
+import org.dom4j.DocumentFactory;
+import org.dom4j.Element;
+import org.jboss.weld.xsd.NamespaceHandler;
 
 /**
  * A superclass for named models

Modified: extensions/trunk/xsd/src/main/java/org/jboss/weld/xsd/model/TypedModel.java
===================================================================
--- extensions/trunk/xsd/src/main/java/org/jboss/webbeans/xsd/model/TypedModel.java	2009-10-08 10:48:12 UTC (rev 3901)
+++ extensions/trunk/xsd/src/main/java/org/jboss/weld/xsd/model/TypedModel.java	2009-10-08 14:22:10 UTC (rev 3903)
@@ -15,9 +15,11 @@
  * limitations under the License.
  */
 
-package org.jboss.webbeans.xsd.model;
+package org.jboss.weld.xsd.model;
 
-import org.jboss.webbeans.xsd.NamespaceHandler;
+import org.dom4j.DocumentFactory;
+import org.dom4j.Element;
+import org.jboss.weld.xsd.NamespaceHandler;
 
 /**
  * The model of a typed member

Copied: extensions/trunk/xsd/src/main/java/org/jboss/weld/xsd/sample-schema.xsd (from rev 3901, extensions/trunk/xsd/src/main/java/org/jboss/webbeans/xsd/sample-schema.xsd)
===================================================================
--- extensions/trunk/xsd/src/main/java/org/jboss/weld/xsd/sample-schema.xsd	                        (rev 0)
+++ extensions/trunk/xsd/src/main/java/org/jboss/weld/xsd/sample-schema.xsd	2009-10-08 14:22:10 UTC (rev 3903)
@@ -0,0 +1,28 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" 
+           elementFormDefault="qualified"
+           targetNamespace="urn:java:com.acme.foo"
+           xmlns:foo="urn:java:com.acme.foo"
+           xmlns:ee="urn:java:ee"
+           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+           xsi:schemaLocation="http://www.w3.org/2001/XMLSchema http://www.w3.org/2001/XMLSchema.xsd"
+           >
+
+  <xs:element name="Foo">
+     <xs:complexType>
+        <xs:choice>
+           <!-- Options to allow people to declare this class a binding type or stereotype - TODO only if an annotation -->
+           <xs:element ref="ee:BindingType"/>
+           <xs:element ref="ee:StereotypType"/>
+           <!-- Somehow import all binding types known etc. to give people that option -->
+           <xs:any /> <!-- Can't guess all types people want to put here -->
+        </xs:choice>
+     </xs:complexType>
+  </xs:element>
+  
+  <xs:element name="Bar">
+  
+  </xs:element>
+
+  
+</xs:schema>
\ No newline at end of file

Copied: extensions/trunk/xsd/src/main/java/org/jboss/weld/xsd/test/Bar.java (from rev 3901, extensions/trunk/xsd/src/main/java/org/jboss/webbeans/xsd/test/Bar.java)
===================================================================
--- extensions/trunk/xsd/src/main/java/org/jboss/weld/xsd/test/Bar.java	                        (rev 0)
+++ extensions/trunk/xsd/src/main/java/org/jboss/weld/xsd/test/Bar.java	2009-10-08 14:22:10 UTC (rev 3903)
@@ -0,0 +1,9 @@
+package org.jboss.weld.xsd.test;
+
+public class Bar
+{
+   public org.jboss.weld.xsd.test.test.Foo pong(org.jboss.weld.xsd.test.test.test.Foo foo)
+   {
+      return null;
+   }
+}

Copied: extensions/trunk/xsd/src/main/java/org/jboss/weld/xsd/test/Foo.java (from rev 3901, extensions/trunk/xsd/src/main/java/org/jboss/webbeans/xsd/test/Foo.java)
===================================================================
--- extensions/trunk/xsd/src/main/java/org/jboss/weld/xsd/test/Foo.java	                        (rev 0)
+++ extensions/trunk/xsd/src/main/java/org/jboss/weld/xsd/test/Foo.java	2009-10-08 14:22:10 UTC (rev 3903)
@@ -0,0 +1,16 @@
+package org.jboss.weld.xsd.test;
+
+
+public class Foo extends Bar
+{
+   public String foo;
+   public int poo;
+   public Bar bar;
+   public org.jboss.weld.xsd.test.test.Foo foo2;
+   public org.jboss.weld.xsd.test.test.test.Foo foo3;
+
+   public void testy(org.jboss.weld.xsd.test.test.Foo a, org.jboss.weld.xsd.test.test.test.Foo b) {
+      
+   }
+   
+}

Copied: extensions/trunk/xsd/src/main/java/org/jboss/weld/xsd/test/Test.java (from rev 3901, extensions/trunk/xsd/src/main/java/org/jboss/webbeans/xsd/test/Test.java)
===================================================================
--- extensions/trunk/xsd/src/main/java/org/jboss/weld/xsd/test/Test.java	                        (rev 0)
+++ extensions/trunk/xsd/src/main/java/org/jboss/weld/xsd/test/Test.java	2009-10-08 14:22:10 UTC (rev 3903)
@@ -0,0 +1,30 @@
+package org.jboss.weld.xsd.test;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import javax.annotation.processing.AbstractProcessor;
+import javax.tools.JavaCompiler;
+import javax.tools.JavaFileObject;
+import javax.tools.StandardJavaFileManager;
+import javax.tools.ToolProvider;
+import javax.tools.JavaCompiler.CompilationTask;
+
+import org.jboss.weld.xsd.PackageSchemaGenerator;
+
+public class Test
+{
+
+   public static void main(String[] args)
+   {
+      JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
+      StandardJavaFileManager fileManager = compiler.getStandardFileManager(null, null, null);
+      Iterable<? extends JavaFileObject> compilationUnits = fileManager.getJavaFileObjects(args);
+      CompilationTask task = compiler.getTask(null, fileManager, null, null, null, compilationUnits);
+      List<AbstractProcessor> processors = new ArrayList<AbstractProcessor>();
+      processors.add(new PackageSchemaGenerator());
+      task.setProcessors(processors);
+      task.call();
+   }
+
+}

Copied: extensions/trunk/xsd/src/main/java/org/jboss/weld/xsd/test/test/Foo.java (from rev 3901, extensions/trunk/xsd/src/main/java/org/jboss/webbeans/xsd/test/test/Foo.java)
===================================================================
--- extensions/trunk/xsd/src/main/java/org/jboss/weld/xsd/test/test/Foo.java	                        (rev 0)
+++ extensions/trunk/xsd/src/main/java/org/jboss/weld/xsd/test/test/Foo.java	2009-10-08 14:22:10 UTC (rev 3903)
@@ -0,0 +1,6 @@
+package org.jboss.weld.xsd.test.test;
+
+public class Foo
+{
+
+}

Copied: extensions/trunk/xsd/src/main/java/org/jboss/weld/xsd/test/test/test (from rev 3901, extensions/trunk/xsd/src/main/java/org/jboss/webbeans/xsd/test/test/test)

Modified: extensions/trunk/xsd/src/main/java/org/jboss/weld/xsd/test/test/test/Foo.java
===================================================================
--- extensions/trunk/xsd/src/main/java/org/jboss/webbeans/xsd/test/test/test/Foo.java	2009-10-08 10:48:12 UTC (rev 3901)
+++ extensions/trunk/xsd/src/main/java/org/jboss/weld/xsd/test/test/test/Foo.java	2009-10-08 14:22:10 UTC (rev 3903)
@@ -1,4 +1,4 @@
-package org.jboss.webbeans.xsd.test.test.test;
+package org.jboss.weld.xsd.test.test.test;
 
 public class Foo
 {



More information about the weld-commits mailing list