ParseContext class is a component of the Java package org.apache.tika.parser, which is used to parse context and pass it on to the Tika (The Apache Tika toolkit detects and extracts metadata and text from over a thousand different file types) parsers org.apache.tika.parser.ParseContext implements a Serializable interface.
public class ParseContext extends Object implements Serializable
Constructor:
1. ParseContext() – ParseContext() initialize a new instance of ParseContext class.
ParseContext p = new ParseContext()
Note: p is the new instance of ParseContext class.
Methods of ParseContext –
S.No. |
Method |
Description |
Return Type |
---|---|---|---|
1. |
getDocumentBuilder() | getDocumentBuilder() returns the DOM builder specified in this parsing context. | DocumentBuilder |
2. |
getSAXParser() | getSAXParser() returns the SAX parser specified in this parsing context. | SAXParser |
3. |
getSAXParserFactory() | getSAXParserFactory() returns the SAX parser factory specified in this parsing context. | SAXParserFactory |
4. |
getTransformer() | getTransformer() returns the transformer specified in this parsing context. | Transformer |
5. |
getXMLInputFactory() | getXMLInputFactory() returns the StAX input factory specified in this parsing context. | XMLInputFactory |
6. |
getXMLReader() | getXMLReader() returns the XMLReader specified in this parsing context. | XMLReader |
7. |
get(Class<T> key) | get(Class<T> key) returns the object in this context that implements the given interface. | <T> T |
8. |
get(Class<T> key, T defaultValue) | get(Class<T> key, T defaultValue) returns the object in this context that implements the given interface. | <T> T |
9. |
set(Class<T> key, T value) | set(Class<T> key, T value) adds the given value to the context to implement the given interface. | <T> void |
Example:
Java
// Java Program To Get Content of the // document using Tika Toolkit and // ContextParser: import java.io.*; // importing File class import java.io.File.*; import org.apache.tika.exception.TikaException; import org.apache.tika.metadata.Metadata; import org.apache.tika.parser.ParseContext; import org.apache.tika.parser.txt.TXTParser; import org.apache.tika.sax.BodyContentHandler; // import the necessary Tika packages import org.xml.sax.SAXException; class GFG { public static void main(String[] args) { // new instance of FIle is created File fileName = new File( "tmp.txt" ); // new instance of FileInputStream is created for // reading purpose FileInputStream fileInputStream = new FileInputStream(fileName); // new instance of parseContext class is created ParseContext parseContext = new ParseContext(); // new instance of MetaData is created MetaData metaData = new MetaData(); // new instance of TXTParser is created for plain // text parsing purpose TXTParser textParser = new TXTParser(); // new instance of BodyContentHandler is created BodyContentHandler bodyContentHandler = new BodyContentHandler(); // TXTParser parse method is called for parsing a // document stream into sequence of XHTML SAX events. textParser.parse(fileInputStream, bodyContentHandler, metaData, parseContext); System.out.println( "Contents of the document:" + bodyContenthandler.toString()); } } |
Output-
Contents of the document:GFG is the best website for programmer
Note – The tmp.txt file contains the following data.