FileAnalyzer.java revision 1327
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 (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
0N/A * Use is subject to license terms.
0N/A */
0N/Apackage org.opensolaris.opengrok.analysis;
0N/A
0N/Aimport java.io.BufferedWriter;
0N/Aimport java.io.File;
0N/Aimport java.io.FileOutputStream;
0N/Aimport java.io.IOException;
0N/Aimport java.io.InputStream;
0N/Aimport java.io.OutputStream;
0N/Aimport java.io.OutputStreamWriter;
0N/Aimport java.io.Reader;
0N/Aimport java.io.Writer;
0N/Aimport java.util.logging.Logger;
0N/Aimport java.util.zip.GZIPOutputStream;
0N/A
0N/Aimport org.apache.lucene.analysis.Analyzer;
0N/Aimport org.apache.lucene.analysis.TokenStream;
0N/Aimport org.apache.lucene.document.Document;
0N/Aimport org.opensolaris.opengrok.configuration.Project;
0N/Aimport org.opensolaris.opengrok.configuration.RuntimeEnvironment;
0N/Aimport org.opensolaris.opengrok.util.IOUtils;
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/Apublic class FileAnalyzer extends Analyzer {
0N/A private static final Logger logger = Logger.getLogger(FileAnalyzer.class.getName());
0N/A protected Project project;
0N/A private final FileAnalyzerFactory factory;
0N/A
0N/A /**
0N/A * What kind of file is this?
0N/A */
0N/A public static enum Genre {
0N/A /** xrefed - line numbered context */
0N/A PLAIN("p"),
0N/A /** xrefed - summarizer context */
0N/A XREFABLE("x"),
0N/A /** not xrefed - no context - used by diff/list */
0N/A IMAGE("i"),
0N/A /** not xrefed - no context */
0N/A DATA("d"),
0N/A /** not xrefed - summarizer context from original file */
0N/A HTML("h")
0N/A ;
0N/A private String typeName;
0N/A private Genre(String typename) {
0N/A this.typeName = typename;
0N/A }
0N/A
0N/A /**
0N/A * Get the type name value used to tag lucence documents.
0N/A * @return a none-null string.
0N/A */
0N/A public String typeName() {
0N/A return typeName;
0N/A }
0N/A
0N/A /**
0N/A * Get the Genre for the given type name.
0N/A * @param typeName name to check
0N/A * @return {@code null} if it doesn't match any genre, the genre otherwise.
0N/A * @see #typeName()
0N/A */
0N/A public static Genre get(String typeName) {
0N/A if (typeName == null) {
0N/A return null;
0N/A }
0N/A for (Genre g : values()) {
0N/A if (g.typeName.equals(typeName)) {
0N/A return g;
0N/A }
0N/A }
0N/A return null;
0N/A }
0N/A }
0N/A protected Ctags ctags;
0N/A
0N/A public void setCtags(Ctags ctags) {
0N/A this.ctags = ctags;
0N/A }
0N/A
0N/A public void setProject(Project project) {
0N/A this.project = project;
0N/A }
0N/A
0N/A /**
0N/A * Get the factory which created this analyzer.
0N/A * @return the {@code FileAnalyzerFactory} which created this analyzer
0N/A */
0N/A public final FileAnalyzerFactory getFactory() {
0N/A return factory;
0N/A }
0N/A
0N/A public Genre getGenre() {
0N/A return factory.getGenre();
0N/A }
0N/A private final HistoryAnalyzer hista;
0N/A
0N/A /** Creates a new instance of FileAnalyzer */
0N/A public FileAnalyzer(FileAnalyzerFactory factory) {
0N/A this.factory = factory;
0N/A hista = new HistoryAnalyzer();
0N/A }
0N/A
0N/A public void analyze(Document doc, InputStream in) throws IOException {
0N/A // not used
0N/A }
0N/A
0N/A @Override
0N/A public TokenStream tokenStream(String fieldName, Reader reader) {
0N/A if ("path".equals(fieldName) || "project".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 logger.warning("Have no analyzer for '" + fieldName + "'");
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 * @throws java.io.IOException if an error occurs
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 public void writeXref(File xrefDir, String path) throws IOException {
0N/A RuntimeEnvironment env = RuntimeEnvironment.getInstance();
0N/A
0N/A final boolean compressed = env.isCompressXref();
0N/A final File file = new File(xrefDir, path + (compressed ? ".gz" : ""));
0N/A OutputStream out = new FileOutputStream(file);
0N/A try {
0N/A if (compressed) {
0N/A out = new GZIPOutputStream(out);
0N/A }
0N/A Writer w = new BufferedWriter(new OutputStreamWriter(out));
0N/A writeXref(w);
0N/A IOUtils.close(w);
0N/A } finally {
0N/A IOUtils.close(out);
0N/A }
0N/A }
0N/A}
0N/A