1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
|
/* ====================================================================
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
==================================================================== */
package org.apache.poi.xssf.usermodel;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import org.apache.poi.POIXMLDocument;
import org.apache.poi.POIXMLDocumentPart;
import org.apache.poi.POIXMLRelation;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.openxml4j.opc.PackagePart;
import org.apache.poi.openxml4j.opc.PackagePartName;
import org.apache.poi.openxml4j.opc.PackageRelationship;
import org.apache.poi.openxml4j.opc.PackageRelationshipCollection;
import org.apache.poi.openxml4j.opc.PackageRelationshipTypes;
import org.apache.poi.openxml4j.opc.PackagingURIHelper;
import org.apache.poi.util.POILogFactory;
import org.apache.poi.util.POILogger;
import org.apache.poi.xssf.model.CalculationChain;
import org.apache.poi.xssf.model.CommentsTable;
import org.apache.poi.xssf.model.ExternalLinksTable;
import org.apache.poi.xssf.model.MapInfo;
import org.apache.poi.xssf.model.SharedStringsTable;
import org.apache.poi.xssf.model.SingleXmlCells;
import org.apache.poi.xssf.model.StylesTable;
import org.apache.poi.xssf.model.ThemesTable;
/**
* Defines namespaces, content types and normal file names / naming
* patterns, for the well-known XSSF format parts.
*/
public final class XSSFRelation extends POIXMLRelation {
private static final POILogger log = POILogFactory.getLogger(XSSFRelation.class);
/**
* A map to lookup POIXMLRelation by its relation type
*/
private static final Map<String, XSSFRelation> _table = new HashMap<String, XSSFRelation>();
public static final XSSFRelation WORKBOOK = new XSSFRelation(
"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet.main+xml",
"http://schemas.openxmlformats.org/officeDocument/2006/relationships/workbook",
"/xl/workbook.xml",
null
);
public static final XSSFRelation MACROS_WORKBOOK = new XSSFRelation(
"application/vnd.ms-excel.sheet.macroEnabled.main+xml",
PackageRelationshipTypes.CORE_DOCUMENT,
"/xl/workbook.xml",
null
);
public static final XSSFRelation TEMPLATE_WORKBOOK = new XSSFRelation(
"application/vnd.openxmlformats-officedocument.spreadsheetml.template.main+xml",
PackageRelationshipTypes.CORE_DOCUMENT,
"/xl/workbook.xml",
null
);
public static final XSSFRelation MACRO_TEMPLATE_WORKBOOK = new XSSFRelation(
"application/vnd.ms-excel.template.macroEnabled.main+xml",
PackageRelationshipTypes.CORE_DOCUMENT,
"/xl/workbook.xml",
null
);
public static final XSSFRelation MACRO_ADDIN_WORKBOOK = new XSSFRelation(
"application/vnd.ms-excel.addin.macroEnabled.main+xml",
PackageRelationshipTypes.CORE_DOCUMENT,
"/xl/workbook.xml",
null
);
public static final XSSFRelation XLSB_BINARY_WORKBOOK = new XSSFRelation(
"application/vnd.ms-excel.sheet.binary.macroEnabled.main",
PackageRelationshipTypes.CORE_DOCUMENT,
"/xl/workbook.bin",
null
);
public static final XSSFRelation WORKSHEET = new XSSFRelation(
"application/vnd.openxmlformats-officedocument.spreadsheetml.worksheet+xml",
"http://schemas.openxmlformats.org/officeDocument/2006/relationships/worksheet",
"/xl/worksheets/sheet#.xml",
XSSFSheet.class
);
public static final XSSFRelation CHARTSHEET = new XSSFRelation(
"application/vnd.openxmlformats-officedocument.spreadsheetml.chartsheet+xml",
"http://schemas.openxmlformats.org/officeDocument/2006/relationships/chartsheet",
"/xl/chartsheets/sheet#.xml",
XSSFChartSheet.class
);
public static final XSSFRelation SHARED_STRINGS = new XSSFRelation(
"application/vnd.openxmlformats-officedocument.spreadsheetml.sharedStrings+xml",
"http://schemas.openxmlformats.org/officeDocument/2006/relationships/sharedStrings",
"/xl/sharedStrings.xml",
SharedStringsTable.class
);
public static final XSSFRelation STYLES = new XSSFRelation(
"application/vnd.openxmlformats-officedocument.spreadsheetml.styles+xml",
PackageRelationshipTypes.STYLE_PART,
"/xl/styles.xml",
StylesTable.class
);
public static final XSSFRelation DRAWINGS = new XSSFRelation(
"application/vnd.openxmlformats-officedocument.drawing+xml",
"http://schemas.openxmlformats.org/officeDocument/2006/relationships/drawing",
"/xl/drawings/drawing#.xml",
XSSFDrawing.class
);
public static final XSSFRelation VML_DRAWINGS = new XSSFRelation(
"application/vnd.openxmlformats-officedocument.vmlDrawing",
"http://schemas.openxmlformats.org/officeDocument/2006/relationships/vmlDrawing",
"/xl/drawings/vmlDrawing#.vml",
XSSFVMLDrawing.class
);
public static final XSSFRelation CHART = new XSSFRelation(
"application/vnd.openxmlformats-officedocument.drawingml.chart+xml",
"http://schemas.openxmlformats.org/officeDocument/2006/relationships/chart",
"/xl/charts/chart#.xml",
XSSFChart.class
);
public static final XSSFRelation CUSTOM_XML_MAPPINGS = new XSSFRelation(
"application/xml",
"http://schemas.openxmlformats.org/officeDocument/2006/relationships/xmlMaps",
"/xl/xmlMaps.xml",
MapInfo.class
);
public static final XSSFRelation SINGLE_XML_CELLS = new XSSFRelation(
"application/vnd.openxmlformats-officedocument.spreadsheetml.tableSingleCells+xml",
"http://schemas.openxmlformats.org/officeDocument/2006/relationships/tableSingleCells",
"/xl/tables/tableSingleCells#.xml",
SingleXmlCells.class
);
public static final XSSFRelation TABLE = new XSSFRelation(
"application/vnd.openxmlformats-officedocument.spreadsheetml.table+xml",
"http://schemas.openxmlformats.org/officeDocument/2006/relationships/table",
"/xl/tables/table#.xml",
XSSFTable.class
);
public static final XSSFRelation IMAGES = new XSSFRelation(
null,
PackageRelationshipTypes.IMAGE_PART,
null,
XSSFPictureData.class
);
public static final XSSFRelation IMAGE_EMF = new XSSFRelation(
"image/x-emf",
PackageRelationshipTypes.IMAGE_PART,
"/xl/media/image#.emf",
XSSFPictureData.class
);
public static final XSSFRelation IMAGE_WMF = new XSSFRelation(
"image/x-wmf",
PackageRelationshipTypes.IMAGE_PART,
"/xl/media/image#.wmf",
XSSFPictureData.class
);
public static final XSSFRelation IMAGE_PICT = new XSSFRelation(
"image/pict",
PackageRelationshipTypes.IMAGE_PART,
"/xl/media/image#.pict",
XSSFPictureData.class
);
public static final XSSFRelation IMAGE_JPEG = new XSSFRelation(
"image/jpeg",
PackageRelationshipTypes.IMAGE_PART,
"/xl/media/image#.jpeg",
XSSFPictureData.class
);
public static final XSSFRelation IMAGE_PNG = new XSSFRelation(
"image/png",
PackageRelationshipTypes.IMAGE_PART,
"/xl/media/image#.png",
XSSFPictureData.class
);
public static final XSSFRelation IMAGE_DIB = new XSSFRelation(
"image/dib",
PackageRelationshipTypes.IMAGE_PART,
"/xl/media/image#.dib",
XSSFPictureData.class
);
public static final XSSFRelation IMAGE_GIF = new XSSFRelation(
"image/gif",
PackageRelationshipTypes.IMAGE_PART,
"/xl/media/image#.gif",
XSSFPictureData.class
);
public static final XSSFRelation IMAGE_TIFF = new XSSFRelation(
"image/tiff",
PackageRelationshipTypes.IMAGE_PART,
"/xl/media/image#.tiff",
XSSFPictureData.class
);
public static final XSSFRelation IMAGE_EPS = new XSSFRelation(
"image/x-eps",
PackageRelationshipTypes.IMAGE_PART,
"/xl/media/image#.eps",
XSSFPictureData.class
);
public static final XSSFRelation IMAGE_BMP = new XSSFRelation(
"image/x-ms-bmp",
PackageRelationshipTypes.IMAGE_PART,
"/xl/media/image#.bmp",
XSSFPictureData.class
);
public static final XSSFRelation IMAGE_WPG = new XSSFRelation(
"image/x-wpg",
PackageRelationshipTypes.IMAGE_PART,
"/xl/media/image#.wpg",
XSSFPictureData.class
);
public static final XSSFRelation SHEET_COMMENTS = new XSSFRelation(
"application/vnd.openxmlformats-officedocument.spreadsheetml.comments+xml",
"http://schemas.openxmlformats.org/officeDocument/2006/relationships/comments",
"/xl/comments#.xml",
CommentsTable.class
);
public static final XSSFRelation SHEET_HYPERLINKS = new XSSFRelation(
null,
PackageRelationshipTypes.HYPERLINK_PART,
null,
null
);
public static final XSSFRelation OLEEMBEDDINGS = new XSSFRelation(
null,
POIXMLDocument.OLE_OBJECT_REL_TYPE,
null,
null
);
public static final XSSFRelation PACKEMBEDDINGS = new XSSFRelation(
null,
POIXMLDocument.PACK_OBJECT_REL_TYPE,
null,
null
);
public static final XSSFRelation VBA_MACROS = new XSSFRelation(
"application/vnd.ms-office.vbaProject",
"http://schemas.microsoft.com/office/2006/relationships/vbaProject",
"/xl/vbaProject.bin",
XSSFVBAPart.class
);
public static final XSSFRelation ACTIVEX_CONTROLS = new XSSFRelation(
"application/vnd.ms-office.activeX+xml",
"http://schemas.openxmlformats.org/officeDocument/2006/relationships/control",
"/xl/activeX/activeX#.xml",
null
);
public static final XSSFRelation ACTIVEX_BINS = new XSSFRelation(
"application/vnd.ms-office.activeX",
"http://schemas.microsoft.com/office/2006/relationships/activeXControlBinary",
"/xl/activeX/activeX#.bin",
null
);
public static final XSSFRelation THEME = new XSSFRelation(
"application/vnd.openxmlformats-officedocument.theme+xml",
"http://schemas.openxmlformats.org/officeDocument/2006/relationships/theme",
"/xl/theme/theme#.xml",
ThemesTable.class
);
public static final XSSFRelation CALC_CHAIN = new XSSFRelation(
"application/vnd.openxmlformats-officedocument.spreadsheetml.calcChain+xml",
"http://schemas.openxmlformats.org/officeDocument/2006/relationships/calcChain",
"/xl/calcChain.xml",
CalculationChain.class
);
public static final XSSFRelation EXTERNAL_LINKS = new XSSFRelation(
"application/vnd.openxmlformats-officedocument.spreadsheetml.externalLink+xml",
"http://schemas.openxmlformats.org/officeDocument/2006/relationships/externalLink",
"/xl/externalLinks/externalLink#.xmll",
ExternalLinksTable.class
);
public static final XSSFRelation PRINTER_SETTINGS = new XSSFRelation(
"application/vnd.openxmlformats-officedocument.spreadsheetml.printerSettings",
"http://schemas.openxmlformats.org/officeDocument/2006/relationships/printerSettings",
"/xl/printerSettings/printerSettings#.bin",
null
);
public static final XSSFRelation PIVOT_TABLE = new XSSFRelation(
"application/vnd.openxmlformats-officedocument.spreadsheetml.pivotTable+xml",
"http://schemas.openxmlformats.org/officeDocument/2006/relationships/pivotTable",
"/xl/pivotTables/pivotTable#.xml",
XSSFPivotTable.class
);
public static final XSSFRelation PIVOT_CACHE_DEFINITION = new XSSFRelation(
"application/vnd.openxmlformats-officedocument.spreadsheetml.pivotCacheDefinition+xml",
"http://schemas.openxmlformats.org/officeDocument/2006/relationships/pivotCacheDefinition",
"/xl/pivotCache/pivotCacheDefinition#.xml",
XSSFPivotCacheDefinition.class
);
public static final XSSFRelation PIVOT_CACHE_RECORDS = new XSSFRelation(
"application/vnd.openxmlformats-officedocument.spreadsheetml.pivotCacheRecords+xml",
"http://schemas.openxmlformats.org/officeDocument/2006/relationships/pivotCacheRecords",
"/xl/pivotCache/pivotCacheRecords#.xml",
XSSFPivotCacheRecords.class
);
public static final XSSFRelation CTRL_PROP_RECORDS = new XSSFRelation(
null,
"http://schemas.openxmlformats.org/officeDocument/2006/relationships/ctrlProp",
"/xl/ctrlProps/ctrlProp#.xml",
null
);
public static final String NS_SPREADSHEETML = "http://schemas.openxmlformats.org/spreadsheetml/2006/main";
public static final String NS_DRAWINGML = "http://schemas.openxmlformats.org/drawingml/2006/main";
public static final String NS_CHART = "http://schemas.openxmlformats.org/drawingml/2006/chart";
private XSSFRelation(String type, String rel, String defaultName, Class<? extends POIXMLDocumentPart> cls) {
super(type, rel, defaultName, cls);
_table.put(rel, this);
}
/**
* Fetches the InputStream to read the contents, based
* of the specified core part, for which we are defined
* as a suitable relationship
*/
public InputStream getContents(PackagePart corePart) throws IOException, InvalidFormatException {
PackageRelationshipCollection prc =
corePart.getRelationshipsByType(_relation);
Iterator<PackageRelationship> it = prc.iterator();
if(it.hasNext()) {
PackageRelationship rel = it.next();
PackagePartName relName = PackagingURIHelper.createPartName(rel.getTargetURI());
PackagePart part = corePart.getPackage().getPart(relName);
return part.getInputStream();
}
log.log(POILogger.WARN, "No part " + _defaultName + " found");
return null;
}
/**
* Get POIXMLRelation by relation type
*
* @param rel relation type, for example,
* <code>http://schemas.openxmlformats.org/officeDocument/2006/relationships/image</code>
* @return registered POIXMLRelation or null if not found
*/
public static XSSFRelation getInstance(String rel){
return _table.get(rel);
}
}
|