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.

ClassID.java 8.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  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 org.apache.poi.util.HexDump;
  17. /**
  18. * <p>Represents a class ID (16 bytes). Unlike other little-endian
  19. * type the {@link ClassID} is not just 16 bytes stored in the wrong
  20. * order. Instead, it is a double word (4 bytes) followed by two
  21. * words (2 bytes each) followed by 8 bytes.</p>
  22. *
  23. * @author Rainer Klute <a
  24. * href="mailto:klute@rainer-klute.de">&lt;klute@rainer-klute.de&gt;</a>
  25. */
  26. public class ClassID
  27. {
  28. public static final ClassID OLE10_PACKAGE = new ClassID("{0003000C-0000-0000-C000-000000000046}");
  29. public static final ClassID PPT_SHOW = new ClassID("{64818D10-4F9B-11CF-86EA-00AA00B929E8}");
  30. public static final ClassID XLS_WORKBOOK = new ClassID("{00020841-0000-0000-C000-000000000046}");
  31. public static final ClassID TXT_ONLY = new ClassID("{5e941d80-bf96-11cd-b579-08002b30bfeb}"); // ???
  32. public static final ClassID EXCEL97 = new ClassID("{00020820-0000-0000-C000-000000000046}");
  33. public static final ClassID EXCEL95 = new ClassID("{00020810-0000-0000-C000-000000000046}");
  34. public static final ClassID WORD97 = new ClassID("{00020906-0000-0000-C000-000000000046}");
  35. public static final ClassID WORD95 = new ClassID("{00020900-0000-0000-C000-000000000046}");
  36. public static final ClassID POWERPOINT97 = new ClassID("{64818D10-4F9B-11CF-86EA-00AA00B929E8}");
  37. public static final ClassID POWERPOINT95 = new ClassID("{EA7BAE70-FB3B-11CD-A903-00AA00510EA3}");
  38. public static final ClassID EQUATION30 = new ClassID("{0002CE02-0000-0000-C000-000000000046}");
  39. /**
  40. * <p>The bytes making out the class ID in correct order,
  41. * i.e. big-endian.</p>
  42. */
  43. protected byte[] bytes;
  44. /**
  45. * <p>Creates a {@link ClassID} and reads its value from a byte
  46. * array.</p>
  47. *
  48. * @param src The byte array to read from.
  49. * @param offset The offset of the first byte to read.
  50. */
  51. public ClassID(final byte[] src, final int offset)
  52. {
  53. read(src, offset);
  54. }
  55. /**
  56. * <p>Creates a {@link ClassID} and initializes its value with
  57. * 0x00 bytes.</p>
  58. */
  59. public ClassID()
  60. {
  61. bytes = new byte[LENGTH];
  62. for (int i = 0; i < LENGTH; i++)
  63. bytes[i] = 0x00;
  64. }
  65. /**
  66. * <p>Creates a {@link ClassID} from a human-readable representation of the Class ID in standard
  67. * format <code>"{xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx}"</code>.</p>
  68. *
  69. * @param externalForm representation of the Class ID represented by this object.
  70. */
  71. public ClassID(String externalForm) {
  72. bytes = new byte[LENGTH];
  73. String clsStr = externalForm.replaceAll("[{}-]", "");
  74. for (int i=0; i<clsStr.length(); i+=2) {
  75. bytes[i/2] = (byte)Integer.parseInt(clsStr.substring(i, i+2), 16);
  76. }
  77. }
  78. /** <p>The number of bytes occupied by this object in the byte
  79. * stream.</p> */
  80. public static final int LENGTH = 16;
  81. /**
  82. * @return The number of bytes occupied by this object in the byte
  83. * stream.
  84. */
  85. public int length()
  86. {
  87. return LENGTH;
  88. }
  89. /**
  90. * <p>Gets the bytes making out the class ID. They are returned in
  91. * correct order, i.e. big-endian.</p>
  92. *
  93. * @return the bytes making out the class ID.
  94. */
  95. public byte[] getBytes()
  96. {
  97. return bytes;
  98. }
  99. /**
  100. * <p>Sets the bytes making out the class ID.</p>
  101. *
  102. * @param bytes The bytes making out the class ID in big-endian format. They
  103. * are copied without their order being changed.
  104. */
  105. public void setBytes(final byte[] bytes)
  106. {
  107. for (int i = 0; i < this.bytes.length; i++)
  108. this.bytes[i] = bytes[i];
  109. }
  110. /**
  111. * <p>Reads the class ID's value from a byte array by turning
  112. * little-endian into big-endian.</p>
  113. *
  114. * @param src The byte array to read from
  115. *
  116. * @param offset The offset within the <var>src</var> byte array
  117. *
  118. * @return A byte array containing the class ID.
  119. */
  120. public byte[] read(final byte[] src, final int offset)
  121. {
  122. bytes = new byte[16];
  123. /* Read double word. */
  124. bytes[0] = src[3 + offset];
  125. bytes[1] = src[2 + offset];
  126. bytes[2] = src[1 + offset];
  127. bytes[3] = src[0 + offset];
  128. /* Read first word. */
  129. bytes[4] = src[5 + offset];
  130. bytes[5] = src[4 + offset];
  131. /* Read second word. */
  132. bytes[6] = src[7 + offset];
  133. bytes[7] = src[6 + offset];
  134. /* Read 8 bytes. */
  135. for (int i = 8; i < 16; i++)
  136. bytes[i] = src[i + offset];
  137. return bytes;
  138. }
  139. /**
  140. * <p>Writes the class ID to a byte array in the
  141. * little-endian format.</p>
  142. *
  143. * @param dst The byte array to write to.
  144. *
  145. * @param offset The offset within the <var>dst</var> byte array.
  146. *
  147. * @exception ArrayStoreException if there is not enough room for the class
  148. * ID 16 bytes in the byte array after the <var>offset</var> position.
  149. */
  150. public void write(final byte[] dst, final int offset)
  151. throws ArrayStoreException
  152. {
  153. /* Check array size: */
  154. if (dst.length < 16)
  155. throw new ArrayStoreException
  156. ("Destination byte[] must have room for at least 16 bytes, " +
  157. "but has a length of only " + dst.length + ".");
  158. /* Write double word. */
  159. dst[0 + offset] = bytes[3];
  160. dst[1 + offset] = bytes[2];
  161. dst[2 + offset] = bytes[1];
  162. dst[3 + offset] = bytes[0];
  163. /* Write first word. */
  164. dst[4 + offset] = bytes[5];
  165. dst[5 + offset] = bytes[4];
  166. /* Write second word. */
  167. dst[6 + offset] = bytes[7];
  168. dst[7 + offset] = bytes[6];
  169. /* Write 8 bytes. */
  170. for (int i = 8; i < 16; i++)
  171. dst[i + offset] = bytes[i];
  172. }
  173. /**
  174. * <p>Checks whether this <code>ClassID</code> is equal to another
  175. * object.</p>
  176. *
  177. * @param o the object to compare this <code>PropertySet</code> with
  178. * @return <code>true</code> if the objects are equal, else
  179. * <code>false</code>.</p>
  180. */
  181. public boolean equals(final Object o)
  182. {
  183. if (o == null || !(o instanceof ClassID))
  184. return false;
  185. final ClassID cid = (ClassID) o;
  186. if (bytes.length != cid.bytes.length)
  187. return false;
  188. for (int i = 0; i < bytes.length; i++)
  189. if (bytes[i] != cid.bytes[i])
  190. return false;
  191. return true;
  192. }
  193. /**
  194. * @see Object#hashCode()
  195. */
  196. public int hashCode()
  197. {
  198. return new String(bytes).hashCode();
  199. }
  200. /**
  201. * <p>Returns a human-readable representation of the Class ID in standard
  202. * format <code>"{xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx}"</code>.</p>
  203. *
  204. * @return String representation of the Class ID represented by this object.
  205. */
  206. public String toString()
  207. {
  208. StringBuffer sbClassId = new StringBuffer(38);
  209. sbClassId.append('{');
  210. for (int i = 0; i < 16; i++)
  211. {
  212. sbClassId.append(HexDump.toHex(bytes[i]));
  213. if (i == 3 || i == 5 || i == 7 || i == 9)
  214. sbClassId.append('-');
  215. }
  216. sbClassId.append('}');
  217. return sbClassId.toString();
  218. }
  219. }