[infinispan-commits] Infinispan SVN: r572 - trunk/tools/src/main/java/org/infinispan/tools/schema.
infinispan-commits at lists.jboss.org
infinispan-commits at lists.jboss.org
Wed Jul 15 06:01:39 EDT 2009
Author: vblagojevic at jboss.com
Date: 2009-07-15 06:01:38 -0400 (Wed, 15 Jul 2009)
New Revision: 572
Modified:
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:
[ISPN-96] - Generate configuration XSD schema
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-15 09:49:28 UTC (rev 571)
+++ trunk/tools/src/main/java/org/infinispan/tools/schema/ConfigurationTreeWalker.java 2009-07-15 10:01:38 UTC (rev 572)
@@ -21,6 +21,7 @@
*/
package org.infinispan.tools.schema;
+import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
@@ -29,7 +30,9 @@
import org.infinispan.config.ConfigurationElement;
import org.infinispan.config.ConfigurationElements;
-import org.infinispan.config.parsing.RootElementBuilder;
+import org.infinispan.config.ConfigurationException;
+import org.infinispan.config.ConfigurationProperties;
+import org.infinispan.config.ConfigurationProperty;
import org.infinispan.config.parsing.TreeNode;
import org.infinispan.config.parsing.TreeWalker;
import org.infinispan.util.logging.Log;
@@ -116,7 +119,28 @@
node.accept(this);
}
- public ConfigurationElement[] configurationElementsOnBean(Class<?> clazz) {
+ public void postTraverseCleanup(){}
+
+ protected ConfigurationElement findConfigurationElementForBean(Class<?> clazz, String name, String parentName){
+ ConfigurationElement[] onBean = configurationElementsOnBean(clazz);
+ for(ConfigurationElement ce:onBean){
+ if(ce.name().equals(name) && ce.parent().equals(parentName)){
+ return ce;
+ }
+ }
+ return null;
+ }
+
+ protected ConfigurationElement findConfigurationElement(List<Class<?>> b, String name, String parentName){
+ ConfigurationElement result = null;
+ Class<?> bean = findBean(b, name, parentName);
+ if(bean != null){
+ result = findConfigurationElementForBean(bean, name, parentName);
+ }
+ return result;
+ }
+
+ protected ConfigurationElement[] configurationElementsOnBean(Class<?> clazz) {
ConfigurationElements configurationElements = clazz.getAnnotation(ConfigurationElements.class);
ConfigurationElement configurationElement = clazz.getAnnotation(ConfigurationElement.class);
ConfigurationElement ces [] = new ConfigurationElement[0];
@@ -128,4 +152,46 @@
}
return ces;
}
+
+ protected 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;
+ }
+
+
+ protected Class<?> findBean(List<Class<?>> b, String name, String parentName) throws ConfigurationException {
+
+ if (parentName.equals("namedCache"))
+ parentName = "default";
+ for (Class<?> clazz : b) {
+ ConfigurationElements elements = clazz.getAnnotation(ConfigurationElements.class);
+ try {
+ if (elements != null) {
+ for (ConfigurationElement ce : elements.elements()) {
+ if (ce.name().equals(name) && ce.parent().equals(parentName)) {
+ return clazz;
+ }
+ }
+ } else {
+ ConfigurationElement ce = clazz.getAnnotation(ConfigurationElement.class);
+ if (ce != null && (ce.name().equals(name) && ce.parent().equals(parentName))) {
+ return clazz;
+ }
+ }
+ } catch (Exception e1) {
+ throw new ConfigurationException("Could not instantiate class " + clazz, e1);
+ }
+ }
+ return null;
+ }
}
\ 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-15 09:49:28 UTC (rev 571)
+++ trunk/tools/src/main/java/org/infinispan/tools/schema/SchemaGenerator.java 2009-07-15 10:01:38 UTC (rev 572)
@@ -39,7 +39,6 @@
import org.infinispan.util.ClassFinder;
import org.w3c.dom.DOMImplementation;
import org.w3c.dom.Document;
-import org.w3c.dom.Element;
/**
* Generates XML Schema for Infinispan configuration
@@ -100,37 +99,20 @@
Document xmldoc = impl.createDocument("http://www.w3.org/2001/XMLSchema", "xs:schema",null);
xmldoc.getDocumentElement().setAttribute("targetNamespace", "urn:infinispan:config:" + Version.getMajorVersion());
xmldoc.getDocumentElement().setAttribute("xmlns:tns","urn:infinispan:config:" + Version.getMajorVersion());
- xmldoc.getDocumentElement().setAttribute("elementFormDefault", "qualified");
-
- Element xsElement = xmldoc.createElement("xs:element");
- xsElement.setAttribute("name", "infinispan");
- xsElement.setAttribute("type", "tns:infinispanTypeIn");
- xmldoc.getDocumentElement().appendChild(xsElement);
+ xmldoc.getDocumentElement().setAttribute("elementFormDefault", "qualified");
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);
-
+ tw.postTraverseCleanup();
+
DOMSource domSource = new DOMSource(xmldoc);
StreamResult streamResult = new StreamResult(fw);
TransformerFactory tf = TransformerFactory.newInstance();
Transformer serializer = tf.newTransformer();
serializer.setOutputProperty(OutputKeys.METHOD, "xml");
serializer.setOutputProperty(OutputKeys.INDENT, "yes");
- serializer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");
+ serializer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
serializer.transform(domSource, streamResult);
fw.flush();
fw.close();
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-15 09:49:28 UTC (rev 571)
+++ trunk/tools/src/main/java/org/infinispan/tools/schema/SchemaGeneratorTreeWalker.java 2009-07-15 10:01:38 UTC (rev 572)
@@ -27,10 +27,9 @@
import org.infinispan.config.ConfigurationAttribute;
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.ConfigurationElement.Cardinality;
import org.infinispan.config.parsing.ConfigurationElementWriter;
import org.infinispan.config.parsing.TreeNode;
import org.w3c.dom.Document;
@@ -56,30 +55,33 @@
}
public void visitNode(TreeNode treeNode) {
- Class<?> bean = findBean(beans, treeNode.getName(), treeNode.getParent().getName());
+ String name = treeNode.getName();
+ String parentName = treeNode.getParent().getName();
+ Class<?> bean = findBean(beans, name, parentName);
if (bean == null) {
log.warn("Did not find bean for node " + treeNode+ ". Should happen only for infinispan node");
writeInfinispanType();
return;
}
- boolean hasCustomWriter = false;
+
+ ConfigurationElement ce = findConfigurationElementForBean(bean, name, parentName);
+ if(ce == null){
+ log.warn("Did not find ConfigurationElement for " + treeNode+ ". Verify annotations on all AbstractConfigurationBeans");
+ return;
+ }
+
+ log.debug("Visiting node " + name);
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;
- }
- }
+ boolean 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);
+ }
}
-
+ log.debug("Visiting node " + name + ((hasCustomWriter)?" will use " + writer:" will use default creation of elements"));
if (hasCustomWriter) {
try {
writer.process(treeNode, xmldoc);
@@ -90,17 +92,35 @@
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);
+ if(treeNode.hasChildren()) {
+ boolean sequence = false;
+ for(TreeNode child:treeNode.getChildren()){
+ ConfigurationElement cce = findConfigurationElement(beans,child.getName(),treeNode.getName());
+ if(cce.cardinalityInParent().equals(Cardinality.UNBOUNDED)){
+ sequence = true;
+ break;
+ }
+ }
+ Element allOrSequence = null;
+ if(sequence){
+ allOrSequence = xmldoc.createElement("xs:sequence");
+ } else {
+ allOrSequence = xmldoc.createElement("xs:all");
+ }
+ complexType.appendChild(allOrSequence);
Set<TreeNode> children = treeNode.getChildren();
for (TreeNode child : children) {
+ ConfigurationElement cce = findConfigurationElement(beans,child.getName(),treeNode.getName());
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);
+ if(cce.cardinalityInParent().equals(Cardinality.UNBOUNDED)){
+ childElement.setAttribute("maxOccurs", "unbounded");
+ } else {
+ childElement.setAttribute("maxOccurs", "1");
+ }
+ allOrSequence.appendChild(childElement);
}
createAttribute(treeNode, complexType);
} else {
@@ -112,6 +132,8 @@
}
protected void postProcess(TreeNode treeNode, Element complexType) {
+
+ //dealing with default/namdeCache intricacies
if(treeNode.getName().equals("default")){
Element element = xmldoc.createElement("xs:attribute");
element.setAttribute("name", "name");
@@ -120,40 +142,27 @@
}
}
- private void writeInfinispanType(){
- Element complexType = xmldoc.createElement("xs:complexType");
- complexType.setAttribute("name", "infinispanTypeIn");
- Element seq = xmldoc.createElement("xs:sequence");
- complexType.appendChild(seq);
+ @Override
+ public void postTraverseCleanup() {
+ //include special property type not visited by TreeWalker
+ 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);
- 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);
+ xmldoc.getDocumentElement().appendChild(property);
}
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");
+ log.warn("Did not find bean for node " + treeNode + ". Verify annotations on all AbstractConfigurationBeans");
return;
}
@@ -193,79 +202,70 @@
private void createProperty(TreeNode treeNode, Element complexType) {
if (treeNode.getParent().getParent() == null)
return;
- Class<?> bean = findBean(beans, treeNode.getParent().getName(), treeNode.getParent().getParent().getName());
+
+ 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;
+ log.warn("Did not find bean for node " + treeNode+ ". Try parent, maybe property is there...");
+ bean = findBean(beans, treeNode.getParent().getName(), treeNode.getParent().getParent().getName());
+ if(bean == null)
+ 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);
- }
+ for (Method m : bean.getMethods()) {
+ 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 boolean isSetterMethod(Method m) {
+ return m.getName().startsWith("set") && m.getParameterTypes().length == 1;
}
- 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 void writeInfinispanType() {
+ Element xsElement = xmldoc.createElement("xs:element");
+ xsElement.setAttribute("name", "infinispan");
+ xsElement.setAttribute("type", "tns:infinispanTypeIn");
+ xmldoc.getDocumentElement().appendChild(xsElement);
-
- private Class<?> findBean(List<Class<?>> b, String name, String parentName) throws ConfigurationException {
-
- if (parentName.equals("namedCache"))
- parentName = "default";
- for (Class<?> clazz : b) {
- ConfigurationElements elements = clazz.getAnnotation(ConfigurationElements.class);
- try {
- if (elements != null) {
- for (ConfigurationElement ce : elements.elements()) {
- if (ce.name().equals(name) && ce.parent().equals(parentName)) {
- return clazz;
- }
- }
- } else {
- ConfigurationElement ce = clazz.getAnnotation(ConfigurationElement.class);
- if (ce != null && (ce.name().equals(name) && ce.parent().equals(parentName))) {
- return clazz;
- }
- }
- } catch (Exception e1) {
- throw new ConfigurationException("Could not instantiate class " + clazz, e1);
- }
- }
- return null;
+ Element complexType = xmldoc.createElement("xs:complexType");
+ 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 boolean isSetterMethod(Method m) {
- return m.getName().startsWith("set") && m.getParameterTypes().length == 1;
- }
-}
+}
\ No newline at end of file
More information about the infinispan-commits
mailing list