AnalyzerGuru.java revision 150
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License (the "License").
* You may not use this file except in compliance with the License.
*
* See LICENSE.txt included in this distribution for the specific
* language governing permissions and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at LICENSE.txt.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright 2007 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
/**
* Manages and porvides Analyzers as needed. Please see
* this</a> page for a great description of the purpose of the AnalyzerGuru.
*
* Created on September 22, 2005
* @author Chandan
*/
public class AnalyzerGuru {
/*
* If you write your own analyzer please register it here
*/
private static ArrayList<Class<? extends FileAnalyzer>> analyzers = new ArrayList<Class<? extends FileAnalyzer>>();
static {
}
private static HashMap<Class<? extends FileAnalyzer>, FileAnalyzer> analyzerInstances = new HashMap<Class<? extends FileAnalyzer>, FileAnalyzer>();
/**
* Initializes an AnalyzerGuru
*/
static {
}
// TODO: have a comparator
}
}
try {
//System.err.println(analyzer.getSimpleName() + " = " + suffix);
}
} catch (Exception e) {
// System.err.println("AnalyzerFinder:" + analyzer.getSimpleName() + e);
}
try {
//System.err.println(analyzer.getSimpleName() + " = " + magic);
}
} catch (Exception e) {
// System.err.println("AnalyzerFinder: " + analyzer.getSimpleName() + e);
}
try {
if (m != null) {
}
} catch (Exception e) {
}
}
//System.err.println("Exts " + ext);
//System.err.println("Matchers " + matchers);
}
/**
* Instruct the AnalyzerGuru to use a given analyzer for a given
* file extension.
* @param extension the file-extension to add
* @param analyzer the analyzer to use for the given extension
* (if you pass null as the analyzer, you will disable
* the analyzer used for that extension)
*/
}
}
/*
* Get the default Analyzer.
*/
public static FileAnalyzer getAnalyzer() {
try {
fa = a.newInstance();
return fa;
} catch (Exception e) {
}
}
return fa;
}
/**
* Get an analyzer suited to analyze a file. This function will reuse
* analyzers since they are costly.
*
* @param in Input stream containing data to be analyzed
* @param file Name of the file to be analyzed
* @return An analyzer suited for that file content
* @throws java.io.IOException If an error occurs while accessing the
* data in the input stream.
*/
if (a == null) {
a = FileAnalyzer.class;
}
if (a != null) {
try {
return fa;
} catch (Exception e) {
}
} else {
return fa;
}
}
return null;
}
/**
* Create a Lucene document and fill in the required fields
* @param file The file to index
* @param in The data to generate the index for
* @param path Where the file is located (from source root)
* @return The Lucene document to add to the index database
* @throws java.io.IOException If an exception occurs while collecting the
* datas
*/
try {
// date = hr.getLastCommentDate() //RFE
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
try {
} catch (Exception e) {
}
try {
}
} catch (Exception e) {
// Ignoring any errors while analysing
}
}
return doc;
}
/**
* Get the content type for a named file.
*
* @param file The file to get the content type for
* @return The contentType suitable for printing to response.setContentType()
*/
return getContentType(a);
}
/**
* Get the content type for a named file.
*
* @param in The input stream we want to get the content type for (if
* we cannot determine the content type by the filename)
* @param file The name of the file
* @return The contentType suitable for printing to response.setContentType()
* @throws java.io.IOException If an error occurs while accessing the input
* stream.
*/
return getContentType(a);
}
/**
* Get the content type the named analyzer accepts
* @param analyzer the analyzer to test
* @return the contentType suitable for printing to response.setContentType()
*/
try {
} catch (Exception e) {
}
}
return contentType;
}
/**
* Write a browsable version of the file
*
* @param analyzer The analyzer for this filetype
* @param in The input stream containing the data
* @param out Where to write the result
* @param annotation Annotation information for the file
* @throws java.io.IOException If an error occurs while creating the
* output
*/
public static void writeXref(Class<? extends FileAnalyzer> analyzer, InputStream in, Writer out, Annotation annotation) throws IOException {
try {
analyzer.getMethod("writeXref", InputStream.class, Writer.class, Annotation.class).invoke(null, in, out, annotation);
} catch (IllegalArgumentException ex) {
} catch (SecurityException ex) {
} catch (NoSuchMethodException ex) {
} catch (InvocationTargetException ex) {
} catch (IllegalAccessException ex) {
}
}
}
/**
* Get the genre of a file
*
* @param file The file to inpect
* @return The genre suitable to decide how to display the file
*/
return getGenre(a);
}
/**
* Get the genre of a file (or the content of the file)
*
* @param in The content of the file
* @param file The file to inpect
* @return The genre suitable to decide how to display the file
* @throws java.io.IOException If an error occurs while getting the content
* of the file
*/
return getGenre(a);
}
/**
* Get the genre of a bulk of data
*
* @param in A stream containing the data
* @return The genre suitable to decide how to display the file
* @throws java.io.IOException If an error occurs while getting the content
*/
return getGenre(a);
}
/**
* Get the genre for a named class (this is most likely an analyzer)
* @param clazz the class to get the genre for
* @return The genre of this class (null if not found)
*/
try {
} catch (Exception e) {
e.printStackTrace();
}
}
return g;
}
/**
* 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.
*
*
* @param in The input stream containing the data
* @param file The file name to get the analyzer for
* @return The analyzer to use
* @throws java.io.IOException If a problem occurs while reading the data
*/
if (a == null) {
}
return a;
}
/**
* Finds a suitable analyser class for file name.
*
* @param file The file name to get the analyzer for
* @return The analyzer to use
*/
int i = 0;
}
}
if (dotpos >= 0) {
//System.err.println(path.substring(dotpos+1).toUpperCase() + " = " + analyzer.getSimpleName());
return analyzer;
}
}
// file doesn't have any of the extensions we know
return null;
}
/**
* Finds a suitable analyser class for the data in this stream
*
* @param in The stream containing the data to analyze
* @return The analyzer to use
* @throws java.io.IOException if an error occurs while reading data from
* the stream
*/
byte[] content = new byte[8];
if (len < 4) {
return null;
}
if (a == null) {
try {
//System.out.println("USING = " + matcher.getName());
// cannot check conversion because of reflection
if (c != null) {
return c;
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
return a;
}
/**
* Finds a suitable analyser class for a magic signature
*
* @param signature the magic signature look up
* @return The analyzer to use
*/
}
}
/**
* Get an analyzer by looking up the "magic signature"
* @param signature the signature to look up
* @return The analyzer to handle data with this signature
*/
if (a == null) {
}
}
}
return a;
}
try {
if (field.isTokenized()) {
if (r == null) {
}
Token t;
}
}
} else {
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
}