您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

TableRow.java 8.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  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.hwpf.usermodel;
  16. import java.util.ArrayList;
  17. import java.util.List;
  18. import org.apache.poi.hwpf.sprm.SprmBuffer;
  19. import org.apache.poi.hwpf.sprm.TableSprmUncompressor;
  20. import org.apache.poi.util.POILogFactory;
  21. import org.apache.poi.util.POILogger;
  22. public final class TableRow extends Range
  23. {
  24. private static final POILogger logger = POILogFactory
  25. .getLogger( TableRow.class );
  26. private final static short SPRM_DXAGAPHALF = (short) 0x9602;
  27. private final static short SPRM_DYAROWHEIGHT = (short) 0x9407;
  28. private final static short SPRM_FCANTSPLIT = 0x3403;
  29. private final static short SPRM_FTABLEHEADER = 0x3404;
  30. private final static short SPRM_TJC = 0x5400;
  31. private final static char TABLE_CELL_MARK = '\u0007';
  32. private TableCell[] _cells;
  33. private boolean _cellsFound;
  34. int _levelNum;
  35. private SprmBuffer _papx;
  36. private TableProperties _tprops;
  37. public TableRow( int startIdxInclusive, int endIdxExclusive, Table parent,
  38. int levelNum )
  39. {
  40. super( startIdxInclusive, endIdxExclusive, parent );
  41. Paragraph last = getParagraph( numParagraphs() - 1 );
  42. _papx = last._papx;
  43. _tprops = TableSprmUncompressor.uncompressTAP( _papx );
  44. _levelNum = levelNum;
  45. initCells();
  46. }
  47. public boolean cantSplit()
  48. {
  49. return _tprops.getFCantSplit();
  50. }
  51. public BorderCode getBarBorder()
  52. {
  53. throw new UnsupportedOperationException( "not applicable for TableRow" );
  54. }
  55. public BorderCode getBottomBorder()
  56. {
  57. return _tprops.getBrcBottom();
  58. }
  59. public TableCell getCell( int index )
  60. {
  61. initCells();
  62. return _cells[index];
  63. }
  64. public int getGapHalf()
  65. {
  66. return _tprops.getDxaGapHalf();
  67. }
  68. public BorderCode getHorizontalBorder()
  69. {
  70. return _tprops.getBrcHorizontal();
  71. }
  72. public BorderCode getLeftBorder()
  73. {
  74. return _tprops.getBrcLeft();
  75. }
  76. public BorderCode getRightBorder()
  77. {
  78. return _tprops.getBrcRight();
  79. }
  80. public int getRowHeight()
  81. {
  82. return _tprops.getDyaRowHeight();
  83. }
  84. public int getRowJustification()
  85. {
  86. return _tprops.getJc();
  87. }
  88. public BorderCode getTopBorder()
  89. {
  90. return _tprops.getBrcTop();
  91. }
  92. public BorderCode getVerticalBorder()
  93. {
  94. return _tprops.getBrcVertical();
  95. }
  96. private void initCells()
  97. {
  98. if ( _cellsFound )
  99. return;
  100. final short expectedCellsCount = _tprops.getItcMac();
  101. int lastCellStart = 0;
  102. List<TableCell> cells = new ArrayList<>(
  103. expectedCellsCount + 1);
  104. for ( int p = 0; p < numParagraphs(); p++ )
  105. {
  106. Paragraph paragraph = getParagraph( p );
  107. String s = paragraph.text();
  108. if ( ( ( s.length() > 0 && s.charAt( s.length() - 1 ) == TABLE_CELL_MARK ) || paragraph
  109. .isEmbeddedCellMark() )
  110. && paragraph.getTableLevel() == _levelNum )
  111. {
  112. TableCellDescriptor tableCellDescriptor = _tprops.getRgtc() != null
  113. && _tprops.getRgtc().length > cells.size() ? _tprops
  114. .getRgtc()[cells.size()] : new TableCellDescriptor();
  115. final short leftEdge = _tprops.getRgdxaCenter() != null
  116. && _tprops.getRgdxaCenter().length > cells.size() ? _tprops
  117. .getRgdxaCenter()[cells.size()] : 0;
  118. final short rightEdge = _tprops.getRgdxaCenter() != null
  119. && _tprops.getRgdxaCenter().length > cells.size() + 1 ? _tprops
  120. .getRgdxaCenter()[cells.size() + 1] : 0;
  121. TableCell tableCell = new TableCell( getParagraph(
  122. lastCellStart ).getStartOffset(), getParagraph( p )
  123. .getEndOffset(), this, _levelNum, tableCellDescriptor,
  124. leftEdge, rightEdge - leftEdge );
  125. cells.add( tableCell );
  126. lastCellStart = p + 1;
  127. }
  128. }
  129. if ( lastCellStart < ( numParagraphs() - 1 ) )
  130. {
  131. TableCellDescriptor tableCellDescriptor = _tprops.getRgtc() != null
  132. && _tprops.getRgtc().length > cells.size() ? _tprops
  133. .getRgtc()[cells.size()] : new TableCellDescriptor();
  134. final short leftEdge = _tprops.getRgdxaCenter() != null
  135. && _tprops.getRgdxaCenter().length > cells.size() ? _tprops
  136. .getRgdxaCenter()[cells.size()] : 0;
  137. final short rightEdge = _tprops.getRgdxaCenter() != null
  138. && _tprops.getRgdxaCenter().length > cells.size() + 1 ? _tprops
  139. .getRgdxaCenter()[cells.size() + 1] : 0;
  140. TableCell tableCell = new TableCell( lastCellStart,
  141. ( numParagraphs() - 1 ), this, _levelNum,
  142. tableCellDescriptor, leftEdge, rightEdge - leftEdge );
  143. cells.add( tableCell );
  144. }
  145. // sometimes there are "fake" cells which we need to exclude
  146. if ( !cells.isEmpty() && cells.size() != expectedCellsCount )
  147. {
  148. TableCell lastCell = cells.get( cells.size() - 1 );
  149. if ( lastCell.numParagraphs() == 1
  150. && ( lastCell.getParagraph( 0 ).isTableRowEnd() ) )
  151. {
  152. // remove "fake" cell
  153. cells.remove( cells.size() - 1 );
  154. }
  155. }
  156. if ( cells.size() != expectedCellsCount )
  157. {
  158. logger.log( POILogger.WARN,
  159. "Number of found table cells (" + cells.size()
  160. + ") for table row [" + getStartOffset() + "c; "
  161. + getEndOffset()
  162. + "c] not equals to stored property value "
  163. + expectedCellsCount );
  164. _tprops.setItcMac( (short) cells.size() );
  165. }
  166. _cells = cells.toArray(new TableCell[0]);
  167. _cellsFound = true;
  168. }
  169. public boolean isTableHeader()
  170. {
  171. return _tprops.getFTableHeader();
  172. }
  173. public int numCells()
  174. {
  175. initCells();
  176. return _cells.length;
  177. }
  178. @Override
  179. protected void reset()
  180. {
  181. _cellsFound = false;
  182. }
  183. public void setCantSplit( boolean cantSplit )
  184. {
  185. _tprops.setFCantSplit( cantSplit );
  186. _papx.updateSprm( SPRM_FCANTSPLIT, (byte) ( cantSplit ? 1 : 0 ) );
  187. }
  188. public void setGapHalf( int dxaGapHalf )
  189. {
  190. _tprops.setDxaGapHalf( dxaGapHalf );
  191. _papx.updateSprm( SPRM_DXAGAPHALF, (short) dxaGapHalf );
  192. }
  193. public void setRowHeight( int dyaRowHeight )
  194. {
  195. _tprops.setDyaRowHeight( dyaRowHeight );
  196. _papx.updateSprm( SPRM_DYAROWHEIGHT, (short) dyaRowHeight );
  197. }
  198. public void setRowJustification( int jc )
  199. {
  200. _tprops.setJc( (short) jc );
  201. _papx.updateSprm( SPRM_TJC, (short) jc );
  202. }
  203. public void setTableHeader( boolean tableHeader )
  204. {
  205. _tprops.setFTableHeader( tableHeader );
  206. _papx.updateSprm( SPRM_FTABLEHEADER, (byte) ( tableHeader ? 1 : 0 ) );
  207. }
  208. }