|
| 1 | +/* |
| 2 | + * CDDL HEADER START |
| 3 | + * |
| 4 | + * The contents of this file are subject to the terms of the |
| 5 | + * Common Development and Distribution License (the "License"). |
| 6 | + * You may not use this file except in compliance with the License. |
| 7 | + * |
| 8 | + * See LICENSE.txt included in this distribution for the specific |
| 9 | + * language governing permissions and limitations under the License. |
| 10 | + * |
| 11 | + * When distributing Covered Code, include this CDDL HEADER in each |
| 12 | + * file and include the License file at LICENSE.txt. |
| 13 | + * If applicable, add the following below this CDDL HEADER, with the |
| 14 | + * fields enclosed by brackets "[]" replaced with your own identifying |
| 15 | + * information: Portions Copyright [yyyy] [name of copyright owner] |
| 16 | + * |
| 17 | + * CDDL HEADER END |
| 18 | + */ |
| 19 | + |
| 20 | + /* |
| 21 | + * Copyright (c) 2017, Chris Fraire <[email protected]>. |
| 22 | + */ |
| 23 | +package org.opensolaris.opengrok.analysis.document; |
| 24 | + |
| 25 | +import java.io.BufferedReader; |
| 26 | +import java.io.IOException; |
| 27 | +import java.io.InputStream; |
| 28 | +import java.io.InputStreamReader; |
| 29 | +import java.util.Arrays; |
| 30 | +import org.opensolaris.opengrok.analysis.FileAnalyzerFactory; |
| 31 | +import org.opensolaris.opengrok.analysis.FileAnalyzerFactory.Matcher; |
| 32 | +import org.opensolaris.opengrok.util.IOUtils; |
| 33 | + |
| 34 | +/** |
| 35 | + * Represents an implementation of {@link Matcher} that detects a troff- |
| 36 | + * or mandoc-like document |
| 37 | + */ |
| 38 | +public class DocumentMatcher implements Matcher { |
| 39 | + |
| 40 | + /** |
| 41 | + * Set to 512K {@code int}, but {@code NUMCHARS_FIRST_LOOK} and |
| 42 | + * {@code LINE_LIMIT} should apply beforehand |
| 43 | + */ |
| 44 | + private static final int MARK_READ_LIMIT = 1024 * 512; |
| 45 | + |
| 46 | + private static final int LINE_LIMIT = 100; |
| 47 | + |
| 48 | + private static final int FIRST_LOOK_WIDTH = 300; |
| 49 | + |
| 50 | + private static final int FIRST_CONTENT_WIDTH = 8; |
| 51 | + |
| 52 | + private final FileAnalyzerFactory factory; |
| 53 | + |
| 54 | + private final String[] lineStarters; |
| 55 | + |
| 56 | + /** |
| 57 | + * Initializes an instance for the required parameters |
| 58 | + * @param factory required factory to return when matched |
| 59 | + * @param lineStarters required list of line starters that indicate a match |
| 60 | + * @throws IllegalArgumentException if any parameter is null |
| 61 | + */ |
| 62 | + public DocumentMatcher(FileAnalyzerFactory factory, String[] lineStarters) { |
| 63 | + if (factory == null) { |
| 64 | + throw new IllegalArgumentException("`factory' is null"); |
| 65 | + } |
| 66 | + if (lineStarters == null) { |
| 67 | + throw new IllegalArgumentException("`lineStarters' is null"); |
| 68 | + } |
| 69 | + if (lineStarters.length < 1) { |
| 70 | + throw new IllegalArgumentException("`lineStarters' is empty"); |
| 71 | + } |
| 72 | + |
| 73 | + String[] copyOf = Arrays.copyOf(lineStarters, lineStarters.length); |
| 74 | + for (String elem : copyOf) { |
| 75 | + if (elem == null) { |
| 76 | + throw new IllegalArgumentException( |
| 77 | + "`lineStarters' has null element"); |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + this.factory = factory; |
| 82 | + this.lineStarters = copyOf; |
| 83 | + } |
| 84 | + |
| 85 | + /** |
| 86 | + * Try to match the file contents by first affirming the document starts |
| 87 | + * with "." or "'" and then looks for {@code lineStarters} in the first |
| 88 | + * 100 lines. |
| 89 | + * <p> |
| 90 | + * The stream is reset before returning. |
| 91 | + * |
| 92 | + * @param contents the first few bytes of a file |
| 93 | + * @param in the input stream from which the full file can be read |
| 94 | + * @return an analyzer factory if the contents match, or {@code null} |
| 95 | + * otherwise |
| 96 | + * @throws IOException in case of any read error |
| 97 | + */ |
| 98 | + @Override |
| 99 | + public FileAnalyzerFactory isMagic(byte[] contents, InputStream in) |
| 100 | + throws IOException { |
| 101 | + |
| 102 | + if (!in.markSupported()) return null; |
| 103 | + in.mark(MARK_READ_LIMIT); |
| 104 | + |
| 105 | + int bomLength = 0; |
| 106 | + String encoding = IOUtils.findBOMEncoding(contents); |
| 107 | + if (encoding == null) { |
| 108 | + encoding = "UTF-8"; |
| 109 | + } else { |
| 110 | + bomLength = IOUtils.skipForBOM(contents); |
| 111 | + if (in.skip(bomLength) != bomLength) { |
| 112 | + in.reset(); |
| 113 | + return null; |
| 114 | + } |
| 115 | + } |
| 116 | + |
| 117 | + BufferedReader rdr = new BufferedReader(new InputStreamReader( |
| 118 | + in, encoding)); |
| 119 | + |
| 120 | + // Before reading a line, read some characters for a first look |
| 121 | + char[] buf = new char[FIRST_LOOK_WIDTH]; |
| 122 | + int lenFirstLook; |
| 123 | + if ((lenFirstLook = rdr.read(buf)) < 1) { |
| 124 | + in.reset(); |
| 125 | + return null; |
| 126 | + } |
| 127 | + |
| 128 | + // Require a "." or "'" as the first non-whitespace character after |
| 129 | + // only a limited number of whitespaces or else infer it is not troff |
| 130 | + // or mandoc. |
| 131 | + int actualFirstContentWidth = lenFirstLook < FIRST_CONTENT_WIDTH ? |
| 132 | + lenFirstLook : FIRST_CONTENT_WIDTH; |
| 133 | + boolean foundContent = false; |
| 134 | + for (int i = 0; i < actualFirstContentWidth; ++i) { |
| 135 | + if (buf[i] == '.' || buf[i] == '\'') { |
| 136 | + foundContent = true; |
| 137 | + break; |
| 138 | + } else if (!Character.isWhitespace(buf[i])) { |
| 139 | + in.reset(); |
| 140 | + return null; |
| 141 | + } |
| 142 | + } |
| 143 | + if (!foundContent) { |
| 144 | + in.reset(); |
| 145 | + return null; |
| 146 | + } |
| 147 | + |
| 148 | + // affirm that a LF is seen in the first look or else quickly |
| 149 | + // infer it is not troff |
| 150 | + boolean foundLF = false; |
| 151 | + for (int i = 0; i < lenFirstLook; ++i) { |
| 152 | + if (buf[i] == '\n') { |
| 153 | + foundLF = true; |
| 154 | + break; |
| 155 | + } |
| 156 | + } |
| 157 | + if (!foundLF) { |
| 158 | + in.reset(); |
| 159 | + return null; |
| 160 | + } |
| 161 | + |
| 162 | + // reset for line-by-line reading below |
| 163 | + in.reset(); |
| 164 | + if (bomLength > 0) in.skip(bomLength); |
| 165 | + rdr = new BufferedReader(new InputStreamReader(in, encoding)); |
| 166 | + |
| 167 | + int numLines = 0; |
| 168 | + String line; |
| 169 | + while ((line = rdr.readLine()) != null) { |
| 170 | + for (int i = 0; i < lineStarters.length; ++i) { |
| 171 | + if (line.startsWith(lineStarters[i])) { |
| 172 | + in.reset(); |
| 173 | + return factory; |
| 174 | + } |
| 175 | + } |
| 176 | + if (++numLines >= LINE_LIMIT) { |
| 177 | + in.reset(); |
| 178 | + return null; |
| 179 | + } |
| 180 | + } |
| 181 | + |
| 182 | + in.reset(); |
| 183 | + return null; |
| 184 | + } |
| 185 | +} |
0 commit comments