AnalyzerGuru.java revision 210
0N/A * The contents of this file are subject to the terms of the 0N/A * Common Development and Distribution License (the "License"). 0N/A * You may not use this file except in compliance with the License. 0N/A * language governing permissions and limitations under the License. 0N/A * When distributing Covered Code, include this CDDL HEADER in each 0N/A * If applicable, add the following below this CDDL HEADER, with the 0N/A * fields enclosed by brackets "[]" replaced with your own identifying 0N/A * information: Portions Copyright [yyyy] [name of copyright owner] 0N/A * Copyright 2007 Sun Microsystems, Inc. All rights reserved. 0N/A * Use is subject to license terms. 36N/A * Manages and porvides Analyzers as needed. Please see 36N/A * this</a> page for a great description of the purpose of the AnalyzerGuru. 36N/A * Created on September 22, 2005 36N/A /** The default {@code FileAnalyzerFactory} instance. */ 58N/A /** Map from file extensions to analyzer factories. */ 0N/A // TODO: have a comparator 0N/A /** Map from magic strings to analyzer factories. */ 36N/A * List of matcher objects which can be used to determine which analyzer 0N/A /** List of all registered {@code FileAnalyzerFactory} instances. */ 0N/A * If you write your own analyzer please register it here 0N/A * Register a {@code FileAnalyzerFactory} instance. 0N/A "suffix '" +
suffix +
"' used in multiple analyzers";
0N/A * Instruct the AnalyzerGuru to use a given analyzer for a given 0N/A * @param extension the file-extension to add 0N/A * @param factory a factory which creates 36N/A * the analyzer to use for the given extension 0N/A * (if you pass null as the analyzer, you will disable 0N/A * the analyzer used for that extension) 0N/A * Get the default Analyzer. 0N/A * Get an analyzer suited to analyze a file. This function will reuse 0N/A * analyzers since they are costly. 99N/A * @param in Input stream containing data to be analyzed 99N/A * @param file Name of the file to be analyzed 99N/A * @return An analyzer suited for that file content 0N/A * @throws java.io.IOException If an error occurs while accessing the 25N/A * data in the input stream. 58N/A * Create a Lucene document and fill in the required fields 58N/A * @param file The file to index 58N/A * @param in The data to generate the index for 58N/A * @param path Where the file is located (from source root) 58N/A * @return The Lucene document to add to the index database 58N/A * @throws java.io.IOException If an exception occurs while collecting the 0N/A // date = hr.getLastCommentDate() //RFE 0N/A // Ignoring any errors while analysing 0N/A * Get the content type for a named file. 0N/A * @param in The input stream we want to get the content type for (if 0N/A * we cannot determine the content type by the filename) 0N/A * @param file The name of the file 0N/A * @return The contentType suitable for printing to response.setContentType() 36N/A * @throws java.io.IOException If an error occurs while accessing the input 0N/A * Write a browsable version of the file 0N/A * @param factory The analyzer factory for this filetype 0N/A * @param in The input stream containing the data 0N/A * @param out Where to write the result 0N/A * @param annotation Annotation information for the file 0N/A * @throws java.io.IOException If an error occurs while creating the 0N/A * Get the genre of a file 0N/A * @param file The file to inpect 0N/A * @return The genre suitable to decide how to display the file 0N/A * Get the genre of a bulk of data 0N/A * @param in A stream containing the data 0N/A * @return The genre suitable to decide how to display the file 0N/A * @throws java.io.IOException If an error occurs while getting the content 0N/A * Get the genre for a named class (this is most likely an analyzer) 0N/A * @param factory the analyzer factory to get the genre for 0N/A * @return The genre of this class (null if not found) 0N/A * Find a {@code FileAnalyzerFactory} with the specified class name. If one 36N/A * doesn't exist, create one and register it. 0N/A * @param factoryClassName name of the factory class 0N/A * @return a file analyzer factory 0N/A * @throws ClassNotFoundException if there is no class with that name 0N/A * @throws ClassCastException if the class is not a subclass of {@code 0N/A * FileAnalyzerFactory} 0N/A * @throws IllegalAccessException if the constructor cannot be accessed 36N/A * @throws InstantiationException if the class cannot be instantiated 36N/A * Find a {@code FileAnalyzerFactory} which is an instance of the specified 58N/A * class. If one doesn't exist, create one and register it. 0N/A * @param factoryClass the factory class 0N/A * @return a file analyzer factory 0N/A * @throws ClassCastException if the class is not a subclass of {@code 0N/A * FileAnalyzerFactory} 36N/A * @throws IllegalAccessException if the constructor cannot be accessed 0N/A * @throws InstantiationException if the class cannot be instantiated 0N/A * Finds a suitable analyser class for file name. If the analyzer cannot 0N/A * be determined by the file extension, try to look at the data in the 0N/A * InputStream to find a suitable analyzer. 36N/A * Use if you just want to find file type. 0N/A * @param in The input stream containing the data 0N/A * @param file The file name to get the analyzer for 0N/A * @return the analyzer factory to use 0N/A * @throws java.io.IOException If a problem occurs while reading the data 0N/A * Finds a suitable analyser class for file name. 0N/A * @param file The file name to get the analyzer for 0N/A * @return the analyzer factory to use 0N/A // file doesn't have any of the extensions we know 0N/A * Finds a suitable analyser class for the data in this stream 0N/A * @param in The stream containing the data to analyze 0N/A * @return the analyzer factory to use 0N/A * @throws java.io.IOException if an error occurs while reading data from * Finds a suitable analyser class for a magic signature * @param signature the magic signature look up * @return the analyzer factory to use * Get an analyzer by looking up the "magic signature" * @param signature the signature to look up * @return the analyzer factory to handle data with this signature // See if text files have the magic sequence if we remove the /** Byte-order markers. */ new String(
new char[] {
0xEF,
0xBB,
0xBF }),
// UTF-8 BOM new String(
new char[] {
0xFE,
0xFF }),
// UTF-16BE BOM new String(
new char[] {
0xFF,
0xFE }),
// UTF-16LE BOM * Strip away the byte-order marker from the string, if it has one. * @param str the string to remove the BOM from * @return a string without the byte-order marker, or <code>null</code> if * the string doesn't start with a BOM