[infinispan-commits] Infinispan SVN: r570 - in trunk/tools/src/main/java/org/infinispan/tools: schema and 1 other directory.

infinispan-commits at lists.jboss.org infinispan-commits at lists.jboss.org
Tue Jul 14 14:12:37 EDT 2009


Author: vblagojevic at jboss.com
Date: 2009-07-14 14:12:36 -0400 (Tue, 14 Jul 2009)
New Revision: 570

Removed:
   trunk/tools/src/main/java/org/infinispan/tools/schema/TreeNode.java
   trunk/tools/src/main/java/org/infinispan/tools/schema/TreeWalker.java
Modified:
   trunk/tools/src/main/java/org/infinispan/tools/doclet/config/ConfigHtmlGenerator.java
   trunk/tools/src/main/java/org/infinispan/tools/doclet/config/XMLTreeOutputWalker.java
   trunk/tools/src/main/java/org/infinispan/tools/schema/ConfigurationTreeWalker.java
   trunk/tools/src/main/java/org/infinispan/tools/schema/SchemaGenerator.java
   trunk/tools/src/main/java/org/infinispan/tools/schema/SchemaGeneratorTreeWalker.java
Log:
schema creation final touches

Modified: trunk/tools/src/main/java/org/infinispan/tools/doclet/config/ConfigHtmlGenerator.java
===================================================================
--- trunk/tools/src/main/java/org/infinispan/tools/doclet/config/ConfigHtmlGenerator.java	2009-07-14 18:10:52 UTC (rev 569)
+++ trunk/tools/src/main/java/org/infinispan/tools/doclet/config/ConfigHtmlGenerator.java	2009-07-14 18:12:36 UTC (rev 570)
@@ -11,8 +11,8 @@
 import org.infinispan.config.ConfigurationElements;
 import org.infinispan.config.ConfigurationProperties;
 import org.infinispan.config.ConfigurationProperty;
+import org.infinispan.config.parsing.TreeNode;
 import org.infinispan.tools.doclet.html.HtmlGenerator;
-import org.infinispan.tools.schema.TreeNode;
 import org.infinispan.util.ClassFinder;
 
 /**
@@ -132,15 +132,13 @@
       sb.append("<tr class=\"b\">");
       sb.append("<td>").append("<code>" + a.name() +"</code>").append("</td>\n");
       
-      //if allowed values specified for attribute, use it
-      if (a.allowedValues().length() > 0) {
-         sb.append("<td>").append("<code>" + a.allowedValues()+"</code>").append("</td>\n");
-      }
-      //otherwise, reflect method and use parameter as allowed value
-      else if (isSetterMethod(m)) {
-         sb.append("<td>").append("<code>" + m.getParameterTypes()[0].getSimpleName() + "</code>").append("</td>\n");
-      }
-      
+      sb.append("<td>").append("<code>" + m.getParameterTypes()[0].getSimpleName() + "</code>");
+      if(a.allowedValues().length()>0){
+         sb.append("*  " + a.allowedValues() +"</td>\n");
+      } else{
+         sb.append("</td>\n");
+      }    
+     
       //if default value specified in annotation use it
       if (a.defaultValue().length() > 0) {
          sb.append("<td>").append(a.defaultValue()).append("</td>\n");

Modified: trunk/tools/src/main/java/org/infinispan/tools/doclet/config/XMLTreeOutputWalker.java
===================================================================
--- trunk/tools/src/main/java/org/infinispan/tools/doclet/config/XMLTreeOutputWalker.java	2009-07-14 18:10:52 UTC (rev 569)
+++ trunk/tools/src/main/java/org/infinispan/tools/doclet/config/XMLTreeOutputWalker.java	2009-07-14 18:12:36 UTC (rev 570)
@@ -22,7 +22,7 @@
 package org.infinispan.tools.doclet.config;
 
 import org.infinispan.tools.schema.ConfigurationTreeWalker;
-import org.infinispan.tools.schema.TreeNode;
+import org.infinispan.config.parsing.TreeNode;
 
 /**
  * TreeWalker that generates XML pretty print of the configuration tree

Modified: trunk/tools/src/main/java/org/infinispan/tools/schema/ConfigurationTreeWalker.java
===================================================================
--- trunk/tools/src/main/java/org/infinispan/tools/schema/ConfigurationTreeWalker.java	2009-07-14 18:10:52 UTC (rev 569)
+++ trunk/tools/src/main/java/org/infinispan/tools/schema/ConfigurationTreeWalker.java	2009-07-14 18:12:36 UTC (rev 570)
@@ -29,6 +29,11 @@
 
 import org.infinispan.config.ConfigurationElement;
 import org.infinispan.config.ConfigurationElements;
+import org.infinispan.config.parsing.RootElementBuilder;
+import org.infinispan.config.parsing.TreeNode;
+import org.infinispan.config.parsing.TreeWalker;
+import org.infinispan.util.logging.Log;
+import org.infinispan.util.logging.LogFactory;
 
 /**
  * TreeWalker abstract super class that should be extended for a particular tool
@@ -41,8 +46,11 @@
  */
 public abstract class ConfigurationTreeWalker implements TreeWalker{
 
+   protected final Log log;
+   
    public ConfigurationTreeWalker() {
       super();
+      log = LogFactory.getLog(getClass());
    }
 
    public TreeNode constructTreeFromBeans(List<Class<?>>configBeans) {
@@ -107,4 +115,17 @@
       }
       node.accept(this);
    }
+   
+   public ConfigurationElement[] configurationElementsOnBean(Class<?> clazz) {
+      ConfigurationElements configurationElements = clazz.getAnnotation(ConfigurationElements.class);
+      ConfigurationElement configurationElement = clazz.getAnnotation(ConfigurationElement.class);
+      ConfigurationElement ces [] = new ConfigurationElement[0];
+      if (configurationElement != null && configurationElements == null) {
+         ces = new ConfigurationElement[]{configurationElement};
+      }
+      if (configurationElements != null && configurationElement == null) {
+         ces = configurationElements.elements();
+      }
+      return ces;
+   }
 }
\ No newline at end of file

Modified: trunk/tools/src/main/java/org/infinispan/tools/schema/SchemaGenerator.java
===================================================================
--- trunk/tools/src/main/java/org/infinispan/tools/schema/SchemaGenerator.java	2009-07-14 18:10:52 UTC (rev 569)
+++ trunk/tools/src/main/java/org/infinispan/tools/schema/SchemaGenerator.java	2009-07-14 18:12:36 UTC (rev 570)
@@ -35,6 +35,7 @@
 
 import org.infinispan.Version;
 import org.infinispan.config.AbstractConfigurationBean;
+import org.infinispan.config.parsing.TreeNode;
 import org.infinispan.util.ClassFinder;
 import org.w3c.dom.DOMImplementation;
 import org.w3c.dom.Document;
@@ -103,12 +104,25 @@
         
          Element xsElement = xmldoc.createElement("xs:element");       
          xsElement.setAttribute("name", "infinispan");
-         xsElement.setAttribute("type", "tns:infinispanType");
+         xsElement.setAttribute("type", "tns:infinispanTypeIn");
          xmldoc.getDocumentElement().appendChild(xsElement);
 
          ConfigurationTreeWalker tw = new SchemaGeneratorTreeWalker(xmldoc,beans);
          TreeNode root = tw.constructTreeFromBeans(beans);         
          tw.preOrderTraverse(root);
+         
+         Element property = xmldoc.createElement("xs:complexType");       
+         property.setAttribute("name", "propertyType");
+         Element att = xmldoc.createElement("xs:attribute");       
+         att.setAttribute("name", "name");
+         att.setAttribute("type", "xs:string");
+         property.appendChild(att);
+         att = xmldoc.createElement("xs:attribute");       
+         att.setAttribute("name", "value");
+         att.setAttribute("type", "xs:string");
+         property.appendChild(att);
+         
+         xmldoc.getDocumentElement().appendChild(property);        
 
          DOMSource domSource = new DOMSource(xmldoc);
          StreamResult streamResult = new StreamResult(fw);

Modified: trunk/tools/src/main/java/org/infinispan/tools/schema/SchemaGeneratorTreeWalker.java
===================================================================
--- trunk/tools/src/main/java/org/infinispan/tools/schema/SchemaGeneratorTreeWalker.java	2009-07-14 18:10:52 UTC (rev 569)
+++ trunk/tools/src/main/java/org/infinispan/tools/schema/SchemaGeneratorTreeWalker.java	2009-07-14 18:12:36 UTC (rev 570)
@@ -29,6 +29,10 @@
 import org.infinispan.config.ConfigurationElement;
 import org.infinispan.config.ConfigurationElements;
 import org.infinispan.config.ConfigurationException;
+import org.infinispan.config.ConfigurationProperties;
+import org.infinispan.config.ConfigurationProperty;
+import org.infinispan.config.parsing.ConfigurationElementWriter;
+import org.infinispan.config.parsing.TreeNode;
 import org.w3c.dom.Document;
 import org.w3c.dom.Element;
 
@@ -52,42 +56,188 @@
    }
    
    public void visitNode(TreeNode treeNode) {
+      Class<?> bean = findBean(beans, treeNode.getName(), treeNode.getParent().getName());
+      if (bean == null) {
+         log.warn("Did not find bean for node " + treeNode+ ". Should happen only for infinispan node");
+         writeInfinispanType();
+         return;
+      }
       
+      boolean hasCustomWriter = false;
+      ConfigurationElementWriter writer = null;
+      ConfigurationElement[] onBean = configurationElementsOnBean(bean);
+      for(ConfigurationElement ce:onBean){
+         if(ce.name().equals(treeNode.getName()) && ce.parent().equals(treeNode.getParent().getName())){
+            hasCustomWriter = !ce.customWriter().equals(ConfigurationElementWriter.class);
+            if(hasCustomWriter){
+               try {
+               writer = ce.customWriter().newInstance();
+               } catch (Exception e1) {
+                  throw new ConfigurationException("Could not instantiate custom writer ", e1);
+               }
+               break;
+            }
+         }
+      }
+      
+      if (hasCustomWriter) {         
+         try {
+            writer.process(treeNode, xmldoc);
+         } catch (Exception e1) {
+            throw new ConfigurationException("Exception while using custom writer ", e1);
+         }
+      } else {      
+         Element complexType = xmldoc.createElement("xs:complexType");
+         complexType.setAttribute("name", treeNode.getName() + "TypeIn" + treeNode.getParent().getName());
+         createProperty(treeNode, complexType);
+         if(treeNode.hasChildren()) {         
+            Element all = xmldoc.createElement("xs:all");
+            complexType.appendChild(all);
+            Set<TreeNode> children = treeNode.getChildren();
+            for (TreeNode child : children) {
+               Element childElement = xmldoc.createElement("xs:element");
+               childElement.setAttribute("name", child.getName());
+               childElement.setAttribute("type", "tns:" + child.getName() + "TypeIn" + child.getParent().getName());
+               childElement.setAttribute("minOccurs", "0");
+               childElement.setAttribute("maxOccurs", "1");            
+               all.appendChild(childElement);
+            }
+            createAttribute(treeNode, complexType);
+         } else { 
+            createAttribute(treeNode, complexType);         
+         }
+         postProcess(treeNode,complexType);
+         xmldoc.getDocumentElement().appendChild(complexType);
+      }
+   }
+   
+   protected void postProcess(TreeNode treeNode, Element complexType) {
+      if(treeNode.getName().equals("default")){
+         Element element = xmldoc.createElement("xs:attribute");
+         element.setAttribute("name", "name");
+         element.setAttribute("type", "xs:string");
+         complexType.appendChild(element);
+      }
+   }
+
+   private void writeInfinispanType(){
       Element complexType = xmldoc.createElement("xs:complexType");
-      complexType.setAttribute("name", treeNode.getName() + "Type");
-      if(treeNode.hasChildren()) {
-         Element all = xmldoc.createElement("xs:all");
-         complexType.appendChild(all);
-         Set<TreeNode> children = treeNode.getChildren();
-         for (TreeNode child : children) {
-            Element childElement = xmldoc.createElement("xs:element");
-            childElement.setAttribute("name", child.getName());
-            childElement.setAttribute("type", "tns:" + child.getName() + "Type");
-            //childElement.setAttribute("minOccurs", "0");
-            //childElement.setAttribute("maxOccurs", "1");            
-            all.appendChild(childElement);
-         }
-      } else { 
-         Class <?> bean = findBean(beans, treeNode.getName(), treeNode.getParent().getName());
-         for (Method m : bean.getMethods()) {
-            ConfigurationAttribute a = m.getAnnotation(ConfigurationAttribute.class);
-            boolean childElement = a != null && a.containingElement().equals(treeNode.getName());           
-            if (childElement) {
-               String type = "";
-               if (isSetterMethod(m)) {
-                  type = m.getParameterTypes()[0].getSimpleName();
-                  type = type.toLowerCase();
-               }                            
-               Element att = xmldoc.createElement("xs:attribute");
-               att.setAttribute("name", a.name());
+      complexType.setAttribute("name", "infinispanTypeIn");
+      Element seq = xmldoc.createElement("xs:sequence");
+      complexType.appendChild(seq);
+      
+      Element e = xmldoc.createElement("xs:element");
+      e.setAttribute("name", "global");
+      e.setAttribute("type", "tns:globalTypeIninfinispan");
+      e.setAttribute("minOccurs", "0");
+      e.setAttribute("maxOccurs", "1");      
+      seq.appendChild(e);
+      
+      e = xmldoc.createElement("xs:element");
+      e.setAttribute("name", "default");
+      e.setAttribute("type", "tns:defaultTypeIninfinispan");
+      e.setAttribute("minOccurs", "0");
+      e.setAttribute("maxOccurs", "1");      
+      seq.appendChild(e);
+      
+      e = xmldoc.createElement("xs:element");
+      e.setAttribute("name", "namedCache");
+      e.setAttribute("type", "tns:defaultTypeIninfinispan");
+      e.setAttribute("minOccurs", "0");
+      e.setAttribute("maxOccurs", "unbounded");      
+      seq.appendChild(e);
+      
+      xmldoc.getDocumentElement().appendChild(complexType);      
+   }
+
+   private void createAttribute(TreeNode treeNode, Element complexType) {
+      Class <?> bean = findBean(beans, treeNode.getName(), treeNode.getParent().getName());
+      if(bean == null){
+         log.warn("Did not find bean for node " + treeNode + ". Should happen only for infinispan node");
+         return;
+      }   
+      
+      for (Method m : bean.getMethods()) {                  
+         ConfigurationAttribute a = m.getAnnotation(ConfigurationAttribute.class);
+         boolean childElement = a != null && a.containingElement().equals(treeNode.getName());           
+         if (childElement) {
+            String type = "";
+            if (isSetterMethod(m)) {
+               type = m.getParameterTypes()[0].getSimpleName();
+               type = type.toLowerCase();
+            }                            
+            Element att = xmldoc.createElement("xs:attribute");
+            att.setAttribute("name", a.name());
+            boolean hasRestriction = a.allowedValues().length()>0;
+            if(!hasRestriction){
                att.setAttribute("type", "xs:" + type);
-               complexType.appendChild(att);
             }
+            else {
+               Element simpleType = xmldoc.createElement("xs:simpleType");
+               att.appendChild(simpleType);
+               Element restriction = xmldoc.createElement("xs:restriction");
+               restriction.setAttribute("base", "xs:" + type);
+               simpleType.appendChild(restriction);
+               String [] values = a.allowedValues().split(",");
+               for (String constraint : values) {
+                  Element restrictionValue = xmldoc.createElement("xs:enumeration");                     
+                  restrictionValue.setAttribute("value", constraint.trim());
+                  restriction.appendChild(restrictionValue);                     
+               }                  
+            }
+            complexType.appendChild(att);
          }         
       }
-      xmldoc.getDocumentElement().appendChild(complexType);
    }
    
+   private void createProperty(TreeNode treeNode, Element complexType) {
+      if (treeNode.getParent().getParent() == null)
+         return;
+      Class<?> bean = findBean(beans, treeNode.getParent().getName(), treeNode.getParent().getParent().getName());
+      if (bean == null) {
+         log.warn("Did not find bean for node " + treeNode+ ". Should happen only for infinispan node");
+         return;
+      }    
+
+      // need to find all bean declarations for this bean
+      String createdForParentElement = null;
+      for (Method m : bean.getMethods()) {
+         if (propertiesElementsOnMethod(m).length > 0) {
+            for (ConfigurationProperty c : propertiesElementsOnMethod(m)) {
+
+               boolean property = treeNode.getName().equals(c.parentElement());
+               if (property && !c.parentElement().equals(createdForParentElement)) {
+                  createdForParentElement = c.parentElement();
+                  Element prop = xmldoc.createElement("xs:sequence");
+                  Element e = xmldoc.createElement("xs:element");
+                  prop.appendChild(e);
+                  e.setAttribute("name", "property");
+                  e.setAttribute("maxOccurs", "unbounded");
+                  e.setAttribute("minOccurs", "0");
+                  e.setAttribute("type", "tns:propertyType");
+                  complexType.appendChild(prop);
+               }
+            }
+         }
+      }
+   }
+   
+   private ConfigurationProperty[] propertiesElementsOnMethod(Method m) {
+      ConfigurationProperty[] cprops = new ConfigurationProperty[0];
+      ConfigurationProperties cp = m.getAnnotation(ConfigurationProperties.class);
+      ConfigurationProperty p = null;
+      if (cp != null) {
+         cprops = cp.elements();
+      } else {
+         p = m.getAnnotation(ConfigurationProperty.class);
+         if (p != null) {
+            cprops = new ConfigurationProperty[]{p};
+         }
+      }
+      return cprops;
+   }
+
+   
    private Class<?> findBean(List<Class<?>> b, String name, String parentName) throws ConfigurationException {
       
       if (parentName.equals("namedCache"))

Deleted: trunk/tools/src/main/java/org/infinispan/tools/schema/TreeNode.java
===================================================================
--- trunk/tools/src/main/java/org/infinispan/tools/schema/TreeNode.java	2009-07-14 18:10:52 UTC (rev 569)
+++ trunk/tools/src/main/java/org/infinispan/tools/schema/TreeNode.java	2009-07-14 18:12:36 UTC (rev 570)
@@ -1,97 +0,0 @@
-/*
- * JBoss, Home of Professional Open Source.
- * Copyright 2009, Red Hat Middleware LLC, and individual contributors
- * as indicated by the @author tags. See the copyright.txt file in the
- * distribution for a full listing of individual contributors.
- *
- * This is free software; you can redistribute it and/or modify it
- * under the terms of the GNU Lesser General Public License as
- * published by the Free Software Foundation; either version 2.1 of
- * the License, or (at your option) any later version.
- *
- * This software is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public
- * License along with this software; if not, write to the Free
- * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
- * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
- */
-package org.infinispan.tools.schema;
-
-import java.util.HashSet;
-import java.util.Set;
-
-/**
- * TreeNode of Infinispan configuration
- *
- * @author Vladimir Blagojevic
- * @version $Id$
- * @since 4.0
- */
-public class TreeNode {
-   private final String name;
-   private final TreeNode parent;
-   private final int depth;
-   private final Set<TreeNode> children = new HashSet<TreeNode>();
-   
-   public TreeNode(String name, TreeNode parent, int depth) {
-      this.name = name;
-      this.parent = parent;
-      this.depth = depth;
-   }   
-   
-   public TreeNode() {
-      this.name="";
-      this.parent=null;
-      this.depth = -1; // :)
-   }
-
-   public String getName() {
-      return name;
-   }
-      
-   public int getDepth() {
-      return depth;
-   }
-
-   public boolean hasChildren(){
-      return !children.isEmpty();
-   }
-
-   public TreeNode getParent() {
-      return parent;
-   }
-   
-   public Set<TreeNode> getChildren() {
-      return children;
-   }
-   
-   public void accept(TreeWalker tw) {
-      tw.visitNode(this);                     
-   }
-
-   public boolean equals(Object other) {
-      if (other == this)
-         return true;
-      if (!(other instanceof TreeNode))
-         return false;
-      TreeNode tn = (TreeNode) other;
-      return this.parent.name != null && tn.parent != null
-               && this.parent.name.equals(tn.parent.name) && this.name.equals(tn.name);
-   }  
-
-   public int hashCode() {
-      int result = 17;
-      result = 31 * result + name.hashCode();
-      result = 31 * result
-               + ((parent != null && parent.name != null) ? parent.name.hashCode() : 0);
-      return result;
-   }
-   
-   public String toString() {
-      return name;
-   }
-}
\ No newline at end of file

Deleted: trunk/tools/src/main/java/org/infinispan/tools/schema/TreeWalker.java
===================================================================
--- trunk/tools/src/main/java/org/infinispan/tools/schema/TreeWalker.java	2009-07-14 18:10:52 UTC (rev 569)
+++ trunk/tools/src/main/java/org/infinispan/tools/schema/TreeWalker.java	2009-07-14 18:12:36 UTC (rev 570)
@@ -1,39 +0,0 @@
-/*
- * JBoss, Home of Professional Open Source.
- * Copyright 2009, Red Hat Middleware LLC, and individual contributors
- * as indicated by the @author tags. See the copyright.txt file in the
- * distribution for a full listing of individual contributors.
- *
- * This is free software; you can redistribute it and/or modify it
- * under the terms of the GNU Lesser General Public License as
- * published by the Free Software Foundation; either version 2.1 of
- * the License, or (at your option) any later version.
- *
- * This software is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public
- * License along with this software; if not, write to the Free
- * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
- * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
- */
-package org.infinispan.tools.schema;
-
-import org.infinispan.tools.schema.TreeNode;
-
-/**
- * TreeWalker visitor
- *
- * @author Vladimir Blagojevic
- * @see SchemaGeneratorTreeWalker
- * @see XMLTreeOutputWalker
- * @version $Id$
- * @since 4.0
- */
-public interface TreeWalker {
-
-   void visitNode(TreeNode treeNode);
-
-}




More information about the infinispan-commits mailing list