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.

PropertyMap.java 4.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. /*
  2. Copyright (c) 2011 James Ahlborn
  3. This library is free software; you can redistribute it and/or
  4. modify it under the terms of the GNU Lesser General Public
  5. License as published by the Free Software Foundation; either
  6. version 2.1 of the License, or (at your option) any later version.
  7. This library is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  10. Lesser General Public License for more details.
  11. You should have received a copy of the GNU Lesser General Public
  12. License along with this library; if not, write to the Free Software
  13. Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
  14. USA
  15. */
  16. package com.healthmarketscience.jackcess;
  17. import java.util.Iterator;
  18. import java.util.LinkedHashMap;
  19. import java.util.Map;
  20. /**
  21. * Map of properties for a given database object.
  22. *
  23. * @author James Ahlborn
  24. */
  25. public class PropertyMap implements Iterable<PropertyMap.Property>
  26. {
  27. public static final String ACCESS_VERSION_PROP = "AccessVersion";
  28. public static final String TITLE_PROP = "Title";
  29. public static final String AUTHOR_PROP = "Author";
  30. public static final String COMPANY_PROP = "Company";
  31. public static final String DEFAULT_VALUE_PROP = "DefaultValue";
  32. public static final String REQUIRED_PROP = "Required";
  33. public static final String ALLOW_ZERO_LEN_PROP = "AllowZeroLength";
  34. public static final String DECIMAL_PLACES_PROP = "DecimalPlaces";
  35. public static final String FORMAT_PROP = "Format";
  36. public static final String INPUT_MASK_PROP = "InputMask";
  37. public static final String CAPTION_PROP = "Caption";
  38. public static final String VALIDATION_RULE_PROP = "ValidationRule";
  39. public static final String VALIDATION_TEXT_PROP = "ValidationText";
  40. public static final String GUID_PROP = "GUID";
  41. public static final String DESCRIPTION_PROP = "Description";
  42. private final String _mapName;
  43. private final short _mapType;
  44. private final Map<String,Property> _props =
  45. new LinkedHashMap<String,Property>();
  46. PropertyMap(String name, short type) {
  47. _mapName = name;
  48. _mapType = type;
  49. }
  50. public String getName() {
  51. return _mapName;
  52. }
  53. public short getType() {
  54. return _mapType;
  55. }
  56. public int getSize() {
  57. return _props.size();
  58. }
  59. public boolean isEmpty() {
  60. return _props.isEmpty();
  61. }
  62. /**
  63. * @return the property with the given name, if any
  64. */
  65. public Property get(String name) {
  66. return _props.get(Database.toLookupName(name));
  67. }
  68. /**
  69. * @return the value of the property with the given name, if any
  70. */
  71. public Object getValue(String name) {
  72. return getValue(name, null);
  73. }
  74. /**
  75. * @return the value of the property with the given name, if any, otherwise
  76. * the given defaultValue
  77. */
  78. public Object getValue(String name, Object defaultValue) {
  79. Property prop = get(name);
  80. Object value = defaultValue;
  81. if((prop != null) && (prop.getValue() != null)) {
  82. value = prop.getValue();
  83. }
  84. return value;
  85. }
  86. /**
  87. * Puts a property into this map with the given information.
  88. */
  89. public void put(String name, DataType type, byte flag, Object value) {
  90. _props.put(Database.toLookupName(name),
  91. new Property(name, type, flag, value));
  92. }
  93. public Iterator<Property> iterator() {
  94. return _props.values().iterator();
  95. }
  96. PropertyMap merge(PropertyMap opm) {
  97. if(opm == null) {
  98. return this;
  99. }
  100. // merge into least map type
  101. PropertyMap dest = opm;
  102. PropertyMap src = this;
  103. if(dest._mapType < src._mapType) {
  104. dest = this;
  105. src = opm;
  106. }
  107. dest._props.putAll(src._props);
  108. return dest;
  109. }
  110. @Override
  111. public String toString() {
  112. StringBuilder sb = new StringBuilder();
  113. sb.append(PropertyMaps.DEFAULT_NAME.equals(getName()) ?
  114. "<DEFAULT>" : getName())
  115. .append(" {");
  116. for(Iterator<Property> iter = iterator(); iter.hasNext(); ) {
  117. sb.append(iter.next());
  118. if(iter.hasNext()) {
  119. sb.append(",");
  120. }
  121. }
  122. sb.append("}");
  123. return sb.toString();
  124. }
  125. /**
  126. * Info about a property defined in a PropertyMap.
  127. */
  128. public static final class Property
  129. {
  130. private final String _name;
  131. private final DataType _type;
  132. private final byte _flag;
  133. private final Object _value;
  134. private Property(String name, DataType type, byte flag, Object value) {
  135. _name = name;
  136. _type = type;
  137. _flag = flag;
  138. _value = value;
  139. }
  140. public String getName() {
  141. return _name;
  142. }
  143. public DataType getType() {
  144. return _type;
  145. }
  146. public Object getValue() {
  147. return _value;
  148. }
  149. @Override
  150. public String toString() {
  151. Object val = getValue();
  152. if(val instanceof byte[]) {
  153. val = ByteUtil.toHexString((byte[])val);
  154. }
  155. return getName() + "[" + getType() + ":" + _flag + "]=" + val;
  156. }
  157. }
  158. }