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 15KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  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.HashMap;
  19. import java.util.Iterator;
  20. import java.util.Map;
  21. import org.apache.poi.POIXMLDocument;
  22. import org.apache.poi.POIXMLDocumentPart;
  23. import org.apache.poi.POIXMLRelation;
  24. import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
  25. import org.apache.poi.openxml4j.opc.PackagePart;
  26. import org.apache.poi.openxml4j.opc.PackagePartName;
  27. import org.apache.poi.openxml4j.opc.PackageRelationship;
  28. import org.apache.poi.openxml4j.opc.PackageRelationshipCollection;
  29. import org.apache.poi.openxml4j.opc.PackageRelationshipTypes;
  30. import org.apache.poi.openxml4j.opc.PackagingURIHelper;
  31. import org.apache.poi.util.POILogFactory;
  32. import org.apache.poi.util.POILogger;
  33. import org.apache.poi.xssf.model.CalculationChain;
  34. import org.apache.poi.xssf.model.CommentsTable;
  35. import org.apache.poi.xssf.model.ExternalLinksTable;
  36. import org.apache.poi.xssf.model.MapInfo;
  37. import org.apache.poi.xssf.model.SharedStringsTable;
  38. import org.apache.poi.xssf.model.SingleXmlCells;
  39. import org.apache.poi.xssf.model.StylesTable;
  40. import org.apache.poi.xssf.model.ThemesTable;
  41. /**
  42. *
  43. */
  44. public final class XSSFRelation extends POIXMLRelation {
  45. private static final POILogger log = POILogFactory.getLogger(XSSFRelation.class);
  46. /**
  47. * A map to lookup POIXMLRelation by its relation type
  48. */
  49. protected static final Map<String, XSSFRelation> _table = new HashMap<String, XSSFRelation>();
  50. public static final XSSFRelation WORKBOOK = new XSSFRelation(
  51. "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet.main+xml",
  52. "http://schemas.openxmlformats.org/officeDocument/2006/relationships/workbook",
  53. "/xl/workbook.xml",
  54. null
  55. );
  56. public static final XSSFRelation MACROS_WORKBOOK = new XSSFRelation(
  57. "application/vnd.ms-excel.sheet.macroEnabled.main+xml",
  58. PackageRelationshipTypes.CORE_DOCUMENT,
  59. "/xl/workbook.xml",
  60. null
  61. );
  62. public static final XSSFRelation TEMPLATE_WORKBOOK = new XSSFRelation(
  63. "application/vnd.openxmlformats-officedocument.spreadsheetml.template.main+xml",
  64. PackageRelationshipTypes.CORE_DOCUMENT,
  65. "/xl/workbook.xml",
  66. null
  67. );
  68. public static final XSSFRelation MACRO_TEMPLATE_WORKBOOK = new XSSFRelation(
  69. "application/vnd.ms-excel.template.macroEnabled.main+xml",
  70. PackageRelationshipTypes.CORE_DOCUMENT,
  71. "/xl/workbook.xml",
  72. null
  73. );
  74. public static final XSSFRelation MACRO_ADDIN_WORKBOOK = new XSSFRelation(
  75. "application/vnd.ms-excel.addin.macroEnabled.main+xml",
  76. PackageRelationshipTypes.CORE_DOCUMENT,
  77. "/xl/workbook.xml",
  78. null
  79. );
  80. public static final XSSFRelation XLSB_BINARY_WORKBOOK = new XSSFRelation(
  81. "application/vnd.ms-excel.sheet.binary.macroEnabled.main",
  82. PackageRelationshipTypes.CORE_DOCUMENT,
  83. "/xl/workbook.bin",
  84. null
  85. );
  86. public static final XSSFRelation WORKSHEET = new XSSFRelation(
  87. "application/vnd.openxmlformats-officedocument.spreadsheetml.worksheet+xml",
  88. "http://schemas.openxmlformats.org/officeDocument/2006/relationships/worksheet",
  89. "/xl/worksheets/sheet#.xml",
  90. XSSFSheet.class
  91. );
  92. public static final XSSFRelation CHARTSHEET = new XSSFRelation(
  93. "application/vnd.openxmlformats-officedocument.spreadsheetml.chartsheet+xml",
  94. "http://schemas.openxmlformats.org/officeDocument/2006/relationships/chartsheet",
  95. "/xl/chartsheets/sheet#.xml",
  96. XSSFChartSheet.class
  97. );
  98. public static final XSSFRelation SHARED_STRINGS = new XSSFRelation(
  99. "application/vnd.openxmlformats-officedocument.spreadsheetml.sharedStrings+xml",
  100. "http://schemas.openxmlformats.org/officeDocument/2006/relationships/sharedStrings",
  101. "/xl/sharedStrings.xml",
  102. SharedStringsTable.class
  103. );
  104. public static final XSSFRelation STYLES = new XSSFRelation(
  105. "application/vnd.openxmlformats-officedocument.spreadsheetml.styles+xml",
  106. PackageRelationshipTypes.STYLE_PART,
  107. "/xl/styles.xml",
  108. StylesTable.class
  109. );
  110. public static final XSSFRelation DRAWINGS = new XSSFRelation(
  111. "application/vnd.openxmlformats-officedocument.drawing+xml",
  112. "http://schemas.openxmlformats.org/officeDocument/2006/relationships/drawing",
  113. "/xl/drawings/drawing#.xml",
  114. XSSFDrawing.class
  115. );
  116. public static final XSSFRelation VML_DRAWINGS = new XSSFRelation(
  117. "application/vnd.openxmlformats-officedocument.vmlDrawing",
  118. "http://schemas.openxmlformats.org/officeDocument/2006/relationships/vmlDrawing",
  119. "/xl/drawings/vmlDrawing#.vml",
  120. XSSFVMLDrawing.class
  121. );
  122. public static final XSSFRelation CHART = new XSSFRelation(
  123. "application/vnd.openxmlformats-officedocument.drawingml.chart+xml",
  124. "http://schemas.openxmlformats.org/officeDocument/2006/relationships/chart",
  125. "/xl/charts/chart#.xml",
  126. XSSFChart.class
  127. );
  128. public static final XSSFRelation CUSTOM_XML_MAPPINGS = new XSSFRelation(
  129. "application/xml",
  130. "http://schemas.openxmlformats.org/officeDocument/2006/relationships/xmlMaps",
  131. "/xl/xmlMaps.xml",
  132. MapInfo.class
  133. );
  134. public static final XSSFRelation SINGLE_XML_CELLS = new XSSFRelation(
  135. "application/vnd.openxmlformats-officedocument.spreadsheetml.tableSingleCells+xml",
  136. "http://schemas.openxmlformats.org/officeDocument/2006/relationships/tableSingleCells",
  137. "/xl/tables/tableSingleCells#.xml",
  138. SingleXmlCells.class
  139. );
  140. public static final XSSFRelation TABLE = new XSSFRelation(
  141. "application/vnd.openxmlformats-officedocument.spreadsheetml.table+xml",
  142. "http://schemas.openxmlformats.org/officeDocument/2006/relationships/table",
  143. "/xl/tables/table#.xml",
  144. XSSFTable.class
  145. );
  146. public static final XSSFRelation IMAGES = new XSSFRelation(
  147. null,
  148. PackageRelationshipTypes.IMAGE_PART,
  149. null,
  150. XSSFPictureData.class
  151. );
  152. public static final XSSFRelation IMAGE_EMF = new XSSFRelation(
  153. "image/x-emf",
  154. PackageRelationshipTypes.IMAGE_PART,
  155. "/xl/media/image#.emf",
  156. XSSFPictureData.class
  157. );
  158. public static final XSSFRelation IMAGE_WMF = new XSSFRelation(
  159. "image/x-wmf",
  160. PackageRelationshipTypes.IMAGE_PART,
  161. "/xl/media/image#.wmf",
  162. XSSFPictureData.class
  163. );
  164. public static final XSSFRelation IMAGE_PICT = new XSSFRelation(
  165. "image/pict",
  166. PackageRelationshipTypes.IMAGE_PART,
  167. "/xl/media/image#.pict",
  168. XSSFPictureData.class
  169. );
  170. public static final XSSFRelation IMAGE_JPEG = new XSSFRelation(
  171. "image/jpeg",
  172. PackageRelationshipTypes.IMAGE_PART,
  173. "/xl/media/image#.jpeg",
  174. XSSFPictureData.class
  175. );
  176. public static final XSSFRelation IMAGE_PNG = new XSSFRelation(
  177. "image/png",
  178. PackageRelationshipTypes.IMAGE_PART,
  179. "/xl/media/image#.png",
  180. XSSFPictureData.class
  181. );
  182. public static final XSSFRelation IMAGE_DIB = new XSSFRelation(
  183. "image/dib",
  184. PackageRelationshipTypes.IMAGE_PART,
  185. "/xl/media/image#.dib",
  186. XSSFPictureData.class
  187. );
  188. public static final XSSFRelation IMAGE_GIF = new XSSFRelation(
  189. "image/gif",
  190. PackageRelationshipTypes.IMAGE_PART,
  191. "/xl/media/image#.gif",
  192. XSSFPictureData.class
  193. );
  194. public static final XSSFRelation IMAGE_TIFF = new XSSFRelation(
  195. "image/tiff",
  196. PackageRelationshipTypes.IMAGE_PART,
  197. "/xl/media/image#.tiff",
  198. XSSFPictureData.class
  199. );
  200. public static final XSSFRelation IMAGE_EPS = new XSSFRelation(
  201. "image/x-eps",
  202. PackageRelationshipTypes.IMAGE_PART,
  203. "/xl/media/image#.eps",
  204. XSSFPictureData.class
  205. );
  206. public static final XSSFRelation IMAGE_BMP = new XSSFRelation(
  207. "image/x-ms-bmp",
  208. PackageRelationshipTypes.IMAGE_PART,
  209. "/xl/media/image#.bmp",
  210. XSSFPictureData.class
  211. );
  212. public static final XSSFRelation IMAGE_WPG = new XSSFRelation(
  213. "image/x-wpg",
  214. PackageRelationshipTypes.IMAGE_PART,
  215. "/xl/media/image#.wpg",
  216. XSSFPictureData.class
  217. );
  218. public static final XSSFRelation SHEET_COMMENTS = new XSSFRelation(
  219. "application/vnd.openxmlformats-officedocument.spreadsheetml.comments+xml",
  220. "http://schemas.openxmlformats.org/officeDocument/2006/relationships/comments",
  221. "/xl/comments#.xml",
  222. CommentsTable.class
  223. );
  224. public static final XSSFRelation SHEET_HYPERLINKS = new XSSFRelation(
  225. null,
  226. PackageRelationshipTypes.HYPERLINK_PART,
  227. null,
  228. null
  229. );
  230. public static final XSSFRelation OLEEMBEDDINGS = new XSSFRelation(
  231. null,
  232. POIXMLDocument.OLE_OBJECT_REL_TYPE,
  233. null,
  234. null
  235. );
  236. public static final XSSFRelation PACKEMBEDDINGS = new XSSFRelation(
  237. null,
  238. POIXMLDocument.PACK_OBJECT_REL_TYPE,
  239. null,
  240. null
  241. );
  242. public static final XSSFRelation VBA_MACROS = new XSSFRelation(
  243. "application/vnd.ms-office.vbaProject",
  244. "http://schemas.microsoft.com/office/2006/relationships/vbaProject",
  245. "/xl/vbaProject.bin",
  246. XSSFVBAPart.class
  247. );
  248. public static final XSSFRelation ACTIVEX_CONTROLS = new XSSFRelation(
  249. "application/vnd.ms-office.activeX+xml",
  250. "http://schemas.openxmlformats.org/officeDocument/2006/relationships/control",
  251. "/xl/activeX/activeX#.xml",
  252. null
  253. );
  254. public static final XSSFRelation ACTIVEX_BINS = new XSSFRelation(
  255. "application/vnd.ms-office.activeX",
  256. "http://schemas.microsoft.com/office/2006/relationships/activeXControlBinary",
  257. "/xl/activeX/activeX#.bin",
  258. null
  259. );
  260. public static final XSSFRelation THEME = new XSSFRelation(
  261. "application/vnd.openxmlformats-officedocument.theme+xml",
  262. "http://schemas.openxmlformats.org/officeDocument/2006/relationships/theme",
  263. "/xl/theme/theme#.xml",
  264. ThemesTable.class
  265. );
  266. public static final XSSFRelation CALC_CHAIN = new XSSFRelation(
  267. "application/vnd.openxmlformats-officedocument.spreadsheetml.calcChain+xml",
  268. "http://schemas.openxmlformats.org/officeDocument/2006/relationships/calcChain",
  269. "/xl/calcChain.xml",
  270. CalculationChain.class
  271. );
  272. public static final XSSFRelation EXTERNAL_LINKS = new XSSFRelation(
  273. "application/vnd.openxmlformats-officedocument.spreadsheetml.externalLink+xml",
  274. "http://schemas.openxmlformats.org/officeDocument/2006/relationships/externalLink",
  275. "/xl/externalLinks/externalLink#.xmll",
  276. ExternalLinksTable.class
  277. );
  278. public static final XSSFRelation PRINTER_SETTINGS = new XSSFRelation(
  279. "application/vnd.openxmlformats-officedocument.spreadsheetml.printerSettings",
  280. "http://schemas.openxmlformats.org/officeDocument/2006/relationships/printerSettings",
  281. "/xl/printerSettings/printerSettings#.bin",
  282. null
  283. );
  284. public static final XSSFRelation PIVOT_TABLE = new XSSFRelation(
  285. "application/vnd.openxmlformats-officedocument.spreadsheetml.pivotTable+xml",
  286. "http://schemas.openxmlformats.org/officeDocument/2006/relationships/pivotTable",
  287. "/xl/pivotTables/pivotTable#.xml",
  288. XSSFPivotTable.class
  289. );
  290. public static final XSSFRelation PIVOT_CACHE_DEFINITION = new XSSFRelation(
  291. "application/vnd.openxmlformats-officedocument.spreadsheetml.pivotCacheDefinition+xml",
  292. "http://schemas.openxmlformats.org/officeDocument/2006/relationships/pivotCacheDefinition",
  293. "/xl/pivotCache/pivotCacheDefinition#.xml",
  294. XSSFPivotCacheDefinition.class
  295. );
  296. public static final XSSFRelation PIVOT_CACHE_RECORDS = new XSSFRelation(
  297. "application/vnd.openxmlformats-officedocument.spreadsheetml.pivotCacheRecords+xml",
  298. "http://schemas.openxmlformats.org/officeDocument/2006/relationships/pivotCacheRecords",
  299. "/xl/pivotCache/pivotCacheRecords#.xml",
  300. XSSFPivotCacheRecords.class
  301. );
  302. public static final XSSFRelation CTRL_PROP_RECORDS = new XSSFRelation(
  303. null,
  304. "http://schemas.openxmlformats.org/officeDocument/2006/relationships/ctrlProp",
  305. "/xl/ctrlProps/ctrlProp#.xml",
  306. null
  307. );
  308. private XSSFRelation(String type, String rel, String defaultName, Class<? extends POIXMLDocumentPart> cls) {
  309. super(type, rel, defaultName, cls);
  310. _table.put(rel, this);
  311. }
  312. /**
  313. * Fetches the InputStream to read the contents, based
  314. * of the specified core part, for which we are defined
  315. * as a suitable relationship
  316. */
  317. public InputStream getContents(PackagePart corePart) throws IOException, InvalidFormatException {
  318. PackageRelationshipCollection prc =
  319. corePart.getRelationshipsByType(_relation);
  320. Iterator<PackageRelationship> it = prc.iterator();
  321. if(it.hasNext()) {
  322. PackageRelationship rel = it.next();
  323. PackagePartName relName = PackagingURIHelper.createPartName(rel.getTargetURI());
  324. PackagePart part = corePart.getPackage().getPart(relName);
  325. return part.getInputStream();
  326. }
  327. log.log(POILogger.WARN, "No part " + _defaultName + " found");
  328. return null;
  329. }
  330. /**
  331. * Get POIXMLRelation by relation type
  332. *
  333. * @param rel relation type, for example,
  334. * <code>http://schemas.openxmlformats.org/officeDocument/2006/relationships/image</code>
  335. * @return registered POIXMLRelation or null if not found
  336. */
  337. public static XSSFRelation getInstance(String rel){
  338. return _table.get(rel);
  339. }
  340. }