View Javadoc

1   /*
2    * Copyright 2010 FatWire Corporation. All Rights Reserved.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *    http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package com.fatwire.gst.foundation.taglib;
18  
19  import java.util.ArrayList;
20  import java.util.Collection;
21  import java.util.Collections;
22  import java.util.Enumeration;
23  import java.util.HashMap;
24  import java.util.HashSet;
25  import java.util.Map;
26  import java.util.Set;
27  
28  import COM.FutureTense.Interfaces.FTVAL;
29  import COM.FutureTense.Interfaces.ICS;
30  import COM.FutureTense.Interfaces.IList;
31  
32  /**
33   * 
34   * This class represents all the ics varariables, objects and lists as a map. It
35   * exposes the ics vars to an expression language, liek JSP EL.
36   * 
37   * @author Dolf.Dijkstra
38   * 
39   */
40  public class ICSAsMap implements Map<String, Object> {
41  
42      private final ICS ics;
43  
44      /**
45       * @param ics
46       */
47      public ICSAsMap(final ICS ics) {
48          super();
49          this.ics = ics;
50      }
51  
52      public void clear() {
53  
54      }
55  
56      public boolean containsKey(final Object key) {
57          return ics.GetVar((String) key) != null;
58  
59      }
60  
61      public boolean containsValue(final Object value) {
62          return false;
63      }
64  
65      @SuppressWarnings("unchecked")
66      public Set<java.util.Map.Entry<String, Object>> entrySet() {
67          final Map<String, Object> tmp = new HashMap<String, Object>();
68          for (final Enumeration<String> enumeration = ics.GetVars(); enumeration.hasMoreElements();) {
69              final String name = enumeration.nextElement();
70              tmp.put(name, get(name));
71          }
72  
73          return tmp.entrySet();
74      }
75  
76      @SuppressWarnings("deprecation")
77      public Object get(final Object key) {
78          if (!(key instanceof String)) {
79              throw new IllegalArgumentException("key must be a String");
80          }
81          // first: check the object pool
82          final Object o = ics.GetObj((String) key);
83          if (o != null) {
84              return o;
85          }
86  
87          // second: lists
88  
89          final IList i = ics.GetList((String) key, true);
90          if (i != null) {
91              // TODO: medium transform into List/Map like
92              return i;
93          }
94          ics.ClearErrno();// GetList sets -4 when list is not found.
95          // FTVAL mungo jungo
96          final FTVAL val = ics.GetCgi((String) key);
97          if (val != null) {
98              switch (val.GetType()) {
99                  case FTVAL.BLOB:
100                     return val.getBlob();
101                 case FTVAL.I4:
102                     return val.GetInt();
103                 case FTVAL.DATE:
104                 case FTVAL.DOUBLE:
105                 case FTVAL.LONG:
106                 case FTVAL.LPSTR:
107                     return val.getString();
108                 case FTVAL.UNKNOWN:
109                     return val.GetObj();
110 
111             }
112         }
113         ics.ClearErrno();// GetList sets -4 when list is not found.
114         return null;
115     }
116 
117     public boolean isEmpty() {
118         return ics.GetVars().hasMoreElements();
119     }
120 
121     @SuppressWarnings("unchecked")
122     public Set<String> keySet() {
123         return new HashSet<String>(Collections.list(ics.GetVars()));
124     }
125 
126     public Object put(final String key, final Object value) {
127         // TODO implement??
128         // ics.SetVar(key,value);
129         // ics.SetVar(key, new FTVAL(value));
130         throw new UnsupportedOperationException();
131     }
132 
133     public void putAll(final Map<? extends String, ? extends Object> t) {
134         throw new UnsupportedOperationException();
135     }
136 
137     public String remove(final Object key) {
138         throw new UnsupportedOperationException();
139     }
140 
141     @SuppressWarnings("unchecked")
142     public int size() {
143         return Collections.list(ics.GetVars()).size();
144     }
145 
146     public Collection<Object> values() {
147         final Collection<Object> tmp = new ArrayList<Object>();
148         for (@SuppressWarnings("unchecked")
149         final Enumeration<String> enumeration = ics.GetVars(); enumeration.hasMoreElements();) {
150             final String name = enumeration.nextElement();
151             tmp.add(get(name));
152         }
153 
154         return tmp;
155     }
156 
157 }