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.

XSSFMap.java 3.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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.util.ArrayList;
  17. import java.util.List;
  18. import org.apache.poi.ooxml.POIXMLDocumentPart;
  19. import org.apache.poi.ooxml.POIXMLDocumentPart.RelationPart;
  20. import org.apache.poi.ss.usermodel.Sheet;
  21. import org.apache.poi.util.Internal;
  22. import org.apache.poi.xssf.model.MapInfo;
  23. import org.apache.poi.xssf.model.SingleXmlCells;
  24. import org.apache.poi.xssf.usermodel.helpers.XSSFSingleXmlCell;
  25. import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTMap;
  26. import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTSchema;
  27. import org.w3c.dom.Node;
  28. /**
  29. * This class implements the Map element (Open Office XML Part 4:
  30. * chapter 3.16.2)
  31. * <p>
  32. * This element contains all of the properties related to the XML map,
  33. * and the behaviors expected during data refresh operations.
  34. */
  35. public class XSSFMap {
  36. private CTMap ctMap;
  37. private MapInfo mapInfo;
  38. public XSSFMap(CTMap ctMap, MapInfo mapInfo) {
  39. this.ctMap = ctMap;
  40. this.mapInfo = mapInfo;
  41. }
  42. @Internal
  43. public CTMap getCtMap() {
  44. return ctMap;
  45. }
  46. @Internal
  47. public CTSchema getCTSchema() {
  48. String schemaId = ctMap.getSchemaID();
  49. return mapInfo.getCTSchemaById(schemaId);
  50. }
  51. public Node getSchema() {
  52. Node xmlSchema = null;
  53. CTSchema schema = getCTSchema();
  54. xmlSchema = schema.getDomNode().getFirstChild();
  55. return xmlSchema;
  56. }
  57. /**
  58. * @return the list of Single Xml Cells that provide a map rule to this mapping.
  59. */
  60. public List<XSSFSingleXmlCell> getRelatedSingleXMLCell() {
  61. List<XSSFSingleXmlCell> relatedSimpleXmlCells = new ArrayList<>();
  62. int sheetNumber = mapInfo.getWorkbook().getNumberOfSheets();
  63. for (int i = 0; i < sheetNumber; i++) {
  64. XSSFSheet sheet = mapInfo.getWorkbook().getSheetAt(i);
  65. for (POIXMLDocumentPart p : sheet.getRelations()) {
  66. if (p instanceof SingleXmlCells) {
  67. SingleXmlCells singleXMLCells = (SingleXmlCells) p;
  68. for (XSSFSingleXmlCell cell : singleXMLCells.getAllSimpleXmlCell()) {
  69. if (cell.getMapId() == ctMap.getID()) {
  70. relatedSimpleXmlCells.add(cell);
  71. }
  72. }
  73. }
  74. }
  75. }
  76. return relatedSimpleXmlCells;
  77. }
  78. /**
  79. * @return the list of all Tables that provide a map rule to this mapping
  80. */
  81. public List<XSSFTable> getRelatedTables() {
  82. List<XSSFTable> tables = new ArrayList<>();
  83. for (Sheet sheet : mapInfo.getWorkbook()) {
  84. for (RelationPart rp : ((XSSFSheet)sheet).getRelationParts()) {
  85. if (rp.getRelationship().getRelationshipType().equals(XSSFRelation.TABLE.getRelation())) {
  86. XSSFTable table = rp.getDocumentPart();
  87. if (table.mapsTo(ctMap.getID())) {
  88. tables.add(table);
  89. }
  90. }
  91. }
  92. }
  93. return tables;
  94. }
  95. }