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.

Types.java 6.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. /* ====================================================================
  2. Licensed to the Apache Software Foundation (ASF) under one or more
  3. contributor license agreements. See the NOTICE file distributed with
  4. this work for additional information regarding copyright ownership.
  5. The ASF licenses this file to You under the Apache License, Version 2.0
  6. (the "License"); you may not use this file except in compliance with
  7. the License. You may obtain a copy of the License at
  8. http://www.apache.org/licenses/LICENSE-2.0
  9. Unless required by applicable law or agreed to in writing, software
  10. distributed under the License is distributed on an "AS IS" BASIS,
  11. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. See the License for the specific language governing permissions and
  13. limitations under the License.
  14. ==================================================================== */
  15. package org.apache.poi.hsmf.datatypes;
  16. import java.util.HashMap;
  17. import java.util.Locale;
  18. import java.util.Map;
  19. /**
  20. * The types list and details are available from
  21. * http://msdn.microsoft.com/en-us/library/microsoft.exchange.data.contenttypes.tnef.tnefpropertytype%28v=EXCHG.140%29.aspx
  22. */
  23. public final class Types {
  24. private static Map<Integer, MAPIType> builtInTypes = new HashMap<>();
  25. private static Map<Integer, MAPIType> customTypes = new HashMap<>();
  26. /** Unspecified */
  27. public static final MAPIType UNSPECIFIED = new MAPIType(0x0000,
  28. "Unspecified", -1);
  29. /** Unknown */
  30. public static final MAPIType UNKNOWN = new MAPIType(-1, "Unknown", -1);
  31. /** Null - NULL property value */
  32. public static final MAPIType NULL = new MAPIType(0x0001, "Null", 0);
  33. /** I2 - signed 16-bit value */
  34. public static final MAPIType SHORT = new MAPIType(0x0002, "Short", 2);
  35. /** Long - signed 32-bit value */
  36. public static final MAPIType LONG = new MAPIType(0x0003, "Long", 4);
  37. /** R4 - 4-byte floating point value */
  38. public static final MAPIType FLOAT = new MAPIType(0x0004, "Float", 4);
  39. /** Double - floating point double */
  40. public static final MAPIType DOUBLE = new MAPIType(0x0005, "Double", 8);
  41. /**
  42. * Currency - signed 64-bit integer that represents a base ten decimal with
  43. * four digits to the right of the decimal point
  44. */
  45. public static final MAPIType CURRENCY = new MAPIType(0x0006, "Currency", 8);
  46. /** AppTime - application time value */
  47. public static final MAPIType APP_TIME = new MAPIType(0x0007, "Application Time", 8);
  48. /** Error - 32-bit error value */
  49. public static final MAPIType ERROR = new MAPIType(0x000A, "Error", 4);
  50. /** Boolean - 16-bit Boolean value. '0' is false. Non-zero is true */
  51. public static final MAPIType BOOLEAN = new MAPIType(0x000B, "Boolean", 2);
  52. /** Object/Directory - embedded object in a property */
  53. public static final MAPIType DIRECTORY = new MAPIType(0x000D, "Directory", -1);
  54. /** I8 - 8-byte signed integer */
  55. public static final MAPIType LONG_LONG = new MAPIType(0x0014, "Long Long", 8);
  56. /**
  57. * SysTime - FILETIME 64-bit integer specifying the number of 100ns periods
  58. * since Jan 1, 1601
  59. */
  60. public static final MAPIType TIME = new MAPIType(0x0040, "Time", 8);
  61. /** ClassId - OLE GUID */
  62. public static final MAPIType CLS_ID = new MAPIType(0x0048, "CLS ID GUID", 16);
  63. /** Binary - counted byte array */
  64. public static final MAPIType BINARY = new MAPIType(0x0102, "Binary", -1);
  65. /**
  66. * An 8-bit string, probably in CP1252, but don't quote us... Normally used
  67. * for everything before Outlook 3.0, and some fields in Outlook 3.0.
  68. */
  69. public static final MAPIType ASCII_STRING = new MAPIType(0x001E, "ASCII String", -1);
  70. /** A string, from Outlook 3.0 onwards. Normally unicode */
  71. public static final MAPIType UNICODE_STRING = new MAPIType(0x001F, "Unicode String", -1);
  72. /** MultiValued - Value part contains multiple values */
  73. public static final int MULTIVALUED_FLAG = 0x1000;
  74. public static final class MAPIType {
  75. private final int id;
  76. private final String name;
  77. private final int length;
  78. /**
  79. * Creates a standard, built-in type
  80. */
  81. private MAPIType(int id, String name, int length) {
  82. this.id = id;
  83. this.name = name;
  84. this.length = length;
  85. builtInTypes.put(id, this);
  86. }
  87. /**
  88. * Creates a custom type
  89. */
  90. private MAPIType(int id, int length) {
  91. this.id = id;
  92. this.name = asCustomName(id);
  93. this.length = length;
  94. customTypes.put(id, this);
  95. }
  96. /**
  97. * Returns the length, in bytes, of values of this type, or -1 if it is
  98. * a variable length type.
  99. */
  100. public int getLength() {
  101. return length;
  102. }
  103. /**
  104. * Is this type a fixed-length type, or a variable-length one?
  105. */
  106. public boolean isFixedLength() {
  107. return (length != -1) && (length <= 8);
  108. }
  109. public int getId() {
  110. return id;
  111. }
  112. public String getName() {
  113. return name;
  114. }
  115. @Override
  116. public String toString() {
  117. return id + " / 0x" + asFileEnding() + " - " + name + " @ "
  118. + length;
  119. }
  120. /**
  121. * Return the 4 character hex encoded version, as used in file endings
  122. */
  123. public String asFileEnding() {
  124. return Types.asFileEnding(id);
  125. }
  126. }
  127. public static MAPIType getById(int typeId) {
  128. return builtInTypes.get(typeId);
  129. }
  130. public static String asFileEnding(int type) {
  131. String str = Integer.toHexString(type).toUpperCase(Locale.ROOT);
  132. while (str.length() < 4) {
  133. str = "0" + str;
  134. }
  135. return str;
  136. }
  137. public static String asName(int typeId) {
  138. MAPIType type = builtInTypes.get(typeId);
  139. if (type != null) {
  140. return type.name;
  141. }
  142. return asCustomName(typeId);
  143. }
  144. private static String asCustomName(int typeId) {
  145. return "0x" + Integer.toHexString(typeId);
  146. }
  147. public static MAPIType createCustom(int typeId) {
  148. // Check they're not being silly, and asking for a built-in one...
  149. if (getById(typeId) != null) {
  150. return getById(typeId);
  151. }
  152. // Try to get an existing definition of this
  153. MAPIType type = customTypes.get(typeId);
  154. // If none, do a thread-safe creation
  155. if (type == null) {
  156. synchronized (customTypes) {
  157. type = customTypes.get(typeId);
  158. if (type == null) {
  159. type = new MAPIType(typeId, -1);
  160. }
  161. }
  162. }
  163. return type;
  164. }
  165. }