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.

DateTimeMCAtom.java 3.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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.hslf.record;
  16. import java.io.IOException;
  17. import java.io.OutputStream;
  18. import java.util.Arrays;
  19. import java.util.Map;
  20. import java.util.function.Supplier;
  21. import org.apache.poi.util.GenericRecordUtil;
  22. import org.apache.poi.util.LittleEndian;
  23. public class DateTimeMCAtom extends RecordAtom {
  24. /**
  25. * Record header.
  26. */
  27. private final byte[] _header;
  28. /**
  29. * A TextPosition that specifies the position of the metacharacter in the corresponding text.
  30. */
  31. private int position;
  32. /**
  33. * An unsigned byte that specifies the Format ID used to stylize datetime. The identifier specified by
  34. * the Format ID is converted based on the LCID [MS-LCID] into a value or string as specified in the
  35. * following tables. The LCID is specified in TextSIException.lid. If no valid LCID is found in
  36. * TextSIException.lid, TextSIException.altLid (if it exists) is used.
  37. * The value MUST be greater than or equal to 0x0 and MUST be less than or equal to 0xC.
  38. */
  39. private int index;
  40. private final byte[] unused = new byte[3];
  41. protected DateTimeMCAtom() {
  42. _header = new byte[8];
  43. position = 0;
  44. index = 0;
  45. LittleEndian.putShort(_header, 2, (short)getRecordType());
  46. LittleEndian.putInt(_header, 4, 8);
  47. }
  48. /**
  49. * Constructs the datetime atom record from its source data.
  50. *
  51. * @param source the source data as a byte array.
  52. * @param start the start offset into the byte array.
  53. * @param len the length of the slice in the byte array.
  54. */
  55. protected DateTimeMCAtom(byte[] source, int start, int len) {
  56. // Get the header.
  57. _header = Arrays.copyOfRange(source, start, start+8);
  58. position = LittleEndian.getInt(source, start+8);
  59. index = LittleEndian.getUByte(source, start+12);
  60. System.arraycopy(source, start+13, unused, 0, 3);
  61. }
  62. /**
  63. * Write the contents of the record back, so it can be written
  64. * to disk
  65. *
  66. * @param out the output stream to write to.
  67. * @throws IOException if an error occurs.
  68. */
  69. @Override
  70. public void writeOut(OutputStream out) throws IOException {
  71. out.write(_header);
  72. LittleEndian.putInt(position, out);
  73. out.write(index);
  74. out.write(unused);
  75. }
  76. public int getPosition() {
  77. return position;
  78. }
  79. public void setPosition(int position) {
  80. this.position = position;
  81. }
  82. public int getIndex() {
  83. return index;
  84. }
  85. public void setIndex(int index) {
  86. this.index = index;
  87. }
  88. /**
  89. * Gets the record type.
  90. * @return the record type.
  91. */
  92. @Override
  93. public long getRecordType() {
  94. return RecordTypes.DateTimeMCAtom.typeID;
  95. }
  96. @Override
  97. public Map<String, Supplier<?>> getGenericProperties() {
  98. return GenericRecordUtil.getGenericProperties(
  99. "position", this::getPosition,
  100. "index", this::getIndex
  101. );
  102. }
  103. }