202N/A/*
202N/A * CDDL HEADER START
202N/A *
202N/A * The contents of this file are subject to the terms of the
202N/A * Common Development and Distribution License (the "License").
202N/A * You may not use this file except in compliance with the License.
202N/A *
202N/A * See LICENSE.txt included in this distribution for the specific
202N/A * language governing permissions and limitations under the License.
202N/A *
202N/A * When distributing Covered Code, include this CDDL HEADER in each
202N/A * file and include the License file at LICENSE.txt.
202N/A * If applicable, add the following below this CDDL HEADER, with the
202N/A * fields enclosed by brackets "[]" replaced with your own identifying
202N/A * information: Portions Copyright [yyyy] [name of copyright owner]
202N/A *
202N/A * CDDL HEADER END
202N/A */
202N/A
202N/A/*
1127N/A * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
202N/A */
202N/A
202N/Apackage org.opensolaris.opengrok.analysis.plain;
202N/A
394N/Aimport java.io.IOException;
202N/Aimport java.io.InputStream;
921N/Aimport java.io.Reader;
1384N/A
956N/Aimport org.opensolaris.opengrok.analysis.AnalyzerGuru;
1127N/Aimport org.opensolaris.opengrok.analysis.Definitions;
202N/Aimport org.opensolaris.opengrok.analysis.FileAnalyzer;
202N/Aimport org.opensolaris.opengrok.analysis.FileAnalyzer.Genre;
202N/Aimport org.opensolaris.opengrok.analysis.FileAnalyzerFactory;
1384N/Aimport org.opensolaris.opengrok.analysis.XrefWriter;
271N/Aimport org.opensolaris.opengrok.configuration.Project;
202N/Aimport org.opensolaris.opengrok.history.Annotation;
202N/A
439N/Apublic final class PlainAnalyzerFactory extends FileAnalyzerFactory {
202N/A
202N/A private static final Matcher MATCHER = new Matcher() {
1327N/A @Override
956N/A public FileAnalyzerFactory isMagic(byte[] content, InputStream in)
956N/A throws IOException {
1327N/A return isPlainText(content) ? DEFAULT_INSTANCE : null;
956N/A }
956N/A
956N/A /**
956N/A * Check whether the byte array contains plain text. First, check
956N/A * assuming US-ASCII encoding. Then, if unsuccessful, try to
956N/A * strip away Unicode byte-order marks and try again.
956N/A */
956N/A private boolean isPlainText(byte[] content) throws IOException {
956N/A String ascii = new String(content, "US-ASCII");
956N/A if (isPlainText(ascii)) {
956N/A return true;
956N/A }
956N/A
956N/A String noBOM = AnalyzerGuru.stripBOM(content);
956N/A return (noBOM != null) && isPlainText(noBOM);
956N/A }
956N/A
956N/A /**
956N/A * Check whether the string only contains plain ASCII characters.
956N/A */
956N/A private boolean isPlainText(String str) {
956N/A for (int i = 0; i < str.length(); i++) {
956N/A char b = str.charAt(i);
504N/A if ((b >= 32 && b < 127) || // ASCII printable characters
504N/A (b == 9) || // horizontal tab
504N/A (b == 10) || // line feed
504N/A (b == 12) || // form feed
504N/A (b == 13)) { // carriage return
504N/A // is plain text so far, go to next byte
504N/A continue;
202N/A }
1327N/A // 8-bit values or unprintable control characters,
1327N/A // probably not plain text
1327N/A return false;
202N/A }
956N/A return true;
202N/A }
202N/A };
202N/A
1238N/A public static final PlainAnalyzerFactory DEFAULT_INSTANCE =
257N/A new PlainAnalyzerFactory();
257N/A
257N/A private PlainAnalyzerFactory() {
483N/A super(null, null, null, MATCHER, "text/plain", Genre.PLAIN);
202N/A }
202N/A
202N/A @Override
202N/A protected FileAnalyzer newAnalyzer() {
202N/A return new PlainAnalyzer(this);
202N/A }
202N/A
202N/A @Override
1384N/A public void writeXref(Reader in, XrefWriter out, Definitions defs, Annotation annotation, Project project)
202N/A throws IOException
202N/A {
1127N/A PlainAnalyzer.writeXref(in, out, defs, annotation, project);
202N/A }
202N/A}