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 2010/04/17 16:53:43 UTC
svn commit: r935183 [2/4] - in /myfaces/extensions/cdi/trunk: ./
jse-modules/ jse-modules/message-module/ jse-modules/message-module/api/
jse-modules/message-module/api/src/ jse-modules/message-module/api/src/main/
jse-modules/message-module/api/src/ma...
Added: myfaces/extensions/cdi/trunk/jse-modules/message-module/api/src/main/resources/LICENSE.txt
URL: http://svn.apache.org/viewvc/myfaces/extensions/cdi/trunk/jse-modules/message-module/api/src/main/resources/LICENSE.txt?rev=935183&view=auto
==============================================================================
--- myfaces/extensions/cdi/trunk/jse-modules/message-module/api/src/main/resources/LICENSE.txt (added)
+++ myfaces/extensions/cdi/trunk/jse-modules/message-module/api/src/main/resources/LICENSE.txt Sat Apr 17 14:53:40 2010
@@ -0,0 +1,174 @@
+ Apache License
+ Version 2.0, January 2004
+ http://www.apache.org/licenses/
+
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
+
+ 1. Definitions.
+
+ "License" shall mean the terms and conditions for use, reproduction,
+ and distribution as defined by Sections 1 through 9 of this document.
+
+ "Licensor" shall mean the copyright owner or entity authorized by
+ the copyright owner that is granting the License.
+
+ "Legal Entity" shall mean the union of the acting entity and all
+ other entities that control, are controlled by, or are under common
+ control with that entity. For the purposes of this definition,
+ "control" means (i) the power, direct or indirect, to cause the
+ direction or management of such entity, whether by contract or
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
+ outstanding shares, or (iii) beneficial ownership of such entity.
+
+ "You" (or "Your") shall mean an individual or Legal Entity
+ exercising permissions granted by this License.
+
+ "Source" form shall mean the preferred form for making modifications,
+ including but not limited to software source code, documentation
+ source, and configuration files.
+
+ "Object" form shall mean any form resulting from mechanical
+ transformation or translation of a Source form, including but
+ not limited to compiled object code, generated documentation,
+ and conversions to other media types.
+
+ "Work" shall mean the work of authorship, whether in Source or
+ Object form, made available under the License, as indicated by a
+ copyright notice that is included in or attached to the work
+ (an example is provided in the Appendix below).
+
+ "Derivative Works" shall mean any work, whether in Source or Object
+ form, that is based on (or derived from) the Work and for which the
+ editorial revisions, annotations, elaborations, or other modifications
+ represent, as a whole, an original work of authorship. For the purposes
+ of this License, Derivative Works shall not include works that remain
+ separable from, or merely link (or bind by name) to the interfaces of,
+ the Work and Derivative Works thereof.
+
+ "Contribution" shall mean any work of authorship, including
+ the original version of the Work and any modifications or additions
+ to that Work or Derivative Works thereof, that is intentionally
+ submitted to Licensor for inclusion in the Work by the copyright owner
+ or by an individual or Legal Entity authorized to submit on behalf of
+ the copyright owner. For the purposes of this definition, "submitted"
+ means any form of electronic, verbal, or written communication sent
+ to the Licensor or its representatives, including but not limited to
+ communication on electronic mailing lists, source code control systems,
+ and issue tracking systems that are managed by, or on behalf of, the
+ Licensor for the purpose of discussing and improving the Work, but
+ excluding communication that is conspicuously marked or otherwise
+ designated in writing by the copyright owner as "Not a Contribution."
+
+ "Contributor" shall mean Licensor and any individual or Legal Entity
+ on behalf of whom a Contribution has been received by Licensor and
+ subsequently incorporated within the Work.
+
+ 2. Grant of Copyright License. Subject to the terms and conditions of
+ this License, each Contributor hereby grants to You a perpetual,
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
+ copyright license to reproduce, prepare Derivative Works of,
+ publicly display, publicly perform, sublicense, and distribute the
+ Work and such Derivative Works in Source or Object form.
+
+ 3. Grant of Patent License. Subject to the terms and conditions of
+ this License, each Contributor hereby grants to You a perpetual,
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
+ (except as stated in this section) patent license to make, have made,
+ use, offer to sell, sell, import, and otherwise transfer the Work,
+ where such license applies only to those patent claims licensable
+ by such Contributor that are necessarily infringed by their
+ Contribution(s) alone or by combination of their Contribution(s)
+ with the Work to which such Contribution(s) was submitted. If You
+ institute patent litigation against any entity (including a
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
+ or a Contribution incorporated within the Work constitutes direct
+ or contributory patent infringement, then any patent licenses
+ granted to You under this License for that Work shall terminate
+ as of the date such litigation is filed.
+
+ 4. Redistribution. You may reproduce and distribute copies of the
+ Work or Derivative Works thereof in any medium, with or without
+ modifications, and in Source or Object form, provided that You
+ meet the following conditions:
+
+ (a) You must give any other recipients of the Work or
+ Derivative Works a copy of this License; and
+
+ (b) You must cause any modified files to carry prominent notices
+ stating that You changed the files; and
+
+ (c) You must retain, in the Source form of any Derivative Works
+ that You distribute, all copyright, patent, trademark, and
+ attribution notices from the Source form of the Work,
+ excluding those notices that do not pertain to any part of
+ the Derivative Works; and
+
+ (d) If the Work includes a "NOTICE" text file as part of its
+ distribution, then any Derivative Works that You distribute must
+ include a readable copy of the attribution notices contained
+ within such NOTICE file, excluding those notices that do not
+ pertain to any part of the Derivative Works, in at least one
+ of the following places: within a NOTICE text file distributed
+ as part of the Derivative Works; within the Source form or
+ documentation, if provided along with the Derivative Works; or,
+ within a display generated by the Derivative Works, if and
+ wherever such third-party notices normally appear. The contents
+ of the NOTICE file are for informational purposes only and
+ do not modify the License. You may add Your own attribution
+ notices within Derivative Works that You distribute, alongside
+ or as an addendum to the NOTICE text from the Work, provided
+ that such additional attribution notices cannot be construed
+ as modifying the License.
+
+ You may add Your own copyright statement to Your modifications and
+ may provide additional or different license terms and conditions
+ for use, reproduction, or distribution of Your modifications, or
+ for any such Derivative Works as a whole, provided Your use,
+ reproduction, and distribution of the Work otherwise complies with
+ the conditions stated in this License.
+
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
+ any Contribution intentionally submitted for inclusion in the Work
+ by You to the Licensor shall be under the terms and conditions of
+ this License, without any additional terms or conditions.
+ Notwithstanding the above, nothing herein shall supersede or modify
+ the terms of any separate license agreement you may have executed
+ with Licensor regarding such Contributions.
+
+ 6. Trademarks. This License does not grant permission to use the trade
+ names, trademarks, service marks, or product names of the Licensor,
+ except as required for reasonable and customary use in describing the
+ origin of the Work and reproducing the content of the NOTICE file.
+
+ 7. Disclaimer of Warranty. Unless required by applicable law or
+ agreed to in writing, Licensor provides the Work (and each
+ Contributor provides its Contributions) on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
+ implied, including, without limitation, any warranties or conditions
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
+ PARTICULAR PURPOSE. You are solely responsible for determining the
+ appropriateness of using or redistributing the Work and assume any
+ risks associated with Your exercise of permissions under this License.
+
+ 8. Limitation of Liability. In no event and under no legal theory,
+ whether in tort (including negligence), contract, or otherwise,
+ unless required by applicable law (such as deliberate and grossly
+ negligent acts) or agreed to in writing, shall any Contributor be
+ liable to You for damages, including any direct, indirect, special,
+ incidental, or consequential damages of any character arising as a
+ result of this License or out of the use or inability to use the
+ Work (including but not limited to damages for loss of goodwill,
+ work stoppage, computer failure or malfunction, or any and all
+ other commercial damages or losses), even if such Contributor
+ has been advised of the possibility of such damages.
+
+ 9. Accepting Warranty or Additional Liability. While redistributing
+ the Work or Derivative Works thereof, You may choose to offer,
+ and charge a fee for, acceptance of support, warranty, indemnity,
+ or other liability obligations and/or rights consistent with this
+ License. However, in accepting such obligations, You may act only
+ on Your own behalf and on Your sole responsibility, not on behalf
+ of any other Contributor, and only if You agree to indemnify,
+ defend, and hold each Contributor harmless for any liability
+ incurred by, or claims asserted against, such Contributor by reason
+ of your accepting any such warranty or additional liability.
Added: myfaces/extensions/cdi/trunk/jse-modules/message-module/api/src/main/resources/NOTICE.txt
URL: http://svn.apache.org/viewvc/myfaces/extensions/cdi/trunk/jse-modules/message-module/api/src/main/resources/NOTICE.txt?rev=935183&view=auto
==============================================================================
--- myfaces/extensions/cdi/trunk/jse-modules/message-module/api/src/main/resources/NOTICE.txt (added)
+++ myfaces/extensions/cdi/trunk/jse-modules/message-module/api/src/main/resources/NOTICE.txt Sat Apr 17 14:53:40 2010
@@ -0,0 +1,9 @@
+Apache MyFaces Extensions CDI (aka MyFaces CODI)
+Copyright 2010 The Apache Software Foundation
+
+This product includes software developed by
+The Apache Software Foundation (http://www.apache.org/).
+
+------------------------------------------------------------------------
+See the file LICENSE.txt
+------------------------------------------------------------------------
\ No newline at end of file
Added: myfaces/extensions/cdi/trunk/jse-modules/message-module/impl/pom.xml
URL: http://svn.apache.org/viewvc/myfaces/extensions/cdi/trunk/jse-modules/message-module/impl/pom.xml?rev=935183&view=auto
==============================================================================
--- myfaces/extensions/cdi/trunk/jse-modules/message-module/impl/pom.xml (added)
+++ myfaces/extensions/cdi/trunk/jse-modules/message-module/impl/pom.xml Sat Apr 17 14:53:40 2010
@@ -0,0 +1,148 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+ 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.
+-->
+<project xmlns="http://maven.apache.org/POM/4.0.0"
+ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+ xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
+ <modelVersion>4.0.0</modelVersion>
+ <packaging>jar</packaging>
+
+ <groupId>org.apache.myfaces.extensions.cdi.jse-modules</groupId>
+ <artifactId>myfaces-extcdi-message-module-impl</artifactId>
+
+ <name>MyFaces Extensions-CDI I18N Message 1.0 Module Impl</name>
+
+ <parent>
+ <groupId>org.apache.myfaces.extensions.cdi.jse-modules</groupId>
+ <artifactId>message-module-project</artifactId>
+ <version>1.0.0-SNAPSHOT</version>
+ </parent>
+
+ <dependencies>
+ <dependency>
+ <groupId>org.apache.myfaces.extensions.cdi.jse-modules</groupId>
+ <artifactId>myfaces-extcdi-message-module-api</artifactId>
+ </dependency>
+
+ <dependency>
+ <groupId>org.apache.geronimo.specs</groupId>
+ <artifactId>geronimo-atinject_1.0_spec</artifactId>
+ <version>1.0-SNAPSHOT</version>
+ <scope>provided</scope>
+ </dependency>
+
+ <dependency>
+ <groupId>juel</groupId>
+ <artifactId>juel</artifactId>
+ <version>2.2.1</version>
+ <scope>provided</scope>
+ </dependency>
+
+ <dependency>
+ <groupId>junit</groupId>
+ <artifactId>junit</artifactId>
+ <version>4.7</version>
+ <scope>test</scope>
+ </dependency>
+ </dependencies>
+
+ <repositories>
+
+ <repository>
+ <id>repository.jboss.org</id>
+ <name>JBoss Repository for Maven</name>
+ <url>http://repository.jboss.org/maven2</url>
+ </repository>
+
+ </repositories>
+
+ <build>
+ <resources>
+ <resource>
+ <directory>src/main/config</directory>
+ <includes>
+ <include>**/*xml</include>
+ </includes>
+ <targetPath>/META-INF</targetPath>
+ </resource>
+ <resource>
+ <directory>src/main/config</directory>
+ <includes>
+ <include>javax.enterprise.inject.spi.Extension</include>
+ </includes>
+ <targetPath>/META-INF/services/</targetPath>
+ </resource>
+ <resource>
+ <directory>src/main/resources</directory>
+ <includes>
+ <include>LICENSE.txt</include>
+ <include>NOTICE.txt</include>
+ </includes>
+ <targetPath>/META-INF</targetPath>
+ </resource>
+ <resource>
+ <directory>src/main/java</directory>
+ <includes>
+ <include>**/*properties</include>
+ </includes>
+ </resource>
+ </resources>
+ <plugins>
+ <plugin>
+ <inherited>true</inherited>
+ <groupId>org.apache.maven.plugins</groupId>
+ <artifactId>maven-source-plugin</artifactId>
+
+ <executions>
+ <execution>
+ <id>attach-sources</id>
+ <goals>
+ <goal>jar</goal>
+ </goals>
+ </execution>
+ </executions>
+ </plugin>
+
+ <plugin>
+ <!-- exclude classes for testing -->
+ <groupId>org.apache.maven.plugins</groupId>
+ <artifactId>maven-surefire-plugin</artifactId>
+ <configuration>
+ <excludes>
+ <exclude>**/Test*.java</exclude>
+ <exclude>**/Abstract*.java</exclude>
+ <exclude>**/*$*.java</exclude>
+ </excludes>
+ </configuration>
+ </plugin>
+ </plugins>
+ <testResources>
+ <testResource>
+ <directory>src/test/java</directory>
+ <includes>
+ <include>**/*</include>
+ </includes>
+ <excludes>
+ <exclude>**/*.java</exclude>
+ </excludes>
+ </testResource>
+ </testResources>
+ </build>
+
+</project>
Added: myfaces/extensions/cdi/trunk/jse-modules/message-module/impl/src/main/config/beans.xml
URL: http://svn.apache.org/viewvc/myfaces/extensions/cdi/trunk/jse-modules/message-module/impl/src/main/config/beans.xml?rev=935183&view=auto
==============================================================================
--- myfaces/extensions/cdi/trunk/jse-modules/message-module/impl/src/main/config/beans.xml (added)
+++ myfaces/extensions/cdi/trunk/jse-modules/message-module/impl/src/main/config/beans.xml Sat Apr 17 14:53:40 2010
@@ -0,0 +1,20 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+ 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.
+-->
+<beans/>
\ No newline at end of file
Added: myfaces/extensions/cdi/trunk/jse-modules/message-module/impl/src/main/java/org/apache/myfaces/extensions/cdi/message/impl/AbstractFormatterAwareMessageInterpolator.java
URL: http://svn.apache.org/viewvc/myfaces/extensions/cdi/trunk/jse-modules/message-module/impl/src/main/java/org/apache/myfaces/extensions/cdi/message/impl/AbstractFormatterAwareMessageInterpolator.java?rev=935183&view=auto
==============================================================================
--- myfaces/extensions/cdi/trunk/jse-modules/message-module/impl/src/main/java/org/apache/myfaces/extensions/cdi/message/impl/AbstractFormatterAwareMessageInterpolator.java (added)
+++ myfaces/extensions/cdi/trunk/jse-modules/message-module/impl/src/main/java/org/apache/myfaces/extensions/cdi/message/impl/AbstractFormatterAwareMessageInterpolator.java Sat Apr 17 14:53:40 2010
@@ -0,0 +1,76 @@
+/*
+ * 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.message.impl;
+
+import org.apache.myfaces.extensions.cdi.message.api.MessageContext;
+import org.apache.myfaces.extensions.cdi.message.api.MessageContextAware;
+import org.apache.myfaces.extensions.cdi.message.api.MessageInterpolator;
+import org.apache.myfaces.extensions.cdi.message.api.Localizable;
+import org.apache.myfaces.extensions.cdi.message.api.Formatter;
+
+import java.io.Serializable;
+
+/**
+ * @author Gerhard Petracek
+ */
+abstract class AbstractFormatterAwareMessageInterpolator
+ implements MessageInterpolator, MessageContextAware, Serializable
+{
+ protected MessageContext messageContext;
+
+ @SuppressWarnings({"unchecked"})
+ protected Object formatAsString(Object value)
+ {
+ if (value == null)
+ {
+ return null;
+ }
+
+ if (value instanceof String)
+ {
+ return value;
+ }
+
+ if (this.messageContext.config().getFormatterFactory() != null)
+ {
+ Formatter formatter = this.messageContext.config().getFormatterFactory().findFormatter(value.getClass());
+ if (formatter != null)
+ {
+ return formatter.format(this.messageContext, value);
+ }
+ }
+
+ if (value instanceof Localizable)
+ {
+ return ((Localizable) value).toString(this.messageContext);
+ }
+
+ return value;
+ }
+
+ public void setMessageContext(MessageContext messageContext)
+ {
+ this.messageContext = messageContext;
+ }
+
+ public MessageContext getMessageContext()
+ {
+ throw new UnsupportedOperationException();
+ }
+}
Added: myfaces/extensions/cdi/trunk/jse-modules/message-module/impl/src/main/java/org/apache/myfaces/extensions/cdi/message/impl/CompositeMessageInterpolator.java
URL: http://svn.apache.org/viewvc/myfaces/extensions/cdi/trunk/jse-modules/message-module/impl/src/main/java/org/apache/myfaces/extensions/cdi/message/impl/CompositeMessageInterpolator.java?rev=935183&view=auto
==============================================================================
--- myfaces/extensions/cdi/trunk/jse-modules/message-module/impl/src/main/java/org/apache/myfaces/extensions/cdi/message/impl/CompositeMessageInterpolator.java (added)
+++ myfaces/extensions/cdi/trunk/jse-modules/message-module/impl/src/main/java/org/apache/myfaces/extensions/cdi/message/impl/CompositeMessageInterpolator.java Sat Apr 17 14:53:40 2010
@@ -0,0 +1,65 @@
+/*
+ * 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.message.impl;
+
+import org.apache.myfaces.extensions.cdi.message.api.MessageContext;
+import org.apache.myfaces.extensions.cdi.message.api.MessageContextAware;
+import org.apache.myfaces.extensions.cdi.message.api.MessageInterpolator;
+
+import java.io.Serializable;
+
+/**
+ * @author Gerhard Petracek
+ */
+public class CompositeMessageInterpolator implements MessageInterpolator, MessageContextAware, Serializable
+{
+ private static final long serialVersionUID = 7138747032627702804L;
+ private MessageInterpolator[] messageInterpolators;
+ private MessageContext messageContext;
+
+ public CompositeMessageInterpolator(MessageInterpolator... messageInterpolators)
+ {
+ this.messageInterpolators = messageInterpolators;
+ }
+
+ public String interpolate(String messageTemplate, Serializable... arguments)
+ {
+ String result = messageTemplate;
+ for (MessageInterpolator messageInterpolator : this.messageInterpolators)
+ {
+ if (messageInterpolator instanceof MessageContextAware)
+ {
+ ((MessageContextAware) messageInterpolator).setMessageContext(this.messageContext);
+ }
+ result = messageInterpolator.interpolate(result, arguments);
+ }
+
+ return result;
+ }
+
+ public void setMessageContext(MessageContext messageContext)
+ {
+ this.messageContext = messageContext;
+ }
+
+ public MessageContext getMessageContext()
+ {
+ throw new UnsupportedOperationException();
+ }
+}
\ No newline at end of file
Added: myfaces/extensions/cdi/trunk/jse-modules/message-module/impl/src/main/java/org/apache/myfaces/extensions/cdi/message/impl/DefaultCompositeMessageHandler.java
URL: http://svn.apache.org/viewvc/myfaces/extensions/cdi/trunk/jse-modules/message-module/impl/src/main/java/org/apache/myfaces/extensions/cdi/message/impl/DefaultCompositeMessageHandler.java?rev=935183&view=auto
==============================================================================
--- myfaces/extensions/cdi/trunk/jse-modules/message-module/impl/src/main/java/org/apache/myfaces/extensions/cdi/message/impl/DefaultCompositeMessageHandler.java (added)
+++ myfaces/extensions/cdi/trunk/jse-modules/message-module/impl/src/main/java/org/apache/myfaces/extensions/cdi/message/impl/DefaultCompositeMessageHandler.java Sat Apr 17 14:53:40 2010
@@ -0,0 +1,116 @@
+/*
+ * 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.message.impl;
+
+import org.apache.myfaces.extensions.cdi.message.api.Message;
+import org.apache.myfaces.extensions.cdi.message.api.MessageContext;
+import org.apache.myfaces.extensions.cdi.message.api.MessageHandler;
+import org.apache.myfaces.extensions.cdi.message.api.MessageFilter;
+import org.apache.myfaces.extensions.cdi.message.api.CompositeMessageHandler;
+
+import java.io.Serializable;
+import java.util.List;
+import java.util.ArrayList;
+import java.util.Set;
+import java.util.HashSet;
+import java.util.Collections;
+
+/**
+ * @author Gerhard Petracek
+ */
+class DefaultCompositeMessageHandler implements CompositeMessageHandler, Serializable
+{
+ private static final long serialVersionUID = 3553885372006874180L;
+
+ private List<MessageHandler> messageHandlers = new ArrayList<MessageHandler>();
+
+ DefaultCompositeMessageHandler(Iterable<MessageHandler> messageHandlerIterable)
+ {
+ for (MessageHandler messageHandler : messageHandlerIterable)
+ {
+ messageHandlers.add(messageHandler);
+ }
+ }
+
+
+ public void addMessage(MessageContext messageContext, Message message)
+ {
+ for (MessageHandler messageHandler : this.messageHandlers)
+ {
+ messageHandler.addMessage(messageContext, message);
+ }
+ }
+
+ public void addMessageFilter(MessageFilter... messageFilters)
+ {
+ for (MessageHandler messageHandler : this.messageHandlers)
+ {
+ messageHandler.addMessageFilter(messageFilters);
+ }
+ }
+
+ public Set<MessageFilter> getMessageFilters()
+ {
+ Set<MessageFilter> result = new HashSet<MessageFilter>();
+ for (MessageHandler messageHandler : this.messageHandlers)
+ {
+ result.addAll(messageHandler.getMessageFilters());
+ }
+ return Collections.unmodifiableSet(result);
+ }
+
+ public void removeMessage(Message message)
+ {
+ for (MessageHandler messageHandler : this.messageHandlers)
+ {
+ messageHandler.removeMessage(message);
+ }
+ }
+
+ public void removeAllMessages()
+ {
+ for (MessageHandler messageHandler : this.messageHandlers)
+ {
+ messageHandler.removeAllMessages();
+ }
+ }
+
+ public List<Message> getMessages()
+ {
+ List<Message> result = new ArrayList<Message>();
+
+ for (MessageHandler messageHandler : this.messageHandlers)
+ {
+ for (Message message : messageHandler.getMessages())
+ {
+ if (!result.contains(message))
+ {
+ result.add(message);
+ }
+ }
+ }
+
+ return result;
+ }
+
+ public List<MessageHandler> getMessageHandlers()
+ {
+ return Collections.unmodifiableList(this.messageHandlers);
+ }
+}
Added: myfaces/extensions/cdi/trunk/jse-modules/message-module/impl/src/main/java/org/apache/myfaces/extensions/cdi/message/impl/DefaultFormatterFactory.java
URL: http://svn.apache.org/viewvc/myfaces/extensions/cdi/trunk/jse-modules/message-module/impl/src/main/java/org/apache/myfaces/extensions/cdi/message/impl/DefaultFormatterFactory.java?rev=935183&view=auto
==============================================================================
--- myfaces/extensions/cdi/trunk/jse-modules/message-module/impl/src/main/java/org/apache/myfaces/extensions/cdi/message/impl/DefaultFormatterFactory.java (added)
+++ myfaces/extensions/cdi/trunk/jse-modules/message-module/impl/src/main/java/org/apache/myfaces/extensions/cdi/message/impl/DefaultFormatterFactory.java Sat Apr 17 14:53:40 2010
@@ -0,0 +1,181 @@
+/*
+ * 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.message.impl;
+
+import org.apache.myfaces.extensions.cdi.message.api.Formatter;
+import org.apache.myfaces.extensions.cdi.message.api.FormatterFactory;
+import org.apache.myfaces.extensions.cdi.message.api.GenericConfig;
+import org.apache.myfaces.extensions.cdi.message.impl.formatter.FormatterBuilder;
+
+import java.io.Serializable;
+import java.util.List;
+import java.util.ArrayList;
+import java.util.Map;
+import java.util.HashMap;
+import java.util.Collections;
+import java.util.Locale;
+import java.util.logging.Logger;
+
+/**
+ * TODO change to producer
+ *
+ * @author Manfred Geiler
+ * @author Gerhard Petracek
+ */
+public class DefaultFormatterFactory implements FormatterFactory
+{
+ private static final long serialVersionUID = -7462205386564870045L;
+
+ protected final Logger logger = Logger.getLogger(getClass().getName());
+
+
+ private List<Formatter> formatters = new ArrayList<Formatter>();
+ private Map<Class<?>, Formatter> formatterCache = null;
+ private Map<FormatterConfigKey, GenericConfig> formatterConfigs = new HashMap<FormatterConfigKey, GenericConfig>();
+
+ public FormatterFactory add(Formatter formatter)
+ {
+ this.formatters.add(formatter);
+ formatterCache = null;
+ return this;
+ }
+
+ public List<Formatter> reset()
+ {
+ List<Formatter> oldFormatters = Collections.unmodifiableList(this.formatters);
+ this.formatters.clear();
+ return oldFormatters;
+ }
+
+ public Formatter findFormatter(Class<?> type)
+ {
+
+ if (this.formatterCache != null && this.formatterCache.containsKey(type))
+ {
+ return this.formatterCache.get(type);
+ }
+ else
+ {
+ if (this.formatterCache == null)
+ {
+ this.formatterCache = new HashMap<Class<?>, Formatter>();
+ }
+
+ Formatter found = findFormatterFor(type, this.formatters);
+
+ if (found == null)
+ {
+ this.logger.warning("default formatter used for: " + type.getName());
+ found = FormatterBuilder.createFormatter(type);
+ }
+
+ this.formatterCache.put(type, found);
+ return found;
+ }
+ }
+
+ public void addFormatterConfig(Class<?> type, GenericConfig formatterConfig)
+ {
+ addFormatterConfig(type, formatterConfig, Locale.getDefault());
+ }
+
+ public void addFormatterConfig(Class<?> type, GenericConfig formatterConfig, Locale locale)
+ {
+ if (formatterConfig.containsProperty(Locale.class.getName()))
+ {
+ locale = formatterConfig.getProperty(Locale.class.getName(), Locale.class);
+ }
+ this.formatterConfigs.put(createKey(type, locale), formatterConfig);
+ }
+
+ public GenericConfig findFormatterConfig(Class<?> type, Locale locale)
+ {
+ return this.formatterConfigs.get(createKey(type, locale));
+ }
+
+ private Formatter findFormatterFor(Class<?> type, Iterable<Formatter> formatters)
+ {
+ for (Formatter formatter : formatters)
+ {
+ if (formatter.isResponsibleFor(type))
+ {
+ return formatter;
+ }
+ }
+ return null;
+ }
+
+ private FormatterConfigKey createKey(Class<?> type, Locale locale)
+ {
+ return new FormatterConfigKey(type, locale);
+ }
+
+ class FormatterConfigKey implements Serializable
+ {
+ private static final long serialVersionUID = -6430653319283563370L;
+
+ private Class type;
+ private Locale locale;
+
+ FormatterConfigKey(Class type, Locale locale)
+ {
+ this.type = type;
+ this.locale = locale;
+ }
+
+ /*
+ * generated
+ */
+
+ @Override
+ public boolean equals(Object o)
+ {
+ if (this == o)
+ {
+ return true;
+ }
+ if (!(o instanceof FormatterConfigKey))
+ {
+ return false;
+ }
+
+ FormatterConfigKey that = (FormatterConfigKey) o;
+
+ if (!locale.equals(that.locale))
+ {
+ return false;
+ }
+ //noinspection RedundantIfStatement
+ if (!type.equals(that.type))
+ {
+ return false;
+ }
+
+ return true;
+ }
+
+ @Override
+ public int hashCode()
+ {
+ int result = type.hashCode();
+ result = 31 * result + locale.hashCode();
+ return result;
+ }
+ }
+}
Added: myfaces/extensions/cdi/trunk/jse-modules/message-module/impl/src/main/java/org/apache/myfaces/extensions/cdi/message/impl/DefaultInternalMessage.java
URL: http://svn.apache.org/viewvc/myfaces/extensions/cdi/trunk/jse-modules/message-module/impl/src/main/java/org/apache/myfaces/extensions/cdi/message/impl/DefaultInternalMessage.java?rev=935183&view=auto
==============================================================================
--- myfaces/extensions/cdi/trunk/jse-modules/message-module/impl/src/main/java/org/apache/myfaces/extensions/cdi/message/impl/DefaultInternalMessage.java (added)
+++ myfaces/extensions/cdi/trunk/jse-modules/message-module/impl/src/main/java/org/apache/myfaces/extensions/cdi/message/impl/DefaultInternalMessage.java Sat Apr 17 14:53:40 2010
@@ -0,0 +1,63 @@
+/*
+ * 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.message.impl;
+
+import org.apache.myfaces.extensions.cdi.message.api.NamedArgument;
+import org.apache.myfaces.extensions.cdi.message.api.payload.InternalMessage;
+import org.apache.myfaces.extensions.cdi.message.api.payload.MessagePayload;
+import org.apache.myfaces.extensions.cdi.message.api.payload.MessageSeverity;
+
+import java.io.Serializable;
+import java.util.Set;
+
+/**
+ * @author Gerhard Petracek
+ */
+public class DefaultInternalMessage extends DefaultMessage
+{
+ private static final long serialVersionUID = 7940762016110497679L;
+
+ public DefaultInternalMessage(String messageTemplate, Serializable... arguments)
+ {
+ super(messageTemplate, MessageSeverity.Info.class, arguments);
+ addPayload(InternalMessage.class);
+ }
+
+ public DefaultInternalMessage(String messageTemplate, Set<NamedArgument> namedArguments)
+ {
+ super(messageTemplate, MessageSeverity.Info.class, namedArguments);
+ addPayload(InternalMessage.class);
+ }
+
+ public DefaultInternalMessage(String messageTemplate,
+ Class<? extends MessagePayload> severity,
+ Serializable... arguments)
+ {
+ super(messageTemplate, severity, arguments);
+ addPayload(InternalMessage.class);
+ }
+
+ public DefaultInternalMessage(String messageTemplate,
+ Class<? extends MessagePayload> severity,
+ Set<NamedArgument> namedArguments)
+ {
+ super(messageTemplate, severity, namedArguments);
+ addPayload(InternalMessage.class);
+ }
+}
\ No newline at end of file
Added: myfaces/extensions/cdi/trunk/jse-modules/message-module/impl/src/main/java/org/apache/myfaces/extensions/cdi/message/impl/DefaultLocaleResolver.java
URL: http://svn.apache.org/viewvc/myfaces/extensions/cdi/trunk/jse-modules/message-module/impl/src/main/java/org/apache/myfaces/extensions/cdi/message/impl/DefaultLocaleResolver.java?rev=935183&view=auto
==============================================================================
--- myfaces/extensions/cdi/trunk/jse-modules/message-module/impl/src/main/java/org/apache/myfaces/extensions/cdi/message/impl/DefaultLocaleResolver.java (added)
+++ myfaces/extensions/cdi/trunk/jse-modules/message-module/impl/src/main/java/org/apache/myfaces/extensions/cdi/message/impl/DefaultLocaleResolver.java Sat Apr 17 14:53:40 2010
@@ -0,0 +1,38 @@
+/*
+ * 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.message.impl;
+
+import org.apache.myfaces.extensions.cdi.message.api.LocaleResolver;
+
+import java.io.Serializable;
+import java.util.Locale;
+
+/**
+ * @author Gerhard Petracek
+ */
+class DefaultLocaleResolver implements LocaleResolver, Serializable
+{
+ private static final long serialVersionUID = 2075618472090834156L;
+
+ public Locale getLocale()
+ {
+ return Locale.ENGLISH;
+ //return Locale.getDefault();
+ }
+}
Added: myfaces/extensions/cdi/trunk/jse-modules/message-module/impl/src/main/java/org/apache/myfaces/extensions/cdi/message/impl/DefaultMessage.java
URL: http://svn.apache.org/viewvc/myfaces/extensions/cdi/trunk/jse-modules/message-module/impl/src/main/java/org/apache/myfaces/extensions/cdi/message/impl/DefaultMessage.java?rev=935183&view=auto
==============================================================================
--- myfaces/extensions/cdi/trunk/jse-modules/message-module/impl/src/main/java/org/apache/myfaces/extensions/cdi/message/impl/DefaultMessage.java (added)
+++ myfaces/extensions/cdi/trunk/jse-modules/message-module/impl/src/main/java/org/apache/myfaces/extensions/cdi/message/impl/DefaultMessage.java Sat Apr 17 14:53:40 2010
@@ -0,0 +1,81 @@
+/*
+ * 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.message.impl;
+
+import org.apache.myfaces.extensions.cdi.message.api.AbstractMessageWithSeverity;
+import org.apache.myfaces.extensions.cdi.message.api.Message;
+import org.apache.myfaces.extensions.cdi.message.api.MessageContext;
+import org.apache.myfaces.extensions.cdi.message.api.NamedArgument;
+import org.apache.myfaces.extensions.cdi.message.api.payload.MessagePayload;
+import org.apache.myfaces.extensions.cdi.message.api.payload.MessageSeverity;
+
+import java.io.Serializable;
+import java.util.Set;
+
+/**
+ * @author Gerhard Petracek
+ */
+public class DefaultMessage extends AbstractMessageWithSeverity
+{
+ private static final long serialVersionUID = -6466664764243947830L;
+
+ public DefaultMessage(Message message)
+ {
+ super(message);
+ }
+
+ public DefaultMessage(String messageTemplate, Serializable... arguments)
+ {
+ super(messageTemplate, MessageSeverity.Info.class, arguments);
+ }
+
+ public DefaultMessage(String messageTemplate, Set<NamedArgument> namedArguments)
+ {
+ super(messageTemplate, MessageSeverity.Info.class, namedArguments);
+ }
+
+ public DefaultMessage(String messageTemplate,
+ Class<? extends MessagePayload> severity,
+ Serializable... arguments)
+ {
+ super(messageTemplate, severity, arguments);
+ }
+
+ public DefaultMessage(String messageTemplate,
+ Class<? extends MessagePayload> severity,
+ Set<NamedArgument> namedArguments)
+ {
+ super(messageTemplate, severity, namedArguments);
+ }
+
+ @Override
+ public String toString()
+ {
+ if (getMessageContextConfig() != null)
+ {
+ return toString(new DefaultMessageContext(getMessageContextConfig()));
+ }
+ return toString(new DefaultMessageContext());
+ }
+
+ public String toString(MessageContext messageContext)
+ {
+ return messageContext.message().text(getTemplate()).argument(getArguments()).toText();
+ }
+}
Added: myfaces/extensions/cdi/trunk/jse-modules/message-module/impl/src/main/java/org/apache/myfaces/extensions/cdi/message/impl/DefaultMessageBuilder.java
URL: http://svn.apache.org/viewvc/myfaces/extensions/cdi/trunk/jse-modules/message-module/impl/src/main/java/org/apache/myfaces/extensions/cdi/message/impl/DefaultMessageBuilder.java?rev=935183&view=auto
==============================================================================
--- myfaces/extensions/cdi/trunk/jse-modules/message-module/impl/src/main/java/org/apache/myfaces/extensions/cdi/message/impl/DefaultMessageBuilder.java (added)
+++ myfaces/extensions/cdi/trunk/jse-modules/message-module/impl/src/main/java/org/apache/myfaces/extensions/cdi/message/impl/DefaultMessageBuilder.java Sat Apr 17 14:53:40 2010
@@ -0,0 +1,308 @@
+/*
+ * 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.message.impl;
+
+import org.apache.myfaces.extensions.cdi.message.api.Message;
+import org.apache.myfaces.extensions.cdi.message.api.MessageContext;
+import org.apache.myfaces.extensions.cdi.message.api.MessageContextAware;
+import org.apache.myfaces.extensions.cdi.message.api.MessageResolver;
+import org.apache.myfaces.extensions.cdi.message.api.MessageInterpolator;
+import org.apache.myfaces.extensions.cdi.message.api.NamedArgument;
+import org.apache.myfaces.extensions.cdi.message.api.payload.MessagePayload;
+import org.apache.myfaces.extensions.cdi.message.api.payload.MessagePayloadKey;
+import org.apache.myfaces.extensions.cdi.message.api.payload.MessageSeverity;
+
+import java.io.Serializable;
+import java.util.List;
+import java.util.ArrayList;
+import java.util.Set;
+import java.util.HashSet;
+import java.util.Map;
+import java.util.HashMap;
+
+/**
+ * @author Gerhard Petracek
+ */
+class DefaultMessageBuilder implements MessageContext.MessageBuilder, Serializable
+{
+ private static final long serialVersionUID = 892218539314030675L;
+
+ private List<Serializable> argumentList;
+
+ private Set<NamedArgument> namedArguments;
+ private MessageContext messageContext;
+ private Map<Class, Class<? extends MessagePayload>> messagePayload;
+ private String messageTemplate;
+
+ public DefaultMessageBuilder(MessageContext messageContext)
+ {
+ reset();
+ this.messageContext = new UnmodifiableMessageContext(messageContext.cloneContext());
+ }
+
+ public MessageContext.MessageBuilder payload(Class<? extends MessagePayload>... messagePayload)
+ {
+ Class key;
+
+ for (Class<? extends MessagePayload> payload : messagePayload)
+ {
+ key = payload;
+ if (payload.isAnnotationPresent(MessagePayloadKey.class))
+ {
+ key = payload.getAnnotation(MessagePayloadKey.class).value();
+ }
+ this.messagePayload.put(key, payload);
+ }
+ return this;
+ }
+
+ public MessageContext.MessageBuilder text(String messageTemplate)
+ {
+ this.messageTemplate = messageTemplate;
+ return this;
+ }
+
+ public MessageContext.MessageBuilder argument(Serializable... arguments)
+ {
+ for (Serializable argument : arguments)
+ {
+ if (argument instanceof NamedArgument)
+ {
+ this.namedArguments.add((NamedArgument) argument);
+ }
+ else
+ {
+ this.argumentList.add(argument);
+ }
+ }
+
+ return this;
+ }
+
+ public MessageContext.MessageBuilder namedArgument(String name, Serializable value)
+ {
+ this.namedArguments.add(new DefaultNamedArgument(name, value));
+ return this;
+ }
+
+ public Message add()
+ {
+ Message result = create();
+ this.messageContext.addMessage(this.messageContext, result);
+ return result;
+ }
+
+ private Message buildMessage()
+ {
+ if (this.messageTemplate == null)
+ {
+ throw new IllegalStateException("messageTemplate is missing");
+ }
+
+ Class<? extends MessagePayload> severity = getMessageSeverity();
+
+ Message result = new DefaultMessage(this.messageTemplate, severity);
+ addArguments(result);
+ addPayload(result);
+
+ return result;
+ }
+
+ private Class<? extends MessagePayload> getMessageSeverity()
+ {
+ Class<? extends MessagePayload> severity = this.messagePayload.get(MessageSeverity.class);
+
+ if (severity == null)
+ {
+ severity = MessageSeverity.Info.class;
+ }
+ return severity;
+ }
+
+ private void addArguments(Message result)
+ {
+ if (!this.argumentList.isEmpty())
+ {
+ result.addArgument(this.argumentList.toArray(new Serializable[this.argumentList.size()]));
+ }
+
+ if (!this.namedArguments.isEmpty())
+ {
+ result.addArgument(this.namedArguments.toArray(new Serializable[this.namedArguments.size()]));
+ }
+ }
+
+ private void addPayload(Message result)
+ {
+ for (Map.Entry<Class, Class<? extends MessagePayload>> entry : this.messagePayload.entrySet())
+ {
+ if (!MessageSeverity.class.equals(entry.getKey()))
+ {
+ result.addPayload(entry.getKey(), entry.getValue());
+ }
+ }
+ }
+
+ private void reset()
+ {
+ this.messageTemplate = null;
+ this.messagePayload = new HashMap<Class, Class<? extends MessagePayload>>();
+ this.argumentList = new ArrayList<Serializable>();
+ this.namedArguments = new HashSet<NamedArgument>();
+ }
+
+ public Message create()
+ {
+ Message result = buildMessage();
+ reset();
+ return result;
+ }
+
+ public String toText()
+ {
+ Message baseMessage = buildMessage();
+
+ return getMessageText(baseMessage);
+ }
+
+ public String toText(Message message)
+ {
+ //it isn't required to try to restore the message-context of the message
+ //to use the original context (config) just call message.toString(); instead of this method
+ return getMessageText(message);
+ }
+
+ private String getMessageText(Message baseMessage)
+ {
+ String message = baseMessage.getTemplate();
+
+ MessageResolver messageResolver = this.messageContext.config().getMessageResolver();
+ if (messageResolver != null)
+ {
+ synchronized (this)
+ {
+ message = resolveMessage(messageResolver, baseMessage);
+ }
+ }
+
+ MessageInterpolator messageInterpolator = this.messageContext.config().getMessageInterpolator();
+
+ if (messageInterpolator != null)
+ {
+ synchronized (this)
+ {
+ return checkedResult(
+ interpolateMessage(messageInterpolator, message, baseMessage.getArguments()),
+ baseMessage);
+ }
+ }
+
+ return checkedResult(message, baseMessage);
+ }
+
+ private String checkedResult(String result, Message baseMessage)
+ {
+ if (result == null || isKey(baseMessage.getTemplate()))
+ {
+ String oldTemplate = extractTemplate(baseMessage.getTemplate()); //minor performance tweak for inline-msg
+
+ if (result == null || result.equals(oldTemplate))
+ {
+ return MessageResolver.MISSING_RESOURCE_MARKER + oldTemplate + MessageResolver.MISSING_RESOURCE_MARKER;
+ }
+ }
+ return result;
+ }
+
+ private String extractTemplate(String template)
+ {
+ String result = getEscapedTemplate(template);
+
+ if (isKey(result))
+ {
+ result = extractTemplateKey(result);
+ }
+
+ return result;
+ }
+
+ private boolean isKey(String key)
+ {
+ return key.startsWith("{") && key.endsWith("}");
+ }
+
+ private String extractTemplateKey(String key)
+ {
+ return key.substring(1, key.length() - 1);
+ }
+
+ private String resolveMessage(MessageResolver messageResolver, Message baseMessage)
+ {
+ if (messageResolver instanceof MessageContextAware)
+ {
+ ((MessageContextAware) messageResolver).setMessageContext(this.messageContext);
+ }
+
+ try
+ {
+ return messageResolver
+ .getMessage(baseMessage.getTemplate(), this.messageContext.getLocale(), baseMessage.getPayload());
+ }
+ finally
+ {
+ cleanupMessageContext(messageResolver);
+ }
+ }
+
+ private String interpolateMessage(MessageInterpolator messageInterpolator,
+ String messageTemplate, Serializable... arguments)
+ {
+ if (messageInterpolator instanceof MessageContextAware)
+ {
+ ((MessageContextAware) messageInterpolator).setMessageContext(this.messageContext);
+ }
+
+ try
+ {
+ return messageInterpolator.interpolate(getEscapedTemplate(messageTemplate), arguments);
+ }
+ finally
+ {
+ cleanupMessageContext(messageInterpolator);
+ }
+ }
+
+ private String getEscapedTemplate(String messageTemplate)
+ {
+ //TODO
+ if (messageTemplate.startsWith("\\{"))
+ {
+ return messageTemplate.substring(1);
+ }
+ return messageTemplate;
+ }
+
+ private void cleanupMessageContext(Object object)
+ {
+ if (object instanceof MessageContextAware)
+ {
+ ((MessageContextAware) object).setMessageContext(null);
+ }
+ }
+}
Added: myfaces/extensions/cdi/trunk/jse-modules/message-module/impl/src/main/java/org/apache/myfaces/extensions/cdi/message/impl/DefaultMessageContext.java
URL: http://svn.apache.org/viewvc/myfaces/extensions/cdi/trunk/jse-modules/message-module/impl/src/main/java/org/apache/myfaces/extensions/cdi/message/impl/DefaultMessageContext.java?rev=935183&view=auto
==============================================================================
--- myfaces/extensions/cdi/trunk/jse-modules/message-module/impl/src/main/java/org/apache/myfaces/extensions/cdi/message/impl/DefaultMessageContext.java (added)
+++ myfaces/extensions/cdi/trunk/jse-modules/message-module/impl/src/main/java/org/apache/myfaces/extensions/cdi/message/impl/DefaultMessageContext.java Sat Apr 17 14:53:40 2010
@@ -0,0 +1,147 @@
+/*
+ * 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.message.impl;
+
+import org.apache.myfaces.extensions.cdi.message.api.Message;
+import org.apache.myfaces.extensions.cdi.message.api.MessageContext;
+import org.apache.myfaces.extensions.cdi.message.api.MessageContextConfig;
+import org.apache.myfaces.extensions.cdi.message.api.MessageFilter;
+
+import java.util.List;
+import java.util.Locale;
+import java.util.Set;
+
+/**
+ * to create a new instance inject the singleton and call cloneContext -
+ * so it's possible to use it e.g. in producer methods
+ *
+ * @author Gerhard Petracek
+ */
+public class DefaultMessageContext implements MessageContext
+{
+ private static final long serialVersionUID = -110779217295211303L;
+
+ private MessageContextConfig config = new DefaultMessageContextConfig();
+
+ public DefaultMessageContext()
+ {
+ }
+
+ DefaultMessageContext(MessageContextConfig config)
+ {
+ this.config = config;
+ }
+
+ public MessageBuilder message()
+ {
+ return new DefaultMessageBuilder(this);
+ }
+
+ public MessageContextConfig config()
+ {
+ return config;
+ }
+
+ public <T extends MessageContext> T typed(Class<T> contextType)
+ {
+ return (T) this;
+ }
+
+ public MessageContext cloneContext()
+ {
+ return config().use().create();
+ }
+
+ public String getMessageText(Message message)
+ {
+ return message().toText(message);
+ }
+
+ public void addMessage(Message message)
+ {
+ addMessage(this, message);
+ }
+
+ public void addMessage(MessageContext messageContext, Message message)
+ {
+ config().getMessageHandler().addMessage(messageContext, message);
+ }
+
+ public void addMessageFilter(MessageFilter... messageFilters)
+ {
+ config().getMessageHandler().addMessageFilter(messageFilters);
+ }
+
+ public Set<MessageFilter> getMessageFilters()
+ {
+ return config().getMessageHandler().getMessageFilters();
+ }
+
+ public void removeMessage(Message message)
+ {
+ config().getMessageHandler().removeMessage(message);
+ }
+
+ public void removeAllMessages()
+ {
+ config().getMessageHandler().removeAllMessages();
+ }
+
+ public List<Message> getMessages()
+ {
+ return config().getMessageHandler().getMessages();
+ }
+
+ public Locale getLocale()
+ {
+ return config().getLocaleResolver().getLocale();
+ }
+
+ /*
+ * generated
+ */
+
+ @Override
+ public boolean equals(Object o)
+ {
+ if (this == o)
+ {
+ return true;
+ }
+ if (!(o instanceof DefaultMessageContext))
+ {
+ return false;
+ }
+
+ DefaultMessageContext that = (DefaultMessageContext) o;
+
+ if (!config.equals(that.config))
+ {
+ return false;
+ }
+
+ return true;
+ }
+
+ @Override
+ public int hashCode()
+ {
+ return config.hashCode();
+ }
+}
Added: myfaces/extensions/cdi/trunk/jse-modules/message-module/impl/src/main/java/org/apache/myfaces/extensions/cdi/message/impl/DefaultMessageContextConfig.java
URL: http://svn.apache.org/viewvc/myfaces/extensions/cdi/trunk/jse-modules/message-module/impl/src/main/java/org/apache/myfaces/extensions/cdi/message/impl/DefaultMessageContextConfig.java?rev=935183&view=auto
==============================================================================
--- myfaces/extensions/cdi/trunk/jse-modules/message-module/impl/src/main/java/org/apache/myfaces/extensions/cdi/message/impl/DefaultMessageContextConfig.java (added)
+++ myfaces/extensions/cdi/trunk/jse-modules/message-module/impl/src/main/java/org/apache/myfaces/extensions/cdi/message/impl/DefaultMessageContextConfig.java Sat Apr 17 14:53:40 2010
@@ -0,0 +1,348 @@
+/*
+ * 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.message.impl;
+
+import org.apache.myfaces.extensions.cdi.message.api.MessageContext;
+import org.apache.myfaces.extensions.cdi.message.api.MessageContextConfig;
+import org.apache.myfaces.extensions.cdi.message.api.LocaleResolver;
+import org.apache.myfaces.extensions.cdi.message.api.MessageResolver;
+import org.apache.myfaces.extensions.cdi.message.api.MessageInterpolator;
+import org.apache.myfaces.extensions.cdi.message.api.MessageHandler;
+import org.apache.myfaces.extensions.cdi.message.api.CompositeMessageHandler;
+import org.apache.myfaces.extensions.cdi.message.api.FormatterFactory;
+import org.apache.myfaces.extensions.cdi.message.api.Formatter;
+import org.apache.myfaces.extensions.cdi.message.api.GenericConfig;
+import org.apache.myfaces.extensions.cdi.message.impl.formatter.FormatterBuilder;
+
+import java.util.HashSet;
+import java.util.Locale;
+import java.util.Set;
+
+/**
+ * @author Gerhard Petracek
+ */
+class DefaultMessageContextConfig implements MessageContextConfig
+{
+ private static final long serialVersionUID = 2919944628020782545L;
+
+ private MessageInterpolator messageInterpolator;
+ private MessageResolver messageResolver;
+ private Set<MessageHandler> messageHandlers;
+ private LocaleResolver localeResolver;
+ private FormatterFactory formatterFactory;
+
+ DefaultMessageContextConfig()
+ {
+ resetMessageContextConfig();
+ addNewFormatter(FormatterBuilder.createFormatter(Number.class));
+ }
+
+ private DefaultMessageContextConfig(MessageContextConfig messageContextConfigTemplate)
+ {
+ this();
+ this.messageInterpolator = messageContextConfigTemplate.getMessageInterpolator();
+ this.messageResolver = messageContextConfigTemplate.getMessageResolver();
+
+ MessageHandler newMessageHandler = messageContextConfigTemplate.getMessageHandler();
+
+ if (newMessageHandler instanceof CompositeMessageHandler)
+ {
+ this.messageHandlers.addAll(((CompositeMessageHandler) newMessageHandler).getMessageHandlers());
+ }
+ else
+ {
+ this.messageHandlers.add(newMessageHandler);
+ }
+ this.localeResolver = messageContextConfigTemplate.getLocaleResolver();
+ this.formatterFactory = messageContextConfigTemplate.getFormatterFactory();
+ }
+
+ public MessageContextBuilder use()
+ {
+ return new MessageContextBuilder()
+ {
+ private DefaultMessageContextConfig newMessageContextConfig =
+ new DefaultMessageContextConfig(DefaultMessageContextConfig.this);
+
+ public MessageContextBuilder messageInterpolator(MessageInterpolator messageInterpolator)
+ {
+ newMessageContextConfig.setMessageInterpolator(messageInterpolator);
+ return this;
+ }
+
+ public MessageContextBuilder messageResolver(MessageResolver messageResolver)
+ {
+ newMessageContextConfig.setMessageResolver(messageResolver);
+ return this;
+ }
+
+ public MessageContextBuilder addFormatter(Formatter formatter)
+ {
+ newMessageContextConfig.addNewFormatter(formatter);
+ return this;
+ }
+
+ public MessageContextBuilder addFormatterConfig(Class<?> type, GenericConfig config)
+ {
+ newMessageContextConfig.addNewFormatterConfig(type, config);
+ return this;
+ }
+
+ public MessageContextBuilder addFormatterConfig(Class<?> type, GenericConfig config, Locale locale)
+ {
+ addNewFormatterConfig(type, config.addProperty(Locale.class.toString(), locale));
+ return this;
+ }
+
+ public MessageContextBuilder formatterFactory(FormatterFactory formatterFactory)
+ {
+ newMessageContextConfig.setFormatterFactory(formatterFactory);
+ return this;
+ }
+
+ public MessageContextBuilder addMessageHandler(MessageHandler messageHandler)
+ {
+ newMessageContextConfig.addNewMessageHandler(messageHandler);
+ return this;
+ }
+
+ public MessageContextBuilder localeResolver(LocaleResolver localeResolver)
+ {
+ newMessageContextConfig.setLocaleResolver(localeResolver);
+ return this;
+ }
+
+ public MessageContextBuilder reset()
+ {
+ newMessageContextConfig.resetMessageContextConfig();
+ return this;
+ }
+
+ public MessageContextBuilder clear()
+ {
+ newMessageContextConfig.clearMessageContextConfig();
+ return this;
+ }
+
+ public MessageContext create()
+ {
+ return new DefaultMessageContext(this.newMessageContextConfig);
+ }
+ };
+ }
+
+ public MessageContextBuilder change()
+ {
+ return new MessageContextBuilder()
+ {
+
+ public MessageContextBuilder messageInterpolator(MessageInterpolator messageInterpolator)
+ {
+ setMessageInterpolator(messageInterpolator);
+ return this;
+ }
+
+ public MessageContextBuilder messageResolver(MessageResolver messageResolver)
+ {
+ setMessageResolver(messageResolver);
+ return this;
+ }
+
+ public MessageContextBuilder addFormatter(Formatter formatter)
+ {
+ addNewFormatter(formatter);
+ return this;
+ }
+
+ public MessageContextBuilder addFormatterConfig(Class<?> type, GenericConfig config)
+ {
+ addNewFormatterConfig(type, config);
+ return this;
+ }
+
+ public MessageContextBuilder addFormatterConfig(Class<?> type, GenericConfig config, Locale locale)
+ {
+ addNewFormatterConfig(type, config.addProperty(Locale.class.toString(), locale));
+ return this;
+ }
+
+ public MessageContextBuilder formatterFactory(FormatterFactory formatterFactory)
+ {
+ setFormatterFactory(formatterFactory);
+ return this;
+ }
+
+ public MessageContextBuilder addMessageHandler(MessageHandler messageHandler)
+ {
+ addNewMessageHandler(messageHandler);
+ return this;
+ }
+
+ public MessageContextBuilder localeResolver(LocaleResolver localeResolver)
+ {
+ setLocaleResolver(localeResolver);
+ return this;
+ }
+
+ public MessageContextBuilder reset()
+ {
+ resetMessageContextConfig();
+ return this;
+ }
+
+ public MessageContextBuilder clear()
+ {
+ clearMessageContextConfig();
+ return this;
+ }
+
+ public MessageContext create()
+ {
+ return new DefaultMessageContext(DefaultMessageContextConfig.this);
+ }
+ };
+ }
+
+ private void resetMessageContextConfig()
+ {
+ this.messageInterpolator = new DefaultMessageInterpolator();
+ this.messageResolver = null;
+ this.messageHandlers = new HashSet<MessageHandler>();
+ this.localeResolver = new org.apache.myfaces.extensions.cdi.message.impl.DefaultLocaleResolver();
+ this.formatterFactory = new DefaultFormatterFactory();
+ }
+
+ private void clearMessageContextConfig()
+ {
+ }
+
+ public MessageInterpolator getMessageInterpolator()
+ {
+ return this.messageInterpolator;
+ }
+
+ public MessageResolver getMessageResolver()
+ {
+ return this.messageResolver;
+ }
+
+ public LocaleResolver getLocaleResolver()
+ {
+ return this.localeResolver;
+ }
+
+ public MessageHandler getMessageHandler()
+ {
+ return new DefaultCompositeMessageHandler(this.messageHandlers);
+ }
+
+ public FormatterFactory getFormatterFactory()
+ {
+ return this.formatterFactory;
+ }
+
+ private void setMessageInterpolator(MessageInterpolator messageInterpolator)
+ {
+ this.messageInterpolator = messageInterpolator;
+ }
+
+ private void setMessageResolver(MessageResolver messageResolver)
+ {
+ this.messageResolver = messageResolver;
+ }
+
+ private void addNewFormatter(Formatter formatter)
+ {
+ this.formatterFactory.add(formatter);
+ }
+
+ private void addNewFormatterConfig(Class<?> type, GenericConfig config)
+ {
+ this.formatterFactory.addFormatterConfig(type, config);
+ }
+
+ private void addNewMessageHandler(MessageHandler messageHandler)
+ {
+ this.messageHandlers.add(messageHandler);
+ }
+
+ private void setLocaleResolver(LocaleResolver localeResolver)
+ {
+ this.localeResolver = localeResolver;
+ }
+
+ private void setFormatterFactory(FormatterFactory formatterFactory)
+ {
+ this.formatterFactory = formatterFactory;
+ }
+
+ /*
+ * generated
+ */
+ @Override
+ public boolean equals(Object o)
+ {
+ if (this == o)
+ {
+ return true;
+ }
+ if (!(o instanceof DefaultMessageContextConfig))
+ {
+ return false;
+ }
+
+ DefaultMessageContextConfig that = (DefaultMessageContextConfig) o;
+
+ if (!formatterFactory.equals(that.formatterFactory))
+ {
+ return false;
+ }
+ if (!localeResolver.equals(that.localeResolver))
+ {
+ return false;
+ }
+ if (!messageHandlers.equals(that.messageHandlers))
+ {
+ return false;
+ }
+ if (messageInterpolator != null
+ ? !messageInterpolator.equals(that.messageInterpolator) : that.messageInterpolator != null)
+ {
+ return false;
+ }
+ //noinspection RedundantIfStatement
+ if (messageResolver != null ? !messageResolver.equals(that.messageResolver) : that.messageResolver != null)
+ {
+ return false;
+ }
+
+ return true;
+ }
+
+ @Override
+ public int hashCode()
+ {
+ int result = messageInterpolator != null ? messageInterpolator.hashCode() : 0;
+ result = 31 * result + (messageResolver != null ? messageResolver.hashCode() : 0);
+ result = 31 * result + messageHandlers.hashCode();
+ result = 31 * result + localeResolver.hashCode();
+ result = 31 * result + formatterFactory.hashCode();
+ return result;
+ }
+}
Added: myfaces/extensions/cdi/trunk/jse-modules/message-module/impl/src/main/java/org/apache/myfaces/extensions/cdi/message/impl/DefaultMessageInterpolator.java
URL: http://svn.apache.org/viewvc/myfaces/extensions/cdi/trunk/jse-modules/message-module/impl/src/main/java/org/apache/myfaces/extensions/cdi/message/impl/DefaultMessageInterpolator.java?rev=935183&view=auto
==============================================================================
--- myfaces/extensions/cdi/trunk/jse-modules/message-module/impl/src/main/java/org/apache/myfaces/extensions/cdi/message/impl/DefaultMessageInterpolator.java (added)
+++ myfaces/extensions/cdi/trunk/jse-modules/message-module/impl/src/main/java/org/apache/myfaces/extensions/cdi/message/impl/DefaultMessageInterpolator.java Sat Apr 17 14:53:40 2010
@@ -0,0 +1,36 @@
+/*
+ * 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.message.impl;
+
+import org.apache.myfaces.extensions.cdi.message.api.MessageInterpolator;
+
+import java.io.Serializable;
+
+/**
+ * @author Gerhard Petracek
+ */
+class DefaultMessageInterpolator implements MessageInterpolator, Serializable
+{
+ private static final long serialVersionUID = -8854087197813424812L;
+
+ public String interpolate(String messageTemplate, Serializable... arguments)
+ {
+ return messageTemplate;
+ }
+}
Added: myfaces/extensions/cdi/trunk/jse-modules/message-module/impl/src/main/java/org/apache/myfaces/extensions/cdi/message/impl/DefaultNamedArgument.java
URL: http://svn.apache.org/viewvc/myfaces/extensions/cdi/trunk/jse-modules/message-module/impl/src/main/java/org/apache/myfaces/extensions/cdi/message/impl/DefaultNamedArgument.java?rev=935183&view=auto
==============================================================================
--- myfaces/extensions/cdi/trunk/jse-modules/message-module/impl/src/main/java/org/apache/myfaces/extensions/cdi/message/impl/DefaultNamedArgument.java (added)
+++ myfaces/extensions/cdi/trunk/jse-modules/message-module/impl/src/main/java/org/apache/myfaces/extensions/cdi/message/impl/DefaultNamedArgument.java Sat Apr 17 14:53:40 2010
@@ -0,0 +1,87 @@
+/*
+ * 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.message.impl;
+
+import org.apache.myfaces.extensions.cdi.message.api.NamedArgument;
+
+import java.io.Serializable;
+
+/**
+ * @author Gerhard Petracek
+ */
+class DefaultNamedArgument implements NamedArgument
+{
+ private String name;
+ private Serializable value;
+ private static final long serialVersionUID = -6901959935281965653L;
+
+ DefaultNamedArgument(String name, Serializable value)
+ {
+ this.name = name;
+ this.value = value;
+ }
+
+ public String getName()
+ {
+ return name;
+ }
+
+ public Serializable getValue()
+ {
+ return value;
+ }
+
+ /*
+ * generated
+ */
+ @Override
+ public boolean equals(Object o)
+ {
+ if (this == o)
+ {
+ return true;
+ }
+ if (!(o instanceof DefaultNamedArgument))
+ {
+ return false;
+ }
+
+ DefaultNamedArgument that = (DefaultNamedArgument) o;
+
+ if (!name.equals(that.name))
+ {
+ return false;
+ }
+ //noinspection RedundantIfStatement
+ if (value != null ? !value.equals(that.value) : that.value != null)
+ {
+ return false;
+ }
+
+ return true;
+ }
+
+ @Override
+ public int hashCode()
+ {
+ int result = name.hashCode();
+ result = 31 * result + (value != null ? value.hashCode() : 0);
+ return result;
+ }
+}
Added: myfaces/extensions/cdi/trunk/jse-modules/message-module/impl/src/main/java/org/apache/myfaces/extensions/cdi/message/impl/ELAwareMessageInterpolator.java
URL: http://svn.apache.org/viewvc/myfaces/extensions/cdi/trunk/jse-modules/message-module/impl/src/main/java/org/apache/myfaces/extensions/cdi/message/impl/ELAwareMessageInterpolator.java?rev=935183&view=auto
==============================================================================
--- myfaces/extensions/cdi/trunk/jse-modules/message-module/impl/src/main/java/org/apache/myfaces/extensions/cdi/message/impl/ELAwareMessageInterpolator.java (added)
+++ myfaces/extensions/cdi/trunk/jse-modules/message-module/impl/src/main/java/org/apache/myfaces/extensions/cdi/message/impl/ELAwareMessageInterpolator.java Sat Apr 17 14:53:40 2010
@@ -0,0 +1,120 @@
+/*
+ * 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.message.impl;
+
+import org.apache.myfaces.extensions.cdi.message.api.NamedArgument;
+import org.apache.myfaces.extensions.cdi.message.impl.spi.ELProvider;
+import org.apache.myfaces.extensions.cdi.message.impl.spi.SimpleELContext;
+
+import javax.el.ExpressionFactory;
+import javax.el.ValueExpression;
+import java.io.Serializable;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+
+/**
+ * @author Manfred Geiler
+ * @author Gerhard Petracek
+ */
+public class ELAwareMessageInterpolator extends AbstractFormatterAwareMessageInterpolator
+{
+ private static final long serialVersionUID = 3451979493272628741L;
+
+ private static final Pattern MESSAGE_ARGS_PATTERN = Pattern.compile("\\{([^\\}]+?)\\}");
+
+ private ELProvider elProvider;
+
+ public ELAwareMessageInterpolator(ELProvider elProvider)
+ {
+ this.elProvider = elProvider;
+ }
+
+ public String interpolate(String messageTemplate, Serializable... arguments)
+ {
+ List<NamedArgument> namedArguments = addNamedArguments(arguments);
+
+ if (namedArguments.size() > 0)
+ {
+ return interpolateNamedArguments(messageTemplate, namedArguments);
+ }
+ return messageTemplate;
+ }
+
+ private List<NamedArgument> addNamedArguments(Serializable[] arguments)
+ {
+ List<NamedArgument> result = new ArrayList<NamedArgument>();
+
+ for (Serializable argument : arguments)
+ {
+ if (argument instanceof NamedArgument)
+ {
+ result.add((NamedArgument) argument);
+ }
+ }
+
+ return result;
+ }
+
+ //TODO add warning for unused arguments,...
+ private String interpolateNamedArguments(String messageTemplate, List<NamedArgument> namedArguments)
+ {
+ ExpressionFactory factory = this.elProvider.createExpressionFactory();
+ SimpleELContext elContext = this.elProvider.createELContext(this.elProvider.createELResolver());
+
+ for (NamedArgument argument : namedArguments)
+ {
+ Serializable value = argument.getValue();
+ elContext.setVariable(argument.getName(), factory.createValueExpression(value, value.getClass()));
+ }
+
+ Matcher matcher = MESSAGE_ARGS_PATTERN.matcher(messageTemplate);
+ StringBuffer buffer = new StringBuffer();
+ while (matcher.find())
+ {
+ String resolvedArgumentValue;
+ String expression = matcher.group(1);
+
+ ValueExpression valueExpression =
+ factory.createValueExpression(elContext, "${" + expression + "}", Object.class);
+
+ Object value = valueExpression.getValue(elContext);
+
+ if (value != null && !isUnresolvedArgument(expression, value))
+ {
+ resolvedArgumentValue = formatAsString(value).toString();
+ }
+ else
+ {
+ resolvedArgumentValue = '{' + expression + '}';
+ }
+
+ matcher.appendReplacement(buffer, resolvedArgumentValue);
+ }
+ matcher.appendTail(buffer);
+
+ return buffer.toString();
+ }
+
+ private boolean isUnresolvedArgument(String expression, Object value)
+ {
+ return value instanceof Long && expression.equals(value.toString());
+ }
+}
\ No newline at end of file
Added: myfaces/extensions/cdi/trunk/jse-modules/message-module/impl/src/main/java/org/apache/myfaces/extensions/cdi/message/impl/NamedArguments.java
URL: http://svn.apache.org/viewvc/myfaces/extensions/cdi/trunk/jse-modules/message-module/impl/src/main/java/org/apache/myfaces/extensions/cdi/message/impl/NamedArguments.java?rev=935183&view=auto
==============================================================================
--- myfaces/extensions/cdi/trunk/jse-modules/message-module/impl/src/main/java/org/apache/myfaces/extensions/cdi/message/impl/NamedArguments.java (added)
+++ myfaces/extensions/cdi/trunk/jse-modules/message-module/impl/src/main/java/org/apache/myfaces/extensions/cdi/message/impl/NamedArguments.java Sat Apr 17 14:53:40 2010
@@ -0,0 +1,84 @@
+/*
+ * 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.message.impl;
+
+import org.apache.myfaces.extensions.cdi.message.api.NamedArgument;
+
+import java.io.Serializable;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Map;
+
+/**
+ * @author Gerhard Petracek
+ */
+public class NamedArguments
+{
+ private NamedArgumentBuilder namedArgumentBuilder = new NamedArgumentBuilder();
+
+ private NamedArguments()
+ {
+ }
+
+ public static Serializable[] convert(Map<String, Serializable> attributes)
+ {
+ if (attributes == null || attributes.size() == 0)
+ {
+ return new Serializable[]{};
+ }
+
+ NamedArguments.NamedArgumentBuilder namedArgumentBuilder = new NamedArguments().getNamedArgumentBuilder();
+ for (Map.Entry<String, Serializable> entry : attributes.entrySet())
+ {
+ namedArgumentBuilder = namedArgumentBuilder.add(entry.getKey(), entry.getValue());
+ }
+
+ return namedArgumentBuilder.create();
+ }
+
+ public static NamedArgumentBuilder add(String name, Serializable value)
+ {
+ return new NamedArguments().getNamedArgumentBuilder().add(name, value);
+ }
+
+ private NamedArgumentBuilder getNamedArgumentBuilder()
+ {
+ return namedArgumentBuilder;
+ }
+
+ public class NamedArgumentBuilder
+ {
+ private List<NamedArgument> parameters = new ArrayList<NamedArgument>();
+
+ private NamedArgumentBuilder()
+ {
+ }
+
+ public NamedArgumentBuilder add(String name, Serializable value)
+ {
+ this.parameters.add(new DefaultNamedArgument(name, value));
+ return this;
+ }
+
+ public NamedArgument[] create()
+ {
+ return this.parameters.toArray(new NamedArgument[this.parameters.size()]);
+ }
+ }
+}