Updated Branches: refs/heads/5.4-js-rewrite b70bcee47 -> ac7f26352
Create a "virtual resource" for decimal format symbols (needed on the client) Project: http://git-wip-us.apache.org/repos/asf/tapestry-5/repo Commit: http://git-wip-us.apache.org/repos/asf/tapestry-5/commit/ac7f2635 Tree: http://git-wip-us.apache.org/repos/asf/tapestry-5/tree/ac7f2635 Diff: http://git-wip-us.apache.org/repos/asf/tapestry-5/diff/ac7f2635 Branch: refs/heads/5.4-js-rewrite Commit: ac7f26352d6ea96a66bcc097d1249e42eeba07de Parents: 97af128 Author: Howard M. Lewis Ship <hls...@apache.org> Authored: Thu Nov 1 17:42:11 2012 -0700 Committer: Howard M. Lewis Ship <hls...@apache.org> Committed: Thu Nov 1 17:42:11 2012 -0700 ---------------------------------------------------------------------- .../META-INF/modules/core/validation.coffee | 3 +- .../pageload/DefaultComponentResourceLocator.java | 13 ++- .../messages/DecimalFormatMessageResource.java | 97 +++++++++++++++ .../translator/NumericTranslatorSupportImpl.java | 62 +--------- .../apache/tapestry5/services/TapestryModule.java | 8 +- .../java/org/apache/tapestry5/ioc/Resource.java | 8 +- 6 files changed, 128 insertions(+), 63 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/tapestry-5/blob/ac7f2635/tapestry-core/src/main/coffeescript/META-INF/modules/core/validation.coffee ---------------------------------------------------------------------- diff --git a/tapestry-core/src/main/coffeescript/META-INF/modules/core/validation.coffee b/tapestry-core/src/main/coffeescript/META-INF/modules/core/validation.coffee index 3ff32a5..9bb60c6 100644 --- a/tapestry-core/src/main/coffeescript/META-INF/modules/core/validation.coffee +++ b/tapestry-core/src/main/coffeescript/META-INF/modules/core/validation.coffee @@ -27,4 +27,5 @@ define ["core/spi", "core/events", "core/utils", "core/fields"], memo.error = true return false - configureDecimals: -> \ No newline at end of file + # Export nothing for now + return null \ No newline at end of file http://git-wip-us.apache.org/repos/asf/tapestry-5/blob/ac7f2635/tapestry-core/src/main/java/org/apache/tapestry5/internal/pageload/DefaultComponentResourceLocator.java ---------------------------------------------------------------------- diff --git a/tapestry-core/src/main/java/org/apache/tapestry5/internal/pageload/DefaultComponentResourceLocator.java b/tapestry-core/src/main/java/org/apache/tapestry5/internal/pageload/DefaultComponentResourceLocator.java index 327b448..e61c510 100644 --- a/tapestry-core/src/main/java/org/apache/tapestry5/internal/pageload/DefaultComponentResourceLocator.java +++ b/tapestry-core/src/main/java/org/apache/tapestry5/internal/pageload/DefaultComponentResourceLocator.java @@ -1,4 +1,4 @@ -// Copyright 2011 The Apache Software Foundation +// Copyright 2011, 2012 The Apache Software Foundation // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. @@ -14,8 +14,6 @@ package org.apache.tapestry5.internal.pageload; -import java.util.List; - import org.apache.tapestry5.func.F; import org.apache.tapestry5.func.Mapper; import org.apache.tapestry5.ioc.Resource; @@ -25,6 +23,9 @@ import org.apache.tapestry5.services.pageload.ComponentResourceLocator; import org.apache.tapestry5.services.pageload.ComponentResourceSelector; import org.apache.tapestry5.services.templates.ComponentTemplateLocator; +import java.util.Arrays; +import java.util.List; + public class DefaultComponentResourceLocator implements ComponentResourceLocator { private ComponentTemplateLocator componentTemplateLocator; @@ -46,6 +47,12 @@ public class DefaultComponentResourceLocator implements ComponentResourceLocator { String baseName = baseResource.getFile(); + // This is the case for some of the "virtual resources" introduced in 5.4 + if (baseName == null) + { + return Arrays.asList(baseResource.forLocale(selector.locale)); + } + return F.flow(new LocalizedNameGenerator(baseName, selector.locale).iterator()).map(new Mapper<String, Resource>() { public Resource map(String element) http://git-wip-us.apache.org/repos/asf/tapestry-5/blob/ac7f2635/tapestry-core/src/main/java/org/apache/tapestry5/internal/services/messages/DecimalFormatMessageResource.java ---------------------------------------------------------------------- diff --git a/tapestry-core/src/main/java/org/apache/tapestry5/internal/services/messages/DecimalFormatMessageResource.java b/tapestry-core/src/main/java/org/apache/tapestry5/internal/services/messages/DecimalFormatMessageResource.java new file mode 100644 index 0000000..070d167 --- /dev/null +++ b/tapestry-core/src/main/java/org/apache/tapestry5/internal/services/messages/DecimalFormatMessageResource.java @@ -0,0 +1,97 @@ +// Copyright 2012 The Apache Software Foundation +// +// Licensed 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.tapestry5.internal.services.messages; + +import org.apache.tapestry5.internal.util.VirtualResource; +import org.apache.tapestry5.ioc.Resource; + +import java.io.ByteArrayInputStream; +import java.io.IOException; +import java.io.InputStream; +import java.net.URL; +import java.text.DecimalFormatSymbols; +import java.util.Locale; + +/** + * Provides a number of symbols related to formatting numbers; by exposing these in the global message catalog, + * they are available to the client (via the "core/messages" module). + * + * @since 5.4 + */ +public class DecimalFormatMessageResource extends VirtualResource +{ + private final Locale locale; + + public DecimalFormatMessageResource() + { + this(null); + } + + DecimalFormatMessageResource(Locale locale) + { + this.locale = locale; + } + + @Override + public Resource withExtension(String extension) + { + return this; + } + + @Override + public String getPath() + { + return String.format("<Virtual DecimalFormat symbols for %s>", locale); + } + + @Override + public String getFile() + { + return null; + } + + @Override + public URL toURL() + { + return null; + } + + @Override + public Resource forLocale(Locale locale) + { + return new DecimalFormatMessageResource(locale); + } + + @Override + public InputStream openStream() throws IOException + { + DecimalFormatSymbols symbols = DecimalFormatSymbols.getInstance(locale); + + StringBuilder builder = new StringBuilder(200); + + write(builder, "group", symbols.getGroupingSeparator()); + write(builder, "minus", symbols.getMinusSign()); + write(builder, "decimal", symbols.getDecimalSeparator()); + + byte[] bytes = builder.toString().getBytes("UTF-8"); + + return new ByteArrayInputStream(bytes); + } + + private void write(StringBuilder builder, String name, char value) + { + builder.append("decimal-symbols.").append(name).append("=").append(value).append("\n"); + } +} http://git-wip-us.apache.org/repos/asf/tapestry-5/blob/ac7f2635/tapestry-core/src/main/java/org/apache/tapestry5/internal/translator/NumericTranslatorSupportImpl.java ---------------------------------------------------------------------- diff --git a/tapestry-core/src/main/java/org/apache/tapestry5/internal/translator/NumericTranslatorSupportImpl.java b/tapestry-core/src/main/java/org/apache/tapestry5/internal/translator/NumericTranslatorSupportImpl.java index a8eb771..5c506ca 100644 --- a/tapestry-core/src/main/java/org/apache/tapestry5/internal/translator/NumericTranslatorSupportImpl.java +++ b/tapestry-core/src/main/java/org/apache/tapestry5/internal/translator/NumericTranslatorSupportImpl.java @@ -15,14 +15,10 @@ package org.apache.tapestry5.internal.translator; import org.apache.tapestry5.Field; -import org.apache.tapestry5.SymbolConstants; -import org.apache.tapestry5.ioc.annotations.Symbol; import org.apache.tapestry5.ioc.internal.util.CollectionFactory; import org.apache.tapestry5.ioc.services.ThreadLocale; import org.apache.tapestry5.ioc.services.TypeCoercer; -import org.apache.tapestry5.json.JSONObject; import org.apache.tapestry5.services.ClientBehaviorSupport; -import org.apache.tapestry5.services.Request; import org.apache.tapestry5.services.javascript.JavaScriptSupport; import java.math.BigDecimal; @@ -32,7 +28,6 @@ import java.text.DecimalFormatSymbols; import java.text.NumberFormat; import java.text.ParseException; import java.util.Locale; -import java.util.Map; import java.util.Set; public class NumericTranslatorSupportImpl implements NumericTranslatorSupport @@ -41,80 +36,35 @@ public class NumericTranslatorSupportImpl implements NumericTranslatorSupport private final ThreadLocale threadLocale; - private final Request request; - private final JavaScriptSupport javascriptSupport; private final ClientBehaviorSupport clientBehaviorSupport; - private final boolean compactJSON; - - private final Map<Locale, DecimalFormatSymbols> symbolsCache = CollectionFactory.newConcurrentMap(); - private final Set<Class> integerTypes = CollectionFactory.newSet(); - private static final String DECIMAL_FORMAT_SYMBOLS_PROVIDED = "tapestry.decimal-format-symbols-provided"; - - public NumericTranslatorSupportImpl(TypeCoercer typeCoercer, ThreadLocale threadLocale, Request request, - JavaScriptSupport javascriptSupport, ClientBehaviorSupport clientBehaviorSupport, - @Symbol(SymbolConstants.COMPACT_JSON) - boolean compactJSON) + public NumericTranslatorSupportImpl(TypeCoercer typeCoercer, ThreadLocale threadLocale, + JavaScriptSupport javascriptSupport, ClientBehaviorSupport clientBehaviorSupport) { this.typeCoercer = typeCoercer; this.threadLocale = threadLocale; - this.request = request; this.javascriptSupport = javascriptSupport; this.clientBehaviorSupport = clientBehaviorSupport; - this.compactJSON = compactJSON; Class[] integerTypes = {Byte.class, Short.class, Integer.class, Long.class, BigInteger.class}; for (Class c : integerTypes) + { this.integerTypes.add(c); + } } public <T extends Number> void addValidation(Class<T> type, Field field, String message) { - if (request.getAttribute(DECIMAL_FORMAT_SYMBOLS_PROVIDED) == null) - { - javascriptSupport.require("core/validation").invoke("configureDecimals").with(createJSONDecimalFormatSymbols()); - - request.setAttribute(DECIMAL_FORMAT_SYMBOLS_PROVIDED, true); - } - clientBehaviorSupport.addValidation(field, "numericformat", message, isIntegerType(type)); } - private JSONObject createJSONDecimalFormatSymbols() - { - Locale locale = threadLocale.getLocale(); - - DecimalFormatSymbols symbols = getSymbols(locale); - - JSONObject result = new JSONObject(); - - result.put("groupingSeparator", toString(symbols.getGroupingSeparator())); - result.put("minusSign", toString(symbols.getMinusSign())); - result.put("decimalSeparator", toString(symbols.getDecimalSeparator())); - - return result; - } - - private DecimalFormatSymbols getSymbols(Locale locale) - { - DecimalFormatSymbols symbols = symbolsCache.get(locale); - - if (symbols == null) - { - symbols = new DecimalFormatSymbols(locale); - symbolsCache.put(locale, symbols); - } - - return symbols; - } - private boolean isIntegerType(Class type) { return integerTypes.contains(type); @@ -132,7 +82,7 @@ public class NumericTranslatorSupportImpl implements NumericTranslatorSupport private NumericFormatter getParseFormatter(Class type) { Locale locale = threadLocale.getLocale(); - DecimalFormatSymbols symbols = getSymbols(locale); + DecimalFormatSymbols symbols = DecimalFormatSymbols.getInstance(locale); if (type.equals(BigInteger.class)) return new BigIntegerNumericFormatter(symbols); @@ -165,7 +115,7 @@ public class NumericTranslatorSupportImpl implements NumericTranslatorSupport { Locale locale = threadLocale.getLocale(); - DecimalFormatSymbols symbols = getSymbols(locale); + DecimalFormatSymbols symbols = DecimalFormatSymbols.getInstance(locale); if (type.equals(BigInteger.class)) return new BigIntegerNumericFormatter(symbols); http://git-wip-us.apache.org/repos/asf/tapestry-5/blob/ac7f2635/tapestry-core/src/main/java/org/apache/tapestry5/services/TapestryModule.java ---------------------------------------------------------------------- diff --git a/tapestry-core/src/main/java/org/apache/tapestry5/services/TapestryModule.java b/tapestry-core/src/main/java/org/apache/tapestry5/services/TapestryModule.java index 8dba498..cb1db09 100644 --- a/tapestry-core/src/main/java/org/apache/tapestry5/services/TapestryModule.java +++ b/tapestry-core/src/main/java/org/apache/tapestry5/services/TapestryModule.java @@ -45,6 +45,7 @@ import org.apache.tapestry5.internal.services.assets.ContextAssetRequestHandler; import org.apache.tapestry5.internal.services.assets.StackAssetRequestHandler; import org.apache.tapestry5.internal.services.linktransform.LinkTransformerImpl; import org.apache.tapestry5.internal.services.linktransform.LinkTransformerInterceptor; +import org.apache.tapestry5.internal.services.messages.DecimalFormatMessageResource; import org.apache.tapestry5.internal.services.messages.PropertiesFileParserImpl; import org.apache.tapestry5.internal.services.meta.ContentTypeExtractor; import org.apache.tapestry5.internal.services.meta.MetaAnnotationExtractor; @@ -2582,6 +2583,8 @@ public final class TapestryModule /** * Contributes: * <dl> + * <dt>DecimalSymbols</dt> + * <dd>A virtual resource of formatting symbols for decimal numbers</dd> * <dt>Core</dt> * <dd>Built in messages used by Tapestry's default validators and components</dd> * <dt>AppCatalog</dt> @@ -2592,9 +2595,10 @@ public final class TapestryModule */ @Contribute(ComponentMessagesSource.class) public static void setupGlobalMessageCatalog(AssetSource assetSource, - @Symbol(SymbolConstants.APPLICATION_CATALOG) - Resource applicationCatalog, OrderedConfiguration<Resource> configuration) + @Symbol(SymbolConstants.APPLICATION_CATALOG) + Resource applicationCatalog, OrderedConfiguration<Resource> configuration) { + configuration.add("DecimalSymbols", new DecimalFormatMessageResource()); configuration.add("Core", assetSource.resourceForPath("org/apache/tapestry5/core.properties")); configuration.add("AppCatalog", applicationCatalog); } http://git-wip-us.apache.org/repos/asf/tapestry-5/blob/ac7f2635/tapestry-ioc/src/main/java/org/apache/tapestry5/ioc/Resource.java ---------------------------------------------------------------------- diff --git a/tapestry-ioc/src/main/java/org/apache/tapestry5/ioc/Resource.java b/tapestry-ioc/src/main/java/org/apache/tapestry5/ioc/Resource.java index bb87611..8b154c9 100644 --- a/tapestry-ioc/src/main/java/org/apache/tapestry5/ioc/Resource.java +++ b/tapestry-ioc/src/main/java/org/apache/tapestry5/ioc/Resource.java @@ -32,6 +32,8 @@ public interface Resource /** * Returns true if the resource exists; if a stream to the content of the file may be opened. A resource exists * if {@link #toURL()} returns a non-null value. Starting in release 5.3.4, the result of this is cached. + * <p/> + * Starting in 5.4, some "virtual resources", may return true even though {@link #toURL()} returns null. * * @return true if the resource exists, false if it does not */ @@ -47,7 +49,7 @@ public interface Resource /** * Returns the URL for the resource, or null if it does not exist. This value is lazily computed; starting in 5.3.4, subclasses may cache - * the result. + * the result. Starting in 5.4, some "virtual resources" may return null. */ URL toURL(); @@ -81,11 +83,15 @@ public interface Resource /** * Returns the file portion of the Resource path, everything that follows the final forward slash. + * <p/> + * Starting in 5.4, certain kinds of "virtual resources" may return null here. */ String getFile(); /** * Return the path (the combination of folder and file). + * <p/> + * Starting in 5.4, certain "virtual resources", may return an arbitrary value here. */ String getPath(); }