FileAnalyzer.java revision 407
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/*
119N/A * Copyright 2005 Sun Microsystems, Inc. All rights reserved.
0N/A * Use is subject to license terms.
0N/A */
0N/A
65N/Apackage org.opensolaris.opengrok.analysis;
125N/A
125N/Aimport java.io.BufferedWriter;
125N/Aimport java.io.File;
125N/Aimport java.io.FileOutputStream;
58N/Aimport java.io.FileWriter;
77N/Aimport java.io.IOException;
125N/Aimport java.io.InputStream;
125N/Aimport java.io.OutputStreamWriter;
125N/Aimport java.io.Reader;
125N/Aimport java.io.Writer;
261N/Aimport java.util.zip.GZIPOutputStream;
261N/Aimport org.apache.lucene.analysis.Analyzer;
312N/Aimport org.apache.lucene.analysis.TokenStream;
312N/Aimport org.apache.lucene.document.Document;
467N/Aimport org.opensolaris.opengrok.configuration.Project;
428N/Aimport org.opensolaris.opengrok.configuration.RuntimeEnvironment;
126N/A
58N/A/**
394N/A * Base class for all different File Analyzers
8N/A *
77N/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>
434N/A *</ol>
439N/A *
312N/A * Created on September 21, 2005
465N/A *
465N/A * @author Chandan
465N/A */
312N/A
320N/Apublic class FileAnalyzer extends Analyzer {
312N/A protected Project project;
312N/A
312N/A private final FileAnalyzerFactory factory;
312N/A
65N/A /**
65N/A * What kind of file is this?
65N/A */
65N/A public static enum Genre {
464N/A PLAIN, // xrefed - line numbered context
0N/A XREFABLE, // xrefed - summarizer context
30N/A IMAGE, // not xrefed - no context - used by diff/list
58N/A DATA, // not xrefed - no context
312N/A HTML // not xrefed - summarizer context from original file
312N/A }
260N/A
112N/A /**
428N/A * Get the factory which created this analyzer.
376N/A * @return the {@code FileAnalyzerFactory} which created this analyzer
376N/A */
0N/A public final FileAnalyzerFactory getFactory() {
11N/A return factory;
0N/A }
240N/A
58N/A public Genre getGenre() {
58N/A return factory.getGenre();
58N/A }
58N/A
77N/A private HistoryAnalyzer hista;
207N/A /** Creates a new instance of FileAnalyzer */
207N/A public FileAnalyzer(FileAnalyzerFactory factory) {
261N/A this.factory = factory;
260N/A hista = new HistoryAnalyzer();
77N/A }
260N/A
112N/A public void analyze(Document doc, InputStream in) {
77N/A }
77N/A
77N/A public TokenStream tokenStream(String fieldName, Reader reader) {
77N/A if ("path".equals(fieldName) || "project".equals(fieldName)) {
260N/A return new PathTokenizer(reader);
77N/A } else if("hist".equals(fieldName)) {
77N/A return hista.tokenStream(fieldName, reader);
77N/A }
0N/A
77N/A if (RuntimeEnvironment.getInstance().isVerbose()) {
111N/A System.out.println("Have no analyzer for: " + fieldName);
111N/A }
111N/A return null;
111N/A }
111N/A
111N/A /**
111N/A * Write a cross referenced HTML file.
111N/A * @param out to writer HTML cross-reference
111N/A * @throws java.io.IOException if an error occurs
111N/A */
111N/A public void writeXref(Writer out) throws IOException {
111N/A out.write("Error General File X-Ref writer!");
77N/A }
77N/A
77N/A public void writeXref(File xrefDir, String path) throws IOException {
207N/A RuntimeEnvironment env = RuntimeEnvironment.getInstance();
207N/A
77N/A if (env.hasProjects()) {
77N/A project = Project.getProject(path);
99N/A } else {
77N/A project = null;
77N/A }
77N/A
77N/A Writer out = null;
77N/A if (env.isCompressXref()) {
77N/A GZIPOutputStream o;
464N/A out = new BufferedWriter(new OutputStreamWriter(new GZIPOutputStream(new FileOutputStream(new File(xrefDir, path + ".gz")))));
77N/A } else {
77N/A out = new BufferedWriter(new FileWriter(new File(xrefDir, path)));
77N/A }
77N/A writeXref(out);
77N/A out.close();
77N/A }
77N/A}
77N/A