FileAnalyzer.java revision 58
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/*
1054N/A * Copyright 2005 Sun Microsystems, Inc. All rights reserved.
1190N/A * Use is subject to license terms.
1185N/A */
0N/A
0N/A/*
65N/A * ident "@(#)FileAnalyzer.java 1.2 05/12/01 SMI"
125N/A */
125N/Apackage org.opensolaris.opengrok.analysis;
1185N/A
58N/Aimport java.io.*;
77N/Aimport org.apache.lucene.document.*;
125N/Aimport org.apache.lucene.analysis.*;
125N/Aimport org.opensolaris.opengrok.web.Util;
125N/Aimport org.opensolaris.opengrok.history.*;
1092N/A
1016N/A/**
125N/A * Base class for all different File Analyzers
1092N/A *
261N/A * An Analyzer for a filetype provides
261N/A *<ol>
583N/A * <li>the file extentions and magic numbers it analyzes</li>
312N/A * <li>a lucene document listing the fields it can support</li>
1062N/A * <li>TokenStreams for each of the field it said requires tokenizing in 2</li>
312N/A * <li>cross reference in HTML format</li>
467N/A * <li>The type of file data, plain text etc</li>
428N/A *</ol>
126N/A *
1088N/A * Created on September 21, 2005
58N/A *
394N/A * @author Chandan
1185N/A */
8N/A
1185N/Apublic class FileAnalyzer extends Analyzer {
1185N/A public static String[] magics = null;
1185N/A public static String[] suffixe = null;
77N/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
491N/A XREFABLE, // xrefed - summarizer context
439N/A IMAGE, // not xrefed - no context - used by diff/list
491N/A DATA, // not xrefed - no context
465N/A HTML // not xrefed - summarizer context from original file
465N/A }
491N/A public static Genre g = Genre.DATA;
491N/A
491N/A public Genre getGenre() {
886N/A return this.g;
886N/A }
886N/A
886N/A private HistoryAnalyzer hista;
886N/A /** Creates a new instance of FileAnalyzer */
886N/A public FileAnalyzer() {
491N/A hista = new HistoryAnalyzer();
491N/A }
491N/A
491N/A public void analyze(Document doc, InputStream in) {
65N/A }
65N/A
65N/A public TokenStream tokenStream(String fieldName, Reader reader) {
65N/A if ("path".equals(fieldName) || "project".equals(fieldName)) {
464N/A return new PathTokenizer(reader);
0N/A } else if("hist".equals(fieldName)) {
58N/A return hista.tokenStream(fieldName, reader);
312N/A }
312N/A return null;
1185N/A }
260N/A
491N/A /**
428N/A * Write a cross referenced HTML file.
376N/A * @param out to writer HTML cross-reference
376N/A */
0N/A public void writeXref(Writer out) throws IOException {
11N/A out.write("Error General File X-Ref writer!");
0N/A }
240N/A
1016N/A /**
58N/A * Write a cross referenced HTML file.
58N/A * @param xrefdir path of file root xref directory
58N/A * @param path path from xrefdir
58N/A */
77N/A public void writeXref(String xrefdir, String path) throws IOException {
207N/A // System.err.println("parent " + xrefdir + " child " + path);
1185N/A Writer out = new BufferedWriter(new FileWriter(new File(xrefdir+ File.separatorChar +path)));
207N/A writeXref(out);
910N/A out.close();
1190N/A }
77N/A
260N/A /**
112N/A * Write a cross referenced HTML file reads the source from in
77N/A * @param in Input source
77N/A * @param out Output xref writer
77N/A */
77N/A public static void writeXref(InputStream in, Writer out) throws IOException {
260N/A throw new UnsupportedOperationException("Not yet implemented");
77N/A }
77N/A
77N/A public void writeXref(File xrefDir, String path) throws IOException {
491N/A Writer out = new BufferedWriter(new FileWriter(new File(xrefDir, path)));
1088N/A writeXref(out);
77N/A out.close();
491N/A }
111N/A
111N/A public static char[] readContent(char[] content, InputStream in, Integer length) throws IOException {
111N/A InputStreamReader inReader = new InputStreamReader(in);
111N/A int len = 0;
1088N/A do{
111N/A int rbytes = inReader.read(content, len, content.length - len);
111N/A if(rbytes > 0 ) {
111N/A if(rbytes == (content.length - len)) {
491N/A char[] content2 = new char[content.length * 2];
1088N/A System.arraycopy(content,0, content2, 0, content.length);
1088N/A content = content2;
1088N/A }
1088N/A len += rbytes;
1088N/A } else {
1088N/A break;
886N/A }
111N/A } while(true);
491N/A length = len;
77N/A return content;
77N/A }
1185N/A}
491N/A