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.

XSSFTable.java 8.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  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.xssf.usermodel;
  16. import java.io.IOException;
  17. import java.io.InputStream;
  18. import java.io.OutputStream;
  19. import java.util.Arrays;
  20. import java.util.List;
  21. import java.util.Vector;
  22. import org.apache.poi.POIXMLDocumentPart;
  23. import org.apache.poi.openxml4j.opc.PackagePart;
  24. import org.apache.poi.openxml4j.opc.PackageRelationship;
  25. import org.apache.poi.ss.util.CellReference;
  26. import org.apache.poi.xssf.usermodel.helpers.XSSFXmlColumnPr;
  27. import org.apache.xmlbeans.XmlException;
  28. import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTTable;
  29. import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTTableColumn;
  30. import org.openxmlformats.schemas.spreadsheetml.x2006.main.TableDocument;
  31. /**
  32. *
  33. * This class implements the Table Part (Open Office XML Part 4:
  34. * chapter 3.5.1)
  35. *
  36. * This implementation works under the assumption that a table contains mappings to a subtree of an XML.
  37. * The root element of this subtree an occur multiple times (one for each row of the table). The child nodes
  38. * of the root element can be only attributes or element with maxOccurs=1 property set
  39. *
  40. *
  41. * @author Roberto Manicardi
  42. */
  43. public class XSSFTable extends POIXMLDocumentPart {
  44. private CTTable ctTable;
  45. private List<XSSFXmlColumnPr> xmlColumnPr;
  46. private CellReference startCellReference;
  47. private CellReference endCellReference;
  48. private String commonXPath;
  49. public XSSFTable() {
  50. super();
  51. ctTable = CTTable.Factory.newInstance();
  52. }
  53. public XSSFTable(PackagePart part, PackageRelationship rel)
  54. throws IOException {
  55. super(part, rel);
  56. readFrom(part.getInputStream());
  57. }
  58. public void readFrom(InputStream is) throws IOException {
  59. try {
  60. TableDocument doc = TableDocument.Factory.parse(is);
  61. ctTable = doc.getTable();
  62. } catch (XmlException e) {
  63. throw new IOException(e.getLocalizedMessage());
  64. }
  65. }
  66. public XSSFSheet getXSSFSheet(){
  67. return (XSSFSheet) getParent();
  68. }
  69. public void writeTo(OutputStream out) throws IOException {
  70. updateHeaders();
  71. TableDocument doc = TableDocument.Factory.newInstance();
  72. doc.setTable(ctTable);
  73. doc.save(out, DEFAULT_XML_OPTIONS);
  74. }
  75. @Override
  76. protected void commit() throws IOException {
  77. PackagePart part = getPackagePart();
  78. OutputStream out = part.getOutputStream();
  79. writeTo(out);
  80. out.close();
  81. }
  82. public CTTable getCTTable(){
  83. return ctTable;
  84. }
  85. /**
  86. * Checks if this Table element contains even a single mapping to the map identified by id
  87. * @param id the XSSFMap ID
  88. * @return true if the Table element contain mappings
  89. */
  90. public boolean mapsTo(long id){
  91. boolean maps =false;
  92. List<XSSFXmlColumnPr> pointers = getXmlColumnPrs();
  93. for(XSSFXmlColumnPr pointer: pointers){
  94. if(pointer.getMapId()==id){
  95. maps=true;
  96. break;
  97. }
  98. }
  99. return maps;
  100. }
  101. /**
  102. *
  103. * Calculates the xpath of the root element for the table. This will be the common part
  104. * of all the mapping's xpaths
  105. *
  106. * @return the xpath of the table's root element
  107. */
  108. public String getCommonXpath() {
  109. if(commonXPath == null){
  110. String[] commonTokens ={};
  111. for(CTTableColumn column :ctTable.getTableColumns().getTableColumnList()){
  112. if(column.getXmlColumnPr()!=null){
  113. String xpath = column.getXmlColumnPr().getXpath();
  114. String[] tokens = xpath.split("/");
  115. if(commonTokens.length==0){
  116. commonTokens = tokens;
  117. }else{
  118. int maxLenght = commonTokens.length>tokens.length? tokens.length:commonTokens.length;
  119. for(int i =0; i<maxLenght;i++){
  120. if(!commonTokens[i].equals(tokens[i])){
  121. List<String> subCommonTokens = Arrays.asList(commonTokens).subList(0, i);
  122. String[] container = {};
  123. commonTokens = subCommonTokens.toArray(container);
  124. break;
  125. }
  126. }
  127. }
  128. }
  129. }
  130. commonXPath ="";
  131. for(int i = 1 ; i< commonTokens.length;i++){
  132. commonXPath +="/"+commonTokens[i];
  133. }
  134. }
  135. return commonXPath;
  136. }
  137. public List<XSSFXmlColumnPr> getXmlColumnPrs() {
  138. if(xmlColumnPr==null){
  139. xmlColumnPr = new Vector<XSSFXmlColumnPr>();
  140. for(CTTableColumn column:ctTable.getTableColumns().getTableColumnList()){
  141. if(column.getXmlColumnPr()!=null){
  142. XSSFXmlColumnPr columnPr = new XSSFXmlColumnPr(this,column,column.getXmlColumnPr());
  143. xmlColumnPr.add(columnPr);
  144. }
  145. }
  146. }
  147. return xmlColumnPr;
  148. }
  149. /**
  150. * @return the name of the Table, if set
  151. */
  152. public String getName() {
  153. return ctTable.getName();
  154. }
  155. /**
  156. * Changes the name of the Table
  157. */
  158. public void setName(String name) {
  159. if(name == null) {
  160. ctTable.unsetName();
  161. return;
  162. }
  163. ctTable.setName(name);
  164. }
  165. /**
  166. * @return the display name of the Table, if set
  167. */
  168. public String getDisplayName() {
  169. return ctTable.getDisplayName();
  170. }
  171. /**
  172. * Changes the display name of the Table
  173. */
  174. public void setDisplayName(String name) {
  175. ctTable.setDisplayName(name);
  176. }
  177. /**
  178. * @return the number of mapped table columns (see Open Office XML Part 4: chapter 3.5.1.4)
  179. */
  180. public long getNumerOfMappedColumns(){
  181. return ctTable.getTableColumns().getCount();
  182. }
  183. /**
  184. * @return The reference for the cell in the top-left part of the table
  185. * (see Open Office XML Part 4: chapter 3.5.1.2, attribute ref)
  186. *
  187. */
  188. public CellReference getStartCellReference() {
  189. if(startCellReference==null){
  190. String ref = ctTable.getRef();
  191. if(ref != null) {
  192. String[] boundaries = ref.split(":");
  193. String from = boundaries[0];
  194. startCellReference = new CellReference(from);
  195. }
  196. }
  197. return startCellReference;
  198. }
  199. /**
  200. * @return The reference for the cell in the bottom-right part of the table
  201. * (see Open Office XML Part 4: chapter 3.5.1.2, attribute ref)
  202. *
  203. */
  204. public CellReference getEndCellReference() {
  205. if(endCellReference==null){
  206. String ref = ctTable.getRef();
  207. String[] boundaries = ref.split(":");
  208. String from = boundaries[1];
  209. endCellReference = new CellReference(from);
  210. }
  211. return endCellReference;
  212. }
  213. /**
  214. * @return the total number of rows in the selection. (Note: in this version autofiltering is ignored)
  215. *
  216. */
  217. public int getRowCount(){
  218. CellReference from = getStartCellReference();
  219. CellReference to = getEndCellReference();
  220. int rowCount = -1;
  221. if (from!=null && to!=null){
  222. rowCount = to.getRow()-from.getRow();
  223. }
  224. return rowCount;
  225. }
  226. /**
  227. * Synchronize table headers with cell values in the parent sheet.
  228. * Headers <em>must</em> be in sync, otherwise Excel will display a
  229. * "Found unreadable content" message on startup.
  230. */
  231. public void updateHeaders(){
  232. XSSFSheet sheet = (XSSFSheet)getParent();
  233. CellReference ref = getStartCellReference();
  234. if(ref == null) return;
  235. int headerRow = ref.getRow();
  236. int firstHeaderColumn = ref.getCol();
  237. XSSFRow row = sheet.getRow(headerRow);
  238. if (row != null && row.getCTRow().validate()) {
  239. int cellnum = firstHeaderColumn;
  240. for (CTTableColumn col : getCTTable().getTableColumns().getTableColumnList()) {
  241. XSSFCell cell = row.getCell(cellnum);
  242. if (cell != null) {
  243. col.setName(cell.getStringCellValue());
  244. }
  245. cellnum++;
  246. }
  247. }
  248. }
  249. }