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/*
1380N/A * Copyright (c) 2005, 2012, Oracle and/or its affiliates. All rights reserved.
0N/A */
0N/Apackage org.opensolaris.opengrok.analysis.archive;
0N/A
392N/Aimport java.io.BufferedInputStream;
392N/Aimport java.io.IOException;
392N/Aimport java.io.InputStream;
392N/Aimport java.io.Reader;
1384N/A
392N/Aimport org.apache.lucene.analysis.TokenStream;
392N/Aimport org.apache.lucene.document.Document;
392N/Aimport org.apache.lucene.document.Field;
392N/Aimport org.apache.tools.bzip2.CBZip2InputStream;
392N/Aimport org.opensolaris.opengrok.analysis.AnalyzerGuru;
392N/Aimport org.opensolaris.opengrok.analysis.FileAnalyzer;
392N/Aimport org.opensolaris.opengrok.analysis.FileAnalyzerFactory;
1384N/Aimport org.opensolaris.opengrok.analysis.XrefWriter;
0N/A
0N/A/**
0N/A * Analyzes a BZip2 file
0N/A * Created on September 22, 2005
0N/A *
0N/A * @author Chandan
0N/A */
0N/Apublic class BZip2Analyzer extends FileAnalyzer {
202N/A private Genre g;
972N/A
1185N/A @Override
0N/A public Genre getGenre() {
972N/A if (g != null) {
202N/A return g;
202N/A }
202N/A return super.getGenre();
0N/A }
202N/A
202N/A protected BZip2Analyzer(FileAnalyzerFactory factory) {
972N/A super(factory);
0N/A }
972N/A
0N/A private FileAnalyzer fa;
972N/A
1185N/A @Override
889N/A public void analyze(Document doc, InputStream in) throws IOException {
889N/A if (in.read() != 'B') {
889N/A throw new IOException("Not BZIP2 format");
889N/A }
889N/A if (in.read() != 'Z') {
889N/A throw new IOException("Not BZIP2 format");
889N/A }
889N/A BufferedInputStream gzis = new BufferedInputStream(new CBZip2InputStream(in));
889N/A String path = doc.get("path");
889N/A if(path != null &&
889N/A (path.endsWith(".bz2") || path.endsWith(".BZ2") || path.endsWith(".bz"))
889N/A ) {
889N/A String newname = path.substring(0, path.lastIndexOf('.'));
889N/A //System.err.println("BZIPPED OF = " + newname);
889N/A fa = AnalyzerGuru.getAnalyzer(gzis, newname);
1250N/A if (fa instanceof BZip2Analyzer) {
1250N/A fa = null;
1250N/A } else {
889N/A if(fa.getGenre() == Genre.PLAIN || fa.getGenre() == Genre.XREFABLE) {
889N/A this.g = Genre.XREFABLE;
889N/A } else {
889N/A this.g = Genre.DATA;
889N/A }
889N/A fa.analyze(doc, gzis);
889N/A if(doc.get("t") != null) {
889N/A doc.removeField("t");
889N/A if (g == Genre.XREFABLE) {
1190N/A doc.add(new Field("t", g.typeName(), Field.Store.YES,
1185N/A Field.Index.NOT_ANALYZED));
889N/A }
889N/A }
392N/A }
889N/A }
0N/A }
1190N/A
1185N/A @Override
1380N/A public TokenStream overridableTokenStream(String fieldName, Reader reader) {
972N/A if (fa != null) {
1380N/A return fa.overridableTokenStream(fieldName, reader);
972N/A }
1380N/A return super.overridableTokenStream(fieldName, reader);
0N/A }
972N/A
0N/A /**
0N/A * Write a cross referenced HTML file.
0N/A * @param out Writer to store HTML cross-reference
0N/A */
1185N/A @Override
1384N/A public void writeXref(XrefWriter out) throws IOException {
972N/A if ((fa != null) && (fa.getGenre() == Genre.PLAIN || fa.getGenre() == Genre.XREFABLE)) {
460N/A fa.writeXref(out);
972N/A }
0N/A }
0N/A}