com.sun.faces.facelets.tag.composite.DeclareFacetHandler Maven / Gradle / Ivy
/*
* Copyright (c) 1997, 2018 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0, which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* This Source Code may also be made available under the following Secondary
* Licenses when the conditions for such availability set forth in the
* Eclipse Public License v. 2.0 are satisfied: GNU General Public License,
* version 2 with the GNU Classpath Exception, which is available at
* https://www.gnu.org/software/classpath/license.html.
*
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
*/
package com.sun.faces.facelets.tag.composite;
import com.sun.faces.facelets.tag.TagHandlerImpl;
import javax.el.ValueExpression;
import javax.faces.component.UIComponent;
import javax.faces.view.facelets.*;
import java.beans.BeanDescriptor;
import java.beans.IntrospectionException;
import java.beans.PropertyDescriptor;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
public class DeclareFacetHandler extends TagHandlerImpl {
private static final String[] ATTRIBUTES = {
"required",
"displayName",
"expert",
"hidden",
"preferred",
"shortDescription",
"default"
};
private static final PropertyHandlerManager ATTRIBUTE_MANAGER =
PropertyHandlerManager.getInstance(ATTRIBUTES);
private TagAttribute name = null;
public DeclareFacetHandler(TagConfig config) {
super(config);
this.name = this.getRequiredAttribute("name");
}
@SuppressWarnings({"unchecked"})
@Override
public void apply(FaceletContext ctx, UIComponent parent) throws IOException {
// only process if it's been created
if (null == parent ||
(null == (parent = parent.getParent())) ||
!(ComponentHandler.isNew(parent))) {
return;
}
Map componentAttrs = parent.getAttributes();
CompositeComponentBeanInfo componentBeanInfo = (CompositeComponentBeanInfo)
componentAttrs.get(UIComponent.BEANINFO_KEY);
// Get the value of required the name propertyDescriptor
ValueExpression ve = name.getValueExpression(ctx, String.class);
String strValue = (String) ve.getValue(ctx);
BeanDescriptor componentBeanDescriptor = componentBeanInfo.getBeanDescriptor();
Map facetDescriptors = (Map)
componentBeanDescriptor.getValue(UIComponent.FACETS_KEY);
if (facetDescriptors == null) {
facetDescriptors = new HashMap<>();
componentBeanDescriptor.setValue(UIComponent.FACETS_KEY,
facetDescriptors);
}
PropertyDescriptor propertyDescriptor;
try {
propertyDescriptor = new PropertyDescriptor(strValue, null, null);
} catch (IntrospectionException ex) {
throw new TagException(tag, "Unable to create property descriptor for facet" + strValue, ex);
}
facetDescriptors.put(strValue, propertyDescriptor);
for (TagAttribute tagAttribute : this.tag.getAttributes().getAll()) {
String attributeName = tagAttribute.getLocalName();
PropertyHandler handler = ATTRIBUTE_MANAGER.getHandler(ctx, attributeName);
if (handler != null) {
handler.apply(ctx, attributeName, propertyDescriptor, tagAttribute);
}
}
this.nextHandler.apply(ctx, parent);
}
}