001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.commons.text;
018
019import java.util.Map;
020import java.util.ResourceBundle;
021
022import org.apache.commons.text.lookup.StringLookup;
023import org.apache.commons.text.lookup.StringLookupFactory;
024
025/**
026 * Lookup a String key to a String value.
027 * <p>
028 * This class represents the simplest form of a string to string map. It has a benefit over a map in that it can create
029 * the result on demand based on the key.
030 * <p>
031 * This class comes complete with various factory methods. If these do not suffice, you can subclass and implement your
032 * own matcher.
033 * <p>
034 * For example, it would be possible to implement a lookup that used the key as a primary key, and looked up the value
035 * on demand from the database
036 *
037 * @param <V> the type of the values supported by the lookup
038 * @since 1.0
039 * @deprecated Deprecated as of 1.3, use {@link StringLookupFactory} instead. This class will be removed in 2.0.
040 */
041@Deprecated
042public abstract class StrLookup<V> implements StringLookup {
043
044    /**
045     * Lookup that always returns null.
046     */
047    private static final StrLookup<String> NONE_LOOKUP = new MapStrLookup<>(null);
048
049    /**
050     * Lookup based on system properties.
051     */
052    private static final StrLookup<String> SYSTEM_PROPERTIES_LOOKUP = new SystemPropertiesStrLookup();
053
054    // -----------------------------------------------------------------------
055    /**
056     * Returns a lookup which always returns null.
057     *
058     * @return a lookup that always returns null, not null
059     */
060    public static StrLookup<?> noneLookup() {
061        return NONE_LOOKUP;
062    }
063
064    /**
065     * Returns a new lookup which uses a copy of the current {@link System#getProperties() System properties}.
066     * <p>
067     * If a security manager blocked access to system properties, then null will be returned from every lookup.
068     * <p>
069     * If a null key is used, this lookup will throw a NullPointerException.
070     *
071     * @return a lookup using system properties, not null
072     */
073    public static StrLookup<String> systemPropertiesLookup() {
074        return SYSTEM_PROPERTIES_LOOKUP;
075    }
076
077    /**
078     * Returns a lookup which looks up values using a map.
079     * <p>
080     * If the map is null, then null will be returned from every lookup. The map result object is converted to a string
081     * using toString().
082     *
083     * @param <V> the type of the values supported by the lookup
084     * @param map the map of keys to values, may be null
085     * @return a lookup using the map, not null
086     */
087    public static <V> StrLookup<V> mapLookup(final Map<String, V> map) {
088        return new MapStrLookup<>(map);
089    }
090
091    /**
092     * Returns a lookup which looks up values using a ResourceBundle.
093     * <p>
094     * If the ResourceBundle is null, then null will be returned from every lookup. The map result object is converted
095     * to a string using toString().
096     *
097     * @param resourceBundle the map of keys to values, may be null
098     * @return a lookup using the map, not null
099     */
100    public static StrLookup<String> resourceBundleLookup(final ResourceBundle resourceBundle) {
101        return new ResourceBundleLookup(resourceBundle);
102    }
103
104    // -----------------------------------------------------------------------
105    /**
106     * Constructor.
107     */
108    protected StrLookup() {
109        super();
110    }
111
112    // -----------------------------------------------------------------------
113    /**
114     * Lookup implementation that uses a Map.
115     *
116     * @param <V> the type of the values supported by the lookup
117     */
118    static class MapStrLookup<V> extends StrLookup<V> {
119
120        /** Map keys are variable names and value. */
121        private final Map<String, V> map;
122
123        /**
124         * Creates a new instance backed by a Map.
125         *
126         * @param map the map of keys to values, may be null
127         */
128        MapStrLookup(final Map<String, V> map) {
129            this.map = map;
130        }
131
132        /**
133         * Looks up a String key to a String value using the map.
134         * <p>
135         * If the map is null, then null is returned. The map result object is converted to a string using toString().
136         *
137         * @param key the key to be looked up, may be null
138         * @return the matching value, null if no match
139         */
140        @Override
141        public String lookup(final String key) {
142            if (map == null) {
143                return null;
144            }
145            final Object obj = map.get(key);
146            if (obj == null) {
147                return null;
148            }
149            return obj.toString();
150        }
151
152        @Override
153        public String toString() {
154            return super.toString() + " [map=" + map + "]";
155        }
156    }
157
158    // -----------------------------------------------------------------------
159    /**
160     * Lookup implementation based on a ResourceBundle.
161     */
162    private static final class ResourceBundleLookup extends StrLookup<String> {
163
164        /** ResourceBundle keys are variable names and value. */
165        private final ResourceBundle resourceBundle;
166
167        /**
168         * Creates a new instance backed by a ResourceBundle.
169         *
170         * @param resourceBundle the ResourceBundle of keys to values, may be null
171         */
172        private ResourceBundleLookup(final ResourceBundle resourceBundle) {
173            this.resourceBundle = resourceBundle;
174        }
175
176        @Override
177        public String lookup(final String key) {
178            if (resourceBundle == null || key == null || !resourceBundle.containsKey(key)) {
179                return null;
180            }
181            return resourceBundle.getString(key);
182        }
183
184        @Override
185        public String toString() {
186            return super.toString() + " [resourceBundle=" + resourceBundle + "]";
187        }
188
189    }
190
191    // -----------------------------------------------------------------------
192    /**
193     * Lookup implementation based on system properties.
194     */
195    private static final class SystemPropertiesStrLookup extends StrLookup<String> {
196        /**
197         * {@inheritDoc} This implementation directly accesses system properties.
198         */
199        @Override
200        public String lookup(final String key) {
201            if (key.length() > 0) {
202                try {
203                    return System.getProperty(key);
204                } catch (final SecurityException scex) {
205                    // Squelched. All lookup(String) will return null.
206                    return null;
207                }
208            }
209            return null;
210        }
211    }
212}