Skip to content

Commit 3364e1c

Browse files
author
Nguyen Nam Thai
committed
Initial commit for Apache Tika
1 parent 5d66bb2 commit 3364e1c

7 files changed

Lines changed: 178 additions & 1 deletion

File tree

‎apache-tika/.gitignore‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
*.docx
2+
temp.xls
3+
temp.xlsx

‎apache-tika/pom.xml‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
3+
<modelVersion>4.0.0</modelVersion>
4+
<groupId>com.baeldung</groupId>
5+
<artifactId>apache-tika</artifactId>
6+
<version>0.0.1-SNAPSHOT</version>
7+
8+
<parent>
9+
<groupId>com.baeldung</groupId>
10+
<artifactId>parent-modules</artifactId>
11+
<version>1.0.0-SNAPSHOT</version>
12+
</parent>
13+
14+
<properties>
15+
<tika.version>1.17</tika.version>
16+
</properties>
17+
18+
<dependencies>
19+
<dependency>
20+
<groupId>org.apache.tika</groupId>
21+
<artifactId>tika-parsers</artifactId>
22+
<version>${tika.version}</version>
23+
</dependency>
24+
</dependencies>
25+
</project>
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
package com.baeldung.tika;
2+
3+
import java.io.IOException;
4+
import java.io.InputStream;
5+
6+
import org.apache.tika.Tika;
7+
import org.apache.tika.detect.DefaultDetector;
8+
import org.apache.tika.detect.Detector;
9+
import org.apache.tika.exception.TikaException;
10+
import org.apache.tika.metadata.Metadata;
11+
import org.apache.tika.mime.MediaType;
12+
import org.apache.tika.parser.AutoDetectParser;
13+
import org.apache.tika.parser.ParseContext;
14+
import org.apache.tika.parser.Parser;
15+
import org.apache.tika.sax.BodyContentHandler;
16+
import org.xml.sax.ContentHandler;
17+
import org.xml.sax.SAXException;
18+
19+
public class TikaAnalysis {
20+
public static String detectDocTypeUsingDetector(InputStream stream) throws IOException {
21+
Detector detector = new DefaultDetector();
22+
Metadata metadata = new Metadata();
23+
24+
MediaType mediaType = detector.detect(stream, metadata);
25+
return mediaType.toString();
26+
}
27+
28+
public static String detectDocTypeUsingFacade(InputStream stream) throws IOException {
29+
Tika tika = new Tika();
30+
String mediaType = tika.detect(stream);
31+
return mediaType;
32+
}
33+
34+
public static String extractContentUsingParser(InputStream stream) throws IOException, TikaException, SAXException {
35+
Parser parser = new AutoDetectParser();
36+
ContentHandler handler = new BodyContentHandler();
37+
Metadata metadata = new Metadata();
38+
ParseContext context = new ParseContext();
39+
40+
parser.parse(stream, handler, metadata, context);
41+
return handler.toString();
42+
}
43+
44+
public static String extractContentUsingFacade(InputStream stream) throws IOException, TikaException {
45+
Tika tika = new Tika();
46+
String content = tika.parseToString(stream);
47+
return content;
48+
}
49+
50+
public static Metadata extractMetadatatUsingParser(InputStream stream) throws IOException, SAXException, TikaException {
51+
Parser parser = new AutoDetectParser();
52+
ContentHandler handler = new BodyContentHandler();
53+
Metadata metadata = new Metadata();
54+
ParseContext context = new ParseContext();
55+
56+
parser.parse(stream, handler, metadata, context);
57+
return metadata;
58+
}
59+
60+
public static Metadata extractMetadatatUsingFacade(InputStream stream) throws IOException, TikaException {
61+
Tika tika = new Tika();
62+
Metadata metadata = new Metadata();
63+
64+
tika.parse(stream, metadata);
65+
return metadata;
66+
}
67+
}
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
package com.baeldung.tika;
2+
3+
import static org.hamcrest.CoreMatchers.containsString;
4+
import static org.junit.Assert.assertEquals;
5+
import static org.junit.Assert.assertThat;
6+
7+
import java.io.IOException;
8+
import java.io.InputStream;
9+
10+
import org.apache.tika.exception.TikaException;
11+
import org.apache.tika.metadata.Metadata;
12+
import org.junit.Test;
13+
import org.xml.sax.SAXException;
14+
15+
public class TikaUnitTest {
16+
private ClassLoader loader = this.getClass().getClassLoader();
17+
18+
@Test
19+
public void whenUsingDetector_thenDocumentTypeIsReturned() throws IOException {
20+
InputStream stream = loader.getResourceAsStream("tika.txt");
21+
String mediaType = TikaAnalysis.detectDocTypeUsingDetector(stream);
22+
23+
assertEquals("application/pdf", mediaType);
24+
25+
stream.close();
26+
}
27+
28+
@Test
29+
public void whenUsingFacade_thenDocumentTypeIsReturned() throws IOException {
30+
InputStream stream = loader.getResourceAsStream("tika.txt");
31+
String mediaType = TikaAnalysis.detectDocTypeUsingFacade(stream);
32+
33+
assertEquals("application/pdf", mediaType);
34+
35+
stream.close();
36+
}
37+
38+
@Test
39+
public void whenUsingParser_thenContentIsReturned() throws IOException, TikaException, SAXException {
40+
InputStream stream = loader.getResourceAsStream("tika.docx");
41+
String content = TikaAnalysis.extractContentUsingParser(stream);
42+
43+
assertThat(content, containsString("Apache Tika - a content analysis toolkit"));
44+
assertThat(content, containsString("detects and extracts metadata and text"));
45+
46+
stream.close();
47+
}
48+
49+
@Test
50+
public void whenUsingFacade_thenContentIsReturned() throws IOException, TikaException {
51+
InputStream stream = loader.getResourceAsStream("tika.docx");
52+
String content = TikaAnalysis.extractContentUsingFacade(stream);
53+
54+
assertThat(content, containsString("Apache Tika - a content analysis toolkit"));
55+
assertThat(content, containsString("detects and extracts metadata and text"));
56+
57+
stream.close();
58+
}
59+
60+
@Test
61+
public void whenUsingParser_thenMetadataIsReturned() throws IOException, TikaException, SAXException {
62+
InputStream stream = loader.getResourceAsStream("tika.xlsx");
63+
Metadata metadata = TikaAnalysis.extractMetadatatUsingParser(stream);
64+
65+
assertEquals("org.apache.tika.parser.DefaultParser", metadata.get("X-Parsed-By"));
66+
assertEquals("Microsoft Office User", metadata.get("Author"));
67+
68+
stream.close();
69+
}
70+
71+
@Test
72+
public void whenUsingFacade_thenMetadataIsReturned() throws IOException, TikaException {
73+
InputStream stream = loader.getResourceAsStream("tika.xlsx");
74+
Metadata metadata = TikaAnalysis.extractMetadatatUsingFacade(stream);
75+
76+
assertEquals("org.apache.tika.parser.DefaultParser", metadata.get("X-Parsed-By"));
77+
assertEquals("Microsoft Office User", metadata.get("Author"));
78+
79+
stream.close();
80+
}
81+
}
35.5 KB
Binary file not shown.
8.15 KB
Binary file not shown.

‎pom.xml‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
<module>apache-cxf</module>
3939
<module>apache-fop</module>
4040
<module>apache-poi</module>
41+
<module>apache-tika</module>
4142
<module>apache-thrift</module>
4243
<module>autovalue</module>
4344
<module>axon</module>
@@ -383,4 +384,4 @@
383384
</extension>
384385
</extensions>
385386
</build>
386-
</project>
387+
</project>

0 commit comments

Comments
 (0)