Skip to content

Commit e10f5ee

Browse files
authored
Merge pull request #46 from levigo/feature/JS-2500_iwork_office_types_1
Feature/js 2500 iwork office types 1
2 parents a7fd282 + 398d3c7 commit e10f5ee

File tree

6 files changed

+69
-14
lines changed

6 files changed

+69
-14
lines changed

pom.xml

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -52,29 +52,29 @@
5252
<zip4j.version>2.11.5</zip4j.version>
5353
<slf4j.version>1.7.36</slf4j.version>
5454

55-
<poi.version>5.2.5</poi.version>
55+
<poi.version>5.3.0</poi.version>
5656
<rtfparserkit.version>1.16.0</rtfparserkit.version>
57-
<commons.io.version>2.16.1</commons.io.version>
58-
<commons-compress.version>1.27.0</commons-compress.version>
59-
<opennlp-tools.version>1.9.3</opennlp-tools.version>
57+
<commons.io.version>2.18.0</commons.io.version>
58+
<commons-compress.version>1.27.1</commons-compress.version>
59+
<opennlp-tools.version>1.9.4</opennlp-tools.version>
6060

61-
<pdfbox.version>2.0.31</pdfbox.version>
61+
<pdfbox.version>2.0.32</pdfbox.version>
6262

6363
<jaxb-api.version>2.3.1</jaxb-api.version>
64-
<jaxb-runtime.version>2.3.6</jaxb-runtime.version>
64+
<jaxb-runtime.version>2.3.9</jaxb-runtime.version>
6565

6666
<!-- testing -->
67-
<junit.version>5.11.0</junit.version>
67+
<junit.version>5.11.3</junit.version>
6868
<hamcrest.version>3.0</hamcrest.version>
6969
<jmock-junit5.version>2.13.1</jmock-junit5.version>
70-
<log4j.version>2.23.1</log4j.version>
70+
<log4j.version>2.24.2</log4j.version>
7171

7272
<!-- sonarcloud -->
7373
<sonar.projectKey>levigo_filetype-analyzer</sonar.projectKey>
7474
<sonar.organization>levigo</sonar.organization>
7575
<sonar.host.url>https://sonarcloud.io</sonar.host.url>
7676

77-
<bouncycastle.version>1.78.1</bouncycastle.version>
77+
<bouncycastle.version>1.79</bouncycastle.version>
7878
</properties>
7979

8080
<build>

src/main/java/org/jadice/filetype/matchers/OfficeOpenXMLMatcher.java

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,10 @@ public enum OfficeOpenType {
191191
"application/vnd.ms-visio.template.main+xml"),
192192
VISIO_TEMPLATE_MACRO_ENABLED("application/vnd.ms-visio.template.macroEnabled.12",
193193
"Microsoft Visio 2013 macro-enabled Template", "vstm",
194-
"application/vnd.ms-visio.template.macroEnabled.main+xml");
194+
"application/vnd.ms-visio.template.macroEnabled.main+xml"),
195+
APPLE_KEYNOTE("application/vnd.apple.keynote","Apple Keynote Document", "key") ,
196+
APPLE_NUMBERS("application/vnd.apple.numbers", "Apple Numbers Document", "numbers"),
197+
APPLE_PAGES("application/vnd.apple.pages", "Apple Pages Document", "pages"),;
195198

196199

197200
final String mimeType;
@@ -258,6 +261,12 @@ private void detect(final Context ctx, final ZipFile archive) throws IOException
258261
final SortedSet<Relationship> rels = getRelationships(archive);
259262
final List<ContentType> contentTypes = getContentTypes(archive);
260263
if (rels.isEmpty() || contentTypes.isEmpty()) {
264+
final OfficeOpenType type = findIWorkDocumentType(ctx, archive);
265+
if (type != null) {
266+
ctx.setProperty(MimeTypeAction.KEY, type.mimeType);
267+
ctx.setProperty(DescriptionAction.KEY, type.description);
268+
ctx.setProperty(ExtensionAction.KEY, type.extension);
269+
}
261270
return;
262271
}
263272

@@ -283,6 +292,27 @@ private void detect(final Context ctx, final ZipFile archive) throws IOException
283292

284293
}
285294

295+
private OfficeOpenType findIWorkDocumentType(final Context ctx, final ZipFile archive) throws IOException {
296+
final InputStream indexDocument = getSafeInputStream("Index/Document.iwa", archive);
297+
final InputStream metadataDocumentIdentifier = getSafeInputStream("Metadata/DocumentIdentifier", archive);
298+
OfficeOpenType mimeType = null;
299+
if (indexDocument != null && metadataDocumentIdentifier != null) {
300+
switch (ctx.getStatedExtension()){
301+
case "pages" :
302+
mimeType = OfficeOpenType.APPLE_PAGES;
303+
break;
304+
case "key" :
305+
mimeType = OfficeOpenType.APPLE_KEYNOTE;
306+
break;
307+
case "numbers" :
308+
mimeType = OfficeOpenType.APPLE_NUMBERS;
309+
break;
310+
default:
311+
}
312+
}
313+
return mimeType;
314+
}
315+
286316
private OfficeOpenType findDocumentType(final SortedSet<Relationship> rels, final List<ContentType> contentTypes) {
287317

288318
final Relationship mainDocRel = getMainDocument(rels);
@@ -354,7 +384,7 @@ private Map<String, Object> readXMLMetaData(final InputStream inputStream) throw
354384
if (n instanceof Element) {
355385
String key = n.getNodeName();
356386
String val = DOMUtil.getNodeText(n);
357-
if (val != null && val.length() > 0) {
387+
if (val != null && !val.isEmpty()) {
358388
// Ignore empty information
359389
result.put(key, val);
360390
}

src/test/java/TestVariousTypes.java

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,32 @@ void testEmptyStream() throws Exception {
6464
assertEquals("Binary data, ASCII Text Document", results.get(DescriptionAction.KEY));
6565
}
6666

67-
public static Stream<Arguments> dataProvider() {
67+
public static Stream<Arguments> dataProviderVarious() {
68+
return Stream.of(
69+
arguments("/various_types/iworks_pages_file.pages", "application/vnd.apple.pages", "Apple Pages Document", "pages"),
70+
arguments("/various_types/iworks_numbers_file.numbers", "application/vnd.apple.numbers", "Apple Numbers Document", "numbers"),
71+
arguments("/various_types/iworks_keynote_file.key", "application/vnd.apple.keynote", "Apple Keynote Document", "key")
72+
);
73+
}
74+
75+
@ParameterizedTest
76+
@MethodSource("dataProviderVarious")
77+
void testVariousTypesExplicitly(String resource, String expectedMimeType, String expectedDescription, String expectedExtension) throws Exception {
78+
final URL url = getClass().getResource(resource);
79+
assertNotNull(url);
80+
final File file = new File(url.toURI());
81+
final Map<String, Object> results = analyzer.analyze(file);
82+
printResult(results);
83+
assertNotNull(results, file + " could not be analyzed");
84+
assertNotNull(results.get(MimeTypeAction.KEY), "mimeType missing");
85+
assertEquals(expectedMimeType, results.get(MimeTypeAction.KEY), "wrong mimeType");
86+
assertNotNull(results.get(DescriptionAction.KEY), "description missing");
87+
assertEquals(expectedDescription, results.get(DescriptionAction.KEY), "wrong description");
88+
assertNotNull(results.get(ExtensionAction.KEY), "could not be analyzed");
89+
assertEquals(expectedExtension, results.get(ExtensionAction.KEY), "wrong extension");
90+
}
91+
92+
public static Stream<Arguments> dataProviderXRechnung() {
6893
return Stream.of(
6994
arguments("/various_types/BASIC_Einfach.pdf", "application/pdf"),
7095
arguments("/various_types/EN16931_Einfach.pdf", "application/pdf"),
@@ -75,7 +100,7 @@ public static Stream<Arguments> dataProvider() {
75100
}
76101

77102
@ParameterizedTest
78-
@MethodSource("dataProvider")
103+
@MethodSource("dataProviderXRechnung")
79104
void testXRechnung(String resource, String expectedMimeType) throws Exception {
80105
final URL url = getClass().getResource(resource);
81106
assertNotNull(url);
@@ -109,7 +134,7 @@ private void ensureXRechnungIsTrue(final Map<String, Object> results, final Stri
109134
}
110135

111136

112-
private static void printResult(final Map<String, Object> results) {
137+
public static void printResult(final Map<String, Object> results) {
113138
for (final Map.Entry<String, Object> e : results.entrySet()) {
114139
LOGGER.info(" {}={}", e.getKey(), e.getValue());
115140
}
454 KB
Binary file not shown.
134 KB
Binary file not shown.
89.5 KB
Binary file not shown.

0 commit comments

Comments
 (0)