Author: alexsmirnov
Date: 2010-07-06 19:35:46 -0400 (Tue, 06 Jul 2010)
New Revision: 17750
Modified:
root/commons/branches/RF8755/api/src/main/java/org/richfaces/renderkit/ComponentAttribute.java
root/commons/branches/RF8755/api/src/main/java/org/richfaces/renderkit/RenderKitUtils.java
Log:
https://jira.jboss.org/browse/RF-8306
Modified:
root/commons/branches/RF8755/api/src/main/java/org/richfaces/renderkit/ComponentAttribute.java
===================================================================
---
root/commons/branches/RF8755/api/src/main/java/org/richfaces/renderkit/ComponentAttribute.java 2010-07-06
23:35:30 UTC (rev 17749)
+++
root/commons/branches/RF8755/api/src/main/java/org/richfaces/renderkit/ComponentAttribute.java 2010-07-06
23:35:46 UTC (rev 17750)
@@ -29,15 +29,39 @@
* @author Nick Belaevski
*/
public class ComponentAttribute implements Comparable<ComponentAttribute> {
+
+ public enum Kind {
+ BOOLEAN,
+ GENERIC,
+ URI
+ }
private final String htmlAttributeName;
private String componentAttributeName;
- private String[] eventNames;
+ private String[] eventNames = {};
+ private Kind kind = Kind.GENERIC;
+
//TODO handling for aliases: "styleClass" -> "class"
+ /**
+ * <p class="changed_added_4_0"></p>
+ * @return the kind
+ */
+ public Kind getKind() {
+ return this.kind;
+ }
+
+ /**
+ * <p class="changed_added_4_0"></p>
+ * @param kind the kind to set
+ */
+ public void setKind(Kind kind) {
+ this.kind = kind;
+ }
+
public ComponentAttribute(String htmlAttributeName) {
super();
this.htmlAttributeName = htmlAttributeName;
Modified:
root/commons/branches/RF8755/api/src/main/java/org/richfaces/renderkit/RenderKitUtils.java
===================================================================
---
root/commons/branches/RF8755/api/src/main/java/org/richfaces/renderkit/RenderKitUtils.java 2010-07-06
23:35:30 UTC (rev 17749)
+++
root/commons/branches/RF8755/api/src/main/java/org/richfaces/renderkit/RenderKitUtils.java 2010-07-06
23:35:46 UTC (rev 17750)
@@ -24,10 +24,12 @@
import java.io.IOException;
import java.util.Arrays;
import java.util.Collection;
+import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
+import java.util.TreeSet;
import javax.faces.component.UIComponent;
import javax.faces.component.UIComponentBase;
@@ -39,6 +41,8 @@
import javax.faces.context.FacesContext;
import javax.faces.context.ResponseWriter;
+import org.richfaces.renderkit.ComponentAttribute.Kind;
+
/**
* @author Nick Belaevski
*
@@ -83,13 +87,14 @@
}
private static Map<String, List<ClientBehavior>>
getClientBehaviorsMap(UIComponent component) {
- Map<String, List<ClientBehavior>> result = null;
+ Map<String, List<ClientBehavior>> result;
if (component instanceof ClientBehaviorHolder) {
ClientBehaviorHolder clientBehaviorHolder = (ClientBehaviorHolder)
component;
result = clientBehaviorHolder.getClientBehaviors();
+ } else {
+ result = Collections.emptyMap();
}
-
return result;
}
@@ -98,14 +103,14 @@
if (disabledAttributeValue == null) {
return false;
}
-
+
if (disabledAttributeValue instanceof Boolean) {
return Boolean.TRUE.equals(disabledAttributeValue);
}
-
+
return Boolean.valueOf(disabledAttributeValue.toString());
}
-
+
static String escape(String s) {
StringBuilder sb = new StringBuilder(s.length());
int start = 0;
@@ -114,43 +119,43 @@
while ((end = s.indexOf('\'', start)) >= 0) {
sb.append(s, start, end);
sb.append("\\'");
-
+
start = end + 1;
}
-
+
sb.append(s, start, s.length());
-
+
return sb.toString();
}
-
+
static boolean chain(StringBuilder sb, Object object, boolean isChained) {
if (object != null) {
String objectString = object.toString().trim();
if (objectString.length() != 0) {
final boolean localIsChained;
-
+
if (!isChained && sb.length() != 0) {
- //extract previously stored handler
+ // extract previously stored handler
String previousHandlerString = sb.toString();
- //clear builder object
+ // clear builder object
sb.setLength(0);
-
- //append escaped handler
+
+ // append escaped handler
sb.append("'");
sb.append(escape(previousHandlerString));
sb.append("'");
-
+
localIsChained = true;
} else {
- //use passed in value of chained indicator
+ // use passed in value of chained indicator
localIsChained = isChained;
}
-
+
if (localIsChained) {
sb.append(",'");
sb.append(escape(objectString));
sb.append("'");
-
+
return true;
} else {
sb.append(objectString);
@@ -158,11 +163,11 @@
}
}
}
-
- //no changes, pass chained indicator we initially used
+
+ // no changes, pass chained indicator we initially used
return isChained;
}
-
+
private static Object createBehaviorsChain(Object inlineHandlerValue,
ClientBehaviorContext behaviorContext,
List<ClientBehavior> behaviors) {
@@ -172,7 +177,7 @@
isChained = chain(result, inlineHandlerValue, isChained);
for (ClientBehavior behavior : behaviors) {
isChained = chain(result, behavior.getScript(behaviorContext), isChained);
-
+
if (behavior.getHints().contains(ClientBehaviorHint.SUBMITTING)) {
break;
}
@@ -181,21 +186,20 @@
if (result.length() == 0) {
return null;
}
-
+
if (isChained) {
result.insert(0, "jsf.util.chain(");
result.append(")");
}
-
+
return result.toString();
}
- //TODO filter out empty strings
- static boolean shouldRenderAttribute(Object attributeValue) {
+ public static boolean shouldRenderAttribute(Object attributeValue) {
if (attributeValue == null) {
return false;
} else if (attributeValue instanceof String) {
- return true;
+ return ((String) attributeValue).length() > 0;
} else if (attributeValue instanceof Boolean &&
Boolean.FALSE.equals(attributeValue)) {
return false;
} else if (attributeValue instanceof Integer && (Integer) attributeValue
== Integer.MIN_VALUE) {
@@ -214,7 +218,7 @@
return false;
}
- return true;
+ return attributeValue.toString().length() > 0;
}
public static String prefixAttributeName(String attributeName, boolean isXhtmlMode)
{
@@ -241,7 +245,7 @@
ResponseWriter writer = facesContext.getResponseWriter();
String prefixedAttributeName = prefixAttributeName(attributeName, writer);
-
+
if (Arrays.binarySearch(URI_ATTRIBUTE_NAMES, attributeName) >= 0) {
writer.writeURIAttribute(prefixedAttributeName, attributeValue, null);
} else if (Arrays.binarySearch(BOOLEAN_ATTRIBUTE_NAMES, attributeName) >= 0)
{
@@ -272,20 +276,20 @@
String componentAttributeName = componentAttribute.getComponentAttributeName();
Object attributeValue = component.getAttributes().get(componentAttributeName);
- Map<String, List<ClientBehavior>> behaviorsMap =
getClientBehaviorsMap(component);
- if (behaviorsMap != null) {
- String[] eventNames = componentAttribute.getEventNames();
- if (eventNames != null) {
+ String[] eventNames = componentAttribute.getEventNames();
+ if (eventNames.length > 0) {
+ Map<String, List<ClientBehavior>> behaviorsMap =
getClientBehaviorsMap(component);
+ if (behaviorsMap.size() > 0) {
for (String eventName : eventNames) {
- List<ClientBehavior> behaviorsList =
behaviorsMap.get(eventName);
- if (behaviorsList != null) {
+ if (behaviorsMap.containsKey(eventName)) {
+ List<ClientBehavior> behaviorsList =
behaviorsMap.get(eventName);
if (!behaviorsList.isEmpty()) {
// TODO - parameters handling
- ClientBehaviorContext behaviorContext =
ClientBehaviorContext.createClientBehaviorContext(
- facesContext, component, eventName, null, null);
+ ClientBehaviorContext behaviorContext =
+
ClientBehaviorContext.createClientBehaviorContext(facesContext, component, eventName,
+ null, null);
attributeValue = createBehaviorsChain(attributeValue,
behaviorContext, behaviorsList);
}
-
break;
}
}
@@ -322,7 +326,7 @@
if (disabled && knownAttribute.getEventNames() != null) {
continue;
}
-
+
renderAttributeAndBehaviors(context, component, knownAttribute);
}
}
@@ -344,18 +348,21 @@
public static void renderPassThroughAttributes(FacesContext context, UIComponent
component,
Map<String, ComponentAttribute> knownAttributesMap) throws IOException {
+ Collection<ComponentAttribute> attributes = knownAttributesMap.values();
+ renderPassThroughAttributes(context, component, attributes);
+ }
+
+ public static void renderPassThroughAttributes(FacesContext context, UIComponent
component, Collection<ComponentAttribute> attributes)
+ throws IOException {
boolean disabled = isDisabled(component);
-
- for (ComponentAttribute knownAttribute : knownAttributesMap.values()) {
- if (disabled && knownAttribute.getEventNames() != null) {
- continue;
+ for (ComponentAttribute knownAttribute : attributes) {
+ if (!disabled || knownAttribute.getEventNames().length == 0) {
+ renderAttributeAndBehaviors(context, component, knownAttribute);
}
-
- renderAttributeAndBehaviors(context, component, knownAttribute);
}
}
-
+
public static String decodeBehaviors(FacesContext context, UIComponent component) {
if (!(component instanceof ClientBehaviorHolder)) {
return null;
@@ -385,12 +392,50 @@
for (ClientBehavior behavior : behaviorsForEvent) {
behavior.decode(context, component);
}
-
+
return behaviorEvent;
}
}
-
+
return null;
}
+ public static Attributes attributes() {
+ return new Attributes();
+ }
+
+ @SuppressWarnings("serial")
+ public static final class Attributes extends TreeSet<ComponentAttribute> {
+
+ public void render(FacesContext context, UIComponent component) throws
IOException {
+ renderPassThroughAttributes(context, component, this);
+ }
+
+ public Attributes literal(String name, String componentAttribute, String...
events) {
+ ComponentAttribute attribute = createAttribute(name, componentAttribute);
+ attribute.setEventNames(events);
+ attribute.setKind(Kind.GENERIC);
+ return this;
+ }
+
+ private ComponentAttribute createAttribute(String name, String
componentAttribute) {
+ ComponentAttribute attribute = new ComponentAttribute(name);
+ attribute.setComponentAttributeName(componentAttribute);
+ return attribute;
+ }
+
+ public Attributes uri(String name, String componentAttribute) {
+ ComponentAttribute attribute = createAttribute(name, componentAttribute);
+ attribute.setKind(Kind.URI);
+ return this;
+ }
+
+ public Attributes bool(String name, String componentAttribute) {
+ ComponentAttribute attribute = createAttribute(name, componentAttribute);
+ attribute.setKind(Kind.BOOLEAN);
+ return this;
+ }
+
+ }
+
}