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.

MapInfo.java 4.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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.model;
  16. import java.io.IOException;
  17. import java.io.InputStream;
  18. import java.io.OutputStream;
  19. import java.util.Collection;
  20. import java.util.HashMap;
  21. import java.util.Map;
  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.xssf.usermodel.XSSFMap;
  26. import org.apache.poi.xssf.usermodel.XSSFWorkbook;
  27. import org.apache.xmlbeans.XmlException;
  28. import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTMap;
  29. import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTMapInfo;
  30. import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTSchema;
  31. import org.openxmlformats.schemas.spreadsheetml.x2006.main.MapInfoDocument;
  32. /**
  33. *
  34. * This class implements the Custom XML Mapping Part (Open Office XML Part 1:
  35. * chapter 12.3.6)
  36. *
  37. * An instance of this part type contains a schema for an XML file, and
  38. * information on the behavior that is used when allowing this custom XML schema
  39. * to be mapped into the spreadsheet.
  40. *
  41. * @author Roberto Manicardi
  42. */
  43. public class MapInfo extends POIXMLDocumentPart {
  44. private CTMapInfo mapInfo;
  45. private Map<Integer, XSSFMap> maps ;
  46. public MapInfo() {
  47. super();
  48. mapInfo = CTMapInfo.Factory.newInstance();
  49. }
  50. public MapInfo(PackagePart part, PackageRelationship rel)
  51. throws IOException {
  52. super(part, rel);
  53. readFrom(part.getInputStream());
  54. }
  55. @SuppressWarnings("deprecation")
  56. public void readFrom(InputStream is) throws IOException {
  57. try {
  58. MapInfoDocument doc = MapInfoDocument.Factory.parse(is);
  59. mapInfo = doc.getMapInfo();
  60. maps= new HashMap<Integer, XSSFMap>();
  61. for(CTMap map :mapInfo.getMapArray()){
  62. maps.put((int)map.getID(), new XSSFMap(map,this));
  63. }
  64. } catch (XmlException e) {
  65. throw new IOException(e.getLocalizedMessage());
  66. }
  67. }
  68. /**
  69. * Returns the parent XSSFWorkbook
  70. *
  71. * @return the parent XSSFWorkbook
  72. */
  73. public XSSFWorkbook getWorkbook() {
  74. return (XSSFWorkbook)getParent();
  75. }
  76. /**
  77. *
  78. * @return the internal data object
  79. */
  80. public CTMapInfo getCTMapInfo(){
  81. return mapInfo;
  82. }
  83. /**
  84. * Gets the
  85. * @param schemaId the schema ID
  86. * @return CTSchema by it's ID
  87. */
  88. @SuppressWarnings("deprecation")
  89. public CTSchema getCTSchemaById(String schemaId){
  90. CTSchema xmlSchema = null;
  91. for(CTSchema schema: mapInfo.getSchemaArray()){
  92. if(schema.getID().equals(schemaId)){
  93. xmlSchema = schema;
  94. break;
  95. }
  96. }
  97. return xmlSchema;
  98. }
  99. public XSSFMap getXSSFMapById(int id){
  100. return maps.get(id);
  101. }
  102. public XSSFMap getXSSFMapByName(String name){
  103. XSSFMap matchedMap = null;
  104. for(XSSFMap map :maps.values()){
  105. if(map.getCtMap().getName()!=null && map.getCtMap().getName().equals(name)){
  106. matchedMap = map;
  107. }
  108. }
  109. return matchedMap;
  110. }
  111. /**
  112. *
  113. * @return all the mappings configured in this document
  114. */
  115. public Collection<XSSFMap> getAllXSSFMaps(){
  116. return maps.values();
  117. }
  118. protected void writeTo(OutputStream out) throws IOException {
  119. MapInfoDocument doc = MapInfoDocument.Factory.newInstance();
  120. doc.setMapInfo(mapInfo);
  121. doc.save(out, DEFAULT_XML_OPTIONS);
  122. }
  123. @Override
  124. protected void commit() throws IOException {
  125. PackagePart part = getPackagePart();
  126. OutputStream out = part.getOutputStream();
  127. writeTo(out);
  128. out.close();
  129. }
  130. }