[richfaces-svn-commits] JBoss Rich Faces SVN: r15172 - in root/cdk/trunk/plugins/generator/src: main/java/org/richfaces/cdk/apt and 3 other directories.

richfaces-svn-commits at lists.jboss.org richfaces-svn-commits at lists.jboss.org
Thu Aug 13 20:52:47 EDT 2009


Author: alexsmirnov
Date: 2009-08-13 20:52:46 -0400 (Thu, 13 Aug 2009)
New Revision: 15172

Added:
   root/cdk/trunk/plugins/generator/src/main/java/org/richfaces/cdk/freemarker/
   root/cdk/trunk/plugins/generator/src/main/java/org/richfaces/cdk/freemarker/CdkConfiguration.java
   root/cdk/trunk/plugins/generator/src/main/java/org/richfaces/cdk/freemarker/FreeMarkerRenderer.java
   root/cdk/trunk/plugins/generator/src/main/java/org/richfaces/cdk/freemarker/LibraryModelWrapper.java
Removed:
   root/cdk/trunk/plugins/generator/src/main/java/org/richfaces/cdk/model/Attribute.java
Modified:
   root/cdk/trunk/plugins/generator/src/main/java/org/richfaces/cdk/CdkWriter.java
   root/cdk/trunk/plugins/generator/src/main/java/org/richfaces/cdk/LibraryBuilder.java
   root/cdk/trunk/plugins/generator/src/main/java/org/richfaces/cdk/apt/CdkProcessor.java
   root/cdk/trunk/plugins/generator/src/main/java/org/richfaces/cdk/apt/ComponentProcessor.java
   root/cdk/trunk/plugins/generator/src/main/java/org/richfaces/cdk/model/ClassDescription.java
   root/cdk/trunk/plugins/generator/src/main/java/org/richfaces/cdk/model/Component.java
   root/cdk/trunk/plugins/generator/src/main/java/org/richfaces/cdk/model/ComponentLibrary.java
   root/cdk/trunk/plugins/generator/src/main/java/org/richfaces/cdk/model/Property.java
   root/cdk/trunk/plugins/generator/src/test/java/org/richfaces/cdk/apt/ProcessorTest.java
Log:
Base FreMarker template renderer.
Improve model.

Modified: root/cdk/trunk/plugins/generator/src/main/java/org/richfaces/cdk/CdkWriter.java
===================================================================
--- root/cdk/trunk/plugins/generator/src/main/java/org/richfaces/cdk/CdkWriter.java	2009-08-13 16:45:29 UTC (rev 15171)
+++ root/cdk/trunk/plugins/generator/src/main/java/org/richfaces/cdk/CdkWriter.java	2009-08-14 00:52:46 UTC (rev 15172)
@@ -36,7 +36,7 @@
 		String getName();
 	}
 	
-	public void init(CdkContext context);
+	public void init(CdkContext context) throws CdkException;
 	
 	public void render(ComponentLibrary library) throws CdkException;
 

Modified: root/cdk/trunk/plugins/generator/src/main/java/org/richfaces/cdk/LibraryBuilder.java
===================================================================
--- root/cdk/trunk/plugins/generator/src/main/java/org/richfaces/cdk/LibraryBuilder.java	2009-08-13 16:45:29 UTC (rev 15171)
+++ root/cdk/trunk/plugins/generator/src/main/java/org/richfaces/cdk/LibraryBuilder.java	2009-08-14 00:52:46 UTC (rev 15172)
@@ -103,7 +103,7 @@
 		for (SourceType type : StandardSources.values()) {
 			ComponentLibrary model = buildModel(type);
 			if (null != library) {
-				library.apply(model);
+				library.merge(model);
 			} else {
 				library = model;
 			}

Modified: root/cdk/trunk/plugins/generator/src/main/java/org/richfaces/cdk/apt/CdkProcessor.java
===================================================================
--- root/cdk/trunk/plugins/generator/src/main/java/org/richfaces/cdk/apt/CdkProcessor.java	2009-08-13 16:45:29 UTC (rev 15171)
+++ root/cdk/trunk/plugins/generator/src/main/java/org/richfaces/cdk/apt/CdkProcessor.java	2009-08-14 00:52:46 UTC (rev 15172)
@@ -34,6 +34,7 @@
 import javax.lang.model.element.Element;
 import javax.lang.model.element.ElementKind;
 import javax.lang.model.element.ExecutableElement;
+import javax.lang.model.element.Modifier;
 import javax.lang.model.element.TypeElement;
 import javax.lang.model.element.TypeParameterElement;
 import javax.lang.model.element.VariableElement;
@@ -43,6 +44,7 @@
 import javax.lang.model.util.ElementFilter;
 
 import org.richfaces.cdk.CdkContext;
+import org.richfaces.cdk.model.ClassDescription;
 import org.richfaces.cdk.model.InvalidNameException;
 import org.richfaces.cdk.util.PropertyUtils;
 
@@ -139,13 +141,17 @@
 			return processingEnv.getElementUtils().getDocComment(element);
 		}
 
-		public String getTypeName() {
-			return type.toString();
+		public TypeMirror getType() {
+			return type;
 		}
 
-		public String getTypeParameters() {
-			// TODO - analyze type parameters for getter/setter methods.
-			return null;
+
+		/**
+		 * <p class="changed_added_4_0"></p>
+		 * @return the exists
+		 */
+		public boolean isExists() {
+			return exists;
 		}
 
 	}
@@ -232,6 +238,7 @@
 				// Have an annotation, infer property name.
 				String name;
 				TypeMirror propertyType;
+				boolean exists = false;
 				if (ElementKind.METHOD.equals(childElement.getKind())) {
 					ExecutableElement method = (ExecutableElement) childElement;
 					propertyType = method.getReturnType();
@@ -255,22 +262,26 @@
 						// exception ?
 						continue;
 					}
-					// TODO - Get method type parameters.
+					exists = method.getModifiers().contains(Modifier.ABSTRACT);
 					// List<? extends TypeParameterElement> typeParameters = method.getTypeParameters();
 				} else if (ElementKind.FIELD
 						.equals(childElement.getKind())) {
 					name = childElement.getSimpleName().toString();
 					propertyType = childElement.asType();
+					// TODO - find getter/setter, check them for abstract.
+					exists = true;
 				} else {
 					continue;
 				}
 				BeanProperty property = new BeanProperty(name);
 				property.type = propertyType;
 				property.element = childElement;
+				property.exists = exists;
 				// TODO - merge properties with same name ?
 				properties.add(property);
 			}
 		}
 		return properties;
 	}
+	
 }

Modified: root/cdk/trunk/plugins/generator/src/main/java/org/richfaces/cdk/apt/ComponentProcessor.java
===================================================================
--- root/cdk/trunk/plugins/generator/src/main/java/org/richfaces/cdk/apt/ComponentProcessor.java	2009-08-13 16:45:29 UTC (rev 15171)
+++ root/cdk/trunk/plugins/generator/src/main/java/org/richfaces/cdk/apt/ComponentProcessor.java	2009-08-14 00:52:46 UTC (rev 15172)
@@ -31,10 +31,14 @@
 import javax.lang.model.element.Element;
 import javax.lang.model.element.Modifier;
 import javax.lang.model.element.TypeElement;
+import javax.lang.model.type.MirroredTypeException;
+import javax.lang.model.type.MirroredTypesException;
+import javax.lang.model.type.TypeMirror;
 
 import org.richfaces.cdk.CdkContext;
 import org.richfaces.cdk.NamingConventions;
 import org.richfaces.cdk.annotations.Attribute;
+import org.richfaces.cdk.annotations.Attributes;
 import org.richfaces.cdk.annotations.Component;
 import org.richfaces.cdk.annotations.Family;
 import org.richfaces.cdk.model.ClassDescription;
@@ -86,7 +90,7 @@
 					// Create/get component instance.
 					org.richfaces.cdk.model.Component componentModel = library
 					.createComponent(componentType, uiComponentClass, baseClassName);
-					// Component Family.
+					// Component Family. TODO - get it from 'COMPONENT_FAMILY' field or infer by NamingConventios.
 					Family family = component.getAnnotation(Family.class);
 					componentModel.setFamily(Strings.isEmpty(family.value())?componentType:family.value());
 					// JavaDoc comments
@@ -97,13 +101,29 @@
 					Set<BeanProperty> properties = getBeanPropertiesAnnotatedWith(Attribute.class, component);
 					// TODO - encapsulate attribute builder into utility class.
 					for (BeanProperty beanProperty : properties) {
-						Property atribute = componentModel.addAtribute(beanProperty.getName());
+						Property atribute = componentModel.addAttribute(beanProperty.getName());
 						// Documentation
 						atribute.setDescription(beanProperty.getDocComment());
 						// Type.
-						atribute.setType(new ClassDescription(beanProperty.getTypeName()));
+						atribute.setType(createClassDescription(beanProperty.getType()));
 						// Flags.
+						atribute.setExists(beanProperty.isExists());
 					}
+					Attributes attributes = component.getAnnotation(Attributes.class);
+					if(null != attributes){
+							Attribute[] attributes2 = attributes.value();
+							for (Attribute attributeAnnotation : attributes2) {
+								Property attribute = componentModel.addAttribute(attributeAnnotation.name());
+								attribute.setExists(false);
+								try {
+									Class<?> type = attributeAnnotation.type();
+									attribute.setType(library.createClassDescription(type.getName()));
+								} catch(MirroredTypeException ex){
+									attribute.setType(createClassDescription(ex.getTypeMirror()));									
+								}
+								// TODO - process additional properties.
+							}
+					}
 					// TODO - Process standard information for parent classes and intrfaces.
 				} catch (InvalidNameException e) {
 					// rise error and continue.
@@ -120,4 +140,8 @@
 		return library;
 	}
 
+	protected ClassDescription createClassDescription(TypeMirror mirror){
+		return getLibrary().createClassDescription(mirror.toString());
+	}
+
 }

Added: root/cdk/trunk/plugins/generator/src/main/java/org/richfaces/cdk/freemarker/CdkConfiguration.java
===================================================================
--- root/cdk/trunk/plugins/generator/src/main/java/org/richfaces/cdk/freemarker/CdkConfiguration.java	                        (rev 0)
+++ root/cdk/trunk/plugins/generator/src/main/java/org/richfaces/cdk/freemarker/CdkConfiguration.java	2009-08-14 00:52:46 UTC (rev 15172)
@@ -0,0 +1,73 @@
+/*
+ * $Id$
+ *
+ * License Agreement.
+ *
+ * Rich Faces - Natural Ajax for Java Server Faces (JSF)
+ *
+ * Copyright (C) 2007 Exadel, Inc.
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License version 2.1 as published by the Free Software Foundation.
+ *
+ * This library 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 library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301  USA
+ */
+
+package org.richfaces.cdk.freemarker;
+
+import org.richfaces.cdk.CdkContext;
+
+import freemarker.template.Configuration;
+import freemarker.template.TemplateHashModel;
+import freemarker.template.TemplateModel;
+import freemarker.template.TemplateModelException;
+
+/**
+ * <p class="changed_added_4_0"></p>
+ * @author asmirnov at exadel.com
+ *
+ */
+public class CdkConfiguration extends Configuration {
+	
+	private final CdkContext context;
+
+	/**
+	 * <p class="changed_added_4_0"></p>
+	 * @return the context
+	 */
+	protected CdkContext getContext() {
+		return context;
+	}
+
+	public CdkConfiguration(CdkContext context) {
+		super();
+		this.context = context;
+		// TODO set proper template loader.
+		setClassForTemplateLoading(context.getClass(), "/META-INF/templates");
+		// TODO create an object wrapper for library model.
+		setObjectWrapper(new LibraryModelWrapper());
+		// Add context variables
+		this.setSharedVariable("context", new TemplateHashModel() {
+
+			@Override
+			public TemplateModel get(String key) throws TemplateModelException {
+				// TODO - define context parameters that could be exposed to template.
+				return null;
+			}
+
+			@Override
+			public boolean isEmpty() throws TemplateModelException {
+				return false;
+			}
+		});
+	}
+
+}


Property changes on: root/cdk/trunk/plugins/generator/src/main/java/org/richfaces/cdk/freemarker/CdkConfiguration.java
___________________________________________________________________
Name: svn:mime-type
   + text/plain

Added: root/cdk/trunk/plugins/generator/src/main/java/org/richfaces/cdk/freemarker/FreeMarkerRenderer.java
===================================================================
--- root/cdk/trunk/plugins/generator/src/main/java/org/richfaces/cdk/freemarker/FreeMarkerRenderer.java	                        (rev 0)
+++ root/cdk/trunk/plugins/generator/src/main/java/org/richfaces/cdk/freemarker/FreeMarkerRenderer.java	2009-08-14 00:52:46 UTC (rev 15172)
@@ -0,0 +1,108 @@
+/*
+ * $Id$
+ *
+ * License Agreement.
+ *
+ * Rich Faces - Natural Ajax for Java Server Faces (JSF)
+ *
+ * Copyright (C) 2007 Exadel, Inc.
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License version 2.1 as published by the Free Software Foundation.
+ *
+ * This library 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 library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301  USA
+ */
+
+package org.richfaces.cdk.freemarker;
+
+import java.io.FileWriter;
+import java.io.IOException;
+
+import org.richfaces.cdk.CdkContext;
+import org.richfaces.cdk.CdkException;
+import org.richfaces.cdk.CdkWriter;
+import org.richfaces.cdk.model.ComponentLibrary;
+import org.richfaces.cdk.model.JsfComponent;
+import org.richfaces.cdk.model.LibraryVisitor;
+
+import freemarker.template.Configuration;
+import freemarker.template.Template;
+import freemarker.template.TemplateException;
+
+/**
+ * <p class="changed_added_4_0">Base class for all output file buildes that use FreeMarker as template engine.</p>
+ * @author asmirnov at exadel.com
+ *
+ */
+public abstract class FreeMarkerRenderer<C extends JsfComponent,P>  implements CdkWriter,LibraryVisitor<Boolean, P> {
+
+	private CdkContext context;
+	private Configuration configuration;
+	private Template template;
+
+	@Override
+	public void init(CdkContext context) throws CdkException {
+		this.context = context;
+		this.configuration = new CdkConfiguration(context);
+		try {
+			template = configuration.getTemplate(getTemplateName());
+		} catch (IOException e) {
+			throw new CdkException(e);
+		}
+	}
+
+	@Override
+	public void render(ComponentLibrary library) throws CdkException {
+			library.accept(this, getVisitorParameter());
+	}
+
+	private P getVisitorParameter() {
+		return null;
+	}
+
+	@Override
+	public Boolean visit(JsfComponent c, P param) {
+		if(isMyComponent(c)){
+			return processComponent((C)c, param);
+		}
+		return null;
+	}
+
+	protected boolean processComponent(C c, P param) {
+		try {
+			FileWriter out = new FileWriter(getOutputFile(c));
+			template.process(c, out);
+			out.close();
+		} catch (IOException e) {
+			// TODO Auto-generated catch block
+			e.printStackTrace();
+		} catch (TemplateException e) {
+			// TODO Auto-generated catch block
+			e.printStackTrace();
+		} finally {
+		}
+		return false;
+	}
+
+	protected abstract String getOutputFile(C c);
+
+	protected abstract boolean isMyComponent(JsfComponent c);
+
+	protected abstract String getTemplateName();
+
+	/**
+	 * <p class="changed_added_4_0"></p>
+	 * @return the context
+	 */
+	protected CdkContext getContext() {
+		return context;
+	}
+}


Property changes on: root/cdk/trunk/plugins/generator/src/main/java/org/richfaces/cdk/freemarker/FreeMarkerRenderer.java
___________________________________________________________________
Name: svn:mime-type
   + text/plain

Added: root/cdk/trunk/plugins/generator/src/main/java/org/richfaces/cdk/freemarker/LibraryModelWrapper.java
===================================================================
--- root/cdk/trunk/plugins/generator/src/main/java/org/richfaces/cdk/freemarker/LibraryModelWrapper.java	                        (rev 0)
+++ root/cdk/trunk/plugins/generator/src/main/java/org/richfaces/cdk/freemarker/LibraryModelWrapper.java	2009-08-14 00:52:46 UTC (rev 15172)
@@ -0,0 +1,44 @@
+/*
+ * $Id$
+ *
+ * License Agreement.
+ *
+ * Rich Faces - Natural Ajax for Java Server Faces (JSF)
+ *
+ * Copyright (C) 2007 Exadel, Inc.
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License version 2.1 as published by the Free Software Foundation.
+ *
+ * This library 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 library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301  USA
+ */
+
+package org.richfaces.cdk.freemarker;
+
+import freemarker.template.DefaultObjectWrapper;
+import freemarker.template.ObjectWrapper;
+import freemarker.template.TemplateModel;
+import freemarker.template.TemplateModelException;
+
+/**
+ * <p class="changed_added_4_0"></p>
+ * @author asmirnov at exadel.com
+ *
+ */
+public class LibraryModelWrapper extends DefaultObjectWrapper implements
+		ObjectWrapper {
+
+	@Override
+	public TemplateModel wrap(Object obj) throws TemplateModelException {
+		// TODO wrap specified model classes.
+		return super.wrap(obj);
+	}
+}


Property changes on: root/cdk/trunk/plugins/generator/src/main/java/org/richfaces/cdk/freemarker/LibraryModelWrapper.java
___________________________________________________________________
Name: svn:mime-type
   + text/plain

Deleted: root/cdk/trunk/plugins/generator/src/main/java/org/richfaces/cdk/model/Attribute.java
===================================================================
--- root/cdk/trunk/plugins/generator/src/main/java/org/richfaces/cdk/model/Attribute.java	2009-08-13 16:45:29 UTC (rev 15171)
+++ root/cdk/trunk/plugins/generator/src/main/java/org/richfaces/cdk/model/Attribute.java	2009-08-14 00:52:46 UTC (rev 15172)
@@ -1,17 +0,0 @@
-package org.richfaces.cdk.model;
-
-public class Attribute {
-
-	private String type;
-
-	private String family;
-
-	private String className;
-
-	private String superClass;
-
-	private String renderKit;
-	
-	private String template;
-	
-}

Modified: root/cdk/trunk/plugins/generator/src/main/java/org/richfaces/cdk/model/ClassDescription.java
===================================================================
--- root/cdk/trunk/plugins/generator/src/main/java/org/richfaces/cdk/model/ClassDescription.java	2009-08-13 16:45:29 UTC (rev 15171)
+++ root/cdk/trunk/plugins/generator/src/main/java/org/richfaces/cdk/model/ClassDescription.java	2009-08-14 00:52:46 UTC (rev 15172)
@@ -33,8 +33,19 @@
  * @author asmirnov at exadel.com
  * 
  */
-public class ClassDescription {
+public class ClassDescription implements JsfComponent {
 
+	/**
+	  * <p class="changed_added_4_0"></p>
+	  */
+	private static final long serialVersionUID = -846623207703750456L;
+
+	public static final class Type extends JsfType {
+		public Type(String type) {
+			super(type);
+		}
+	}
+
 	private static final ImmutableMap<String, String> primitiveTypes = ImmutableMap
 			.<String, String> builder().put(boolean.class.getName(),
 					Boolean.class.getName()).put(byte.class.getName(),
@@ -46,13 +57,24 @@
 					Float.class.getName()).put(double.class.getName(),
 					Double.class.getName()).build();
 
+	/**
+	  * <p class="changed_added_4_0"></p>
+	  */
 	private final String name;
 
+	private final Type type ;
+
 	private final boolean primitive;
 
 	private final String boxingClassName;
 
 	private ClassDescription superClass;
+	
+	/**
+	  * <p class="changed_added_4_0">Type parameters for that class</p>
+	  * TODO append type parameters to key.
+	  */
+	private String typeParameters;
 
 	/**
 	 * <p class="changed_added_4_0">
@@ -69,8 +91,14 @@
 			boxingClassName = name;
 			primitive = false;
 		}
+		this.type = new Type(name);
 	}
 
+	@Override
+	public JsfType getType() {
+		return type;
+	}
+
 	/**
 	 * <p class="changed_added_4_0">
 	 * </p>
@@ -93,6 +121,22 @@
 	}
 
 	/**
+	 * <p class="changed_added_4_0"></p>
+	 * @return the typeParameters
+	 */
+	public String getTypeParameters() {
+		return typeParameters;
+	}
+
+	/**
+	 * <p class="changed_added_4_0"></p>
+	 * @param typeParameters the typeParameters to set
+	 */
+	public void setTypeParameters(String typeParameters) {
+		this.typeParameters = typeParameters;
+	}
+
+	/**
 	 * <p class="changed_added_4_0">
 	 * </p>
 	 * 
@@ -127,4 +171,46 @@
 		return name;
 	}
 
+	/* (non-Javadoc)
+	 * @see java.lang.Object#hashCode()
+	 */
+	@Override
+	public int hashCode() {
+		final int prime = 31;
+		int result = 1;
+		result = prime * result + ((name == null) ? 0 : name.hashCode());
+		return result;
+	}
+
+	/* (non-Javadoc)
+	 * @see java.lang.Object#equals(java.lang.Object)
+	 */
+	@Override
+	public boolean equals(Object obj) {
+		if (this == obj) {
+			return true;
+		}
+		if (obj == null) {
+			return false;
+		}
+		if (!(obj instanceof ClassDescription)) {
+			return false;
+		}
+		ClassDescription other = (ClassDescription) obj;
+		if (name == null) {
+			if (other.name != null) {
+				return false;
+			}
+		} else if (!name.equals(other.name)) {
+			return false;
+		}
+		return true;
+	}
+
+	@Override
+	public <R, P> R accept(LibraryVisitor<R, P> visitor, P param) {
+		// TODO Auto-generated method stub
+		return null;
+	}
+
 }

Modified: root/cdk/trunk/plugins/generator/src/main/java/org/richfaces/cdk/model/Component.java
===================================================================
--- root/cdk/trunk/plugins/generator/src/main/java/org/richfaces/cdk/model/Component.java	2009-08-13 16:45:29 UTC (rev 15171)
+++ root/cdk/trunk/plugins/generator/src/main/java/org/richfaces/cdk/model/Component.java	2009-08-14 00:52:46 UTC (rev 15172)
@@ -21,6 +21,7 @@
 	  * <p class="changed_added_4_0">Component type</p>
 	  */
 	private final Type type;
+	
 
 	/**
 	  * <p class="changed_added_4_0">component family</p>
@@ -177,7 +178,7 @@
 		return Collections.unmodifiableCollection(attributes.values());
 	}
 
-	public Property addAtribute(String attributeName) {
+	public Property addAttribute(String attributeName) {
 		Property attribute;
 		if(attributes.containsKey(attributeName)){
 			attribute = attributes.get(attributeName);

Modified: root/cdk/trunk/plugins/generator/src/main/java/org/richfaces/cdk/model/ComponentLibrary.java
===================================================================
--- root/cdk/trunk/plugins/generator/src/main/java/org/richfaces/cdk/model/ComponentLibrary.java	2009-08-13 16:45:29 UTC (rev 15171)
+++ root/cdk/trunk/plugins/generator/src/main/java/org/richfaces/cdk/model/ComponentLibrary.java	2009-08-14 00:52:46 UTC (rev 15172)
@@ -29,11 +29,17 @@
 import java.util.Collections;
 import java.util.List;
 import java.util.Map;
+import java.util.NoSuchElementException;
+import java.util.Set;
 
 import org.richfaces.cdk.model.Component.Type;
 import org.richfaces.cdk.util.Strings;
 
+import com.google.common.base.Predicate;
+import com.google.common.base.Predicates;
+import com.google.common.collect.Iterables;
 import com.google.common.collect.Maps;
+import com.google.common.collect.Sets;
 
 /**
  * <p class="changed_added_4_0">
@@ -55,16 +61,9 @@
 	 */
 	private static final long serialVersionUID = -6055670836731899832L;
 
-	/**
-	 * <p class="changed_added_4_0">
-	 * JSF components in that library
-	 * </p>
-	 */
-	private final Map<Component.Type, Component> components = Maps.newHashMap();
+	private final Set<Component> componentsSet = Sets.newHashSet();
 
-	private final Collection<Component> componentsSet = Collections
-			.unmodifiableCollection(components.values());
-
+	private final Set<ClassDescription> classes = Sets.newHashSet();
 	/**
 	 * <p class="changed_added_4_0">
 	 * JSF renderer associated with that library
@@ -87,8 +86,6 @@
 	 */
 	private final TagLibrary tagLibrary;
 
-
-
 	/**
 	 * <p class="changed_added_4_0">
 	 * </p>
@@ -106,10 +103,29 @@
 	 * 
 	 * @param otherLibrary
 	 */
-	public void apply(ComponentLibrary otherLibrary) {
+	public void merge(ComponentLibrary otherLibrary) {
 		// TODO Auto-generated method stub
 	}
 
+	public <R,P> R accept(LibraryVisitor<R, P> visitor,P param){
+		R result = accept(componentsSet,visitor, param, null);
+		result = accept(renderers,visitor, param, result);
+		result = accept(converters,visitor, param, result);
+		result = accept(validators,visitor, param, result);
+		result = accept(listeners,visitor, param, result);
+		return result;
+	}
+
+	private <R,P,T extends JsfComponent> R accept(Iterable<T> components, LibraryVisitor<R, P> visitor,P param, R result){
+		for (T t : components) {
+			R accept = t.accept(visitor, param);
+			if(null != accept){
+				result = accept;
+			}
+		}
+		return result;
+	}
+
 	/**
 	 * <p class="changed_added_4_0">
 	 * </p>
@@ -133,43 +149,77 @@
 	 */
 	public Component getComponent(String type, boolean create) {
 		Component.Type componentType = new Component.Type(type);
-		Component component = components.get(componentType);
-		if (null == component && create) {
-			component = new Component(componentType);
-			components.put(componentType, component);
+		Component component = null;
+		try {
+			component = find(componentsSet, componentType);
+		} catch (NoSuchElementException e) {
+			if (create) {
+				component = new Component(componentType);
+				componentsSet.add(component);
+			}
 		}
 		return component;
 	}
 
 	/**
-	 * <p class="changed_added_4_0">Create a new component description.</p>
-	 * @param type component type.
-	 * @param className final component class name.
-	 * @param superClassName name of the component superclass. May be empty or null for already existed components.
-	 * @return 
+	 * <p class="changed_added_4_0">
+	 * Create a new component description.
+	 * </p>
+	 * 
+	 * @param type
+	 *            component type.
+	 * @param className
+	 *            final component class name.
+	 * @param superClassName
+	 *            name of the component superclass. May be empty or null for
+	 *            already existed components.
+	 * @return
 	 */
-	public Component createComponent(String type, String className, String superClassName) {
+	public Component createComponent(String type, String className,
+			String superClassName) {
 		Component component = getComponent(type, true);
 		ClassDescription componentClass;
-		if(!Strings.isEmpty(className)){
-			componentClass = new ClassDescription(className);
-			if(!Strings.isEmpty(superClassName) && !className.equals(superClassName)){
-				componentClass.setSuperClass(new ClassDescription(superClassName));
+		if (!Strings.isEmpty(className)) {
+			componentClass = createClassDescription(className);
+			if (!Strings.isEmpty(superClassName)
+					&& !className.equals(superClassName)) {
+				componentClass
+						.setSuperClass(createClassDescription(superClassName));
 			}
-		} else if(!Strings.isEmpty(superClassName)) {
-			componentClass = new ClassDescription(superClassName);			
+		} else if (!Strings.isEmpty(superClassName)) {
+			componentClass = createClassDescription(superClassName);
 		} else {
-			// Do not modify class descriptions because it would be already existed component.
+			// Do not modify class descriptions because it would be already
+			// existed component.
 			return component;
 		}
 		component.setComponentClass(componentClass);
 		return component;
 	}
-	
+
 	/**
 	 * <p class="changed_added_4_0">
+	 * Gets or creates {@link ClassDescription} object.
 	 * </p>
 	 * 
+	 * @param className
+	 * @return
+	 */
+	public ClassDescription createClassDescription(String className) {
+		ClassDescription result;
+		try {
+			result = find(classes, new ClassDescription.Type(className));
+		} catch (NoSuchElementException e) {
+			result = new ClassDescription(className);
+			classes.add(result);
+		}
+		return result;
+	}
+
+	/**
+	 * <p class="changed_added_4_0">
+	 * </p>
+	 * 
 	 * @return the renderers
 	 */
 	public List<Renderer> getRenderers() {
@@ -226,5 +276,34 @@
 		return tagLibrary;
 	}
 
+	// Utility methods.
 
+	/**
+	 * <p class="changed_added_4_0">
+	 * Find element in the model collection.
+	 * </p>
+	 * 
+	 * @param <T>
+	 *            type of element to find.
+	 * @param collection
+	 *            of elements.
+	 * @param key
+	 *            for search.
+	 * @return existing element in the collection.
+	 * @throws NoSuchElementException
+	 *             if there was no such element in collection.
+	 */
+	private <T extends JsfComponent> T find(Iterable<T> collection,
+			final JsfType key) throws NoSuchElementException {
+		return Iterables.find(collection, new Predicate<T>() {
+
+			@Override
+			public boolean apply(T input) {
+
+				return key.equals(input.getType());
+			}
+
+		});
+	}
+
 }

Modified: root/cdk/trunk/plugins/generator/src/main/java/org/richfaces/cdk/model/Property.java
===================================================================
--- root/cdk/trunk/plugins/generator/src/main/java/org/richfaces/cdk/model/Property.java	2009-08-13 16:45:29 UTC (rev 15171)
+++ root/cdk/trunk/plugins/generator/src/main/java/org/richfaces/cdk/model/Property.java	2009-08-14 00:52:46 UTC (rev 15172)
@@ -46,6 +46,11 @@
 	  */
 	private String displayname;
 	
+	/**
+	  * <p class="changed_added_4_0">Is that bean property exists in the class or should be generated ?</p>
+	  */
+	private boolean exists;
+	
 
 	/**
 	 * <p class="changed_added_4_0"></p>
@@ -111,6 +116,22 @@
 		this.type = type;
 	}
 
+	/**
+	 * <p class="changed_added_4_0"></p>
+	 * @return the exists
+	 */
+	public boolean isExists() {
+		return exists;
+	}
+
+	/**
+	 * <p class="changed_added_4_0"></p>
+	 * @param exists the exists to set
+	 */
+	public void setExists(boolean exists) {
+		this.exists = exists;
+	}
+
 	/* (non-Javadoc)
 	 * @see java.lang.Object#hashCode()
 	 */

Modified: root/cdk/trunk/plugins/generator/src/test/java/org/richfaces/cdk/apt/ProcessorTest.java
===================================================================
--- root/cdk/trunk/plugins/generator/src/test/java/org/richfaces/cdk/apt/ProcessorTest.java	2009-08-13 16:45:29 UTC (rev 15171)
+++ root/cdk/trunk/plugins/generator/src/test/java/org/richfaces/cdk/apt/ProcessorTest.java	2009-08-14 00:52:46 UTC (rev 15172)
@@ -289,19 +289,17 @@
 						assertEquals(3, beanProperties.size());
 						BeanProperty property = Iterables.get(beanProperties,2);
 						assertEquals("foo", property.getName());
-						assertEquals("int", property.getTypeName());
+						assertEquals("int", property.getType().toString());
 						assertNull(property.getDocComment());
-						assertNull(property.getTypeParameters());
 						
 						property = Iterables.get(beanProperties,1);
 						assertEquals("testValue", property.getName());
-						assertEquals("java.util.List<java.lang.String>", property.getTypeName());
+						assertEquals("java.util.List<java.lang.String>", property.getType().toString());
 						assertEquals(" Test Attribute\n", property.getDocComment());
-						assertNull(property.getTypeParameters());
 						
 						property = Iterables.get(beanProperties,0);
 						assertEquals("barValue", property.getName());
-						assertEquals("java.util.List<M>", property.getTypeName());
+						assertEquals("java.util.List<M>", property.getType().toString());
 						assertEquals(" Bar Attribute\n", property.getDocComment());
 //						assertEquals("<M>" ,property.getTypeParameters());
 						



More information about the richfaces-svn-commits mailing list