private void copyRendererAttributes(final RendererModel renderer, ComponentModel component) { for (PropertyBase property : renderer.getAttributes()) { PropertyBase attribute = component.getOrCreateAttribute(property.getName()); attribute.merge(property); verifyAttribute(attribute, component); } renderer.setFamily(component.getFamily()); }
/** * @param library * @param component * @param verified */ protected void verifyComponentAttributes( ComponentLibrary library, final ComponentModel component, Collection<ComponentModel> verified) { // There is potential StackOverflow, so we process only components which have not been // verified before. if (!verified.contains(component)) { // Propagate attributes from parent component, if any. verified.add(component); if (null != component.getBaseClass()) { try { // Step one, lookup for parent. ComponentModel parentComponent = findParent(library.getComponents(), component); component.setParent(parentComponent); if (null == component.getFamily()) { component.setFamily(parentComponent.getFamily()); } // To be sure what all properties for parent component were propagated. verifyComponentAttributes(library, parentComponent, verified); for (PropertyBase parentAttribute : parentComponent.getAttributes()) { PropertyBase attribute = component.getOrCreateAttribute(parentAttribute.getName()); attribute.merge(parentAttribute); // already exists in parent component. attribute.setGenerate(false); } } catch (NoSuchElementException e) { // No parent component in the library } } // Check attributes. for (PropertyBase attribute : component.getAttributes()) { verifyAttribute(attribute, component); } // compact(component.getAttributes()); // Check renderers. // Check Tag for (TagModel tag : component.getTags()) { verifyTag(tag, component.getId(), DEFAULT_COMPONENT_HANDLER); } verifyDescription(component); for (FacetModel facet : component.getFacets()) { verifyDescription(facet); } } }
private BeanProperty findBeanProperty(PropertyBase attribute, GeneratedFacesComponent component) { SourceUtils sourceUtils = sourceUtilsProvider.get(); BeanProperty beanProperty = sourceUtils.getBeanProperty(component.getBaseClass(), attribute.getName()); if (beanProperty instanceof DummyPropertyImpl && component instanceof ComponentModel) { ComponentModel model = (ComponentModel) component; if (null != model.getParent()) { beanProperty = findBeanProperty(attribute, model.getParent()); } } if (beanProperty instanceof DummyPropertyImpl && component instanceof ModelElementBase) { ModelElementBase model = (ModelElementBase) component; for (ClassName interfaceName : model.getInterfaces()) { beanProperty = sourceUtils.getBeanProperty(interfaceName, attribute.getName()); if (!(beanProperty instanceof DummyPropertyImpl)) { break; } } } return beanProperty; }
public static ComponentModel createComponent() { ComponentModel component = new ComponentModel(FacesId.parseId("foo.bar")); component.setGenerate(true); component.setTargetClass( ClassName.parseName("org.richfaces.cdk.generate.java.GeneratedComponent")); component.setBaseClass(ClassName.parseName(UIOutput.class.getName())); component.setRendererType(FacesId.parseId("foo.barRenderer")); PropertyBase attribute = component.getOrCreateAttribute("testValue"); attribute.setType(new ClassName(Object.class)); attribute.setGenerate(true); attribute = component.getOrCreateAttribute("testFlag"); attribute.setType(new ClassName(Boolean.TYPE)); attribute.setRequired(true); attribute.setGenerate(true); attribute = component.getOrCreateAttribute("testBinding"); attribute.setType(new ClassName(MethodBinding.class)); attribute.setGenerate(true); attribute.setBinding(true); attribute.setBindingAttribute(true); attribute = component.getOrCreateAttribute("testExpr"); attribute.setType(new ClassName(MethodExpression.class)); attribute.setGenerate(true); attribute.setBindingAttribute(true); MethodSignature signature = new MethodSignature(); signature.setParameters( Arrays.asList(new ClassName(String.class), new ClassName(Integer.class))); attribute.setSignature(signature); attribute = component.getOrCreateAttribute("id"); attribute.setType(new ClassName(String.class)); attribute.setGenerate(false); attribute = component.getOrCreateAttribute("listStrings"); attribute.setType(new ClassName(new ArrayList<String>().getClass())); attribute.setGenerate(true); attribute = component.getOrCreateAttribute("listInteger"); attribute.setType(new ClassName(new ArrayList<Integer>().getClass())); attribute.setGenerate(true); attribute = component.getOrCreateAttribute("list"); attribute.setType(new ClassName(ArrayList.class)); attribute.setGenerate(true); Set<EventName> eventNames = attribute.getEventNames(); eventNames.add(getEvent("id", false)); eventNames.add(getEvent("action", true)); return component; }
@Test public void propertyTest() throws Exception { Collection<PropertyBase> properties = parser.parseProperties("urn:resource:org/richfaces/cdk/xmlconfig/properties.xml"); assertEquals(1, properties.size()); PropertyBase property = Iterables.getOnlyElement(properties); assertEquals("ontest2", property.getName()); assertEquals("int", property.getType().getName()); assertEquals("test2 property", property.getDescription()); assertEquals("ontest2.png", property.getIcon().getSmallIcon()); assertEquals("test2 event property", property.getDisplayName()); assertEquals("3", property.getDefaultValue()); assertEquals("15", property.getSuggestedValue()); // CDK extensions. assertTrue(property.getGenerate()); assertTrue(property.isHidden()); assertTrue(property.isLiteral()); assertTrue(property.isPassThrough()); assertTrue(property.isRequired()); List<ClassName> signature = property.getSignature().getParameters(); assertEquals(2, signature.size()); String alias = property.getAliasFor(); assertEquals("bar", alias); }
protected void verifyAttribute(PropertyBase attribute, GeneratedFacesComponent component) { // Check name. if (Strings.isEmpty(attribute.getName())) { log.error("No name for attribute " + attribute); return; } if (attribute.getName().contains(".") || Character.isDigit(attribute.getName().charAt(0)) || attribute.getName().contains(" ")) { log.error("Invalid attribute name [" + attribute.getName() + "]"); return; } // Check type BeanProperty beanProperty = findBeanProperty(attribute, component); if (null == attribute.getType()) { log.warn("Unknown type of attribute [" + attribute.getName() + "]"); attribute.setType(beanProperty.getType()); } if (attribute.getType().isPrimitive() && null == attribute.getDefaultValue()) { // Set default value for primitive attribute.setDefaultValue(attribute.getType().getDefaultValue()); } // Check binding properties. if ("javax.faces.el.MethodBinding".equals(attribute.getType().getName())) { attribute.setBinding(true); attribute.setBindingAttribute(true); } else if ("javax.el.MethodExpression".equals(attribute.getType().getName())) { attribute.setBindingAttribute(true); } // if(attribute.isBindingAttribute() && attribute.getSignature().isEmpty() && // !attribute.isHidden()) { // log.error("Signature for method expression attribute "+attribute.getName()+" has not been // set"); // } // Check "generate" flag. if (Boolean.TRUE.equals(component.getGenerate())) { // TODO Attribute should be only generated if it does not exist or abstract in the base class. // Step one - check base class if (SPECIAL_PROPERTIES.contains(attribute.getName())) { attribute.setGenerate(false); } else if (null == attribute.getGenerate()) { attribute.setGenerate(!beanProperty.isExists()); } } else { attribute.setGenerate(false); } verifyDescription(attribute); }