You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@myfaces.apache.org by gp...@apache.org on 2011/05/29 01:10:41 UTC
svn commit: r1128795 - in
/myfaces/extensions/cdi/trunk/jee-modules/jsf-module/impl/src/main/java/org/apache/myfaces/extensions/cdi/jsf/impl/message:
JsfAwareMessageContextProducer.java MessageHelperMap.java
SimpleNamedArgument.java
Author: gpetracek
Date: Sat May 28 23:10:41 2011
New Revision: 1128795
URL: http://svn.apache.org/viewvc?rev=1128795&view=rev
Log:
EXTCDI-193 messageContext support in el-expressions
Added:
myfaces/extensions/cdi/trunk/jee-modules/jsf-module/impl/src/main/java/org/apache/myfaces/extensions/cdi/jsf/impl/message/MessageHelperMap.java
myfaces/extensions/cdi/trunk/jee-modules/jsf-module/impl/src/main/java/org/apache/myfaces/extensions/cdi/jsf/impl/message/SimpleNamedArgument.java
Modified:
myfaces/extensions/cdi/trunk/jee-modules/jsf-module/impl/src/main/java/org/apache/myfaces/extensions/cdi/jsf/impl/message/JsfAwareMessageContextProducer.java
Modified: myfaces/extensions/cdi/trunk/jee-modules/jsf-module/impl/src/main/java/org/apache/myfaces/extensions/cdi/jsf/impl/message/JsfAwareMessageContextProducer.java
URL: http://svn.apache.org/viewvc/myfaces/extensions/cdi/trunk/jee-modules/jsf-module/impl/src/main/java/org/apache/myfaces/extensions/cdi/jsf/impl/message/JsfAwareMessageContextProducer.java?rev=1128795&r1=1128794&r2=1128795&view=diff
==============================================================================
--- myfaces/extensions/cdi/trunk/jee-modules/jsf-module/impl/src/main/java/org/apache/myfaces/extensions/cdi/jsf/impl/message/JsfAwareMessageContextProducer.java (original)
+++ myfaces/extensions/cdi/trunk/jee-modules/jsf-module/impl/src/main/java/org/apache/myfaces/extensions/cdi/jsf/impl/message/JsfAwareMessageContextProducer.java Sat May 28 23:10:41 2011
@@ -40,16 +40,16 @@ public class JsfAwareMessageContextProdu
{
/**
* Creates a specialized {@link MessageContext} for JSF which delegates to jsf mechanisms (as fallback)
- * @param defaultMessageContext pre-configured message-context
+ *
+ * @param defaultMessageContext pre-configured message-context
* @param messageFactoryInstance current message-factory (optional)
- * @param elProviderInstance current el-provider (optional)
+ * @param elProviderInstance current el-provider (optional)
* @param argumentFilterInstance current argument-filter (optional)
* @return optimized message-context for jsf-applications
*/
@Produces
@Dependent
@Jsf
- @Named(MESSAGE_CONTEXT)
public MessageContext createContext(MessageContext defaultMessageContext,
Instance<MessageFactory> messageFactoryInstance,
Instance<ELProvider> elProviderInstance,
@@ -76,10 +76,10 @@ public class JsfAwareMessageContextProdu
MessageContext result = defaultMessageContext.config()
.use()
- .localeResolver(new JsfAwareLocaleResolver())
- .messageResolver(new JsfAwareApplicationMessagesMessageResolver())
- .messageInterpolator(new FacesMessageInterpolator(elProvider, argumentFilter))
- .addMessageHandler(new JsfAwareMessageHandler())
+ .localeResolver(new JsfAwareLocaleResolver())
+ .messageResolver(new JsfAwareApplicationMessagesMessageResolver())
+ .messageInterpolator(new FacesMessageInterpolator(elProvider, argumentFilter))
+ .addMessageHandler(new JsfAwareMessageHandler())
.create();
if (messageFactory != null)
@@ -89,4 +89,17 @@ public class JsfAwareMessageContextProdu
return result;
}
+
+ /**
+ * Creates a map for using the el-map-trick.
+ * @param messageContext jsf specific {@link MessageContext}
+ * @return helper map for el-expressions
+ */
+ @Produces
+ @Dependent
+ @Named(MESSAGE_CONTEXT)
+ public MessageHelperMap createContextForEL(final @Jsf MessageContext messageContext)
+ {
+ return new MessageHelperMap(messageContext);
+ }
}
Added: myfaces/extensions/cdi/trunk/jee-modules/jsf-module/impl/src/main/java/org/apache/myfaces/extensions/cdi/jsf/impl/message/MessageHelperMap.java
URL: http://svn.apache.org/viewvc/myfaces/extensions/cdi/trunk/jee-modules/jsf-module/impl/src/main/java/org/apache/myfaces/extensions/cdi/jsf/impl/message/MessageHelperMap.java?rev=1128795&view=auto
==============================================================================
--- myfaces/extensions/cdi/trunk/jee-modules/jsf-module/impl/src/main/java/org/apache/myfaces/extensions/cdi/jsf/impl/message/MessageHelperMap.java (added)
+++ myfaces/extensions/cdi/trunk/jee-modules/jsf-module/impl/src/main/java/org/apache/myfaces/extensions/cdi/jsf/impl/message/MessageHelperMap.java Sat May 28 23:10:41 2011
@@ -0,0 +1,197 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.myfaces.extensions.cdi.jsf.impl.message;
+
+import org.apache.myfaces.extensions.cdi.core.impl.util.UnmodifiableMap;
+import org.apache.myfaces.extensions.cdi.message.api.Localizable;
+import org.apache.myfaces.extensions.cdi.message.api.Message;
+import org.apache.myfaces.extensions.cdi.message.api.MessageContext;
+
+import javax.enterprise.inject.Typed;
+import javax.faces.context.FacesContext;
+import java.io.Serializable;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+/**
+ * Examples for the supported syntax: <br/>
+ * #{messageContext['msgKey'].toText} <br/>
+ * #{messageContext['msgKey'].toMessage} <br/>
+ *
+ * and
+ * <br/>
+ * for numbered arguments:<br/>
+ * #{messageContext['msgKey'][bean.value].toText}
+ * <br/>
+ * for named arguments:<br/>
+ * #{messageContext['msgKey']['argKey:#{bean.value}'].toText}
+ *
+ * @author Gerhard Petracek
+ */
+@Typed()
+class MessageHelperMap extends UnmodifiableMap<String, Object>
+{
+ private static final long serialVersionUID = 2530702568624997067L;
+
+ private MessageContext messageContext;
+
+ private List<Serializable> numberedArgument = new ArrayList<Serializable>();
+
+ private Map<String, Serializable> namedArgument = new HashMap<String, Serializable>();
+
+ private String messageKey;
+
+ MessageHelperMap(MessageContext messageContext)
+ {
+ this.messageContext = messageContext;
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public Object get(Object arg)
+ {
+ Serializable argument;
+
+ if(arg instanceof Serializable)
+ {
+ argument = (Serializable)arg;
+ }
+ else if(arg instanceof Localizable)
+ {
+ argument = ((Localizable)arg).toString(this.messageContext);
+ }
+ else
+ {
+ argument = arg.toString();
+ }
+
+ if("toText".equals(argument))
+ {
+ return getToText();
+ }
+
+ if("toMessage".equals(argument))
+ {
+ return getToMessage();
+ }
+
+ if(this.messageKey == null)
+ {
+ this.messageKey = "{" + argument + "}"; //always use a key - a hardcoded msg wouldn't be useful in this case
+
+ return this;
+ }
+
+ String argumentKey = null;
+ Object argumentValue = null;
+ String stringArgument;
+ if(argument instanceof String && ((String)argument).contains(":"))
+ {
+ stringArgument = ((String)argument);
+ String[] keyValuePair = stringArgument.split(":");
+
+ if(keyValuePair.length > 1)
+ {
+ String key = keyValuePair[0];
+
+ if(!key.contains(" "))
+ {
+ argumentKey = key;
+ }
+ argumentValue = stringArgument.substring(stringArgument.indexOf(":") + 1);
+ String expression = argumentValue.toString().trim();
+ if(expression.startsWith("#{") && expression.endsWith("}"))
+ {
+ FacesContext facesContext = FacesContext.getCurrentInstance();
+ argumentValue = facesContext.getApplication()
+ .evaluateExpressionGet(facesContext, expression, Object.class);
+ }
+ }
+ }
+
+ if(argumentValue == null)
+ {
+ argumentValue = argument;
+ }
+
+ Serializable value;
+
+ if(argumentValue instanceof Serializable)
+ {
+ value = (Serializable)argumentValue;
+ }
+ else if(argumentValue instanceof Localizable)
+ {
+ value = ((Localizable)argumentValue).toString(this.messageContext);
+ }
+ else if(argumentValue != null)
+ {
+ value = argumentValue.toString();
+ }
+ else
+ {
+ value = null;
+ }
+
+ if(argumentKey == null)
+ {
+ this.numberedArgument.add(value);
+ }
+ else
+ {
+ this.namedArgument.put(argumentKey, value);
+ }
+ return this;
+ }
+
+ /**
+ * Creates the message for the current state.
+ * @return message for the current state
+ */
+ public Message getToMessage()
+ {
+ Message message = this.messageContext.message().text(this.messageKey).create();
+
+ for(Serializable argument : this.numberedArgument)
+ {
+ message.addArgument(argument);
+ }
+
+ for(Map.Entry<String, Serializable> entry : this.namedArgument.entrySet())
+ {
+ message.addArgument(new SimpleNamedArgument(entry.getKey(), entry.getValue()));
+ }
+ return message;
+ }
+
+ /**
+ * Creates the message and returns the text of it.
+ * @return text of the message
+ */
+ public String getToText()
+ {
+ Message message = getToMessage();
+
+ return message.toString(this.messageContext);
+ }
+}
Added: myfaces/extensions/cdi/trunk/jee-modules/jsf-module/impl/src/main/java/org/apache/myfaces/extensions/cdi/jsf/impl/message/SimpleNamedArgument.java
URL: http://svn.apache.org/viewvc/myfaces/extensions/cdi/trunk/jee-modules/jsf-module/impl/src/main/java/org/apache/myfaces/extensions/cdi/jsf/impl/message/SimpleNamedArgument.java?rev=1128795&view=auto
==============================================================================
--- myfaces/extensions/cdi/trunk/jee-modules/jsf-module/impl/src/main/java/org/apache/myfaces/extensions/cdi/jsf/impl/message/SimpleNamedArgument.java (added)
+++ myfaces/extensions/cdi/trunk/jee-modules/jsf-module/impl/src/main/java/org/apache/myfaces/extensions/cdi/jsf/impl/message/SimpleNamedArgument.java Sat May 28 23:10:41 2011
@@ -0,0 +1,58 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.myfaces.extensions.cdi.jsf.impl.message;
+
+import org.apache.myfaces.extensions.cdi.message.api.NamedArgument;
+
+import javax.enterprise.inject.Typed;
+import java.io.Serializable;
+
+/**
+ * @author Gerhard Petracek
+ */
+@Typed()
+class SimpleNamedArgument implements NamedArgument
+{
+ private static final long serialVersionUID = -2658137685121716653L;
+
+ private final String name;
+ private final Serializable value;
+
+ SimpleNamedArgument(String name, Serializable value)
+ {
+ this.name = name;
+ this.value = value;
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public String getName()
+ {
+ return name;
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public Serializable getValue()
+ {
+ return value;
+ }
+}