1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 package com.sun.star.report.pentaho.output.text;
24 
25 import java.util.HashMap;
26 import java.util.Iterator;
27 import java.util.Map;
28 
29 import org.jfree.report.util.AttributeNameGenerator;
30 
31 
32 /**
33  * A collection that holds all used variables. A variable is primarily keyed by
34  * its original name. If a variable contains more than one type, it is also
35  * keyed by the type.
36  *
37  * @author Thomas Morgner
38  * @since 26.03.2007
39  */
40 public class VariablesDeclarations
41 {
42 
43     private final AttributeNameGenerator nameGenerator;
44     private final Map variables;
45 
VariablesDeclarations()46     public VariablesDeclarations()
47     {
48         variables = new HashMap();
49         nameGenerator = new AttributeNameGenerator();
50     }
51 
produceVariable(final String name, final String type)52     public String produceVariable(final String name,
53             final String type)
54     {
55         HashMap holder = (HashMap) variables.get(name);
56         if (holder == null)
57         {
58             holder = new HashMap();
59             variables.put(name, holder);
60         }
61 
62         final String mapping = (String) holder.get(type);
63         if (mapping != null)
64         {
65             return mapping;
66         }
67         final String result = nameGenerator.generateName(name);
68         if (holder.isEmpty())
69         {
70             // create the default mapping as well..
71             holder.put(null, name);
72             holder.put("time", name);
73             holder.put("date", name);
74             holder.put("datetime", name);
75             holder.put("float", name);
76             holder.put("string", name);
77             holder.put("boolean", name);
78         }
79         holder.put(type, name);
80         return result;
81     }
82 
getDefinedMappings()83     public Map getDefinedMappings()
84     {
85         final HashMap mappings = new HashMap();
86         final Iterator vars = variables.values().iterator();
87         while (vars.hasNext())
88         {
89             final HashMap types = (HashMap) vars.next();
90             final Iterator varsByType = types.entrySet().iterator();
91             while (varsByType.hasNext())
92             {
93                 final Map.Entry entry = (Map.Entry) varsByType.next();
94                 final String type = (String) entry.getKey();
95                 if (type != null)
96                 {
97                     final String varName = (String) entry.getValue();
98                     mappings.put(varName, type);
99                 }
100             }
101         }
102         return mappings;
103     }
104 }
105