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

webbeans-commits at lists.jboss.org webbeans-commits at lists.jboss.org
Tue Mar 17 03:38:36 EDT 2009


Author: nickarls
Date: 2009-03-17 03:38:35 -0400 (Tue, 17 Mar 2009)
New Revision: 2061

Modified:
   extensions/trunk/xsd/src/main/java/org/jboss/webbeans/xsd/PackageInfo.java
   extensions/trunk/xsd/src/main/java/org/jboss/webbeans/xsd/helpers/NamespaceGenerator.java
   extensions/trunk/xsd/src/main/java/org/jboss/webbeans/xsd/helpers/XSDHelper.java
   extensions/trunk/xsd/src/main/java/org/jboss/webbeans/xsd/model/ClassModel.java
   extensions/trunk/xsd/src/main/java/org/jboss/webbeans/xsd/model/TypedModel.java
Log:
xsd namespace stuff

Modified: extensions/trunk/xsd/src/main/java/org/jboss/webbeans/xsd/PackageInfo.java
===================================================================
--- extensions/trunk/xsd/src/main/java/org/jboss/webbeans/xsd/PackageInfo.java	2009-03-17 00:07:35 UTC (rev 2060)
+++ extensions/trunk/xsd/src/main/java/org/jboss/webbeans/xsd/PackageInfo.java	2009-03-17 07:38:35 UTC (rev 2061)
@@ -1,3 +1,20 @@
+/*
+ * 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.HashMap;
@@ -2,16 +19,21 @@
 import java.util.HashSet;
-import java.util.List;
 import java.util.Map;
 import java.util.Set;
 
 import org.dom4j.Document;
+import org.jboss.webbeans.xsd.helpers.NamespaceGenerator;
 import org.jboss.webbeans.xsd.model.TypedModel;
 
+/**
+ * Package information
+ * 
+ * @author Nicklas Karlsson
+ * 
+ */
 public class PackageInfo
 {
-   private List<String> namespaces;
    private Document schema;
    private String packageName;
    private Map<String, Set<String>> typeReferences;
-   private String namespace;
+   private NamespaceGenerator namespaceGenerator;
 
@@ -21,6 +43,7 @@
    {
       this.packageName = packageName;
       typeReferences = new HashMap<String, Set<String>>();
+      namespaceGenerator = new NamespaceGenerator(packageName);
    }
 
    public void addTypeReferences(Set<TypedModel> references)
@@ -37,16 +60,6 @@
       }
    }
 
-   public List<String> getNamespaces()
-   {
-      return namespaces;
-   }
-
-   public void setNamespaces(List<String> namespaces)
-   {
-      this.namespaces = namespaces;
-   }
-
    public Document getSchema()
    {
       return schema;
@@ -72,13 +85,21 @@
       return typeReferences;
    }
 
-   public String getNamespace()
+   // TODO: dummy, remove
+   public void refreshNamespaces()
    {
-      return namespace;
+      for (String p : typeReferences.keySet())
+      {
+         if (!"".equals(p))
+         {
+            String dummy = namespaceGenerator.getShortNamespace(p);
+         }
+      }
    }
 
-   public void setNamespace(String namespace)
+   public Set<String> getNamespaces()
    {
-      this.namespace = namespace;
+      return namespaceGenerator.getUsedNamespaces();
    }
+
 }

Modified: extensions/trunk/xsd/src/main/java/org/jboss/webbeans/xsd/helpers/NamespaceGenerator.java
===================================================================
--- extensions/trunk/xsd/src/main/java/org/jboss/webbeans/xsd/helpers/NamespaceGenerator.java	2009-03-17 00:07:35 UTC (rev 2060)
+++ extensions/trunk/xsd/src/main/java/org/jboss/webbeans/xsd/helpers/NamespaceGenerator.java	2009-03-17 07:38:35 UTC (rev 2061)
@@ -1,3 +1,20 @@
+/*
+ * 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.util.Arrays;
@@ -6,43 +23,148 @@
 import java.util.Map;
 import java.util.Set;
 
+/**
+ * Helper for generating and keeping track of namespaces in a schema
+ * 
+ * @author Nicklas Karlsson
+ * 
+ */
 public class NamespaceGenerator
 {
+   // The set of reserved EE packages
    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"));
 
-   public Map<String, Integer> counters = new HashMap<String, Integer>();
+   // The local package of the scema
+   private String localPackage;
 
-   public String getNamespace(String packageName)
+   // Duplicate shortname counters
+   private Map<String, Integer> counters = new HashMap<String, Integer>();
+   // Namespace infos
+   private Map<String, NamespaceInfo> packageNamespaceInfo = new HashMap<String, NamespaceInfo>();
+
+   /**
+    * Creats a new namespace generator
+    * 
+    * @param localPackage The local package
+    */
+   public NamespaceGenerator(String localPackage)
    {
-      String shortName = getShortName(packageName);
-      if (URN_JAVA_EE.contains(getBasePackage(packageName)))
+      this.localPackage = localPackage;
+   }
+
+   /**
+    * Data for a package namespace
+    * 
+    * @author Nicklas Karlsson
+    * 
+    */
+   private class NamespaceInfo
+   {
+      // The package name
+      String packageName;
+      // The full namespace
+      String namespace;
+      // The namespace abbreviation
+      String shortNamespace;
+      // Is this a EE reserved package?
+      boolean ee;
+
+      public NamespaceInfo(String packageName, String shortNamespace, boolean ee)
       {
-         return "xmlns=\"urn:java:ee\"";
+         this.packageName = packageName;
+         this.shortNamespace = shortNamespace;
+         // Skip ":" for default namespace
+         String colon = "".equals(shortNamespace) ? "" : ":";
+         // Hardcode "ee" for EE reserved packages
+         String url = ee ? "ee" : packageName;
+         this.namespace = "xmlns" + colon + shortNamespace + "=\"urn:java:" + url + "\"";
+         this.ee = ee;
       }
-      Integer count = counters.get(shortName);
-      String countString = "";
-      if (count == null)
+   }
+
+   /**
+    * Gets all used namespaces for the schema
+    * 
+    * @return The used namespaces
+    */
+   public Set<String> getUsedNamespaces()
+   {
+      Set<String> usedNamespaces = new HashSet<String>();
+      for (NamespaceInfo namespaceInfo : packageNamespaceInfo.values())
       {
-         count = new Integer(1);
-         counters.put(shortName, count);
+         usedNamespaces.add(namespaceInfo.namespace);
       }
+      return usedNamespaces;
+   }
+
+   /**
+    * Gets a namespace abbreviation for a package
+    * 
+    * @param packageName The name of the package
+    * @return The namespace abbreviation
+    */
+   public String getShortNamespace(String packageName)
+   {
+      if (packageNamespaceInfo.containsKey(packageName))
+      {
+         return packageNamespaceInfo.get(packageName).shortNamespace;
+      }
+      String shortNamespace = "";
+      boolean ee = false;
+      if (localPackage.equals(packageName))
+      {
+         // Nothing to do but want to hit this case first for performance
+      }
+      else if (URN_JAVA_EE.contains(packageName))
+      {
+         shortNamespace = "ee";
+         ee = true;
+      }
       else
       {
-         count++;
-         countString = String.valueOf(count);
+         String shortName = getShortName(packageName);
+         Integer count = counters.get(shortName);
+         String countString = "";
+         if (count == null)
+         {
+            count = new Integer(1);
+            counters.put(shortName, count);
+         }
+         else
+         {
+            count++;
+            countString = String.valueOf(count);
+         }
+         shortNamespace = getShortName(packageName) + countString;
       }
-      return "xmlns:" + shortName + countString + "=\"java:urn:" + packageName + "\"";
+      packageNamespaceInfo.put(packageName, new NamespaceInfo(packageName, shortNamespace, ee));
+      return shortNamespace;
    }
 
-   private String getBasePackage(String packageName)
-   {
-      return packageName.substring(0, getShortName(packageName).length());
-   }
-
+   /**
+    * Gets the short name (last part) of a package
+    * 
+    * @param packageName The package name to parse
+    * @return The short name
+    */
    private String getShortName(String packageName)
    {
       int lastDot = packageName.lastIndexOf(".");
       return lastDot < 0 ? packageName : packageName.substring(lastDot + 1);
    }
+   
+   // TODO testing, remove
+   public static void main(String[] params)
+   {
+      NamespaceGenerator ng = new NamespaceGenerator("com.acme.foo");
+      System.out.println(ng.getShortNamespace("com.acme.foo"));
+      System.out.println(ng.getShortNamespace("com.acme.foo.foo"));
+      System.out.println(ng.getShortNamespace("com.acme.foo.foo.foo"));
+      System.out.println(ng.getShortNamespace("java.util"));
+      for (String ns : ng.getUsedNamespaces())
+      {
+         System.out.println(ns);
+      }
+   }
 
 }

Modified: extensions/trunk/xsd/src/main/java/org/jboss/webbeans/xsd/helpers/XSDHelper.java
===================================================================
--- extensions/trunk/xsd/src/main/java/org/jboss/webbeans/xsd/helpers/XSDHelper.java	2009-03-17 00:07:35 UTC (rev 2060)
+++ extensions/trunk/xsd/src/main/java/org/jboss/webbeans/xsd/helpers/XSDHelper.java	2009-03-17 07:38:35 UTC (rev 2061)
@@ -20,14 +20,9 @@
 import java.io.IOException;
 import java.io.InputStream;
 import java.io.OutputStream;
-import java.util.ArrayList;
-import java.util.Arrays;
 import java.util.HashMap;
-import java.util.HashSet;
 import java.util.List;
 import java.util.Map;
-import java.util.Set;
-import java.util.Map.Entry;
 
 import javax.annotation.processing.Filer;
 import javax.tools.StandardLocation;
@@ -57,7 +52,6 @@
    private Map<String, ClassModel> classModelCache = new HashMap<String, ClassModel>();
    // The XSD documents of the affected packages
    private Map<String, PackageInfo> packageInfoMap = new HashMap<String, PackageInfo>();
-   private NamespaceGenerator namespaceGenerator = new NamespaceGenerator();
 
    /**
     * Creates a new helper
@@ -80,25 +74,12 @@
    private PackageInfo readPackageInfo(String packageName) throws DocumentException, IOException
    {
       PackageInfo packageInfo = new PackageInfo(packageName);
-      packageInfo.setNamespaces(readNamespaces(packageName));
       Document schema = readSchema(packageName);
       packageInfo.setSchema(schema != null ? schema : createSchema(packageName));
       return packageInfo;
    }
 
    /**
-    * Reads the namespaces for a package
-    * 
-    * @param packageName The name of the package
-    * @return The namespaces
-    */
-   private List<String> readNamespaces(String packageName)
-   {
-      // TODO dummy
-      return new ArrayList<String>();
-   }
-
-   /**
     * Creates a new schema document
     * 
     * @param packageName The package name of the schema
@@ -155,21 +136,9 @@
       {
          throw new RuntimeException("Could not write schema for " + packageInfo.getPackageName());
       }
-      writeNamespaces(packageInfo.getPackageName(), packageInfo.getNamespaces());
    }
 
    /**
-    * Writes the namespaces to disk
-    * 
-    * @param packageName The package name
-    * @param namespaces The namespaces
-    */
-   private void writeNamespaces(String packageName, List<String> namespaces)
-   {
-      // TODO dummy
-   }
-
-   /**
     * Writes a schema to disk
     * 
     * @param packageName The package name
@@ -235,8 +204,9 @@
    {
       for (PackageInfo packageInfo : packageInfoMap.values())
       {
-         packageInfo.setNamespace(namespaceGenerator.getNamespace(packageInfo.getPackageName()));
-         System.out.println(packageInfo.getPackageName() + " (" + packageInfo.getNamespace() + ")");
+         // TODO: dummy, remove
+         packageInfo.refreshNamespaces();
+         System.out.println(packageInfo.getPackageName() + " (" + packageInfo.getNamespaces() + ")");
          System.out.println(packageInfo.getTypeReferences());
          writePackageInfo(packageInfo);
       }

Modified: extensions/trunk/xsd/src/main/java/org/jboss/webbeans/xsd/model/ClassModel.java
===================================================================
--- extensions/trunk/xsd/src/main/java/org/jboss/webbeans/xsd/model/ClassModel.java	2009-03-17 00:07:35 UTC (rev 2060)
+++ extensions/trunk/xsd/src/main/java/org/jboss/webbeans/xsd/model/ClassModel.java	2009-03-17 07:38:35 UTC (rev 2061)
@@ -70,18 +70,6 @@
       methods.add(methodModel);
    }
 
-   @Override
-   public String toString()
-   {
-      StringBuilder buffer = new StringBuilder();
-      String annotationString = (annotations.isEmpty()) ? "" : "@" + annotations + ": ";
-      buffer.append("----------------------------------\n" + annotationString + name + "\n");
-      buffer.append("Constructors:\n " + getMergedConstructors() + "\n");
-      buffer.append("Methods:\n" + getMergedMethods() + "\n");
-      buffer.append("Fields:\n" + getMergedFields() + "\n");
-      return buffer.toString();
-   }
-
    /**
     * Gets the parent class model of the class
     * 
@@ -167,6 +155,11 @@
       return mergedMethods;
    }
 
+   /**
+    * Gets the type references used in the package and its contained members
+    * 
+    * @return The set of types
+    */
    public Set<TypedModel> getTypeReferences()
    {
       Set<TypedModel> typeReferences = new HashSet<TypedModel>();
@@ -192,16 +185,38 @@
       return typeReferences;
    }
 
+   /**
+    * Gets the package of the class
+    * 
+    * @return The package (or "nopak" if root package)
+    */
    public String getPackage()
    {
       int lastDot = name.lastIndexOf(".");
       return lastDot < 0 ? "nopak" : name.substring(0, lastDot);
    }
 
+   /**
+    * Gets the simple name of a class
+    * 
+    * @return The simple name
+    */
    public String getSimpleName()
    {
       int lastDot = name.lastIndexOf(".");
       return lastDot < 0 ? name : name.substring(lastDot + 1);
    }
 
+   @Override
+   public String toString()
+   {
+      StringBuilder buffer = new StringBuilder();
+      String annotationString = (annotations.isEmpty()) ? "" : "@" + annotations + ": ";
+      buffer.append("----------------------------------\n" + annotationString + name + "\n");
+      buffer.append("Constructors:\n " + getMergedConstructors() + "\n");
+      buffer.append("Methods:\n" + getMergedMethods() + "\n");
+      buffer.append("Fields:\n" + getMergedFields() + "\n");
+      return buffer.toString();
+   }
+
 }

Modified: extensions/trunk/xsd/src/main/java/org/jboss/webbeans/xsd/model/TypedModel.java
===================================================================
--- extensions/trunk/xsd/src/main/java/org/jboss/webbeans/xsd/model/TypedModel.java	2009-03-17 00:07:35 UTC (rev 2060)
+++ extensions/trunk/xsd/src/main/java/org/jboss/webbeans/xsd/model/TypedModel.java	2009-03-17 07:38:35 UTC (rev 2061)
@@ -1,5 +1,28 @@
+/*
+ * 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.model;
 
+/**
+ * The model of a typed member
+ * 
+ * @author Nicklas Karlsson
+ *
+ */
 public class TypedModel
 {
    protected String type;




More information about the weld-commits mailing list