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 java.util.Collections;
23 import java.util.HashMap;
24 import java.util.Map;
25 import org.apache.velocity.runtime.resource.loader.StringResourceLoader;
26
27 /**
28 * Default implementation of StringResourceRepository.
29 * Uses a HashMap for storage
30 *
31 * @author <a href="mailto:eelco.hillenius@openedge.nl">Eelco Hillenius</a>
32 * @author <a href="mailto:henning@apache.org">Henning P. Schmiedehausen</a>
33 * @version $Id: StringResourceRepositoryImpl.java 685724 2008-08-13 23:12:12Z nbubna $
34 * @since 1.5
35 */
36 public class StringResourceRepositoryImpl implements StringResourceRepository
37 {
38 /**
39 * mem store
40 */
41 protected Map resources = Collections.synchronizedMap(new HashMap());
42
43 /**
44 * Current Repository encoding.
45 */
46 private String encoding = StringResourceLoader.REPOSITORY_ENCODING_DEFAULT;
47
48 /**
49 * @see StringResourceRepository#getStringResource(java.lang.String)
50 */
51 public StringResource getStringResource(final String name)
52 {
53 return (StringResource)resources.get(name);
54 }
55
56 /**
57 * @see StringResourceRepository#putStringResource(java.lang.String, java.lang.String)
58 */
59 public void putStringResource(final String name, final String body)
60 {
61 resources.put(name, new StringResource(body, getEncoding()));
62 }
63
64 /**
65 * @see StringResourceRepository#putStringResource(java.lang.String, java.lang.String, java.lang.String)
66 * @since 1.6
67 */
68 public void putStringResource(final String name, final String body, final String encoding)
69 {
70 resources.put(name, new StringResource(body, encoding));
71 }
72
73 /**
74 * @see StringResourceRepository#removeStringResource(java.lang.String)
75 */
76 public void removeStringResource(final String name)
77 {
78 resources.remove(name);
79 }
80
81 /**
82 * @see org.apache.velocity.runtime.resource.util.StringResourceRepository#getEncoding()
83 */
84 public String getEncoding()
85 {
86 return encoding;
87 }
88
89 /**
90 * @see org.apache.velocity.runtime.resource.util.StringResourceRepository#setEncoding(java.lang.String)
91 */
92 public void setEncoding(final String encoding)
93 {
94 this.encoding = encoding;
95 }
96 }