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 * @see StringLookupFactory#resourceBundleStringLookup(String) 100 */ 101 public static StrLookup<String> resourceBundleLookup(final ResourceBundle resourceBundle) { 102 return new ResourceBundleLookup(resourceBundle); 103 } 104 105 // ----------------------------------------------------------------------- 106 /** 107 * Constructor. 108 */ 109 protected StrLookup() { 110 super(); 111 } 112 113 // ----------------------------------------------------------------------- 114 /** 115 * Lookup implementation that uses a Map. 116 * 117 * @param <V> the type of the values supported by the lookup 118 */ 119 static class MapStrLookup<V> extends StrLookup<V> { 120 121 /** Map keys are variable names and value. */ 122 private final Map<String, V> map; 123 124 /** 125 * Creates a new instance backed by a Map. 126 * 127 * @param map the map of keys to values, may be null 128 */ 129 MapStrLookup(final Map<String, V> map) { 130 this.map = map; 131 } 132 133 /** 134 * Looks up a String key to a String value using the map. 135 * <p> 136 * If the map is null, then null is returned. The map result object is converted to a string using toString(). 137 * 138 * @param key the key to be looked up, may be null 139 * @return The matching value, null if no match 140 */ 141 @Override 142 public String lookup(final String key) { 143 if (map == null) { 144 return null; 145 } 146 final Object obj = map.get(key); 147 if (obj == null) { 148 return null; 149 } 150 return obj.toString(); 151 } 152 153 @Override 154 public String toString() { 155 return super.toString() + " [map=" + map + "]"; 156 } 157 } 158 159 // ----------------------------------------------------------------------- 160 /** 161 * Lookup implementation based on a ResourceBundle. 162 */ 163 private static final class ResourceBundleLookup extends StrLookup<String> { 164 165 /** ResourceBundle keys are variable names and value. */ 166 private final ResourceBundle resourceBundle; 167 168 /** 169 * Creates a new instance backed by a ResourceBundle. 170 * 171 * @param resourceBundle the ResourceBundle of keys to values, may be null 172 */ 173 private ResourceBundleLookup(final ResourceBundle resourceBundle) { 174 this.resourceBundle = resourceBundle; 175 } 176 177 @Override 178 public String lookup(final String key) { 179 if (resourceBundle == null || key == null || !resourceBundle.containsKey(key)) { 180 return null; 181 } 182 return resourceBundle.getString(key); 183 } 184 185 @Override 186 public String toString() { 187 return super.toString() + " [resourceBundle=" + resourceBundle + "]"; 188 } 189 190 } 191 192 // ----------------------------------------------------------------------- 193 /** 194 * Lookup implementation based on system properties. 195 */ 196 private static final class SystemPropertiesStrLookup extends StrLookup<String> { 197 /** 198 * {@inheritDoc} This implementation directly accesses system properties. 199 */ 200 @Override 201 public String lookup(final String key) { 202 if (key.length() > 0) { 203 try { 204 return System.getProperty(key); 205 } catch (final SecurityException scex) { 206 // Squelched. All lookup(String) will return null. 207 return null; 208 } 209 } 210 return null; 211 } 212 } 213}