ParseContext class is a component of the Java packages org.apache.tika.parser, which is used to parse context and pass it on to the Tika (The Apache Tika toolkit detecting and extracts the metadata and the text from over a thousand different file types) parsers org.apache.tika.parser.ParseContext implements a Serializable interface.
The parser’s context is yet another concept. An instance(object) of the Parse context class is created before a non-terminal starting the parsing and is destructed after parsing has concluded
The context is provided for extensibility. it has the main purpose is to expose the start and end of the non-terminal’s parse member function to accommodate external hooks. We can extend the non-terminal in a multitude of ways by writing specialized context classes, without modifying the class itself. For example, we can make the non-terminal emitting debug diagnostics information by writing for a context class that prints out the current state of the scanner at each point in the parse traversal where the non-terminal is invoked by it.
Class ParseContext- org.apache.tika.parser All Implemented Interfaces: Serializable Class ParseContext -java.lang.Object org.apache.tika.parser.ParseContext
The Parse context Use pass context information to Tika parsers
Syntax:
public class ParseContext extends Object implements Serializable
Constructor of Parse Context Class
1. ParseContext(): The Constructor ParseContext() initializes a new instance of The ParseContext class. For example:
ParseContext ab = new ParseContext()
Note: ab is the new instance of the ParseContext Class
Methods of Parse Context Class
Method | Description |
---|---|
get(Class<T> key) | Returning the object in this context that implements the given interface |
getDocumentBuilder() | Returning the DOM builder specified in this parsing context |
getSAXParser() | Returning the SAX parser specified in this parsing context |
getSAXParserFactory() | Returning the SAX parser factory specified in this parsing context |
getTransformer() | Returning the transformer specified in this parsing context |
getXMLInputFactory() | Returning the StAX input factory specified in this parsing context |
getXMLReader() | Returning the XMLReader specified in this parsing context |
set(Class<T> key, T value) | Adding the given value to context as an implementation of the given interfaces |
get(Class<T> key, T defaultValue) | Returning the objects in this context that implementing the given interface, or the given default value if such an object it is not found |
Example:
Java
// Java Program To Getting Content of Document // Using Tika Toolkit and ContextParser // Importing required classes import java.io.*; 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; // Importing necessary Tika packages to it import org.xml.sax.SAXException; // Main class class Cowin { // Main driver method public static void main(String[] args) { // Creating a File File fileName = new File( "abc.txt" ); // Instance of File created using new keyword FileInputStream fileInputStream = new FileInputStream(fileName); ParseContext parseContext = new ParseContext(); // new instance of parseContext class is created MetaData metaData = new MetaData(); // Instance of MetaData is created TXTParser textParser = new TXTParser(); // Instance of BodyContentHandler is created for it BodyContentHandler bodyContentHandler = new BodyContentHandler(); // TXTParser parse method is called for parsing to // it textParser.parse(fileInputStream, bodyContentHandler, metaData, parseContext); System.out.println( "Contents in the File=" + bodyContenthandler.toString()); } } |
Output:
Contents in the File= Cowin is the webportal for Vaccination
The abc.txt file contains the following data-