You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

PropertyMapImpl.java 7.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. /*
  2. Copyright (c) 2011 James Ahlborn
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package com.healthmarketscience.jackcess.impl;
  14. import java.io.IOException;
  15. import java.util.Date;
  16. import java.util.HashMap;
  17. import java.util.Iterator;
  18. import java.util.LinkedHashMap;
  19. import java.util.Map;
  20. import com.healthmarketscience.jackcess.DataType;
  21. import com.healthmarketscience.jackcess.PropertyMap;
  22. /**
  23. * Map of properties for a database object.
  24. *
  25. * @author James Ahlborn
  26. */
  27. public class PropertyMapImpl implements PropertyMap
  28. {
  29. private static final Map<String,DataType> DEFAULT_TYPES =
  30. new HashMap<String,DataType>();
  31. static {
  32. DEFAULT_TYPES.put(ACCESS_VERSION_PROP, DataType.TEXT);
  33. DEFAULT_TYPES.put(TITLE_PROP, DataType.TEXT);
  34. DEFAULT_TYPES.put(AUTHOR_PROP, DataType.TEXT);
  35. DEFAULT_TYPES.put(COMPANY_PROP, DataType.TEXT);
  36. DEFAULT_TYPES.put(DEFAULT_VALUE_PROP, DataType.MEMO);
  37. DEFAULT_TYPES.put(REQUIRED_PROP, DataType.BOOLEAN);
  38. DEFAULT_TYPES.put(ALLOW_ZERO_LEN_PROP, DataType.BOOLEAN);
  39. DEFAULT_TYPES.put(DECIMAL_PLACES_PROP, DataType.BYTE);
  40. DEFAULT_TYPES.put(FORMAT_PROP, DataType.TEXT);
  41. DEFAULT_TYPES.put(INPUT_MASK_PROP, DataType.TEXT);
  42. DEFAULT_TYPES.put(CAPTION_PROP, DataType.MEMO);
  43. DEFAULT_TYPES.put(VALIDATION_RULE_PROP, DataType.TEXT);
  44. DEFAULT_TYPES.put(VALIDATION_TEXT_PROP, DataType.TEXT);
  45. DEFAULT_TYPES.put(GUID_PROP, DataType.BINARY);
  46. DEFAULT_TYPES.put(DESCRIPTION_PROP, DataType.MEMO);
  47. DEFAULT_TYPES.put(RESULT_TYPE_PROP, DataType.BYTE);
  48. DEFAULT_TYPES.put(EXPRESSION_PROP, DataType.MEMO);
  49. }
  50. private final String _mapName;
  51. private final short _mapType;
  52. private final Map<String,Property> _props =
  53. new LinkedHashMap<String,Property>();
  54. private final PropertyMaps _owner;
  55. public PropertyMapImpl(String name, short type, PropertyMaps owner) {
  56. _mapName = name;
  57. _mapType = type;
  58. _owner = owner;
  59. }
  60. public String getName() {
  61. return _mapName;
  62. }
  63. public short getType() {
  64. return _mapType;
  65. }
  66. public PropertyMaps getOwner() {
  67. return _owner;
  68. }
  69. public int getSize() {
  70. return _props.size();
  71. }
  72. public boolean isEmpty() {
  73. return _props.isEmpty();
  74. }
  75. public Property get(String name) {
  76. return _props.get(DatabaseImpl.toLookupName(name));
  77. }
  78. public Object getValue(String name) {
  79. return getValue(name, null);
  80. }
  81. public Object getValue(String name, Object defaultValue) {
  82. Property prop = get(name);
  83. Object value = defaultValue;
  84. if((prop != null) && (prop.getValue() != null)) {
  85. value = prop.getValue();
  86. }
  87. return value;
  88. }
  89. public PropertyImpl put(String name, Object value) {
  90. return put(name, null, (byte)0, value);
  91. }
  92. public PropertyImpl put(String name, DataType type, Object value) {
  93. return put(name, type, (byte)0, value);
  94. }
  95. public void putAll(Iterable<? extends Property> props) {
  96. if(props == null) {
  97. return;
  98. }
  99. for(Property prop : props) {
  100. put(prop);
  101. }
  102. }
  103. public PropertyImpl put(Property prop) {
  104. byte flag = 0;
  105. if(prop instanceof PropertyImpl) {
  106. flag = ((PropertyImpl)prop).getFlag();
  107. }
  108. return put(prop.getName(), prop.getType(), flag, prop.getValue());
  109. }
  110. /**
  111. * Puts a property into this map with the given information.
  112. */
  113. public PropertyImpl put(String name, DataType type, byte flag, Object value) {
  114. PropertyImpl prop = (PropertyImpl)createProperty(name, type, flag, value);
  115. _props.put(DatabaseImpl.toLookupName(name), prop);
  116. return prop;
  117. }
  118. public PropertyImpl remove(String name) {
  119. return (PropertyImpl)_props.remove(DatabaseImpl.toLookupName(name));
  120. }
  121. public Iterator<Property> iterator() {
  122. return _props.values().iterator();
  123. }
  124. public void save() throws IOException {
  125. getOwner().save();
  126. }
  127. @Override
  128. public String toString() {
  129. return toString(this);
  130. }
  131. public static String toString(PropertyMap map) {
  132. StringBuilder sb = new StringBuilder();
  133. sb.append(PropertyMaps.DEFAULT_NAME.equals(map.getName()) ?
  134. "<DEFAULT>" : map.getName())
  135. .append(" {");
  136. for(Iterator<Property> iter = map.iterator(); iter.hasNext(); ) {
  137. sb.append(iter.next());
  138. if(iter.hasNext()) {
  139. sb.append(",");
  140. }
  141. }
  142. sb.append("}");
  143. return sb.toString();
  144. }
  145. public static Property createProperty(String name, DataType type, Object value) {
  146. return createProperty(name, type, (byte)0, value);
  147. }
  148. public static Property createProperty(String name, DataType type, byte flag,
  149. Object value) {
  150. if(type == null) {
  151. // attempt to get the default type for this property
  152. type = DEFAULT_TYPES.get(name);
  153. if(type == null) {
  154. // choose the type based on the value
  155. if(value instanceof String) {
  156. type = DataType.TEXT;
  157. } else if(value instanceof Boolean) {
  158. type = DataType.BOOLEAN;
  159. } else if(value instanceof Byte) {
  160. type = DataType.BYTE;
  161. } else if(value instanceof Short) {
  162. type = DataType.INT;
  163. } else if(value instanceof Integer) {
  164. type = DataType.LONG;
  165. } else if(value instanceof Float) {
  166. type = DataType.FLOAT;
  167. } else if(value instanceof Double) {
  168. type = DataType.DOUBLE;
  169. } else if(value instanceof Date) {
  170. type = DataType.SHORT_DATE_TIME;
  171. } else if(value instanceof byte[]) {
  172. type = DataType.OLE;
  173. } else if(value instanceof Long) {
  174. type = DataType.BIG_INT;
  175. } else {
  176. throw new IllegalArgumentException(
  177. "Could not determine type for property " + name +
  178. " with value " + value);
  179. }
  180. }
  181. }
  182. return new PropertyImpl(name, type, flag, value);
  183. }
  184. /**
  185. * Info about a property defined in a PropertyMap.
  186. */
  187. static final class PropertyImpl implements PropertyMap.Property
  188. {
  189. private final String _name;
  190. private final DataType _type;
  191. private final byte _flag;
  192. private Object _value;
  193. private PropertyImpl(String name, DataType type, byte flag, Object value) {
  194. _name = name;
  195. _type = type;
  196. _flag = flag;
  197. _value = value;
  198. }
  199. public String getName() {
  200. return _name;
  201. }
  202. public DataType getType() {
  203. return _type;
  204. }
  205. public Object getValue() {
  206. return _value;
  207. }
  208. public void setValue(Object newValue) {
  209. _value = newValue;
  210. }
  211. public byte getFlag() {
  212. return _flag;
  213. }
  214. @Override
  215. public String toString() {
  216. Object val = getValue();
  217. if(val instanceof byte[]) {
  218. val = ByteUtil.toHexString((byte[])val);
  219. }
  220. return getName() + "[" + getType() + ":" + _flag + "]=" + val;
  221. }
  222. }
  223. }