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

webbeans-commits at lists.jboss.org webbeans-commits at lists.jboss.org
Fri Mar 13 09:53:27 EDT 2009


Author: nickarls
Date: 2009-03-13 09:53:27 -0400 (Fri, 13 Mar 2009)
New Revision: 1970

Added:
   ri/trunk/impl/src/main/java/org/jboss/webbeans/xsd/helpers/XSDHelper.java
Modified:
   ri/trunk/impl/src/main/java/org/jboss/webbeans/xsd/PackageSchemaGenerator.java
Log:
some xsd refactorings

Modified: ri/trunk/impl/src/main/java/org/jboss/webbeans/xsd/PackageSchemaGenerator.java
===================================================================
--- ri/trunk/impl/src/main/java/org/jboss/webbeans/xsd/PackageSchemaGenerator.java	2009-03-13 13:51:00 UTC (rev 1969)
+++ ri/trunk/impl/src/main/java/org/jboss/webbeans/xsd/PackageSchemaGenerator.java	2009-03-13 13:53:27 UTC (rev 1970)
@@ -1,16 +1,11 @@
 package org.jboss.webbeans.xsd;
 
-import java.io.IOException;
-import java.io.InputStream;
-import java.io.OutputStream;
 import java.util.ArrayList;
-import java.util.HashMap;
 import java.util.List;
-import java.util.Map;
 import java.util.Set;
-import java.util.Map.Entry;
 
 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;
@@ -20,29 +15,25 @@
 import javax.lang.model.type.DeclaredType;
 import javax.lang.model.type.TypeKind;
 import javax.lang.model.util.ElementFilter;
-import javax.tools.StandardLocation;
 
-import org.dom4j.Document;
-import org.dom4j.DocumentException;
-import org.dom4j.DocumentHelper;
-import org.dom4j.Namespace;
-import org.dom4j.Node;
-import org.dom4j.QName;
-import org.dom4j.io.OutputFormat;
-import org.dom4j.io.SAXReader;
-import org.dom4j.io.XMLWriter;
 import org.jboss.webbeans.xsd.helpers.DataSetter;
+import org.jboss.webbeans.xsd.helpers.XSDHelper;
 import org.jboss.webbeans.xsd.model.ClassModel;
-import org.xml.sax.SAXException;
 
 @SupportedSourceVersion(SourceVersion.RELEASE_6)
 @SupportedAnnotationTypes("*")
 public class PackageSchemaGenerator extends AbstractProcessor
 {
-   private Map<String, ClassModel> classModelCache = new HashMap<String, ClassModel>();
-   private Map<String, Document> packageXSDs = new HashMap<String, Document>();
+   XSDHelper helper;
 
    @Override
+   public synchronized void init(ProcessingEnvironment processingEnv)
+   {
+      super.init(processingEnv);
+      helper = new XSDHelper(processingEnv);
+   }
+
+   @Override
    public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv)
    {
       List<ClassModel> workingSet = new ArrayList<ClassModel>();
@@ -52,132 +43,12 @@
       }
       if (!roundEnv.processingOver())
       {
-         updatePackageXSDs(workingSet);
-         flushPackageXSDs();
+         helper.updatePackageXSDs(workingSet);
+         helper.flushPackageXSDs();
       }
       return false;
    }
 
-   private Document getPackageXSD(String packageName)
-   {
-      Document packageXSD = packageXSDs.get(packageName);
-      if (packageXSD == null)
-      {
-         try
-         {
-            packageXSD = readPackageXSD(packageName);
-         }
-         catch (IOException e)
-         {
-            throw new RuntimeException("Could not read schema for package " + packageName);
-         }
-         catch (DocumentException e)
-         {
-            throw new RuntimeException("Could not parse schema for package " + packageName);
-         }
-         if (packageXSD == null)
-         {
-            packageXSD = createPackageXSD(packageName);
-         }
-         packageXSDs.put(packageName, packageXSD);
-      }
-      return packageXSD;
-   }
-
-   private Document readPackageXSD(String packageName) throws IOException, DocumentException
-   {
-      InputStream in = null;
-      try
-      {
-         in = processingEnv.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 flushPackageXSDs()
-   {
-      for (Entry<String, Document> entry : packageXSDs.entrySet())
-      {
-         try
-         {
-            writePackageXSD(entry.getKey(), entry.getValue());
-         }
-         catch (IOException e)
-         {
-            throw new RuntimeException("Could not flush XSD for " + entry.getKey());
-         }
-         catch (SAXException e)
-         {
-            throw new RuntimeException("Could not parse XSD when flushing for " + entry.getKey());
-         }
-      }
-   }
-
-   private void writePackageXSD(String packageName, Document packageXSD) throws IOException, SAXException
-   {
-      OutputStream out = null;
-      try
-      {
-         OutputFormat format = OutputFormat.createPrettyPrint();
-         out = processingEnv.getFiler().createResource(StandardLocation.CLASS_OUTPUT, packageName, "schema.xsd").openOutputStream();
-         XMLWriter writer = new XMLWriter(out, format);
-         writer.write(packageXSD);
-         writer.flush();
-         writer.close();
-      }
-      finally
-      {
-         if (out != null)
-         {
-            out.close();
-         }
-      }
-   }
-
-   private String getShortName(String packageName)
-   {
-      int lastDot = packageName.lastIndexOf(".");
-      return lastDot < 0 ? packageName : packageName.substring(lastDot + 1);
-   }
-
-   private Document createPackageXSD(String packageName)
-   {
-      Document packageXSD = DocumentHelper.createDocument();
-
-      packageXSD.addElement(new QName("Package", new Namespace(getShortName(packageName), "urn:java:" + packageName)));
-      return packageXSD;
-   }
-
-   private void updatePackageXSDs(List<ClassModel> classModels)
-   {
-      for (ClassModel classModel : classModels)
-      {
-         Document packageXSD = getPackageXSD(classModel.getPackage());
-         updateClass(packageXSD, classModel);
-      }
-   }
-
-   private void updateClass(Document packageXSD, ClassModel classModel)
-   {
-      Node oldClassModel = packageXSD.selectSingleNode("//" + classModel.getSimpleName());
-      if (oldClassModel != null)
-      {
-         packageXSD.getRootElement().remove(oldClassModel);
-      }
-      packageXSD.getRootElement().addElement(classModel.getSimpleName());
-   }
-
    private ClassModel inspectClass(Element element)
    {
       TypeElement typeElement = (TypeElement) element;
@@ -188,7 +59,7 @@
          inspectClass(((DeclaredType) typeElement.getSuperclass()).asElement());
       }
 
-      ClassModel parent = classModelCache.get(typeElement.getSuperclass().toString());
+      ClassModel parent = helper.getCachedClassModel(typeElement.getSuperclass().toString());
       DataSetter.populateClassModel(classModel, element, parent);
       for (Element field : ElementFilter.fieldsIn(element.getEnclosedElements()))
       {
@@ -202,8 +73,9 @@
       {
          DataSetter.populateMethodModel(classModel, constructor);
       }
-      classModelCache.put(classModel.getName(), classModel);
+      helper.setCachedClassModel(classModel.getName(), classModel);
       return classModel;
    }
 
+
 }

Added: ri/trunk/impl/src/main/java/org/jboss/webbeans/xsd/helpers/XSDHelper.java
===================================================================
--- ri/trunk/impl/src/main/java/org/jboss/webbeans/xsd/helpers/XSDHelper.java	                        (rev 0)
+++ ri/trunk/impl/src/main/java/org/jboss/webbeans/xsd/helpers/XSDHelper.java	2009-03-13 13:53:27 UTC (rev 1970)
@@ -0,0 +1,166 @@
+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 java.util.Map.Entry;
+
+import javax.annotation.processing.ProcessingEnvironment;
+import javax.tools.StandardLocation;
+
+import org.dom4j.Document;
+import org.dom4j.DocumentException;
+import org.dom4j.DocumentHelper;
+import org.dom4j.Namespace;
+import org.dom4j.Node;
+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.xml.sax.SAXException;
+
+public class XSDHelper
+{
+   private ProcessingEnvironment processingEnvironment;
+   private Map<String, ClassModel> classModelCache = new HashMap<String, ClassModel>();
+   private Map<String, Document> packageXSDs = new HashMap<String, Document>();
+
+   public XSDHelper(ProcessingEnvironment processingEnvironment)
+   {
+      this.processingEnvironment = processingEnvironment;
+   }
+
+   public void updatePackageXSDs(List<ClassModel> classModels)
+   {
+      for (ClassModel classModel : classModels)
+      {
+         Document packageXSD = getPackageXSD(classModel.getPackage());
+         updateClass(packageXSD, classModel);
+      }
+   }
+
+   public void flushPackageXSDs()
+   {
+      for (Entry<String, Document> entry : packageXSDs.entrySet())
+      {
+         try
+         {
+            writePackageXSD(entry.getKey(), entry.getValue());
+         }
+         catch (IOException e)
+         {
+            throw new RuntimeException("Could not flush XSD for " + entry.getKey());
+         }
+         catch (SAXException e)
+         {
+            throw new RuntimeException("Could not parse XSD when flushing for " + entry.getKey());
+         }
+      }
+   }
+
+   private Document getPackageXSD(String packageName)
+   {
+      Document packageXSD = packageXSDs.get(packageName);
+      if (packageXSD == null)
+      {
+         try
+         {
+            packageXSD = readPackageXSD(packageName);
+         }
+         catch (IOException e)
+         {
+            throw new RuntimeException("Could not read schema for package " + packageName);
+         }
+         catch (DocumentException e)
+         {
+            throw new RuntimeException("Could not parse schema for package " + packageName);
+         }
+         if (packageXSD == null)
+         {
+            packageXSD = createPackageXSD(packageName);
+         }
+         packageXSDs.put(packageName, packageXSD);
+      }
+      return packageXSD;
+   }
+
+   private void updateClass(Document packageXSD, ClassModel classModel)
+   {
+      Node oldClassModel = packageXSD.selectSingleNode("//" + classModel.getSimpleName());
+      if (oldClassModel != null)
+      {
+         packageXSD.getRootElement().remove(oldClassModel);
+      }
+      packageXSD.getRootElement().addElement(classModel.getSimpleName());
+   }
+
+   private Document readPackageXSD(String packageName) throws IOException, DocumentException
+   {
+      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 Document createPackageXSD(String packageName)
+   {
+      Document packageXSD = DocumentHelper.createDocument();
+
+      packageXSD.addElement(new QName("Package", new Namespace(getShortName(packageName), "urn:java:" + packageName)));
+      return packageXSD;
+   }
+
+   private String getShortName(String packageName)
+   {
+      int lastDot = packageName.lastIndexOf(".");
+      return lastDot < 0 ? packageName : packageName.substring(lastDot + 1);
+   }
+
+   private void writePackageXSD(String packageName, Document packageXSD) throws IOException, SAXException
+   {
+      OutputStream out = null;
+      try
+      {
+         OutputFormat format = OutputFormat.createPrettyPrint();
+         out = processingEnvironment.getFiler().createResource(StandardLocation.CLASS_OUTPUT, packageName, "schema.xsd").openOutputStream();
+         XMLWriter writer = new XMLWriter(out, format);
+         writer.write(packageXSD);
+         writer.flush();
+         writer.close();
+      }
+      finally
+      {
+         if (out != null)
+         {
+            out.close();
+         }
+      }
+   }
+
+   public ClassModel getCachedClassModel(String packageName)
+   {
+      return classModelCache.get(packageName);
+   }
+
+   public void setCachedClassModel(String packageName, ClassModel classModel)
+   {
+      classModelCache.put(packageName, classModel);
+   }
+}




More information about the weld-commits mailing list