Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

ClassID.java 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  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.hpsf;
  16. import java.nio.ByteBuffer;
  17. import java.util.Arrays;
  18. import java.util.Locale;
  19. import java.util.Map;
  20. import java.util.UUID;
  21. import java.util.function.Supplier;
  22. import org.apache.poi.common.Duplicatable;
  23. import org.apache.poi.common.usermodel.GenericRecord;
  24. import org.apache.poi.util.GenericRecordUtil;
  25. import org.apache.poi.util.LittleEndianInput;
  26. import org.apache.poi.util.LittleEndianOutput;
  27. /**
  28. * Represents a class ID (16 bytes). Unlike other little-endian
  29. * type the {@link ClassID} is not just 16 bytes stored in the wrong
  30. * order. Instead, it is a double word (4 bytes) followed by two
  31. * words (2 bytes each) followed by 8 bytes.<p>
  32. *
  33. * The ClassID (or CLSID) is a UUID - see RFC 4122
  34. */
  35. public class ClassID implements Duplicatable, GenericRecord {
  36. /** @deprecated use enum {@link ClassIDPredefined} */ @Deprecated
  37. public static final ClassID OLE10_PACKAGE = ClassIDPredefined.OLE_V1_PACKAGE.getClassID();
  38. /** @deprecated use enum {@link ClassIDPredefined} */ @Deprecated
  39. public static final ClassID PPT_SHOW = ClassIDPredefined.POWERPOINT_V8.getClassID();
  40. /** @deprecated use enum {@link ClassIDPredefined} */ @Deprecated
  41. public static final ClassID XLS_WORKBOOK = ClassIDPredefined.EXCEL_V7_WORKBOOK.getClassID();
  42. /** @deprecated use enum {@link ClassIDPredefined} */ @Deprecated
  43. public static final ClassID TXT_ONLY = ClassIDPredefined.TXT_ONLY.getClassID();
  44. /** @deprecated use enum {@link ClassIDPredefined} */ @Deprecated
  45. public static final ClassID EXCEL_V3 = ClassIDPredefined.EXCEL_V3.getClassID();
  46. /** @deprecated use enum {@link ClassIDPredefined} */ @Deprecated
  47. public static final ClassID EXCEL_V3_CHART = ClassIDPredefined.EXCEL_V3_CHART.getClassID();
  48. /** @deprecated use enum {@link ClassIDPredefined} */ @Deprecated
  49. public static final ClassID EXCEL_V3_MACRO = ClassIDPredefined.EXCEL_V3_MACRO.getClassID();
  50. /** @deprecated use enum {@link ClassIDPredefined} */ @Deprecated
  51. public static final ClassID EXCEL95 = ClassIDPredefined.EXCEL_V7.getClassID();
  52. /** @deprecated use enum {@link ClassIDPredefined} */ @Deprecated
  53. public static final ClassID EXCEL95_CHART = ClassIDPredefined.EXCEL_V7_CHART.getClassID();
  54. /** @deprecated use enum {@link ClassIDPredefined} */ @Deprecated
  55. public static final ClassID EXCEL97 = ClassIDPredefined.EXCEL_V8.getClassID();
  56. /** @deprecated use enum {@link ClassIDPredefined} */ @Deprecated
  57. public static final ClassID EXCEL97_CHART = ClassIDPredefined.EXCEL_V8_CHART.getClassID();
  58. /** @deprecated use enum {@link ClassIDPredefined} */ @Deprecated
  59. public static final ClassID EXCEL2003 = ClassIDPredefined.EXCEL_V11.getClassID();
  60. /** @deprecated use enum {@link ClassIDPredefined} */ @Deprecated
  61. public static final ClassID EXCEL2007 = ClassIDPredefined.EXCEL_V12.getClassID();
  62. /** @deprecated use enum {@link ClassIDPredefined} */ @Deprecated
  63. public static final ClassID EXCEL2007_MACRO= ClassIDPredefined.EXCEL_V12_MACRO.getClassID();
  64. /** @deprecated use enum {@link ClassIDPredefined} */ @Deprecated
  65. public static final ClassID EXCEL2007_XLSB = ClassIDPredefined.EXCEL_V12_XLSB.getClassID();
  66. /** @deprecated use enum {@link ClassIDPredefined} */ @Deprecated
  67. public static final ClassID EXCEL2010 = ClassIDPredefined.EXCEL_V14.getClassID();
  68. /** @deprecated use enum {@link ClassIDPredefined} */ @Deprecated
  69. public static final ClassID EXCEL2010_CHART= ClassIDPredefined.EXCEL_V14_CHART.getClassID();
  70. /** @deprecated use enum {@link ClassIDPredefined} */ @Deprecated
  71. public static final ClassID EXCEL2010_ODS = ClassIDPredefined.EXCEL_V14_ODS.getClassID();
  72. /** @deprecated use enum {@link ClassIDPredefined} */ @Deprecated
  73. public static final ClassID WORD95 = ClassIDPredefined.WORD_V7.getClassID();
  74. /** @deprecated use enum {@link ClassIDPredefined} */ @Deprecated
  75. public static final ClassID WORD97 = ClassIDPredefined.WORD_V8.getClassID();
  76. /** @deprecated use enum {@link ClassIDPredefined} */ @Deprecated
  77. public static final ClassID WORD2007 = ClassIDPredefined.WORD_V12.getClassID();
  78. /** @deprecated use enum {@link ClassIDPredefined} */ @Deprecated
  79. public static final ClassID WORD2007_MACRO = ClassIDPredefined.WORD_V12_MACRO.getClassID();
  80. /** @deprecated use enum {@link ClassIDPredefined} */ @Deprecated
  81. public static final ClassID POWERPOINT97 = ClassIDPredefined.POWERPOINT_V8.getClassID();
  82. /** @deprecated use enum {@link ClassIDPredefined} */ @Deprecated
  83. public static final ClassID POWERPOINT95 = ClassIDPredefined.POWERPOINT_V7.getClassID();
  84. /** @deprecated use enum {@link ClassIDPredefined} */ @Deprecated
  85. public static final ClassID POWERPOINT2007 = ClassIDPredefined.POWERPOINT_V12.getClassID();
  86. /** @deprecated use enum {@link ClassIDPredefined} */ @Deprecated
  87. public static final ClassID POWERPOINT2007_MACRO = ClassIDPredefined.POWERPOINT_V12_MACRO.getClassID();
  88. /** @deprecated use enum {@link ClassIDPredefined} */ @Deprecated
  89. public static final ClassID EQUATION30 = ClassIDPredefined.EQUATION_V3.getClassID();
  90. /** The number of bytes occupied by this object in the byte stream. */
  91. public static final int LENGTH = 16;
  92. /**
  93. * The bytes making out the class ID in correct order, i.e. big-endian.
  94. */
  95. private final byte[] bytes = new byte[LENGTH];
  96. /**
  97. * Creates a {@link ClassID} and reads its value from a byte array.
  98. *
  99. * @param src The byte array to read from.
  100. * @param offset The offset of the first byte to read.
  101. */
  102. public ClassID(final byte[] src, final int offset) {
  103. read(src, offset);
  104. }
  105. /**
  106. * Creates a {@link ClassID} and initializes its value with 0x00 bytes.
  107. */
  108. public ClassID() {
  109. Arrays.fill(bytes, (byte)0);
  110. }
  111. /**
  112. * Clones the given ClassID
  113. */
  114. public ClassID(ClassID other) {
  115. System.arraycopy(other.bytes, 0, bytes, 0, bytes.length);
  116. }
  117. /**
  118. * Creates a {@link ClassID} from a human-readable representation of the Class ID in standard
  119. * format {@code "{xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx}"}.
  120. *
  121. * @param externalForm representation of the Class ID represented by this object.
  122. */
  123. public ClassID(String externalForm) {
  124. String clsStr = externalForm.replaceAll("[{}-]", "");
  125. for (int i=0; i<clsStr.length(); i+=2) {
  126. bytes[i/2] = (byte)Integer.parseInt(clsStr.substring(i, i+2), 16);
  127. }
  128. }
  129. /**
  130. * Reads the ClassID from the input
  131. * @param lei the input (stream)
  132. */
  133. public ClassID(LittleEndianInput lei) {
  134. byte[] buf = bytes.clone();
  135. lei.readFully(buf);
  136. read(buf, 0);
  137. }
  138. /**
  139. * @return The number of bytes occupied by this object in the byte stream.
  140. */
  141. public int length() {
  142. return LENGTH;
  143. }
  144. /**
  145. * Gets the bytes making out the class ID. They are returned in correct order, i.e. big-endian.
  146. *
  147. * @return the bytes making out the class ID.
  148. */
  149. public byte[] getBytes() {
  150. return bytes;
  151. }
  152. /**
  153. * Sets the bytes making out the class ID.
  154. *
  155. * @param bytes The bytes making out the class ID in big-endian format. They
  156. * are copied without their order being changed.
  157. */
  158. public void setBytes(final byte[] bytes) {
  159. System.arraycopy(bytes, 0, this.bytes, 0, LENGTH);
  160. }
  161. /**
  162. * Reads the class ID's value from a byte array by turning little-endian into big-endian.
  163. *
  164. * @param src The byte array to read from
  165. * @param offset The offset within the {@code src} byte array
  166. * @return A byte array containing the class ID.
  167. */
  168. @SuppressWarnings("PointlessArithmeticExpression")
  169. public byte[] read(final byte[] src, final int offset) {
  170. /* Read double word. */
  171. bytes[0] = src[3 + offset];
  172. bytes[1] = src[2 + offset];
  173. bytes[2] = src[1 + offset];
  174. bytes[3] = src[0 + offset];
  175. /* Read first word. */
  176. bytes[4] = src[5 + offset];
  177. bytes[5] = src[4 + offset];
  178. /* Read second word. */
  179. bytes[6] = src[7 + offset];
  180. bytes[7] = src[6 + offset];
  181. /* Read 8 bytes. */
  182. System.arraycopy(src, 8 + offset, bytes, 8, 8);
  183. return bytes;
  184. }
  185. /**
  186. * Writes the class ID to a byte array in the little-endian format.
  187. *
  188. * @param dst The byte array to write to.
  189. *
  190. * @param offset The offset within the {@code dst} byte array.
  191. *
  192. * @exception ArrayStoreException if there is not enough room for the class
  193. * ID 16 bytes in the byte array after the {@code offset} position.
  194. */
  195. @SuppressWarnings("PointlessArithmeticExpression")
  196. public void write(final byte[] dst, final int offset)
  197. throws ArrayStoreException {
  198. /* Check array size: */
  199. if (dst.length < LENGTH) {
  200. throw new ArrayStoreException
  201. ("Destination byte[] must have room for at least 16 bytes, " +
  202. "but has a length of only " + dst.length + ".");
  203. }
  204. /* Write double word. */
  205. dst[0 + offset] = bytes[3];
  206. dst[1 + offset] = bytes[2];
  207. dst[2 + offset] = bytes[1];
  208. dst[3 + offset] = bytes[0];
  209. /* Write first word. */
  210. dst[4 + offset] = bytes[5];
  211. dst[5 + offset] = bytes[4];
  212. /* Write second word. */
  213. dst[6 + offset] = bytes[7];
  214. dst[7 + offset] = bytes[6];
  215. /* Write 8 bytes. */
  216. System.arraycopy(bytes, 8, dst, 8 + offset, 8);
  217. }
  218. /**
  219. * Write the class ID to a LittleEndianOutput (stream)
  220. *
  221. * @param leo the output
  222. */
  223. public void write(LittleEndianOutput leo) {
  224. byte[] buf = bytes.clone();
  225. write(buf, 0);
  226. leo.write(buf);
  227. }
  228. /**
  229. * Checks whether this {@code ClassID} is equal to another object.
  230. *
  231. * @param o the object to compare this {@code ClassID} with
  232. * @return {@code true} if the objects are equal, else {@code false}.
  233. */
  234. @Override
  235. public boolean equals(final Object o) {
  236. return (o instanceof ClassID) && Arrays.equals(bytes, ((ClassID)o).bytes);
  237. }
  238. /**
  239. * Checks whether this {@code ClassID} is equal to another ClassID with inverted endianess,
  240. * because there are apparently not only version 1 GUIDs (aka "network" with big-endian encoding),
  241. * but also version 2 GUIDs (aka "native" with little-endian encoding) out there.
  242. *
  243. * @param o the object to compare this {@code ClassID} with
  244. * @return {@code true} if the objects are equal, else {@code false}.
  245. */
  246. public boolean equalsInverted(ClassID o) {
  247. return
  248. o.bytes[0] == bytes[3] &&
  249. o.bytes[1] == bytes[2] &&
  250. o.bytes[2] == bytes[1] &&
  251. o.bytes[3] == bytes[0] &&
  252. o.bytes[4] == bytes[5] &&
  253. o.bytes[5] == bytes[4] &&
  254. o.bytes[6] == bytes[7] &&
  255. o.bytes[7] == bytes[6] &&
  256. o.bytes[8] == bytes[8] &&
  257. o.bytes[9] == bytes[9] &&
  258. o.bytes[10] == bytes[10] &&
  259. o.bytes[11] == bytes[11] &&
  260. o.bytes[12] == bytes[12] &&
  261. o.bytes[13] == bytes[13] &&
  262. o.bytes[14] == bytes[14] &&
  263. o.bytes[15] == bytes[15]
  264. ;
  265. }
  266. /**
  267. * @see Object#hashCode()
  268. */
  269. @Override
  270. public int hashCode() {
  271. return toString().hashCode();
  272. }
  273. /**
  274. * Returns a human-readable representation of the Class ID in standard
  275. * format {@code "{xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx}"}.
  276. *
  277. * @return String representation of the Class ID represented by this object.
  278. */
  279. @Override
  280. public String toString() {
  281. return "{" + toUUIDString() + "}";
  282. }
  283. /**
  284. * Returns a human-readable representation of the Class ID in UUID
  285. * format {@code "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"}.
  286. *
  287. * @return UUID String representation of the Class ID represented by this object.
  288. */
  289. public String toUUIDString() {
  290. return toUUID().toString().toUpperCase(Locale.ROOT);
  291. }
  292. /**
  293. * Converts the ClassID to an UUID
  294. * @return the ClassID as UUID
  295. *
  296. * @since POI 5.0.0
  297. */
  298. public UUID toUUID() {
  299. final long mostSigBits = ByteBuffer.wrap(bytes, 0, 8).getLong();
  300. final long leastSigBits = ByteBuffer.wrap(bytes, 8, 8).getLong();
  301. return new UUID(mostSigBits, leastSigBits);
  302. }
  303. @Override
  304. public ClassID copy() {
  305. return new ClassID(this);
  306. }
  307. @Override
  308. public Map<String, Supplier<?>> getGenericProperties() {
  309. return GenericRecordUtil.getGenericProperties("uuid", this::toString);
  310. }
  311. }