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.

EscherTextboxRecord.java 5.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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.ddf;
  16. import org.apache.poi.hslf.record.RecordTypes;
  17. import org.apache.poi.util.HexDump;
  18. import org.apache.poi.util.LittleEndian;
  19. import org.apache.poi.util.RecordFormatException;
  20. /**
  21. * Holds data from the parent application. Most commonly used to store
  22. * text in the format of the parent application, rather than in
  23. * Escher format. We don't attempt to understand the contents, since
  24. * they will be in the parent's format, not Escher format.
  25. *
  26. * @author Glen Stampoultzis (glens at apache.org)
  27. * @author Nick Burch (nick at torchbox dot com)
  28. */
  29. public class EscherTextboxRecord extends EscherRecord
  30. {
  31. public static final short RECORD_ID = (short)RecordTypes.EscherClientTextbox;
  32. public static final String RECORD_DESCRIPTION = "msofbtClientTextbox";
  33. private static final byte[] NO_BYTES = new byte[0];
  34. /** The data for this record not including the the 8 byte header */
  35. private byte[] thedata = NO_BYTES;
  36. public EscherTextboxRecord()
  37. {
  38. }
  39. public int fillFields(byte[] data, int offset, EscherRecordFactory recordFactory) {
  40. int bytesRemaining = readHeader( data, offset );
  41. // Save the data, ready for the calling code to do something
  42. // useful with it
  43. thedata = new byte[bytesRemaining];
  44. System.arraycopy( data, offset + 8, thedata, 0, bytesRemaining );
  45. return bytesRemaining + 8;
  46. }
  47. public int serialize( int offset, byte[] data, EscherSerializationListener listener )
  48. {
  49. listener.beforeRecordSerialize( offset, getRecordId(), this );
  50. LittleEndian.putShort(data, offset, getOptions());
  51. LittleEndian.putShort(data, offset+2, getRecordId());
  52. int remainingBytes = thedata.length;
  53. LittleEndian.putInt(data, offset+4, remainingBytes);
  54. System.arraycopy(thedata, 0, data, offset+8, thedata.length);
  55. int pos = offset+8+thedata.length;
  56. listener.afterRecordSerialize( pos, getRecordId(), pos - offset, this );
  57. int size = pos - offset;
  58. if (size != getRecordSize())
  59. throw new RecordFormatException(size + " bytes written but getRecordSize() reports " + getRecordSize());
  60. return size;
  61. }
  62. /**
  63. * Returns any extra data associated with this record. In practice excel
  64. * does not seem to put anything here, but with PowerPoint this will
  65. * contain the bytes that make up a TextHeaderAtom followed by a
  66. * TextBytesAtom/TextCharsAtom
  67. */
  68. public byte[] getData()
  69. {
  70. return thedata;
  71. }
  72. /**
  73. * Sets the extra data (in the parent application's format) to be
  74. * contained by the record. Used when the parent application changes
  75. * the contents.
  76. */
  77. public void setData(byte[] b, int start, int length)
  78. {
  79. thedata = new byte[length];
  80. System.arraycopy(b,start,thedata,0,length);
  81. }
  82. public void setData(byte[] b) {
  83. setData(b,0,b.length);
  84. }
  85. public int getRecordSize()
  86. {
  87. return 8 + thedata.length;
  88. }
  89. public Object clone()
  90. {
  91. // shallow clone
  92. return super.clone();
  93. }
  94. public String getRecordName() {
  95. return "ClientTextbox";
  96. }
  97. public String toString()
  98. {
  99. String nl = System.getProperty( "line.separator" );
  100. String theDumpHex = "";
  101. try
  102. {
  103. if (thedata.length != 0)
  104. {
  105. theDumpHex = " Extra Data:" + nl;
  106. theDumpHex += HexDump.dump(thedata, 0, 0);
  107. }
  108. }
  109. catch ( Exception e )
  110. {
  111. theDumpHex = "Error!!";
  112. }
  113. return getClass().getName() + ":" + nl +
  114. " isContainer: " + isContainerRecord() + nl +
  115. " version: 0x" + HexDump.toHex( getVersion() ) + nl +
  116. " instance: 0x" + HexDump.toHex( getInstance() ) + nl +
  117. " recordId: 0x" + HexDump.toHex( getRecordId() ) + nl +
  118. " numchildren: " + getChildRecords().size() + nl +
  119. theDumpHex;
  120. }
  121. @Override
  122. public String toXml(String tab) {
  123. String theDumpHex = "";
  124. try
  125. {
  126. if (thedata.length != 0)
  127. {
  128. theDumpHex += HexDump.dump(thedata, 0, 0);
  129. }
  130. }
  131. catch ( Exception e )
  132. {
  133. theDumpHex = "Error!!";
  134. }
  135. StringBuilder builder = new StringBuilder();
  136. builder.append(tab).append(formatXmlRecordHeader(getClass().getSimpleName(), HexDump.toHex(getRecordId()), HexDump.toHex(getVersion()), HexDump.toHex(getInstance())))
  137. .append(tab).append("\t").append("<ExtraData>").append(theDumpHex).append("</ExtraData>\n");
  138. builder.append(tab).append("</").append(getClass().getSimpleName()).append(">\n");
  139. return builder.toString();
  140. }
  141. }