PageRenderTime 120ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/thirdparties-extension/org.apache.poi.xwpf.converter.core/src/main/java/org/apache/poi/xwpf/converter/core/styles/TableInfo.java

https://github.com/minstrelsy/xdocreport
Java | 176 lines | 124 code | 25 blank | 27 comment | 15 complexity | f46e26eb8ff79bab3e084501a3034914 MD5 | raw file
  1. /**
  2. * Copyright (C) 2011-2012 The XDocReport Team <xdocreport@googlegroups.com>
  3. *
  4. * All rights reserved.
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining
  7. * a copy of this software and associated documentation files (the
  8. * "Software"), to deal in the Software without restriction, including
  9. * without limitation the rights to use, copy, modify, merge, publish,
  10. * distribute, sublicense, and/or sell copies of the Software, and to
  11. * permit persons to whom the Software is furnished to do so, subject to
  12. * the following conditions:
  13. *
  14. * The above copyright notice and this permission notice shall be
  15. * included in all copies or substantial portions of the Software.
  16. *
  17. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  18. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  19. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  20. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  21. * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  22. * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  23. * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  24. */
  25. package org.apache.poi.xwpf.converter.core.styles;
  26. import java.math.BigInteger;
  27. import java.util.HashMap;
  28. import java.util.List;
  29. import java.util.Map;
  30. import org.apache.poi.xwpf.converter.core.utils.XWPFTableUtil;
  31. import org.apache.poi.xwpf.usermodel.XWPFTable;
  32. import org.apache.poi.xwpf.usermodel.XWPFTableCell;
  33. import org.apache.poi.xwpf.usermodel.XWPFTableRow;
  34. import org.apache.xmlbeans.XmlCursor;
  35. import org.apache.xmlbeans.XmlObject;
  36. import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTRow;
  37. import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTSdtCell;
  38. import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTc;
  39. public class TableInfo
  40. {
  41. private final XWPFTable table;
  42. private final XWPFStylesDocument stylesDocument;
  43. private final Map<XWPFTableCell, TableCellInfo> cellInfos;
  44. private Integer nbColumns;
  45. private final boolean canApplyFirstRow;
  46. private final boolean canApplyLastRow;
  47. private boolean canApplyFirstCol;
  48. private final boolean canApplyLastCol;
  49. public TableInfo( XWPFTable table, XWPFStylesDocument stylesDocument )
  50. {
  51. this.table = table;
  52. this.cellInfos = new HashMap<XWPFTableCell, TableCellInfo>();
  53. this.stylesDocument = stylesDocument;
  54. this.nbColumns = null;
  55. // Compute value apply lastRow etc from the tblLook
  56. int tblLookVal = XWPFTableUtil.getTblLookVal( table );
  57. this.canApplyFirstRow = XWPFTableUtil.canApplyFirstRow( tblLookVal );
  58. this.canApplyLastRow = XWPFTableUtil.canApplyLastRow( tblLookVal );
  59. this.canApplyFirstCol = XWPFTableUtil.canApplyFirstCol( tblLookVal );
  60. this.canApplyLastCol = XWPFTableUtil.canApplyLastCol( tblLookVal );
  61. }
  62. public TableCellInfo getCellInfo( XWPFTableCell cell )
  63. {
  64. TableCellInfo cellInfo = cellInfos.get( cell );
  65. if ( cellInfo != null )
  66. {
  67. return cellInfo;
  68. }
  69. // Compute it
  70. computeCellInfos( cell.getTableRow() );
  71. return cellInfos.get( cell );
  72. }
  73. private void computeCellInfos( XWPFTableRow row )
  74. {
  75. if ( nbColumns == null )
  76. {
  77. nbColumns = XWPFTableUtil.computeColWidths( row.getTable() ).length;
  78. }
  79. int rowIndex = table.getRows().indexOf( row );
  80. boolean firstRow = rowIndex == 0;
  81. boolean lastRow = rowIndex == ( table.getRows().size() - 1 );
  82. boolean firstCol = true;
  83. boolean lastCol = false;
  84. int cellIndex = 0;
  85. CTRow ctRow = row.getCtRow();
  86. XmlCursor c = ctRow.newCursor();
  87. c.selectPath( "./*" );
  88. while ( c.toNextSelection() )
  89. {
  90. XmlObject o = c.getObject();
  91. if ( o instanceof CTTc )
  92. {
  93. CTTc tc = (CTTc) o;
  94. XWPFTableCell cell = row.getTableCell( tc );
  95. cellIndex = getCellIndex( cellIndex, cell );
  96. lastCol = ( cellIndex == nbColumns - 1 );
  97. addCellInfo( cell, firstRow, lastRow, firstCol, lastCol );
  98. firstCol = false;
  99. }
  100. else if ( o instanceof CTSdtCell )
  101. {
  102. // Fix bug of POI
  103. CTSdtCell sdtCell = (CTSdtCell) o;
  104. List<CTTc> tcList = sdtCell.getSdtContent().getTcList();
  105. for ( CTTc ctTc : tcList )
  106. {
  107. XWPFTableCell cell = new XWPFTableCell( ctTc, row, row.getTable().getBody() );
  108. cellIndex = getCellIndex( cellIndex, cell );
  109. lastCol = ( cellIndex == nbColumns - 1 );
  110. addCellInfo( cell, firstRow, lastRow, firstCol, lastCol );
  111. firstCol = false;
  112. }
  113. }
  114. }
  115. c.dispose();
  116. }
  117. public void addCellInfo( XWPFTableCell cell, boolean firstRow, boolean lastRow, boolean firstCol, boolean lastCol )
  118. {
  119. TableCellInfo cellInfo = new TableCellInfo( this, firstRow, lastRow, firstCol, lastCol );
  120. cellInfos.put( cell, cellInfo );
  121. }
  122. private int getCellIndex( int cellIndex, XWPFTableCell cell )
  123. {
  124. BigInteger gridSpan = stylesDocument.getTableCellGridSpan( cell.getCTTc().getTcPr() );
  125. if ( gridSpan != null )
  126. {
  127. cellIndex = cellIndex + gridSpan.intValue();
  128. }
  129. else
  130. {
  131. cellIndex++;
  132. }
  133. return cellIndex;
  134. }
  135. public boolean canApplyFirstRow()
  136. {
  137. return canApplyFirstRow;
  138. }
  139. public boolean canApplyLastRow()
  140. {
  141. return canApplyLastRow;
  142. }
  143. public boolean canApplyFirstCol()
  144. {
  145. return canApplyFirstCol;
  146. }
  147. public boolean canApplyLastCol()
  148. {
  149. return canApplyLastCol;
  150. }
  151. }