FileAnalyzer.java revision 99
2026N/A/*
2026N/A * CDDL HEADER START
2026N/A *
2026N/A * The contents of this file are subject to the terms of the
2026N/A * Common Development and Distribution License (the "License").
2026N/A * You may not use this file except in compliance with the License.
2026N/A *
2026N/A * See LICENSE.txt included in this distribution for the specific
2026N/A * language governing permissions and limitations under the License.
2026N/A *
2026N/A * When distributing Covered Code, include this CDDL HEADER in each
2026N/A * file and include the License file at LICENSE.txt.
2026N/A * If applicable, add the following below this CDDL HEADER, with the
2026N/A * fields enclosed by brackets "[]" replaced with your own identifying
2026N/A * information: Portions Copyright [yyyy] [name of copyright owner]
2026N/A *
2026N/A * CDDL HEADER END
2026N/A */
2026N/A
2026N/A/*
2026N/A * Copyright 2005 Sun Microsystems, Inc. All rights reserved.
2026N/A * Use is subject to license terms.
2026N/A */
2026N/A
2026N/A/*
2026N/A * ident "@(#)FileAnalyzer.java 1.2 05/12/01 SMI"
2026N/A */
2026N/Apackage org.opensolaris.opengrok.analysis;
2026N/A
2026N/Aimport java.io.*;
2026N/Aimport org.apache.lucene.document.*;
2026N/Aimport org.apache.lucene.analysis.*;
2026N/Aimport org.opensolaris.opengrok.configuration.RuntimeEnvironment;
2026N/Aimport org.opensolaris.opengrok.history.*;
2026N/A
2026N/A/**
2026N/A * Base class for all different File Analyzers
2026N/A *
2026N/A * An Analyzer for a filetype provides
2026N/A *<ol>
2026N/A * <li>the file extentions and magic numbers it analyzes</li>
2026N/A * <li>a lucene document listing the fields it can support</li>
2026N/A * <li>TokenStreams for each of the field it said requires tokenizing in 2</li>
2026N/A * <li>cross reference in HTML format</li>
2026N/A * <li>The type of file data, plain text etc</li>
2026N/A *</ol>
2026N/A *
2026N/A * Created on September 21, 2005
2026N/A *
2026N/A * @author Chandan
2026N/A */
2026N/A
2026N/Apublic class FileAnalyzer extends Analyzer {
2026N/A public static String[] magics = null;
2026N/A public static String[] suffixe = null;
2026N/A public static String contentType = null;
2026N/A
2026N/A /**
2026N/A * What kind of file is this?
2026N/A */
2026N/A public static enum Genre {
2026N/A PLAIN, // xrefed - line numbered context
2026N/A XREFABLE, // xrefed - summarizer context
2026N/A IMAGE, // not xrefed - no context - used by diff/list
2026N/A DATA, // not xrefed - no context
2026N/A HTML // not xrefed - summarizer context from original file
2026N/A }
2026N/A public static Genre g = Genre.DATA;
2026N/A
2026N/A public Genre getGenre() {
2026N/A return this.g;
}
private HistoryAnalyzer hista;
/** Creates a new instance of FileAnalyzer */
public FileAnalyzer() {
hista = new HistoryAnalyzer();
}
public void analyze(Document doc, InputStream in) {
}
public TokenStream tokenStream(String fieldName, Reader reader) {
if ("path".equals(fieldName) || "project".equals(fieldName)) {
return new PathTokenizer(reader);
} else if("hist".equals(fieldName)) {
return hista.tokenStream(fieldName, reader);
}
if (RuntimeEnvironment.getInstance().isVerbose()) {
System.out.println("Have no analyzer for: " + fieldName);
}
return null;
}
/**
* Write a cross referenced HTML file.
* @param out to writer HTML cross-reference
*/
public void writeXref(Writer out) throws IOException {
out.write("Error General File X-Ref writer!");
}
/**
* Write a cross referenced HTML file.
* @param xrefdir path of file root xref directory
* @param path path from xrefdir
*/
public void writeXref(String xrefdir, String path) throws IOException {
// System.err.println("parent " + xrefdir + " child " + path);
Writer out = new BufferedWriter(new FileWriter(new File(xrefdir+ File.separatorChar +path)));
writeXref(out);
out.close();
}
/**
* Write a cross referenced HTML file reads the source from in
* @param in Input source
* @param out Output xref writer
* @param annotation annotation for the file (could be null)
*/
public static void writeXref(InputStream in, Writer out,
Annotation annotation) throws IOException {
throw new UnsupportedOperationException("Not yet implemented");
}
public void writeXref(File xrefDir, String path) throws IOException {
Writer out = new BufferedWriter(new FileWriter(new File(xrefDir, path)));
writeXref(out);
out.close();
}
public static char[] readContent(char[] content, InputStream in, Integer length) throws IOException {
InputStreamReader inReader = new InputStreamReader(in);
int len = 0;
do{
int rbytes = inReader.read(content, len, content.length - len);
if(rbytes > 0 ) {
if(rbytes == (content.length - len)) {
char[] content2 = new char[content.length * 2];
System.arraycopy(content,0, content2, 0, content.length);
content = content2;
}
len += rbytes;
} else {
break;
}
} while(true);
length = len;
return content;
}
}