View Javadoc

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 532546 2007-04-26 00:06:33Z nbubna $
34   */
35  public class StringResourceRepositoryImpl implements StringResourceRepository
36  {
37      /**
38       * mem store
39       */
40      protected Map resources = Collections.synchronizedMap(new HashMap());
41  
42      /**
43       * Current Repository encoding.
44       */
45      private String encoding = StringResourceLoader.REPOSITORY_ENCODING_DEFAULT;
46      
47      /**
48       * @see StringResourceRepository#getStringResource(java.lang.String)
49       */
50      public StringResource getStringResource(final String name)
51      {
52          return (StringResource)resources.get(name);
53      }
54  
55      /**
56       * @see StringResourceRepository#putStringResource(java.lang.String, java.lang.String)
57       */
58      public void putStringResource(final String name, final String body)
59      {
60          resources.put(name, new StringResource(body, getEncoding()));
61      }
62  
63      /**
64       * @see StringResourceRepository#putStringResource(java.lang.String, java.lang.String, java.lang.String)
65       */
66      public void putStringResource(final String name, final String body, final String encoding)
67      {
68          resources.put(name, new StringResource(body, encoding));
69      }
70  
71      /**
72       * @see StringResourceRepository#removeStringResource(java.lang.String)
73       */
74      public void removeStringResource(final String name)
75      {
76          resources.remove(name);
77      }
78  
79      /**
80       * @see org.apache.velocity.runtime.resource.util.StringResourceRepository#getEncoding()
81       */
82      public String getEncoding()
83      {
84  	    return encoding;
85      }
86  
87      /**
88       * @see org.apache.velocity.runtime.resource.util.StringResourceRepository#setEncoding(java.lang.String)
89       */
90      public void setEncoding(final String encoding)
91      {
92  	    this.encoding = encoding;
93      }
94  }