1 package org.apache.velocity.runtime.resource.util; 2 3 /* 4 * Licensed to the Apache Software Foundation (ASF) under one 5 * or more contributor license agreements. See the NOTICE file 6 * distributed with this work for additional information 7 * regarding copyright ownership. The ASF licenses this file 8 * to you under the Apache License, Version 2.0 (the 9 * "License"); you may not use this file except in compliance 10 * with the License. You may obtain a copy of the License at 11 * 12 * http://www.apache.org/licenses/LICENSE-2.0 13 * 14 * Unless required by applicable law or agreed to in writing, 15 * software distributed under the License is distributed on an 16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 17 * KIND, either express or implied. See the License for the 18 * specific language governing permissions and limitations 19 * under the License. 20 */ 21 22 import org.apache.velocity.runtime.RuntimeConstants; 23 import org.apache.velocity.runtime.resource.Resource; 24 25 import java.util.Collections; 26 import java.util.HashMap; 27 import java.util.Map; 28 29 /** 30 * Default implementation of StringResourceRepository. 31 * Uses a HashMap for storage 32 * 33 * @author <a href="mailto:eelco.hillenius@openedge.nl">Eelco Hillenius</a> 34 * @author <a href="mailto:henning@apache.org">Henning P. Schmiedehausen</a> 35 * @version $Id$ 36 * @since 1.5 37 */ 38 public class StringResourceRepositoryImpl implements StringResourceRepository 39 { 40 /** 41 * mem store 42 */ 43 protected Map<String, StringResource> resources = Collections.synchronizedMap(new HashMap<>()); 44 45 /** 46 * Current Repository encoding. 47 */ 48 private String encoding = RuntimeConstants.ENCODING_DEFAULT; 49 50 /** 51 * @see StringResourceRepository#getStringResource(java.lang.String) 52 */ 53 @Override getStringResource(final String name)54 public StringResource getStringResource(final String name) 55 { 56 return resources.get(name); 57 } 58 59 /** 60 * @see StringResourceRepository#putStringResource(java.lang.String, java.lang.String) 61 */ 62 @Override putStringResource(final String name, final String body)63 public void putStringResource(final String name, final String body) 64 { 65 resources.put(name, new StringResource(body, getEncoding())); 66 } 67 68 /** 69 * @see StringResourceRepository#putStringResource(java.lang.String, java.lang.String, java.lang.String) 70 * @since 1.6 71 */ 72 @Override putStringResource(final String name, final String body, final String encoding)73 public void putStringResource(final String name, final String body, final String encoding) 74 { 75 resources.put(name, new StringResource(body, encoding)); 76 } 77 78 /** 79 * @see StringResourceRepository#removeStringResource(java.lang.String) 80 */ 81 @Override removeStringResource(final String name)82 public void removeStringResource(final String name) 83 { 84 resources.remove(name); 85 } 86 87 /** 88 * @see org.apache.velocity.runtime.resource.util.StringResourceRepository#getEncoding() 89 */ 90 @Override getEncoding()91 public String getEncoding() 92 { 93 return encoding; 94 } 95 96 /** 97 * @see org.apache.velocity.runtime.resource.util.StringResourceRepository#setEncoding(java.lang.String) 98 */ 99 @Override setEncoding(final String encoding)100 public void setEncoding(final String encoding) 101 { 102 this.encoding = encoding; 103 } 104 } 105