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

webbeans-commits at lists.jboss.org webbeans-commits at lists.jboss.org
Mon Mar 23 03:56:33 EDT 2009


Author: nickarls
Date: 2009-03-23 03:56:32 -0400 (Mon, 23 Mar 2009)
New Revision: 2139

Removed:
   extensions/trunk/xsd/src/main/java/org/jboss/webbeans/xsd/helpers/XSDHelper.java
Modified:
   extensions/trunk/xsd/src/main/java/org/jboss/webbeans/xsd/PackageSchemaGenerator.java
   extensions/trunk/xsd/src/main/java/org/jboss/webbeans/xsd/Schema.java
Log:
refactor, ditch XSDHelper

Modified: extensions/trunk/xsd/src/main/java/org/jboss/webbeans/xsd/PackageSchemaGenerator.java
===================================================================
--- extensions/trunk/xsd/src/main/java/org/jboss/webbeans/xsd/PackageSchemaGenerator.java	2009-03-23 07:09:01 UTC (rev 2138)
+++ extensions/trunk/xsd/src/main/java/org/jboss/webbeans/xsd/PackageSchemaGenerator.java	2009-03-23 07:56:32 UTC (rev 2139)
@@ -17,8 +17,9 @@
 
 package org.jboss.webbeans.xsd;
 
-import java.util.ArrayList;
-import java.util.List;
+import java.io.IOException;
+import java.util.HashMap;
+import java.util.Map;
 import java.util.Set;
 
 import javax.annotation.processing.AbstractProcessor;
@@ -34,8 +35,8 @@
 import javax.lang.model.type.TypeKind;
 import javax.lang.model.util.ElementFilter;
 
+import org.dom4j.DocumentException;
 import org.jboss.webbeans.xsd.helpers.DataSetter;
-import org.jboss.webbeans.xsd.helpers.XSDHelper;
 import org.jboss.webbeans.xsd.model.ClassModel;
 
 /**
@@ -49,41 +50,66 @@
 @SupportedAnnotationTypes("*")
 public class PackageSchemaGenerator extends AbstractProcessor
 {
-   // A helper for the XSD operations
-   XSDHelper helper;
+   private Map<String, ClassModel> classModelCache;
+   private Map<String, Schema> schemas;
 
    @Override
    public synchronized void init(ProcessingEnvironment processingEnvironment)
    {
       super.init(processingEnvironment);
-      helper = new XSDHelper(processingEnvironment);
+      classModelCache = new HashMap<String, ClassModel>();
+      schemas = new HashMap<String, Schema>();
    }
 
    @Override
    public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnvironment)
    {
-      List<ClassModel> workingSet = new ArrayList<ClassModel>();
-
-      // Iterates over the classes compiled, creates a model of the classes and
-      // add them to a working set
       for (Element element : roundEnvironment.getRootElements())
       {
          if (ElementKind.CLASS.equals(element.getKind()) || ElementKind.ANNOTATION_TYPE.equals(element.getKind()))
          {
             ClassModel classModel = inspectClass(element);
-            workingSet.add(classModel);
+            try
+            {
+               addClassToSchema(classModel);
+            }
+            catch (DocumentException e)
+            {
+               // TODO: real logging
+               System.out.println("Could not read or create schema for " + classModel.getPackage());
+            }
          }
       }
       if (!roundEnvironment.processingOver())
       {
-         // Update the package XSDs for the files changed
-         helper.updateSchemas(workingSet);
-         // And flush the changes to disk
-         helper.writeSchemas();
+         for (Schema schema : schemas.values())
+         {
+            try
+            {
+               schema.rebuild().write(processingEnv.getFiler());
+            }
+            catch (IOException e)
+            {
+               // TODO: real logging
+               System.out.println("Could not write schema.xsd for " + schema);
+            }
+         }
       }
       return false;
    }
 
+   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);
+   }
+
    /**
     * Creates a class model from a class element
     * 
@@ -103,7 +129,7 @@
 
       // Gets the parent from the cache. We know it's there since we has scanned
       // the hierarchy already
-      classModel.setParent(helper.getCachedClassModel(typeElement.getSuperclass().toString()));
+      classModel.setParent(classModelCache.get(typeElement.getSuperclass().toString()));
       // Filter out the fields and populate the model
       for (Element field : ElementFilter.fieldsIn(element.getEnclosedElements()))
       {
@@ -120,7 +146,7 @@
          DataSetter.populateConstructorModel(classModel, constructor);
       }
       // Place the new class model in the cache
-      helper.cacheClassModel(classModel);
+      classModelCache.put(classModel.getName(), classModel);
       return classModel;
    }
 

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 07:09:01 UTC (rev 2138)
+++ extensions/trunk/xsd/src/main/java/org/jboss/webbeans/xsd/Schema.java	2009-03-23 07:56:32 UTC (rev 2139)
@@ -17,18 +17,30 @@
 
 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 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.webbeans.xsd.model.ClassModel;
 import org.jboss.webbeans.xsd.model.TypedModel;
 
@@ -62,25 +74,65 @@
     * Creates a new package
     * 
     * @param packageName The name of the package
+    * @throws DocumentException 
     */
-   public Schema(String packageName, PackageElement packageElement)
+   public Schema(String packageName, PackageElement packageElement, Filer filer) throws DocumentException
    {
-      classModels = new HashSet<ClassModel>();
       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();
    }
 
-   /**
-    * Gets the name of the package
-    * 
-    * @return The package name
-    */
-   public String getPackageName()
+   private Document createDocument()
    {
-      return packageName;
+      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;
    }
 
+   public static Schema of(String packageName, PackageElement packageElement, Filer filer) throws DocumentException
+   {
+      return new Schema(packageName, packageElement, filer);
+   }
+
+   private Document readDocument(Filer filer) throws IOException, DocumentException
+   {
+      InputStream in = filer.getResource(StandardLocation.CLASS_OUTPUT, packageName, "schema.xsd").openInputStream();
+      return new SAXReader().read(in);
+   }
+
+   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
@@ -97,27 +149,6 @@
    }
 
    /**
-    * 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;
-      init();
-   }
-
-   /**
     * Cleans out namespaces and XSD for files that are no longer present in the
     * package
     */

Deleted: 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 07:09:01 UTC (rev 2138)
+++ extensions/trunk/xsd/src/main/java/org/jboss/webbeans/xsd/helpers/XSDHelper.java	2009-03-23 07:56:32 UTC (rev 2139)
@@ -1,221 +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.helpers;
-
-import java.io.IOException;
-import java.io.InputStream;
-import java.io.OutputStream;
-import java.util.HashMap;
-import java.util.List;
-import java.util.Map;
-
-import javax.annotation.processing.ProcessingEnvironment;
-import javax.lang.model.element.PackageElement;
-import javax.tools.StandardLocation;
-
-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.webbeans.xsd.Schema;
-import org.jboss.webbeans.xsd.model.ClassModel;
-
-/**
- * Helper for XSD related operations
- * 
- * @author Nicklas Karlsson
- * 
- */
-public class XSDHelper
-{
-   // The annotation processing environment
-   private ProcessingEnvironment processingEnvironment;
-   // The cache of already processed classes
-   private Map<String, ClassModel> classModelCache = new HashMap<String, ClassModel>();
-   // The XSD documents of the affected packages
-   private Map<String, Schema> schemaMap = new HashMap<String, Schema>();
-
-   /**
-    * Creates a new helper
-    * 
-    * @param filer The filer of the processing environment
-    */
-   public XSDHelper(ProcessingEnvironment processingEnvironment)
-   {
-      this.processingEnvironment = processingEnvironment;
-   }
-
-   /**
-    * Reads package info
-    * 
-    * @param packageName The package name
-    * @return The package info of the package
-    * @throws DocumentException If the schema could not be parsed
-    * @throws IOException If the schema could not be read
-    */
-   private Schema createSchema(String packageName, PackageElement packageElement) throws DocumentException, IOException
-   {
-      Schema schema = new Schema(packageName, packageElement);
-      Document document = readSchema(packageName);
-      if (document == null)
-      {
-         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");
-         for (Namespace namespace : Schema.defaultNamespaces)
-         {
-            rootElement.add(namespace);
-         }
-         document.setRootElement(rootElement);
-      }
-      schema.setDocument(document);
-      return schema;
-   }
-
-   /**
-    * Reads a schema for a package
-    * 
-    * @param packageName The package name
-    * @return The schema document
-    * @throws DocumentException If the document could not be parsed
-    * @throws IOException If the document could not be read
-    */
-   private Document readSchema(String packageName) throws DocumentException, IOException
-   {
-      InputStream in = null;
-      try
-      {
-         in = processingEnvironment.getFiler().getResource(StandardLocation.CLASS_OUTPUT, packageName, "schema.xsd").openInputStream();
-         return new SAXReader().read(in);
-      }
-      catch (IOException e)
-      {
-         return null;
-      }
-      finally
-      {
-         if (in != null)
-         {
-            in.close();
-         }
-      }
-   }
-
-   private void writeSchema(Schema schema) throws IOException
-   {
-      OutputStream out = null;
-      try
-      {
-         OutputFormat format = OutputFormat.createPrettyPrint();
-         out = processingEnvironment.getFiler().createResource(StandardLocation.CLASS_OUTPUT, schema.getPackageName(), "schema.xsd").openOutputStream();
-         XMLWriter writer = new XMLWriter(out, format);
-         writer.write(schema.getDocument());
-         writer.flush();
-         writer.close();
-      }
-      finally
-      {
-         if (out != null)
-         {
-            out.close();
-         }
-      }
-   }
-
-   /**
-    * Updates the schemas for the affected packages
-    * 
-    * @param classModels The list of class models in the batch
-    */
-   public void updateSchemas(List<ClassModel> classModels)
-   {
-      for (ClassModel classModel : classModels)
-      {
-         String packageName = classModel.getPackage();
-         Schema schema = schemaMap.get(packageName);
-         if (schema == null)
-         {
-            try
-            {
-               schema = createSchema(packageName, classModel.getPackageElement());
-            }
-            catch (DocumentException e)
-            {
-               throw new RuntimeException("Could not parse schema for package " + packageName, e);
-            }
-            catch (IOException e)
-            {
-               throw new RuntimeException("Could not read schema for package " + packageName, e);
-            }
-            schemaMap.put(packageName, schema);
-         }
-         schema.addClass(classModel);
-      }
-   }
-
-   /**
-    * Writes the schemas back to disk
-    * 
-    * @param packageModels
-    */
-   public void writeSchemas()
-   {
-      for (Schema schema : schemaMap.values())
-      {
-         schema.rebuild();
-         try
-         {
-            writeSchema(schema);
-         }
-         catch (IOException e)
-         {
-            throw new RuntimeException("Could not write schema for " + schema.getPackageName(), e);
-         }
-      }
-   }
-
-   /**
-    * Gets a cached class model
-    * 
-    * @param FQN The FQN of the class
-    * @return The class model (or null if not cached)
-    */
-   public ClassModel getCachedClassModel(String FQN)
-   {
-      return classModelCache.get(FQN);
-   }
-
-   /**
-    * Puts a class model in the cache
-    * 
-    * @param classModel The class model
-    */
-   public void cacheClassModel(ClassModel classModel)
-   {
-      classModelCache.put(classModel.getName(), classModel);
-   }
-}




More information about the weld-commits mailing list