FileAnalyzer.java revision 889
0N/A/*
0N/A * CDDL HEADER START
0N/A *
0N/A * The contents of this file are subject to the terms of the
407N/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/Apackage org.opensolaris.opengrok.analysis;
0N/A
335N/Aimport java.io.BufferedWriter;
335N/Aimport java.io.File;
335N/Aimport java.io.FileOutputStream;
335N/Aimport java.io.IOException;
335N/Aimport java.io.InputStream;
509N/Aimport java.io.OutputStream;
335N/Aimport java.io.OutputStreamWriter;
335N/Aimport java.io.Reader;
335N/Aimport java.io.Writer;
335N/Aimport java.util.zip.GZIPOutputStream;
335N/Aimport org.apache.lucene.analysis.Analyzer;
335N/Aimport org.apache.lucene.analysis.TokenStream;
335N/Aimport org.apache.lucene.document.Document;
427N/Aimport org.opensolaris.opengrok.OpenGrokLogger;
271N/Aimport org.opensolaris.opengrok.configuration.Project;
99N/Aimport org.opensolaris.opengrok.configuration.RuntimeEnvironment;
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 */
656N/Apublic class FileAnalyzer extends Analyzer {
0N/A
271N/A protected Project project;
202N/A private final FileAnalyzerFactory factory;
202N/A
0N/A /**
0N/A * What kind of file is this?
0N/A */
0N/A public static enum Genre {
656N/A
656N/A PLAIN, // xrefed - line numbered context
656N/A XREFABLE, // xrefed - summarizer context
656N/A IMAGE, // not xrefed - no context - used by diff/list
656N/A DATA, // not xrefed - no context
656N/A HTML // not xrefed - summarizer context from original file
656N/A }
656N/A protected Ctags ctags;
656N/A
656N/A public void setCtags(Ctags ctags) {
656N/A this.ctags = ctags;
0N/A }
202N/A
202N/A /**
202N/A * Get the factory which created this analyzer.
202N/A * @return the {@code FileAnalyzerFactory} which created this analyzer
202N/A */
202N/A public final FileAnalyzerFactory getFactory() {
202N/A return factory;
202N/A }
202N/A
0N/A public Genre getGenre() {
202N/A return factory.getGenre();
0N/A }
656N/A private final HistoryAnalyzer hista;
202N/A
0N/A /** Creates a new instance of FileAnalyzer */
202N/A public FileAnalyzer(FileAnalyzerFactory factory) {
202N/A this.factory = factory;
656N/A hista = new HistoryAnalyzer();
0N/A }
656N/A
889N/A public void analyze(Document doc, InputStream in) throws IOException {
456N/A // not used
0N/A }
656N/A
0N/A public TokenStream tokenStream(String fieldName, Reader reader) {
656N/A if ("path".equals(fieldName) || "project".equals(fieldName)) {
656N/A return new PathTokenizer(reader);
656N/A } else if ("hist".equals(fieldName)) {
656N/A return hista.tokenStream(fieldName, reader);
58N/A }
656N/A
99N/A if (RuntimeEnvironment.getInstance().isVerbose()) {
427N/A OpenGrokLogger.getLogger().info("Have no analyzer for: " + fieldName);
99N/A }
656N/A return null;
0N/A }
656N/A
0N/A /**
0N/A * Write a cross referenced HTML file.
0N/A * @param out to writer HTML cross-reference
271N/A * @throws java.io.IOException if an error occurs
0N/A */
0N/A public void writeXref(Writer out) throws IOException {
656N/A out.write("Error General File X-Ref writer!");
0N/A }
656N/A
0N/A public void writeXref(File xrefDir, String path) throws IOException {
335N/A RuntimeEnvironment env = RuntimeEnvironment.getInstance();
335N/A
335N/A if (env.hasProjects()) {
271N/A project = Project.getProject(path);
271N/A } else {
271N/A project = null;
271N/A }
335N/A
509N/A final boolean compressed = env.isCompressXref();
509N/A final File file = new File(xrefDir, path + (compressed ? ".gz" : ""));
509N/A OutputStream out = new FileOutputStream(file);
509N/A try {
509N/A if (compressed) {
509N/A out = new GZIPOutputStream(out);
509N/A }
559N/A Writer w = new BufferedWriter(new OutputStreamWriter(out));
559N/A writeXref(w);
559N/A w.close();
509N/A } finally {
509N/A out.close();
335N/A }
0N/A }
0N/A}