/* $$ Clover has instrumented this file $$ */// Copyright 2004 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.tapestry.services.impl;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
import java.util.Properties;
import org.apache.hivemind.ApplicationRuntimeException;
import org.apache.hivemind.Messages;
import org.apache.hivemind.Resource;
import org.apache.tapestry.Defense;
import org.apache.tapestry.IComponent;
import org.apache.tapestry.Tapestry;
import org.apache.tapestry.engine.ComponentMessages;
import org.apache.tapestry.engine.DefaultComponentPropertySource;
import org.apache.tapestry.engine.IPropertySource;
import org.apache.tapestry.event.ResetEventListener;
import org.apache.tapestry.services.ComponentMessagesSource;
import org.apache.tapestry.util.MultiKey;
import org.apache.tapestry.util.text.LocalizedProperties;
/**
* Service used to access localized properties for a component.
*
* @author Howard Lewis Ship
* @since 2.0.4
*/
public class ComponentMessagesSourceImpl implements ComponentMessagesSource, ResetEventListener
{public static com.cortexeb.tools.clover.d __CLOVER_305_0 = com.cortexeb.tools.clover.aq.getRecorder(new char[] {67,58,92,119,111,114,107,115,112,97,99,101,92,106,97,107,97,114,116,97,45,116,97,112,101,115,116,114,121,92,102,114,97,109,101,119,111,114,107,92,116,97,114,103,101,116,92,99,108,111,118,101,114,45,100,98},1097439627296L);
private Properties _emptyProperties = new Properties();
private static final String SUFFIX = ".properties";
/**
* The name of the component/application/etc property that will be used to determine the
* encoding to use when loading the messages
*/
public static final String MESSAGES_ENCODING_PROPERTY_NAME = "org.apache.tapestry.messages-encoding";
/**
* Map of {@link Properties}, keyed on a {@link MultiKey}of component specification path and
* locale.
*/
private Map _cache = new HashMap();
private IPropertySource _applicationPropertySource;
public void setApplicationPropertySource(IPropertySource source)
{try { __CLOVER_305_0.M[3283]++;
__CLOVER_305_0.S[13974]++;_applicationPropertySource = source;
} finally { }}
/**
* Returns an instance of {@link Properties}containing the properly localized messages for the
* component, in the {@link Locale}identified by the component's containing page.
*/
protected synchronized Properties getLocalizedProperties(IComponent component)
{try { __CLOVER_305_0.M[3284]++;
__CLOVER_305_0.S[13975]++;Defense.notNull(component, "component");
__CLOVER_305_0.S[13976]++;Resource specificationLocation = component.getSpecification().getSpecificationLocation();
__CLOVER_305_0.S[13977]++;Locale locale = component.getPage().getLocale();
// Check to see if already in the cache
__CLOVER_305_0.S[13978]++;MultiKey key = buildKey(specificationLocation, locale);
__CLOVER_305_0.S[13979]++;Properties result = (Properties) _cache.get(key);
__CLOVER_305_0.S[13980]++;if ((((result != null) && (++__CLOVER_305_0.CT[2338] != 0)) || (++__CLOVER_305_0.CF[2338] == 0))){
__CLOVER_305_0.S[13981]++;return result;}
// Not found, create it now.
__CLOVER_305_0.S[13982]++;result = assembleProperties(component, specificationLocation, locale);
__CLOVER_305_0.S[13983]++;_cache.put(key, result);
__CLOVER_305_0.S[13984]++;return result;
} finally { }}
private Properties assembleProperties(IComponent component, Resource baseResourceLocation,
Locale locale)
{try { __CLOVER_305_0.M[3285]++;
__CLOVER_305_0.S[13985]++;String name = baseResourceLocation.getName();
__CLOVER_305_0.S[13986]++;int dotx = name.indexOf('.');
__CLOVER_305_0.S[13987]++;String baseName = name.substring(0, dotx);
__CLOVER_305_0.S[13988]++;String language = locale.getLanguage();
__CLOVER_305_0.S[13989]++;String country = locale.getCountry();
__CLOVER_305_0.S[13990]++;String variant = locale.getVariant();
__CLOVER_305_0.S[13991]++;Properties parent = (Properties) _cache.get(baseResourceLocation);
__CLOVER_305_0.S[13992]++;if ((((parent == null) && (++__CLOVER_305_0.CT[2339] != 0)) || (++__CLOVER_305_0.CF[2339] == 0))){
{
__CLOVER_305_0.S[13993]++;parent = readProperties(component, baseResourceLocation, baseName, null, null);
__CLOVER_305_0.S[13994]++;if ((((parent == null) && (++__CLOVER_305_0.CT[2340] != 0)) || (++__CLOVER_305_0.CF[2340] == 0))){
{
__CLOVER_305_0.S[13995]++;parent = _emptyProperties;
__CLOVER_305_0.S[13996]++;_cache.put(baseResourceLocation, parent);
}}
}}
__CLOVER_305_0.S[13997]++;Properties result = parent;
__CLOVER_305_0.S[13998]++;if ((((!Tapestry.isBlank(language)) && (++__CLOVER_305_0.CT[2341] != 0)) || (++__CLOVER_305_0.CF[2341] == 0))){
{
__CLOVER_305_0.S[13999]++;Locale l = new Locale(language, "");
__CLOVER_305_0.S[14000]++;MultiKey key = buildKey(baseResourceLocation, l);
__CLOVER_305_0.S[14001]++;result = (Properties) _cache.get(key);
__CLOVER_305_0.S[14002]++;if ((((result == null) && (++__CLOVER_305_0.CT[2342] != 0)) || (++__CLOVER_305_0.CF[2342] == 0))){
{
__CLOVER_305_0.S[14003]++;result = readProperties(component, baseResourceLocation, baseName, l, parent);
__CLOVER_305_0.S[14004]++;_cache.put(key, result);
}}
__CLOVER_305_0.S[14005]++;parent = result;
}}
else{
__CLOVER_305_0.S[14006]++;language = "";}
__CLOVER_305_0.S[14007]++;if ((((Tapestry.isNonBlank(country)) && (++__CLOVER_305_0.CT[2343] != 0)) || (++__CLOVER_305_0.CF[2343] == 0))){
{
__CLOVER_305_0.S[14008]++;Locale l = new Locale(language, country);
__CLOVER_305_0.S[14009]++;MultiKey key = buildKey(baseResourceLocation, l);
__CLOVER_305_0.S[14010]++;result = (Properties) _cache.get(key);
__CLOVER_305_0.S[14011]++;if ((((result == null) && (++__CLOVER_305_0.CT[2344] != 0)) || (++__CLOVER_305_0.CF[2344] == 0))){
{
__CLOVER_305_0.S[14012]++;result = readProperties(component, baseResourceLocation, baseName, l, parent);
__CLOVER_305_0.S[14013]++;_cache.put(key, result);
}}
__CLOVER_305_0.S[14014]++;parent = result;
}}
else{
__CLOVER_305_0.S[14015]++;country = "";}
__CLOVER_305_0.S[14016]++;if ((((Tapestry.isNonBlank(variant)) && (++__CLOVER_305_0.CT[2345] != 0)) || (++__CLOVER_305_0.CF[2345] == 0))){
{
__CLOVER_305_0.S[14017]++;Locale l = new Locale(language, country, variant);
__CLOVER_305_0.S[14018]++;MultiKey key = buildKey(baseResourceLocation, l);
__CLOVER_305_0.S[14019]++;result = (Properties) _cache.get(key);
__CLOVER_305_0.S[14020]++;if ((((result == null) && (++__CLOVER_305_0.CT[2346] != 0)) || (++__CLOVER_305_0.CF[2346] == 0))){
{
__CLOVER_305_0.S[14021]++;result = readProperties(component, baseResourceLocation, baseName, l, parent);
__CLOVER_305_0.S[14022]++;_cache.put(key, result);
}}
}}
__CLOVER_305_0.S[14023]++;return result;
} finally { }}
private MultiKey buildKey(Resource location, Locale locale)
{try { __CLOVER_305_0.M[3286]++;
__CLOVER_305_0.S[14024]++;return new MultiKey(new Object[]
{ location, locale.toString() }, false);
} finally { }}
private Properties readProperties(IComponent component, Resource baseLocation, String baseName,
Locale locale, Properties parent)
{try { __CLOVER_305_0.M[3287]++;
__CLOVER_305_0.S[14025]++;StringBuffer buffer = new StringBuffer(baseName);
__CLOVER_305_0.S[14026]++;if ((((locale != null) && (++__CLOVER_305_0.CT[2347] != 0)) || (++__CLOVER_305_0.CF[2347] == 0))){
{
__CLOVER_305_0.S[14027]++;buffer.append('_');
__CLOVER_305_0.S[14028]++;buffer.append(locale.toString());
}}
__CLOVER_305_0.S[14029]++;buffer.append(SUFFIX);
__CLOVER_305_0.S[14030]++;Resource localized = baseLocation.getRelativeResource(buffer.toString());
__CLOVER_305_0.S[14031]++;URL propertiesURL = localized.getResourceURL();
__CLOVER_305_0.S[14032]++;if ((((propertiesURL == null) && (++__CLOVER_305_0.CT[2348] != 0)) || (++__CLOVER_305_0.CF[2348] == 0))){
__CLOVER_305_0.S[14033]++;return parent;}
__CLOVER_305_0.S[14034]++;Properties result = null;
__CLOVER_305_0.S[14035]++;if ((((parent == null) && (++__CLOVER_305_0.CT[2349] != 0)) || (++__CLOVER_305_0.CF[2349] == 0))){
__CLOVER_305_0.S[14036]++;result = new Properties();}
else{
__CLOVER_305_0.S[14037]++;result = new Properties(parent);}
__CLOVER_305_0.S[14038]++;LocalizedProperties localizedResult = new LocalizedProperties(result);
__CLOVER_305_0.S[14039]++;String encoding = getMessagesEncoding(component, locale);
__CLOVER_305_0.S[14040]++;try
{
__CLOVER_305_0.S[14041]++;InputStream input = propertiesURL.openStream();
__CLOVER_305_0.S[14042]++;if ((((encoding == null) && (++__CLOVER_305_0.CT[2350] != 0)) || (++__CLOVER_305_0.CF[2350] == 0))){
__CLOVER_305_0.S[14043]++;localizedResult.load(input);}
else{
__CLOVER_305_0.S[14044]++;localizedResult.load(input, encoding);}
__CLOVER_305_0.S[14045]++;input.close();
}
catch (IOException ex)
{
__CLOVER_305_0.S[14046]++;throw new ApplicationRuntimeException(ImplMessages.unableToLoadProperties(
propertiesURL,
ex), ex);
}
__CLOVER_305_0.S[14047]++;return result;
} finally { }}
/**
* Clears the cache of read properties files.
*/
public synchronized void resetEventDidOccur()
{try { __CLOVER_305_0.M[3288]++;
__CLOVER_305_0.S[14048]++;_cache.clear();
} finally { }}
public Messages getMessages(IComponent component)
{try { __CLOVER_305_0.M[3289]++;
__CLOVER_305_0.S[14049]++;return new ComponentMessages(component.getPage().getLocale(),
getLocalizedProperties(component));
} finally { }}
private String getMessagesEncoding(IComponent component, Locale locale)
{try { __CLOVER_305_0.M[3290]++;
__CLOVER_305_0.S[14050]++;IPropertySource source = new DefaultComponentPropertySource(component,
_applicationPropertySource, locale);
__CLOVER_305_0.S[14051]++;String encoding = source.getPropertyValue(MESSAGES_ENCODING_PROPERTY_NAME);
__CLOVER_305_0.S[14052]++;if ((((encoding == null) && (++__CLOVER_305_0.CT[2351] != 0)) || (++__CLOVER_305_0.CF[2351] == 0))){
__CLOVER_305_0.S[14053]++;encoding = source.getPropertyValue(TemplateSourceImpl.TEMPLATE_ENCODING_PROPERTY_NAME);}
__CLOVER_305_0.S[14054]++;return encoding;
} finally { }}
}