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