View Javadoc
1   package org.apache.maven.doxia.docrenderer;
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.HashMap;
23  import java.util.Map;
24  
25  import org.codehaus.plexus.util.ReaderFactory;
26  
27  /**
28   * Context when processing Velocity files using a {@link java.util.HashMap} for data storage.
29   *
30   * @author <a href="mailto:vincent.siveton@gmail.com">Vincent Siveton</a>
31   * @since 1.1.2
32   */
33  public class DocumentRendererContext
34  {
35      private String inputEncoding = ReaderFactory.UTF_8;
36  
37      /**
38       *  Storage for key/value pairs.
39       */
40      private final Map<String, Object> context;
41  
42      /**
43       *  Default constructor.
44       */
45      public DocumentRendererContext()
46      {
47          context = new HashMap<String, Object>();
48      }
49  
50      /**
51       * @return The input encoding when processing files.
52       */
53      public String getInputEncoding()
54      {
55          return inputEncoding;
56      }
57  
58      /**
59       * @param inputEncoding new input encoding value when processing files.
60       */
61      public void setInputEncoding( String inputEncoding )
62      {
63          this.inputEncoding = inputEncoding;
64      }
65  
66      /**
67       * Adds a name/value pair to the context.
68       *
69       * @param key The name to key the provided value with.
70       * @param value The corresponding value.
71       * @return Object that was replaced in the the Context if applicable or null if not.
72       */
73      public Object put( String key, Object value )
74      {
75          if ( key == null )
76          {
77              return null;
78          }
79  
80          return context.put( key, value );
81      }
82  
83      /**
84       *  Gets the value corresponding to the provided key from the context.
85       *
86       *  @param key The name of the desired value.
87       *  @return The value corresponding to the provided key or null if the key param is null.
88       */
89      public Object get( String key )
90      {
91          if ( key == null )
92          {
93              return null;
94          }
95  
96          return context.get( key );
97      }
98  
99      /**
100      *  Indicates whether the specified key is in the context.
101      *
102      * @param key The key to look for.
103      * @return true if the key is in the context, false if not.
104      */
105     public boolean containsKey( Object key )
106     {
107         if ( !( key instanceof String ) ) // this includes null check
108         {
109             return false;
110         }
111 
112         return context.containsKey( key.toString() );
113     }
114 
115     /**
116      *  Get all the keys for the values in the context
117      *
118      *  @return Object[] of keys in the Context.
119      */
120     public Object[] getKeys()
121     {
122         return context.keySet().toArray();
123     }
124 
125     /**
126      * Removes the value associated with the specified key from the context.
127      *
128      * @param key The name of the value to remove.
129      * @return The value that the key was mapped to, or <code>null</code> if unmapped.
130      */
131     public Object remove( Object key )
132     {
133         if ( !( key instanceof String ) ) // this includes null check
134         {
135             return null;
136         }
137 
138         return context.remove( key.toString() );
139     }
140 }