|
| 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. This value is "effectively |
| 43 | + * unbounded" without being literally 2_147_483_647 -- as the other limits |
| 44 | + * will apply first, and the {@link java.io.BufferedInputStream} will |
| 45 | + * manage a reasonably-sized buffer. |
| 46 | + */ |
| 47 | + private static final int MARK_READ_LIMIT = 1024 * 512; |
| 48 | + |
| 49 | + private static final int LINE_LIMIT = 100; |
| 50 | + |
| 51 | + private static final int FIRST_LOOK_WIDTH = 300; |
| 52 | + |
| 53 | + private final FileAnalyzerFactory factory; |
| 54 | + |
| 55 | + private final String[] lineStarters; |
| 56 | + |
| 57 | + /** |
| 58 | + * Initializes an instance for the required parameters. |
| 59 | + * @param factory required factory to return when matched |
| 60 | + * @param lineStarters required list of line starters that indicate a match |
| 61 | + * @throws IllegalArgumentException thrown if any parameter is null |
| 62 | + */ |
| 63 | + public DocumentMatcher(FileAnalyzerFactory factory, String[] lineStarters) { |
| 64 | + if (factory == null) { |
| 65 | + throw new IllegalArgumentException("`factory' is null"); |
| 66 | + } |
| 67 | + if (lineStarters == null) { |
| 68 | + throw new IllegalArgumentException("`lineStarters' is null"); |
| 69 | + } |
| 70 | + if (lineStarters.length < 1) { |
| 71 | + throw new IllegalArgumentException("`lineStarters' is empty"); |
| 72 | + } |
| 73 | + |
| 74 | + String[] copyOf = Arrays.copyOf(lineStarters, lineStarters.length); |
| 75 | + for (String elem : copyOf) { |
| 76 | + if (elem == null) { |
| 77 | + throw new IllegalArgumentException( |
| 78 | + "`lineStarters' has null element"); |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + this.factory = factory; |
| 83 | + this.lineStarters = copyOf; |
| 84 | + } |
| 85 | + |
| 86 | + /** |
| 87 | + * Try to match the file contents by looking for {@code lineStarters} in |
| 88 | + * the first 100 lines while also affirming that the document starts |
| 89 | + * with "." or "'" after a limited amount of whitespace. |
| 90 | + * <p> |
| 91 | + * The stream is reset before returning. |
| 92 | + * |
| 93 | + * @param contents the first few bytes of a file |
| 94 | + * @param in the input stream from which the full file can be read |
| 95 | + * @return an analyzer factory if the contents match, or {@code null} |
| 96 | + * otherwise |
| 97 | + * @throws IOException in case of any read error |
| 98 | + */ |
| 99 | + @Override |
| 100 | + public FileAnalyzerFactory isMagic(byte[] contents, InputStream in) |
| 101 | + throws IOException { |
| 102 | + |
| 103 | + if (!in.markSupported()) return null; |
| 104 | + in.mark(MARK_READ_LIMIT); |
| 105 | + |
| 106 | + // read encoding, and skip past any BOM |
| 107 | + int bomLength = 0; |
| 108 | + String encoding = IOUtils.findBOMEncoding(contents); |
| 109 | + if (encoding == null) { |
| 110 | + encoding = "UTF-8"; |
| 111 | + } else { |
| 112 | + bomLength = IOUtils.skipForBOM(contents); |
| 113 | + if (in.skip(bomLength) != bomLength) { |
| 114 | + in.reset(); |
| 115 | + return null; |
| 116 | + } |
| 117 | + } |
| 118 | + |
| 119 | + // affirm that a LF exists in a first block |
| 120 | + boolean foundLF = hasLineFeed(in, encoding); |
| 121 | + in.reset(); |
| 122 | + if (!foundLF) return null; |
| 123 | + if (bomLength > 0) in.skip(bomLength); |
| 124 | + |
| 125 | + // read line-by-line for a first few lines |
| 126 | + BufferedReader rdr = new BufferedReader(new InputStreamReader( |
| 127 | + in, encoding)); |
| 128 | + boolean foundContent = false; |
| 129 | + int numFirstChars = 0; |
| 130 | + int numLines = 0; |
| 131 | + String line; |
| 132 | + while ((line = rdr.readLine()) != null) { |
| 133 | + for (int i = 0; i < lineStarters.length; ++i) { |
| 134 | + if (line.startsWith(lineStarters[i])) { |
| 135 | + in.reset(); |
| 136 | + return factory; |
| 137 | + } |
| 138 | + } |
| 139 | + if (++numLines >= LINE_LIMIT) { |
| 140 | + in.reset(); |
| 141 | + return null; |
| 142 | + } |
| 143 | + |
| 144 | + // If not yet `foundContent', then only a limited allowance is |
| 145 | + // given until a sentinel '.' or '\'' must be seen after nothing |
| 146 | + // else but whitespace. |
| 147 | + if (!foundContent) { |
| 148 | + for (int i = 0; i < line.length() && numFirstChars < |
| 149 | + FIRST_LOOK_WIDTH; ++i, ++numFirstChars) { |
| 150 | + char c = line.charAt(i); |
| 151 | + if (c == '.' || c == '\'') { |
| 152 | + foundContent = true; |
| 153 | + break; |
| 154 | + } else if (!Character.isWhitespace(c)) { |
| 155 | + in.reset(); |
| 156 | + return null; |
| 157 | + } |
| 158 | + } |
| 159 | + if (!foundContent && numFirstChars >= FIRST_LOOK_WIDTH) { |
| 160 | + in.reset(); |
| 161 | + return null; |
| 162 | + } |
| 163 | + } |
| 164 | + } |
| 165 | + |
| 166 | + in.reset(); |
| 167 | + return null; |
| 168 | + } |
| 169 | + |
| 170 | + /** |
| 171 | + * Determines if the {@code in} stream has a line feed character within the |
| 172 | + * first {@code FIRST_LOOK_WIDTH} characters. |
| 173 | + * @param in the input stream has any BOM (not {@code reset} after use) |
| 174 | + * @param encoding the input stream charset |
| 175 | + * @return true if a line feed '\n' was found |
| 176 | + * @throws IOException thrown on any error in reading |
| 177 | + */ |
| 178 | + private boolean hasLineFeed(InputStream in, String encoding) |
| 179 | + throws IOException { |
| 180 | + byte[] buf; |
| 181 | + int nextra; |
| 182 | + int noff; |
| 183 | + switch (encoding) { |
| 184 | + case "UTF-16LE": |
| 185 | + buf = new byte[FIRST_LOOK_WIDTH * 2]; |
| 186 | + nextra = 1; |
| 187 | + noff = 0; |
| 188 | + break; |
| 189 | + case "UTF-16BE": |
| 190 | + buf = new byte[FIRST_LOOK_WIDTH * 2]; |
| 191 | + nextra = 1; |
| 192 | + noff = 1; |
| 193 | + break; |
| 194 | + default: |
| 195 | + buf = new byte[FIRST_LOOK_WIDTH]; |
| 196 | + nextra = 0; |
| 197 | + noff = 0; |
| 198 | + break; |
| 199 | + } |
| 200 | + |
| 201 | + int nread = in.read(buf); |
| 202 | + for (int i = 0; i + nextra < nread; i += 1 + nextra) { |
| 203 | + if (nextra > 0) { |
| 204 | + if (buf[i + noff] == '\n' && buf[i + 1 - noff] == '\0') { |
| 205 | + return true; |
| 206 | + } |
| 207 | + } else { |
| 208 | + if (buf[i] == '\n') return true; |
| 209 | + } |
| 210 | + } |
| 211 | + return false; |
| 212 | + } |
| 213 | +} |
0 commit comments