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.

XSSFRelation.java 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  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.util.Iterator;
  19. import java.util.Map;
  20. import java.util.HashMap;
  21. import org.apache.poi.POIXMLDocument;
  22. import org.apache.poi.POIXMLRelation;
  23. import org.apache.poi.POIXMLDocumentPart;
  24. import org.apache.poi.xssf.model.MapInfo;
  25. import org.apache.poi.xssf.model.SingleXmlCells;
  26. import org.apache.poi.xssf.model.StylesTable;
  27. import org.apache.poi.xssf.model.SharedStringsTable;
  28. import org.apache.poi.xssf.model.CommentsTable;
  29. import org.apache.poi.xssf.model.CalculationChain;
  30. import org.apache.poi.xssf.model.Table;
  31. import org.apache.poi.util.POILogFactory;
  32. import org.apache.poi.util.POILogger;
  33. import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
  34. import org.apache.poi.openxml4j.opc.PackagePart;
  35. import org.apache.poi.openxml4j.opc.PackagePartName;
  36. import org.apache.poi.openxml4j.opc.PackageRelationship;
  37. import org.apache.poi.openxml4j.opc.PackageRelationshipCollection;
  38. import org.apache.poi.openxml4j.opc.PackagingURIHelper;
  39. /**
  40. *
  41. */
  42. public final class XSSFRelation extends POIXMLRelation {
  43. private static POILogger log = POILogFactory.getLogger(XSSFRelation.class);
  44. /**
  45. * A map to lookup POIXMLRelation by its relation type
  46. */
  47. protected static Map<String, XSSFRelation> _table = new HashMap<String, XSSFRelation>();
  48. public static final XSSFRelation WORKBOOK = new XSSFRelation(
  49. "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet.main+xml",
  50. "http://schemas.openxmlformats.org/officeDocument/2006/relationships/workbook",
  51. "/xl/workbook.xml",
  52. null
  53. );
  54. public static final XSSFRelation MACROS_WORKBOOK = new XSSFRelation(
  55. "application/vnd.ms-excel.sheet.macroEnabled.main+xml",
  56. "http://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument",
  57. "/xl/workbook.xml",
  58. null
  59. );
  60. public static final XSSFRelation TEMPLATE_WORKBOOK = new XSSFRelation(
  61. "application/vnd.openxmlformats-officedocument.spreadsheetml.template.main+xml",
  62. "http://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument",
  63. "/xl/workbook.xml",
  64. null
  65. );
  66. public static final XSSFRelation MACRO_TEMPLATE_WORKBOOK = new XSSFRelation(
  67. "application/vnd.ms-excel.template.macroEnabled.main+xml",
  68. "http://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument",
  69. "/xl/workbook.xml",
  70. null
  71. );
  72. public static final XSSFRelation MACRO_ADDIN_WORKBOOK = new XSSFRelation(
  73. "application/vnd.ms-excel.addin.macroEnabled.main+xml",
  74. "http://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument",
  75. "/xl/workbook.xml",
  76. null
  77. );
  78. public static final XSSFRelation WORKSHEET = new XSSFRelation(
  79. "application/vnd.openxmlformats-officedocument.spreadsheetml.worksheet+xml",
  80. "http://schemas.openxmlformats.org/officeDocument/2006/relationships/worksheet",
  81. "/xl/worksheets/sheet#.xml",
  82. XSSFSheet.class
  83. );
  84. public static final XSSFRelation CHARTSHEET = new XSSFRelation(
  85. "application/vnd.openxmlformats-officedocument.spreadsheetml.chartsheet+xml",
  86. "http://schemas.openxmlformats.org/officeDocument/2006/relationships/chartsheet",
  87. "/xl/chartsheets/sheet#.xml",
  88. XSSFChartSheet.class
  89. );
  90. public static final XSSFRelation SHARED_STRINGS = new XSSFRelation(
  91. "application/vnd.openxmlformats-officedocument.spreadsheetml.sharedStrings+xml",
  92. "http://schemas.openxmlformats.org/officeDocument/2006/relationships/sharedStrings",
  93. "/xl/sharedStrings.xml",
  94. SharedStringsTable.class
  95. );
  96. public static final XSSFRelation STYLES = new XSSFRelation(
  97. "application/vnd.openxmlformats-officedocument.spreadsheetml.styles+xml",
  98. "http://schemas.openxmlformats.org/officeDocument/2006/relationships/styles",
  99. "/xl/styles.xml",
  100. StylesTable.class
  101. );
  102. public static final XSSFRelation DRAWINGS = new XSSFRelation(
  103. "application/vnd.openxmlformats-officedocument.drawing+xml",
  104. "http://schemas.openxmlformats.org/officeDocument/2006/relationships/drawing",
  105. "/xl/drawings/drawing#.xml",
  106. XSSFDrawing.class
  107. );
  108. public static final XSSFRelation VML_DRAWINGS = new XSSFRelation(
  109. "application/vnd.openxmlformats-officedocument.vmlDrawing",
  110. "http://schemas.openxmlformats.org/officeDocument/2006/relationships/vmlDrawing",
  111. "/xl/drawings/vmlDrawing#.vml",
  112. null
  113. );
  114. public static final XSSFRelation CUSTOM_XML_MAPPINGS = new XSSFRelation(
  115. "application/xml",
  116. "http://schemas.openxmlformats.org/officeDocument/2006/relationships/xmlMaps",
  117. "/xl/xmlMaps.xml",
  118. MapInfo.class
  119. );
  120. public static final XSSFRelation SINGLE_XML_CELLS = new XSSFRelation(
  121. "application/vnd.openxmlformats-officedocument.spreadsheetml.tableSingleCells+xml",
  122. "http://schemas.openxmlformats.org/officeDocument/2006/relationships/tableSingleCells",
  123. "/tables/tableSingleCells#.xml",
  124. SingleXmlCells.class
  125. );
  126. public static final XSSFRelation TABLE = new XSSFRelation(
  127. "application/vnd.openxmlformats-officedocument.spreadsheetml.table+xml",
  128. "http://schemas.openxmlformats.org/officeDocument/2006/relationships/table",
  129. "/tables/table#.xml",
  130. Table.class
  131. );
  132. public static final XSSFRelation IMAGES = new XSSFRelation(
  133. null,
  134. "http://schemas.openxmlformats.org/officeDocument/2006/relationships/image",
  135. null,
  136. XSSFPictureData.class
  137. );
  138. public static final XSSFRelation IMAGE_EMF = new XSSFRelation(
  139. "image/x-emf",
  140. "http://schemas.openxmlformats.org/officeDocument/2006/relationships/image",
  141. "/xl/media/image#.emf",
  142. XSSFPictureData.class
  143. );
  144. public static final XSSFRelation IMAGE_WMF = new XSSFRelation(
  145. "image/x-wmf",
  146. "http://schemas.openxmlformats.org/officeDocument/2006/relationships/image",
  147. "/xl/media/image#.wmf",
  148. XSSFPictureData.class
  149. );
  150. public static final XSSFRelation IMAGE_PICT = new XSSFRelation(
  151. "image/pict",
  152. "http://schemas.openxmlformats.org/officeDocument/2006/relationships/image",
  153. "/xl/media/image#.pict",
  154. XSSFPictureData.class
  155. );
  156. public static final XSSFRelation IMAGE_JPEG = new XSSFRelation(
  157. "image/jpeg",
  158. "http://schemas.openxmlformats.org/officeDocument/2006/relationships/image",
  159. "/xl/media/image#.jpeg",
  160. XSSFPictureData.class
  161. );
  162. public static final XSSFRelation IMAGE_PNG = new XSSFRelation(
  163. "image/png",
  164. "http://schemas.openxmlformats.org/officeDocument/2006/relationships/image",
  165. "/xl/media/image#.png",
  166. XSSFPictureData.class
  167. );
  168. public static final XSSFRelation IMAGE_DIB = new XSSFRelation(
  169. "image/dib",
  170. "http://schemas.openxmlformats.org/officeDocument/2006/relationships/image",
  171. "/xl/media/image#.dib",
  172. XSSFPictureData.class
  173. );
  174. public static final XSSFRelation SHEET_COMMENTS = new XSSFRelation(
  175. "application/vnd.openxmlformats-officedocument.spreadsheetml.comments+xml",
  176. "http://schemas.openxmlformats.org/officeDocument/2006/relationships/comments",
  177. "/xl/comments#.xml",
  178. CommentsTable.class
  179. );
  180. public static final XSSFRelation SHEET_HYPERLINKS = new XSSFRelation(
  181. null,
  182. "http://schemas.openxmlformats.org/officeDocument/2006/relationships/hyperlink",
  183. null,
  184. null
  185. );
  186. public static final XSSFRelation OLEEMBEDDINGS = new XSSFRelation(
  187. null,
  188. POIXMLDocument.OLE_OBJECT_REL_TYPE,
  189. null,
  190. null
  191. );
  192. public static final XSSFRelation PACKEMBEDDINGS = new XSSFRelation(
  193. null,
  194. POIXMLDocument.PACK_OBJECT_REL_TYPE,
  195. null,
  196. null
  197. );
  198. public static final XSSFRelation VBA_MACROS = new XSSFRelation(
  199. "application/vnd.ms-office.vbaProject",
  200. "http://schemas.microsoft.com/office/2006/relationships/vbaProject",
  201. "/xl/vbaProject.bin",
  202. null
  203. );
  204. public static final XSSFRelation ACTIVEX_CONTROLS = new XSSFRelation(
  205. "application/vnd.ms-office.activeX+xml",
  206. "http://schemas.openxmlformats.org/officeDocument/2006/relationships/control",
  207. "/xl/activeX/activeX#.xml",
  208. null
  209. );
  210. public static final XSSFRelation ACTIVEX_BINS = new XSSFRelation(
  211. "application/vnd.ms-office.activeX",
  212. "http://schemas.microsoft.com/office/2006/relationships/activeXControlBinary",
  213. "/xl/activeX/activeX#.bin",
  214. null
  215. );
  216. public static final XSSFRelation THEME = new XSSFRelation(
  217. "application/vnd.openxmlformats-officedocument.theme+xml",
  218. "http://schemas.openxmlformats.org/officeDocument/2006/relationships/theme",
  219. "/xl/theme/theme#.xml",
  220. null
  221. );
  222. public static final XSSFRelation CALC_CHAIN = new XSSFRelation(
  223. "application/vnd.openxmlformats-officedocument.spreadsheetml.calcChain+xml",
  224. "http://schemas.openxmlformats.org/officeDocument/2006/relationships/calcChain",
  225. "/xl/calcChain.xml",
  226. CalculationChain.class
  227. );
  228. private XSSFRelation(String type, String rel, String defaultName, Class<? extends POIXMLDocumentPart> cls) {
  229. super(type, rel, defaultName, cls);
  230. if(cls != null && !_table.containsKey(rel)) _table.put(rel, this);
  231. }
  232. /**
  233. * Fetches the InputStream to read the contents, based
  234. * of the specified core part, for which we are defined
  235. * as a suitable relationship
  236. */
  237. public InputStream getContents(PackagePart corePart) throws IOException, InvalidFormatException {
  238. PackageRelationshipCollection prc =
  239. corePart.getRelationshipsByType(_relation);
  240. Iterator<PackageRelationship> it = prc.iterator();
  241. if(it.hasNext()) {
  242. PackageRelationship rel = it.next();
  243. PackagePartName relName = PackagingURIHelper.createPartName(rel.getTargetURI());
  244. PackagePart part = corePart.getPackage().getPart(relName);
  245. return part.getInputStream();
  246. }
  247. log.log(POILogger.WARN, "No part " + _defaultName + " found");
  248. return null;
  249. }
  250. /**
  251. * Get POIXMLRelation by relation type
  252. *
  253. * @param rel relation type, for example,
  254. * <code>http://schemas.openxmlformats.org/officeDocument/2006/relationships/image</code>
  255. * @return registered POIXMLRelation or null if not found
  256. */
  257. public static XSSFRelation getInstance(String rel){
  258. return _table.get(rel);
  259. }
  260. }