001/*
002 * (C) Copyright 2006-2008 Nuxeo SAS (http://nuxeo.com/) and contributors.
003 *
004 * All rights reserved. This program and the accompanying materials
005 * are made available under the terms of the GNU Lesser General Public License
006 * (LGPL) version 2.1 which accompanies this distribution, and is available at
007 * http://www.gnu.org/licenses/lgpl.html
008 *
009 * This library is distributed in the hope that it will be useful,
010 * but WITHOUT ANY WARRANTY; without even the implied warranty of
011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012 * Lesser General Public License for more details.
013 *
014 * Contributors:
015 *     bstefanescu
016 *
017 * $Id$
018 */
019
020package org.nuxeo.ecm.webengine.model;
021
022import java.util.Enumeration;
023import java.util.HashMap;
024import java.util.Iterator;
025import java.util.Map;
026import java.util.ResourceBundle;
027
028/**
029 * @author <a href="mailto:bs@nuxeo.com">Bogdan Stefanescu</a>
030 */
031public class MessagesBundle extends ResourceBundle {
032
033    protected final Map<String, String> messages;
034
035    public MessagesBundle(ResourceBundle parent, Map<String, String> messages) {
036        this.parent = parent;
037        this.messages = messages == null ? new HashMap<String, String>() : messages;
038    }
039
040    @Override
041    public Object handleGetObject(String key) {
042        if (key == null) {
043            throw new NullPointerException();
044        }
045        return messages.get(key);
046    }
047
048    /**
049     * Implementation of ResourceBundle.getKeys.
050     */
051    @Override
052    public Enumeration<String> getKeys() {
053        ResourceBundle parent = this.parent;
054        return new Keys(messages.keySet().iterator(), (parent != null) ? parent.getKeys() : null);
055    }
056
057    static class Keys implements Enumeration<String> {
058        protected final Iterator<String> it;
059
060        protected final Enumeration<String> parent;
061
062        Keys(Iterator<String> it, Enumeration<String> parent) {
063            this.it = it;
064            this.parent = parent;
065        }
066
067        public boolean hasMoreElements() {
068            if (it.hasNext()) {
069                return true;
070            }
071            return parent.hasMoreElements();
072        }
073
074        public String nextElement() {
075            if (it.hasNext()) {
076                return it.next();
077            }
078            return parent.nextElement();
079        }
080    }
081
082}