PageRenderTime 45ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/components/forks/poi/src/loci/poi/hssf/record/TextObjectRecord.java

http://github.com/openmicroscopy/bioformats
Java | 276 lines | 198 code | 34 blank | 44 comment | 24 complexity | 10a515b4a6cb5d23d22fa4427212bb8c MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1, Apache-2.0, BSD-2-Clause, MPL-2.0-no-copyleft-exception
  1. /*
  2. * #%L
  3. * Fork of Apache Jakarta POI.
  4. * %%
  5. * Copyright (C) 2008 - 2013 Open Microscopy Environment:
  6. * - Board of Regents of the University of Wisconsin-Madison
  7. * - Glencoe Software, Inc.
  8. * - University of Dundee
  9. * %%
  10. * Licensed under the Apache License, Version 2.0 (the "License");
  11. * you may not use this file except in compliance with the License.
  12. * You may obtain a copy of the License at
  13. *
  14. * http://www.apache.org/licenses/LICENSE-2.0
  15. *
  16. * Unless required by applicable law or agreed to in writing, software
  17. * distributed under the License is distributed on an "AS IS" BASIS,
  18. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  19. * See the License for the specific language governing permissions and
  20. * limitations under the License.
  21. * #L%
  22. */
  23. /* ====================================================================
  24. Licensed to the Apache Software Foundation (ASF) under one or more
  25. contributor license agreements. See the NOTICE file distributed with
  26. this work for additional information regarding copyright ownership.
  27. The ASF licenses this file to You under the Apache License, Version 2.0
  28. (the "License"); you may not use this file except in compliance with
  29. the License. You may obtain a copy of the License at
  30. http://www.apache.org/licenses/LICENSE-2.0
  31. Unless required by applicable law or agreed to in writing, software
  32. distributed under the License is distributed on an "AS IS" BASIS,
  33. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  34. See the License for the specific language governing permissions and
  35. limitations under the License.
  36. ==================================================================== */
  37. package loci.poi.hssf.record;
  38. import loci.poi.hssf.usermodel.HSSFRichTextString;
  39. import loci.poi.util.LittleEndian;
  40. import loci.poi.util.HexDump;
  41. import java.io.UnsupportedEncodingException;
  42. import java.io.ByteArrayOutputStream;
  43. public class TextObjectRecord
  44. extends TextObjectBaseRecord
  45. {
  46. HSSFRichTextString str;
  47. public TextObjectRecord()
  48. {
  49. }
  50. public TextObjectRecord( RecordInputStream in )
  51. {
  52. super( in );
  53. if (str == null)
  54. str = new HSSFRichTextString("");
  55. }
  56. protected void fillFields(RecordInputStream in)
  57. {
  58. super.fillFields(in);
  59. if (getTextLength() > 0) {
  60. if (in.isContinueNext() && in.remaining() == 0) {
  61. //1st Continue
  62. in.nextRecord();
  63. processRawString(in);
  64. } else
  65. throw new RecordFormatException("Expected Continue record to hold string data for TextObjectRecord");
  66. }
  67. if (getFormattingRunLength() > 0) {
  68. if (in.isContinueNext() && in.remaining() == 0) {
  69. in.nextRecord();
  70. processFontRuns(in);
  71. } else throw new RecordFormatException("Expected Continue Record to hold font runs for TextObjectRecord");
  72. }
  73. }
  74. public int getRecordSize()
  75. {
  76. int continue1Size = 0;
  77. int continue2Size = 0;
  78. if (str.length() != 0)
  79. {
  80. int length = str.length() * 2;
  81. while(length > 0){
  82. int chunkSize = Math.min(RecordInputStream.MAX_RECORD_DATA_SIZE-2, length);
  83. length -= chunkSize;
  84. continue1Size += chunkSize;
  85. continue1Size += 1 + 4;
  86. }
  87. continue2Size = (str.numFormattingRuns() + 1) * 8 + 4;
  88. }
  89. return super.getRecordSize() + continue1Size + continue2Size;
  90. }
  91. public int serialize( int offset, byte[] data )
  92. {
  93. // Temporarily blank out str so that record size is calculated without the continue records.
  94. HSSFRichTextString temp = str;
  95. str = new HSSFRichTextString("");
  96. int bytesWritten1 = super.serialize( offset, data );
  97. str = temp;
  98. int pos = offset + bytesWritten1;
  99. if ( str.getString().equals( "" ) == false )
  100. {
  101. ContinueRecord c2 = createContinue2();
  102. int bytesWritten2 = 0;
  103. try
  104. {
  105. byte[] c1Data = str.getString().getBytes( "UTF-16LE" );
  106. int length = c1Data.length;
  107. int charsWritten = 0;
  108. int spos = pos;
  109. while(length > 0){
  110. int chunkSize = Math.min(RecordInputStream.MAX_RECORD_DATA_SIZE-2 , length);
  111. length -= chunkSize;
  112. //continue header
  113. LittleEndian.putShort(data, spos, ContinueRecord.sid);
  114. spos += LittleEndian.SHORT_SIZE;
  115. LittleEndian.putShort(data, spos, (short)(chunkSize+1));
  116. spos += LittleEndian.SHORT_SIZE;
  117. //The first byte specifies if the text is compressed unicode or unicode.
  118. //(regardless what was read, we always serialize double-byte unicode characters (UTF-16LE).
  119. data[spos] = 1;
  120. spos += LittleEndian.BYTE_SIZE;
  121. //copy characters data
  122. System.arraycopy(c1Data, charsWritten, data, spos, chunkSize);
  123. spos += chunkSize;
  124. charsWritten += chunkSize;
  125. }
  126. bytesWritten2 = (spos-pos);
  127. }
  128. catch ( UnsupportedEncodingException e )
  129. {
  130. throw new RuntimeException( e.getMessage(), e );
  131. }
  132. pos += bytesWritten2;
  133. int bytesWritten3 = c2.serialize( pos, data );
  134. pos += bytesWritten3;
  135. int size = bytesWritten1 + bytesWritten2 + bytesWritten3;
  136. if ( size != getRecordSize() )
  137. throw new RecordFormatException(size + " bytes written but getRecordSize() reports " + getRecordSize());
  138. return size;
  139. }
  140. if ( bytesWritten1 != getRecordSize() )
  141. throw new RecordFormatException(bytesWritten1 + " bytes written but getRecordSize() reports " + getRecordSize());
  142. return bytesWritten1;
  143. }
  144. private ContinueRecord createContinue2()
  145. {
  146. ContinueRecord c2 = new ContinueRecord();
  147. byte[] c2Data = new byte[str.numFormattingRuns() * 8 + 8];
  148. int pos = 0;
  149. for ( int i = 0; i < str.numFormattingRuns(); i++ )
  150. {
  151. LittleEndian.putShort( c2Data, pos, (short) str.getIndexOfFormattingRun( i ) );
  152. pos += 2;
  153. LittleEndian.putShort( c2Data, pos, str.getFontOfFormattingRun( i ) == str.NO_FONT ? 0 : str.getFontOfFormattingRun( i ) );
  154. pos += 2;
  155. pos += 4; // skip reserved
  156. }
  157. LittleEndian.putShort( c2Data, pos, (short) str.length() );
  158. pos += 2;
  159. LittleEndian.putShort( c2Data, pos, (short) 0 );
  160. pos += 2;
  161. pos += 4; // skip reserved
  162. c2.setData( c2Data );
  163. return c2;
  164. }
  165. private void processFontRuns( RecordInputStream in )
  166. {
  167. while (in.remaining() > 0)
  168. {
  169. short index = in.readShort();
  170. short iFont = in.readShort();
  171. in.readInt(); // skip reserved.
  172. str.applyFont( index, str.length(), iFont );
  173. }
  174. }
  175. private void processRawString( RecordInputStream in )
  176. {
  177. String s;
  178. byte compressByte = in.readByte();
  179. boolean isCompressed = compressByte == 0;
  180. if ( isCompressed )
  181. {
  182. s = in.readCompressedUnicode(getTextLength());
  183. }
  184. else
  185. {
  186. s = in.readUnicodeLEString(getTextLength());
  187. }
  188. str = new HSSFRichTextString( s );
  189. }
  190. public HSSFRichTextString getStr()
  191. {
  192. return str;
  193. }
  194. public void setStr( HSSFRichTextString str )
  195. {
  196. this.str = str;
  197. }
  198. public String toString()
  199. {
  200. StringBuffer buffer = new StringBuffer();
  201. buffer.append( "[TXO]\n" );
  202. buffer.append( " .options = " )
  203. .append( "0x" ).append( HexDump.toHex( getOptions() ) )
  204. .append( " (" ).append( getOptions() ).append( " )" );
  205. buffer.append( System.getProperty( "line.separator" ) );
  206. buffer.append( " .reserved1 = " ).append( isReserved1() ).append( '\n' );
  207. buffer.append( " .HorizontalTextAlignment = " ).append( getHorizontalTextAlignment() ).append( '\n' );
  208. buffer.append( " .VerticalTextAlignment = " ).append( getVerticalTextAlignment() ).append( '\n' );
  209. buffer.append( " .reserved2 = " ).append( getReserved2() ).append( '\n' );
  210. buffer.append( " .textLocked = " ).append( isTextLocked() ).append( '\n' );
  211. buffer.append( " .reserved3 = " ).append( getReserved3() ).append( '\n' );
  212. buffer.append( " .textOrientation = " )
  213. .append( "0x" ).append( HexDump.toHex( getTextOrientation() ) )
  214. .append( " (" ).append( getTextOrientation() ).append( " )" );
  215. buffer.append( System.getProperty( "line.separator" ) );
  216. buffer.append( " .reserved4 = " )
  217. .append( "0x" ).append( HexDump.toHex( getReserved4() ) )
  218. .append( " (" ).append( getReserved4() ).append( " )" );
  219. buffer.append( System.getProperty( "line.separator" ) );
  220. buffer.append( " .reserved5 = " )
  221. .append( "0x" ).append( HexDump.toHex( getReserved5() ) )
  222. .append( " (" ).append( getReserved5() ).append( " )" );
  223. buffer.append( System.getProperty( "line.separator" ) );
  224. buffer.append( " .reserved6 = " )
  225. .append( "0x" ).append( HexDump.toHex( getReserved6() ) )
  226. .append( " (" ).append( getReserved6() ).append( " )" );
  227. buffer.append( System.getProperty( "line.separator" ) );
  228. buffer.append( " .textLength = " )
  229. .append( "0x" ).append( HexDump.toHex( getTextLength() ) )
  230. .append( " (" ).append( getTextLength() ).append( " )" );
  231. buffer.append( System.getProperty( "line.separator" ) );
  232. buffer.append( " .reserved7 = " )
  233. .append( "0x" ).append( HexDump.toHex( getReserved7() ) )
  234. .append( " (" ).append( getReserved7() ).append( " )" );
  235. buffer.append( System.getProperty( "line.separator" ) );
  236. buffer.append( " .string = " ).append(str).append('\n');
  237. buffer.append( "[/TXO]\n" );
  238. return buffer.toString();
  239. }
  240. }