org.opensolaris.opengrok.analysis
Class AnalyzerGuru

java.lang.Object
  extended by org.opensolaris.opengrok.analysis.AnalyzerGuru

public class AnalyzerGuru
extends java.lang.Object

Manages and porvides Analyzers as needed. Please see this page for a great description of the purpose of the AnalyzerGuru. Created on September 22, 2005


Constructor Summary
AnalyzerGuru()
           
 
Method Summary
static void addExtension(java.lang.String extension, FileAnalyzerFactory factory)
          Instruct the AnalyzerGuru to use a given analyzer for a given file extension.
static FileAnalyzerFactory find(byte[] signature)
          Finds a suitable analyser class for a magic signature
static FileAnalyzerFactory find(java.io.InputStream in)
          Finds a suitable analyser class for the data in this stream
static FileAnalyzerFactory find(java.io.InputStream in, java.lang.String file)
          Finds a suitable analyser class for file name.
static FileAnalyzerFactory find(java.lang.String file)
          Finds a suitable analyser class for file name.
static FileAnalyzerFactory findFactory(java.lang.String factoryClassName)
          Find a FileAnalyzerFactory with the specified class name.
static FileAnalyzerFactory findMagic(java.lang.String signature)
          Get an analyzer by looking up the "magic signature"
static FileAnalyzer getAnalyzer()
          Get the default Analyzer.
static FileAnalyzer getAnalyzer(java.io.InputStream in, java.lang.String file)
          Get an analyzer suited to analyze a file.
static java.lang.String getContentType(java.io.InputStream in, java.lang.String file)
          Get the content type for a named file.
 org.apache.lucene.document.Document getDocument(java.io.File file, java.io.InputStream in, java.lang.String path, FileAnalyzer fa)
          Create a Lucene document and fill in the required fields
static FileAnalyzer.Genre getGenre(FileAnalyzerFactory factory)
          Get the genre for a named class (this is most likely an analyzer)
static FileAnalyzer.Genre getGenre(java.io.InputStream in)
          Get the genre of a bulk of data
static FileAnalyzer.Genre getGenre(java.lang.String file)
          Get the genre of a file
static void main(java.lang.String[] args)
           
static void writeXref(FileAnalyzerFactory factory, java.io.InputStream in, java.io.Writer out, Annotation annotation, Project project)
          Write a browsable version of the file
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

AnalyzerGuru

public AnalyzerGuru()
Method Detail

addExtension

public static void addExtension(java.lang.String extension,
                                FileAnalyzerFactory factory)
Instruct the AnalyzerGuru to use a given analyzer for a given file extension.

Parameters:
extension - the file-extension to add
factory - a factory which creates the analyzer to use for the given extension (if you pass null as the analyzer, you will disable the analyzer used for that extension)

getAnalyzer

public static FileAnalyzer getAnalyzer()
Get the default Analyzer.


getAnalyzer

public static FileAnalyzer getAnalyzer(java.io.InputStream in,
                                       java.lang.String file)
                                throws java.io.IOException
Get an analyzer suited to analyze a file. This function will reuse analyzers since they are costly.

Parameters:
in - Input stream containing data to be analyzed
file - Name of the file to be analyzed
Returns:
An analyzer suited for that file content
Throws:
java.io.IOException - If an error occurs while accessing the data in the input stream.

getDocument

public org.apache.lucene.document.Document getDocument(java.io.File file,
                                                       java.io.InputStream in,
                                                       java.lang.String path,
                                                       FileAnalyzer fa)
                                                throws java.io.IOException
Create a Lucene document and fill in the required fields

Parameters:
file - The file to index
in - The data to generate the index for
path - Where the file is located (from source root)
Returns:
The Lucene document to add to the index database
Throws:
java.io.IOException - If an exception occurs while collecting the datas

getContentType

public static java.lang.String getContentType(java.io.InputStream in,
                                              java.lang.String file)
                                       throws java.io.IOException
Get the content type for a named file.

Parameters:
in - The input stream we want to get the content type for (if we cannot determine the content type by the filename)
file - The name of the file
Returns:
The contentType suitable for printing to response.setContentType() or null if the factory was not found
Throws:
java.io.IOException - If an error occurs while accessing the input stream.

writeXref

public static void writeXref(FileAnalyzerFactory factory,
                             java.io.InputStream in,
                             java.io.Writer out,
                             Annotation annotation,
                             Project project)
                      throws java.io.IOException
Write a browsable version of the file

Parameters:
factory - The analyzer factory for this filetype
in - The input stream containing the data
out - Where to write the result
annotation - Annotation information for the file
project - Project the file belongs to
Throws:
java.io.IOException - If an error occurs while creating the output

getGenre

public static FileAnalyzer.Genre getGenre(java.lang.String file)
Get the genre of a file

Parameters:
file - The file to inpect
Returns:
The genre suitable to decide how to display the file

getGenre

public static FileAnalyzer.Genre getGenre(java.io.InputStream in)
                                   throws java.io.IOException
Get the genre of a bulk of data

Parameters:
in - A stream containing the data
Returns:
The genre suitable to decide how to display the file
Throws:
java.io.IOException - If an error occurs while getting the content

getGenre

public static FileAnalyzer.Genre getGenre(FileAnalyzerFactory factory)
Get the genre for a named class (this is most likely an analyzer)

Parameters:
factory - the analyzer factory to get the genre for
Returns:
The genre of this class (null if not found)

findFactory

public static FileAnalyzerFactory findFactory(java.lang.String factoryClassName)
                                       throws java.lang.ClassNotFoundException,
                                              java.lang.IllegalAccessException,
                                              java.lang.InstantiationException
Find a FileAnalyzerFactory with the specified class name. If one doesn't exist, create one and register it.

Parameters:
factoryClassName - name of the factory class
Returns:
a file analyzer factory
Throws:
java.lang.ClassNotFoundException - if there is no class with that name
java.lang.ClassCastException - if the class is not a subclass of FileAnalyzerFactory
java.lang.IllegalAccessException - if the constructor cannot be accessed
java.lang.InstantiationException - if the class cannot be instantiated

find

public static FileAnalyzerFactory find(java.io.InputStream in,
                                       java.lang.String file)
                                throws java.io.IOException
Finds a suitable analyser class for file name. If the analyzer cannot be determined by the file extension, try to look at the data in the InputStream to find a suitable analyzer. Use if you just want to find file type.

Parameters:
in - The input stream containing the data
file - The file name to get the analyzer for
Returns:
the analyzer factory to use
Throws:
java.io.IOException - If a problem occurs while reading the data

find

public static FileAnalyzerFactory find(java.lang.String file)
Finds a suitable analyser class for file name.

Parameters:
file - The file name to get the analyzer for
Returns:
the analyzer factory to use

find

public static FileAnalyzerFactory find(java.io.InputStream in)
                                throws java.io.IOException
Finds a suitable analyser class for the data in this stream

Parameters:
in - The stream containing the data to analyze
Returns:
the analyzer factory to use
Throws:
java.io.IOException - if an error occurs while reading data from the stream

find

public static FileAnalyzerFactory find(byte[] signature)
Finds a suitable analyser class for a magic signature

Parameters:
signature - the magic signature look up
Returns:
the analyzer factory to use

findMagic

public static FileAnalyzerFactory findMagic(java.lang.String signature)
Get an analyzer by looking up the "magic signature"

Parameters:
signature - the signature to look up
Returns:
the analyzer factory to handle data with this signature

main

public static void main(java.lang.String[] args)
                 throws java.lang.Exception
Throws:
java.lang.Exception