aboutsummaryrefslogtreecommitdiffstats
path: root/src/main/java/com/healthmarketscience/jackcess/impl/PropertyMapImpl.java
blob: 25bb5315733bb6a56f69b919be53a651c00bd5b7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
/*
Copyright (c) 2011 James Ahlborn

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package com.healthmarketscience.jackcess.impl;

import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map;

import com.healthmarketscience.jackcess.DataType;
import com.healthmarketscience.jackcess.PropertyMap;

/**
 * Map of properties for a database object.
 *
 * @author James Ahlborn
 */
public class PropertyMapImpl implements PropertyMap
{
  private static final Map<String,DataType> DEFAULT_TYPES =
    new HashMap<String,DataType>();

  static {
    DEFAULT_TYPES.put(ACCESS_VERSION_PROP, DataType.TEXT);
    DEFAULT_TYPES.put(TITLE_PROP, DataType.TEXT);
    DEFAULT_TYPES.put(AUTHOR_PROP, DataType.TEXT);
    DEFAULT_TYPES.put(COMPANY_PROP, DataType.TEXT);

    DEFAULT_TYPES.put(DEFAULT_VALUE_PROP, DataType.MEMO);
    DEFAULT_TYPES.put(REQUIRED_PROP, DataType.BOOLEAN);
    DEFAULT_TYPES.put(ALLOW_ZERO_LEN_PROP, DataType.BOOLEAN);
    DEFAULT_TYPES.put(DECIMAL_PLACES_PROP, DataType.BYTE);
    DEFAULT_TYPES.put(FORMAT_PROP, DataType.TEXT);
    DEFAULT_TYPES.put(INPUT_MASK_PROP, DataType.TEXT);
    DEFAULT_TYPES.put(CAPTION_PROP, DataType.MEMO);
    DEFAULT_TYPES.put(VALIDATION_RULE_PROP, DataType.TEXT);
    DEFAULT_TYPES.put(VALIDATION_TEXT_PROP, DataType.TEXT);
    DEFAULT_TYPES.put(GUID_PROP, DataType.BINARY);
    DEFAULT_TYPES.put(DESCRIPTION_PROP, DataType.MEMO);
    DEFAULT_TYPES.put(RESULT_TYPE_PROP, DataType.BYTE);
    DEFAULT_TYPES.put(EXPRESSION_PROP, DataType.MEMO);
  }

  private final String _mapName;
  private final short _mapType;
  private final Map<String,Property> _props = 
    new LinkedHashMap<String,Property>();
  private final PropertyMaps _owner;

  public PropertyMapImpl(String name, short type, PropertyMaps owner) {
    _mapName = name;
    _mapType = type;
    _owner = owner;
  }

  public String getName() {
    return _mapName;
  }

  public short getType() {
    return _mapType;
  }

  public PropertyMaps getOwner() {
    return _owner;
  }

  public int getSize() {
    return _props.size();
  }

  public boolean isEmpty() {
    return _props.isEmpty();
  }

  public Property get(String name) {
    return _props.get(DatabaseImpl.toLookupName(name));
  }

  public Object getValue(String name) {
    return getValue(name, null);
  }

  public Object getValue(String name, Object defaultValue) {
    Property prop = get(name);
    Object value = defaultValue;
    if((prop != null) && (prop.getValue() != null)) {
      value = prop.getValue();
    }
    return value;
  }

  public PropertyImpl put(String name, Object value) {
    return put(name, null, (byte)0, value);
  }

  public PropertyImpl put(String name, DataType type, Object value) {
    return put(name, type, (byte)0, value);
  }

  public void putAll(Iterable<? extends Property> props) {
    if(props == null) {
      return;
    }

    for(Property prop : props) {
      byte flag = 0;
      if(prop instanceof PropertyImpl) {
        flag = ((PropertyImpl)prop).getFlag();
      }
      put(prop.getName(), prop.getType(), flag, prop.getValue());
    }
  }  
  
  /**
   * Puts a property into this map with the given information.
   */
  public PropertyImpl put(String name, DataType type, byte flag, Object value) {
    PropertyImpl prop = (PropertyImpl)createProperty(name, type, flag, value);
    _props.put(DatabaseImpl.toLookupName(name), prop);
    return prop;
  }

  public PropertyImpl remove(String name) {
    return (PropertyImpl)_props.remove(DatabaseImpl.toLookupName(name));
  }

  public Iterator<Property> iterator() {
    return _props.values().iterator();
  }

  public void save() throws IOException {
    getOwner().save();
  }

  @Override
  public String toString() {
    StringBuilder sb = new StringBuilder();
    sb.append(PropertyMaps.DEFAULT_NAME.equals(getName()) ?
              "<DEFAULT>" : getName())
      .append(" {");
    for(Iterator<Property> iter = iterator(); iter.hasNext(); ) {
      sb.append(iter.next());
      if(iter.hasNext()) {
        sb.append(",");
      }
    }
    sb.append("}");
    return sb.toString();
  }      

  public static Property createProperty(String name, DataType type, Object value) {
    return createProperty(name, type, (byte)0, value);
  }
  
  public static Property createProperty(String name, DataType type, byte flag,
                                        Object value) {
    if(type == null) {
      
      // attempt to get the default type for this property
      type = DEFAULT_TYPES.get(name);

      if(type == null) {
        // choose the type based on the value
        if(value instanceof String) {
          type = DataType.TEXT;
        } else if(value instanceof Boolean) {
          type = DataType.BOOLEAN;
        } else if(value instanceof Byte) {
          type = DataType.BYTE;
        } else if(value instanceof Short) {
          type = DataType.INT;
        } else if(value instanceof Integer) {
          type = DataType.LONG;
        } else if(value instanceof Float) {
          type = DataType.FLOAT;
        } else if(value instanceof Double) {
          type = DataType.DOUBLE;
        } else if(value instanceof Date) {
          type = DataType.SHORT_DATE_TIME;
        } else if(value instanceof byte[]) {
          type = DataType.OLE;
        } else {
          throw new IllegalArgumentException(
              "Could not determine type for property " + name +
              " with value " + value);
        }
      }
    }
    
    return new PropertyImpl(name, type, flag, value);
  }
  
  /**
   * Info about a property defined in a PropertyMap.
   */ 
  static final class PropertyImpl implements PropertyMap.Property
  {
    private final String _name;
    private final DataType _type;
    private final byte _flag;
    private Object _value;

    private PropertyImpl(String name, DataType type, byte flag, Object value) {
      _name = name;
      _type = type;
      _flag = flag;
      _value = value;
    }

    public String getName() {
      return _name;
    }

    public DataType getType() {
      return _type;
    }

    public Object getValue() {
      return _value;
    }

    public void setValue(Object newValue) {
      _value = newValue;
    }

    public byte getFlag() {
      return _flag;
    }

    @Override
    public String toString() {
      Object val = getValue();
      if(val instanceof byte[]) {
        val = ByteUtil.toHexString((byte[])val);
      }
      return getName() + "[" + getType() + ":" + _flag + "]=" + val;
    }
  }

}