001/*
002 * (C) Copyright 2006-2008 Nuxeo SA (http://nuxeo.com/) and others.
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 *
016 * Contributors:
017 *     bstefanescu
018 *
019 * $Id$
020 */
021
022package org.nuxeo.ecm.webengine.model;
023
024import java.util.Enumeration;
025import java.util.HashMap;
026import java.util.Iterator;
027import java.util.Map;
028import java.util.ResourceBundle;
029
030/**
031 * @author <a href="mailto:bs@nuxeo.com">Bogdan Stefanescu</a>
032 */
033public class MessagesBundle extends ResourceBundle {
034
035    protected final Map<String, String> messages;
036
037    public MessagesBundle(ResourceBundle parent, Map<String, String> messages) {
038        this.parent = parent;
039        this.messages = messages == null ? new HashMap<String, String>() : messages;
040    }
041
042    @Override
043    public Object handleGetObject(String key) {
044        if (key == null) {
045            throw new NullPointerException();
046        }
047        return messages.get(key);
048    }
049
050    /**
051     * Implementation of ResourceBundle.getKeys.
052     */
053    @Override
054    public Enumeration<String> getKeys() {
055        ResourceBundle parent = this.parent;
056        return new Keys(messages.keySet().iterator(), (parent != null) ? parent.getKeys() : null);
057    }
058
059    static class Keys implements Enumeration<String> {
060        protected final Iterator<String> it;
061
062        protected final Enumeration<String> parent;
063
064        Keys(Iterator<String> it, Enumeration<String> parent) {
065            this.it = it;
066            this.parent = parent;
067        }
068
069        public boolean hasMoreElements() {
070            if (it.hasNext()) {
071                return true;
072            }
073            return parent.hasMoreElements();
074        }
075
076        public String nextElement() {
077            if (it.hasNext()) {
078                return it.next();
079            }
080            return parent.nextElement();
081        }
082    }
083
084}