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