PageRenderTime 60ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/components/forks/poi/src/loci/poi/hpsf/CustomProperties.java

http://github.com/openmicroscopy/bioformats
Java | 391 lines | 156 code | 42 blank | 193 comment | 16 complexity | f1e598413903d1dc0b1adc5794c23463 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1, Apache-2.0, BSD-2-Clause, MPL-2.0-no-copyleft-exception
  1. /*
  2. * #%L
  3. * Fork of Apache Jakarta POI.
  4. * %%
  5. * Copyright (C) 2008 - 2013 Open Microscopy Environment:
  6. * - Board of Regents of the University of Wisconsin-Madison
  7. * - Glencoe Software, Inc.
  8. * - University of Dundee
  9. * %%
  10. * Licensed under the Apache License, Version 2.0 (the "License");
  11. * you may not use this file except in compliance with the License.
  12. * You may obtain a copy of the License at
  13. *
  14. * http://www.apache.org/licenses/LICENSE-2.0
  15. *
  16. * Unless required by applicable law or agreed to in writing, software
  17. * distributed under the License is distributed on an "AS IS" BASIS,
  18. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  19. * See the License for the specific language governing permissions and
  20. * limitations under the License.
  21. * #L%
  22. */
  23. /* ====================================================================
  24. Licensed to the Apache Software Foundation (ASF) under one or more
  25. contributor license agreements. See the NOTICE file distributed with
  26. this work for additional information regarding copyright ownership.
  27. The ASF licenses this file to You under the Apache License, Version 2.0
  28. (the "License"); you may not use this file except in compliance with
  29. the License. You may obtain a copy of the License at
  30. http://www.apache.org/licenses/LICENSE-2.0
  31. Unless required by applicable law or agreed to in writing, software
  32. distributed under the License is distributed on an "AS IS" BASIS,
  33. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  34. See the License for the specific language governing permissions and
  35. limitations under the License.
  36. ==================================================================== */
  37. package loci.poi.hpsf;
  38. import java.util.Date;
  39. import java.util.HashMap;
  40. import java.util.Iterator;
  41. import java.util.Map;
  42. import loci.poi.hpsf.wellknown.PropertyIDMap;
  43. /**
  44. * <p>Maintains the instances of {@link CustomProperty} that belong to a
  45. * {@link DocumentSummaryInformation}. The class maintains the names of the
  46. * custom properties in a dictionary. It implements the {@link Map} interface
  47. * and by this provides a simplified view on custom properties: A property's
  48. * name is the key that maps to a typed value. This implementation hides
  49. * property IDs from the developer and regards the property names as keys to
  50. * typed values.</p>
  51. *
  52. * <p>While this class provides a simple API to custom properties, it ignores
  53. * the fact that not names, but IDs are the real keys to properties. Under the
  54. * hood this class maintains a 1:1 relationship between IDs and names. Therefore
  55. * you should not use this class to process property sets with several IDs
  56. * mapping to the same name or with properties without a name: the result will
  57. * contain only a subset of the original properties. If you really need to deal
  58. * such property sets, use HPSF's low-level access methods.</p>
  59. *
  60. * <p>An application can call the {@link #isPure} method to check whether a
  61. * property set parsed by {@link CustomProperties} is still pure (i.e.
  62. * unmodified) or whether one or more properties have been dropped.</p>
  63. *
  64. * <p>This class is not thread-safe; concurrent access to instances of this
  65. * class must be syncronized.</p>
  66. *
  67. * @author Rainer Klute <a
  68. * href="mailto:klute@rainer-klute.de">&lt;klute@rainer-klute.de&gt;</a>
  69. * @since 2006-02-09
  70. * @version $Id$
  71. */
  72. public class CustomProperties extends HashMap
  73. {
  74. /**
  75. * <p>Maps property IDs to property names.</p>
  76. */
  77. private Map dictionaryIDToName = new HashMap();
  78. /**
  79. * <p>Maps property names to property IDs.</p>
  80. */
  81. private Map dictionaryNameToID = new HashMap();
  82. /**
  83. * <p>Tells whether this object is pure or not.</p>
  84. */
  85. private boolean isPure = true;
  86. /**
  87. * <p>Puts a {@link CustomProperty} into this map. It is assumed that the
  88. * {@link CustomProperty} already has a valid ID. Otherwise use
  89. * {@link #put(CustomProperty)}.</p>
  90. */
  91. public Object put(final Object name, final Object customProperty) throws ClassCastException
  92. {
  93. final CustomProperty cp = (CustomProperty) customProperty;
  94. if (name == null)
  95. {
  96. /* Ignoring a property without a name. */
  97. isPure = false;
  98. return null;
  99. }
  100. if (!(name instanceof String))
  101. throw new ClassCastException("The name of a custom property must " +
  102. "be a java.lang.String, but it is a " +
  103. name.getClass().getName());
  104. if (!(name.equals(cp.getName())))
  105. throw new IllegalArgumentException("Parameter \"name\" (" + name +
  106. ") and custom property's name (" + cp.getName() +
  107. ") do not match.");
  108. /* Register name and ID in the dictionary. Mapping in both directions is possible. If there is already a */
  109. final Long idKey = new Long(cp.getID());
  110. final Object oldID = dictionaryNameToID.get(name);
  111. dictionaryIDToName.remove(oldID);
  112. dictionaryNameToID.put(name, idKey);
  113. dictionaryIDToName.put(idKey, name);
  114. /* Put the custom property into this map. */
  115. final Object oldCp = super.remove(oldID);
  116. super.put(idKey, cp);
  117. return oldCp;
  118. }
  119. /**
  120. * <p>Puts a {@link CustomProperty} that has not yet a valid ID into this
  121. * map. The method will allocate a suitable ID for the custom property:</p>
  122. *
  123. * <ul>
  124. *
  125. * <li><p>If there is already a property with the same name, take the ID
  126. * of that property.</p></li>
  127. *
  128. * <li><p>Otherwise find the highest ID and use its value plus one.</p></li>
  129. *
  130. * </ul>
  131. *
  132. * @param customProperty
  133. * @return If the was already a property with the same name, the
  134. * @throws ClassCastException
  135. */
  136. private Object put(final CustomProperty customProperty) throws ClassCastException
  137. {
  138. final String name = customProperty.getName();
  139. /* Check whether a property with this name is in the map already. */
  140. final Long oldId = (Long) dictionaryNameToID.get(name);
  141. if (oldId != null)
  142. customProperty.setID(oldId.longValue());
  143. else
  144. {
  145. long max = 1;
  146. for (final Iterator i = dictionaryIDToName.keySet().iterator(); i.hasNext();)
  147. {
  148. final long id = ((Long) i.next()).longValue();
  149. if (id > max)
  150. max = id;
  151. }
  152. customProperty.setID(max + 1);
  153. }
  154. return this.put(name, customProperty);
  155. }
  156. /**
  157. * <p>Removes a custom property.</p>
  158. * @param name The name of the custom property to remove
  159. * @return The removed property or <code>null</code> if the specified property was not found.
  160. *
  161. * @see java.util.HashSet#remove(java.lang.Object)
  162. */
  163. public Object remove(final String name)
  164. {
  165. final Long id = (Long) dictionaryNameToID.get(name);
  166. if (id == null)
  167. return null;
  168. dictionaryIDToName.remove(id);
  169. dictionaryNameToID.remove(name);
  170. return super.remove(id);
  171. }
  172. /**
  173. * <p>Adds a named string property.</p>
  174. *
  175. * @param name The property's name.
  176. * @param value The property's value.
  177. * @return the property that was stored under the specified name before, or
  178. * <code>null</code> if there was no such property before.
  179. */
  180. public Object put(final String name, final String value)
  181. {
  182. final MutableProperty p = new MutableProperty();
  183. p.setID(-1);
  184. p.setType(Variant.VT_LPWSTR);
  185. p.setValue(value);
  186. final CustomProperty cp = new CustomProperty(p, name);
  187. return put(cp);
  188. }
  189. /**
  190. * <p>Adds a named long property.</p>
  191. *
  192. * @param name The property's name.
  193. * @param value The property's value.
  194. * @return the property that was stored under the specified name before, or
  195. * <code>null</code> if there was no such property before.
  196. */
  197. public Object put(final String name, final Long value)
  198. {
  199. final MutableProperty p = new MutableProperty();
  200. p.setID(-1);
  201. p.setType(Variant.VT_I8);
  202. p.setValue(value);
  203. final CustomProperty cp = new CustomProperty(p, name);
  204. return put(cp);
  205. }
  206. /**
  207. * <p>Adds a named double property.</p>
  208. *
  209. * @param name The property's name.
  210. * @param value The property's value.
  211. * @return the property that was stored under the specified name before, or
  212. * <code>null</code> if there was no such property before.
  213. */
  214. public Object put(final String name, final Double value)
  215. {
  216. final MutableProperty p = new MutableProperty();
  217. p.setID(-1);
  218. p.setType(Variant.VT_R8);
  219. p.setValue(value);
  220. final CustomProperty cp = new CustomProperty(p, name);
  221. return put(cp);
  222. }
  223. /**
  224. * <p>Adds a named integer property.</p>
  225. *
  226. * @param name The property's name.
  227. * @param value The property's value.
  228. * @return the property that was stored under the specified name before, or
  229. * <code>null</code> if there was no such property before.
  230. */
  231. public Object put(final String name, final Integer value)
  232. {
  233. final MutableProperty p = new MutableProperty();
  234. p.setID(-1);
  235. p.setType(Variant.VT_I4);
  236. p.setValue(value);
  237. final CustomProperty cp = new CustomProperty(p, name);
  238. return put(cp);
  239. }
  240. /**
  241. * <p>Adds a named boolean property.</p>
  242. *
  243. * @param name The property's name.
  244. * @param value The property's value.
  245. * @return the property that was stored under the specified name before, or
  246. * <code>null</code> if there was no such property before.
  247. */
  248. public Object put(final String name, final Boolean value)
  249. {
  250. final MutableProperty p = new MutableProperty();
  251. p.setID(-1);
  252. p.setType(Variant.VT_BOOL);
  253. p.setValue(value);
  254. final CustomProperty cp = new CustomProperty(p, name);
  255. return put(cp);
  256. }
  257. /**
  258. * <p>Gets a named value from the custom properties.</p>
  259. *
  260. * @param name the name of the value to get
  261. * @return the value or <code>null</code> if a value with the specified
  262. * name is not found in the custom properties.
  263. */
  264. public Object get(final String name)
  265. {
  266. final Long id = (Long) dictionaryNameToID.get(name);
  267. final CustomProperty cp = (CustomProperty) super.get(id);
  268. return cp != null ? cp.getValue() : null;
  269. }
  270. /**
  271. * <p>Adds a named date property.</p>
  272. *
  273. * @param name The property's name.
  274. * @param value The property's value.
  275. * @return the property that was stored under the specified name before, or
  276. * <code>null</code> if there was no such property before.
  277. */
  278. public Object put(final String name, final Date value)
  279. {
  280. final MutableProperty p = new MutableProperty();
  281. p.setID(-1);
  282. p.setType(Variant.VT_FILETIME);
  283. p.setValue(value);
  284. final CustomProperty cp = new CustomProperty(p, name);
  285. return put(cp);
  286. }
  287. /**
  288. * <p>Sets the codepage.</p>
  289. *
  290. * @param codepage the codepage
  291. */
  292. public void setCodepage(final int codepage)
  293. {
  294. final MutableProperty p = new MutableProperty();
  295. p.setID(PropertyIDMap.PID_CODEPAGE);
  296. p.setType(Variant.VT_I2);
  297. p.setValue(new Integer(codepage));
  298. put(new CustomProperty(p));
  299. }
  300. /**
  301. * <p>Gets the dictionary which contains IDs and names of the named custom
  302. * properties.
  303. *
  304. * @return the dictionary.
  305. */
  306. Map getDictionary()
  307. {
  308. return dictionaryIDToName;
  309. }
  310. /**
  311. * <p>Gets the codepage.</p>
  312. *
  313. * @return the codepage or -1 if the codepage is undefined.
  314. */
  315. public int getCodepage()
  316. {
  317. int codepage = -1;
  318. for (final Iterator i = this.values().iterator(); codepage == -1 && i.hasNext();)
  319. {
  320. final CustomProperty cp = (CustomProperty) i.next();
  321. if (cp.getID() == PropertyIDMap.PID_CODEPAGE)
  322. codepage = ((Integer) cp.getValue()).intValue();
  323. }
  324. return codepage;
  325. }
  326. /**
  327. * <p>Tells whether this {@link CustomProperties} instance is pure or one or
  328. * more properties of the underlying low-level property set has been
  329. * dropped.</p>
  330. *
  331. * @return <code>true</code> if the {@link CustomProperties} is pure, else
  332. * <code>false</code>.
  333. */
  334. public boolean isPure()
  335. {
  336. return isPure;
  337. }
  338. /**
  339. * <p>Sets the purity of the custom property set.</p>
  340. *
  341. * @param isPure the purity
  342. */
  343. public void setPure(final boolean isPure)
  344. {
  345. this.isPure = isPure;
  346. }
  347. }