Skip to content

Commit 95a37cc

Browse files
authored
Merge pull request eugenp#3777 from nguyennamthai/BAEL-1549
Bael 1549
2 parents dffd5fd + 734ace9 commit 95a37cc

7 files changed

Lines changed: 172 additions & 0 deletions

File tree

‎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: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
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+
@Test
17+
public void whenUsingDetector_thenDocumentTypeIsReturned() throws IOException {
18+
InputStream stream = this.getClass().getClassLoader().getResourceAsStream("tika.txt");
19+
String mediaType = TikaAnalysis.detectDocTypeUsingDetector(stream);
20+
21+
assertEquals("application/pdf", mediaType);
22+
23+
stream.close();
24+
}
25+
26+
@Test
27+
public void whenUsingFacade_thenDocumentTypeIsReturned() throws IOException {
28+
InputStream stream = this.getClass().getClassLoader().getResourceAsStream("tika.txt");
29+
String mediaType = TikaAnalysis.detectDocTypeUsingFacade(stream);
30+
31+
assertEquals("application/pdf", mediaType);
32+
33+
stream.close();
34+
}
35+
36+
@Test
37+
public void whenUsingParser_thenContentIsReturned() throws IOException, TikaException, SAXException {
38+
InputStream stream = this.getClass().getClassLoader().getResourceAsStream("tika.docx");
39+
String content = TikaAnalysis.extractContentUsingParser(stream);
40+
41+
assertThat(content, containsString("Apache Tika - a content analysis toolkit"));
42+
assertThat(content, containsString("detects and extracts metadata and text"));
43+
44+
stream.close();
45+
}
46+
47+
@Test
48+
public void whenUsingFacade_thenContentIsReturned() throws IOException, TikaException {
49+
InputStream stream = this.getClass().getClassLoader().getResourceAsStream("tika.docx");
50+
String content = TikaAnalysis.extractContentUsingFacade(stream);
51+
52+
assertThat(content, containsString("Apache Tika - a content analysis toolkit"));
53+
assertThat(content, containsString("detects and extracts metadata and text"));
54+
55+
stream.close();
56+
}
57+
58+
@Test
59+
public void whenUsingParser_thenMetadataIsReturned() throws IOException, TikaException, SAXException {
60+
InputStream stream = this.getClass().getClassLoader().getResourceAsStream("tika.xlsx");
61+
Metadata metadata = TikaAnalysis.extractMetadatatUsingParser(stream);
62+
63+
assertEquals("org.apache.tika.parser.DefaultParser", metadata.get("X-Parsed-By"));
64+
assertEquals("Microsoft Office User", metadata.get("Author"));
65+
66+
stream.close();
67+
}
68+
69+
@Test
70+
public void whenUsingFacade_thenMetadataIsReturned() throws IOException, TikaException {
71+
InputStream stream = this.getClass().getClassLoader().getResourceAsStream("tika.xlsx");
72+
Metadata metadata = TikaAnalysis.extractMetadatatUsingFacade(stream);
73+
74+
assertEquals("org.apache.tika.parser.DefaultParser", metadata.get("X-Parsed-By"));
75+
assertEquals("Microsoft Office User", metadata.get("Author"));
76+
77+
stream.close();
78+
}
79+
}
12.5 KB
Binary file not shown.
35.5 KB
Binary file not shown.
8.15 KB
Binary file not shown.

‎pom.xml‎

Lines changed: 1 addition & 0 deletions
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>apache-curator</module>
4344
<module>apache-zookeeper</module>

0 commit comments

Comments
 (0)