001/*
002 *  Copyright 2013 Anyware Services
003 *
004 *  Licensed under the Apache License, Version 2.0 (the "License");
005 *  you may not use this file except in compliance with the License.
006 *  You may obtain a copy of the License at
007 *
008 *      http://www.apache.org/licenses/LICENSE-2.0
009 *
010 *  Unless required by applicable law or agreed to in writing, software
011 *  distributed under the License is distributed on an "AS IS" BASIS,
012 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 *  See the License for the specific language governing permissions and
014 *  limitations under the License.
015 */
016package org.ametys.core.cocoon;
017
018import java.util.Locale;
019
020import org.apache.avalon.framework.service.ServiceManager;
021import org.apache.cocoon.i18n.Bundle;
022import org.apache.cocoon.xml.ParamSaxBuffer;
023import org.apache.excalibur.source.SourceResolver;
024
025import org.ametys.core.util.cocoon.InvalidSourceValidity;
026
027/**
028 * This implementation of <code>Bundle</code> interface for XML resources allows to be invalidated. 
029 *
030 */
031public class XMLResourceBundle extends org.apache.cocoon.i18n.XMLResourceBundle
032{
033    /**
034     * Construct a bundle.
035     * @param sourceURI source URI of the XML bundle
036     * @param locale locale
037     * @param parentBundle parent bundle of this bundle
038     */
039    public XMLResourceBundle(String sourceURI, Locale locale, Bundle parentBundle)
040    {
041        super(sourceURI, locale, parentBundle);
042    }
043
044    @Override
045    public boolean reload(ServiceManager manager, SourceResolver resolver, long interval)
046    {
047        return super.reload(manager, resolver, interval);
048    }
049    
050    /**
051     * Invalidate bundle
052     */
053    public void invalidate ()
054    {
055        setValidity(new InvalidSourceValidity());
056    }
057    
058    /**
059     * Expert method !
060     * Get an instance of the {@link ParamSaxBuffer} associated with the key. 
061     * This method delegates to parent object only if related to the same Locale. 
062     * @param key the key
063     * @return the value, or null if no value associated with the key in this Locale.
064     */
065    public Object getRawObject(String key) 
066    {
067        if (key == null) 
068        {
069            return null;
070        }
071
072        Object value = this.values.get(key);
073        if (value != null) 
074        {
075            return value;
076        }
077        
078        // get the parent only if it concerns the same locale, so that it is still possible to override plugins with WEB-INF/i18n
079        // but get null anyway if not found for current locale
080        if (parent != null && ((XMLResourceBundle) parent).getLocale().equals(getLocale()))
081        {
082            return ((XMLResourceBundle) this.parent).getRawObject(key);
083        }
084
085        return null;
086    }
087}