Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

XSSFRelation.java 13KB

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