Search in sources :

Example 31 with LineNumberReader

use of java.io.LineNumberReader in project lucene-solr by apache.

the class Dictionary method readAffixFile.

/**
   * Reads the affix file through the provided InputStream, building up the prefix and suffix maps
   *
   * @param affixStream InputStream to read the content of the affix file from
   * @param decoder CharsetDecoder to decode the content of the file
   * @throws IOException Can be thrown while reading from the InputStream
   */
private void readAffixFile(InputStream affixStream, CharsetDecoder decoder) throws IOException, ParseException {
    TreeMap<String, List<Integer>> prefixes = new TreeMap<>();
    TreeMap<String, List<Integer>> suffixes = new TreeMap<>();
    Map<String, Integer> seenPatterns = new HashMap<>();
    // zero condition -> 0 ord
    seenPatterns.put(".*", 0);
    patterns.add(null);
    // zero strip -> 0 ord
    Map<String, Integer> seenStrips = new LinkedHashMap<>();
    seenStrips.put("", 0);
    LineNumberReader reader = new LineNumberReader(new InputStreamReader(affixStream, decoder));
    String line = null;
    while ((line = reader.readLine()) != null) {
        // ignore any BOM marker on first line
        if (reader.getLineNumber() == 1 && line.startsWith("")) {
            line = line.substring(1);
        }
        if (line.startsWith(ALIAS_KEY)) {
            parseAlias(line);
        } else if (line.startsWith(MORPH_ALIAS_KEY)) {
            parseMorphAlias(line);
        } else if (line.startsWith(PREFIX_KEY)) {
            parseAffix(prefixes, line, reader, PREFIX_CONDITION_REGEX_PATTERN, seenPatterns, seenStrips);
        } else if (line.startsWith(SUFFIX_KEY)) {
            parseAffix(suffixes, line, reader, SUFFIX_CONDITION_REGEX_PATTERN, seenPatterns, seenStrips);
        } else if (line.startsWith(FLAG_KEY)) {
            // Assume that the FLAG line comes before any prefix or suffixes
            // Store the strategy so it can be used when parsing the dic file
            flagParsingStrategy = getFlagParsingStrategy(line);
        } else if (line.equals(COMPLEXPREFIXES_KEY)) {
            // 2-stage prefix+1-stage suffix instead of 2-stage suffix+1-stage prefix
            complexPrefixes = true;
        } else if (line.startsWith(CIRCUMFIX_KEY)) {
            String[] parts = line.split("\\s+");
            if (parts.length != 2) {
                throw new ParseException("Illegal CIRCUMFIX declaration", reader.getLineNumber());
            }
            circumfix = flagParsingStrategy.parseFlag(parts[1]);
        } else if (line.startsWith(KEEPCASE_KEY)) {
            String[] parts = line.split("\\s+");
            if (parts.length != 2) {
                throw new ParseException("Illegal KEEPCASE declaration", reader.getLineNumber());
            }
            keepcase = flagParsingStrategy.parseFlag(parts[1]);
        } else if (line.startsWith(NEEDAFFIX_KEY) || line.startsWith(PSEUDOROOT_KEY)) {
            String[] parts = line.split("\\s+");
            if (parts.length != 2) {
                throw new ParseException("Illegal NEEDAFFIX declaration", reader.getLineNumber());
            }
            needaffix = flagParsingStrategy.parseFlag(parts[1]);
        } else if (line.startsWith(ONLYINCOMPOUND_KEY)) {
            String[] parts = line.split("\\s+");
            if (parts.length != 2) {
                throw new ParseException("Illegal ONLYINCOMPOUND declaration", reader.getLineNumber());
            }
            onlyincompound = flagParsingStrategy.parseFlag(parts[1]);
        } else if (line.startsWith(IGNORE_KEY)) {
            String[] parts = line.split("\\s+");
            if (parts.length != 2) {
                throw new ParseException("Illegal IGNORE declaration", reader.getLineNumber());
            }
            ignore = parts[1].toCharArray();
            Arrays.sort(ignore);
            needsInputCleaning = true;
        } else if (line.startsWith(ICONV_KEY) || line.startsWith(OCONV_KEY)) {
            String[] parts = line.split("\\s+");
            String type = parts[0];
            if (parts.length != 2) {
                throw new ParseException("Illegal " + type + " declaration", reader.getLineNumber());
            }
            int num = Integer.parseInt(parts[1]);
            FST<CharsRef> res = parseConversions(reader, num);
            if (type.equals("ICONV")) {
                iconv = res;
                needsInputCleaning |= iconv != null;
            } else {
                oconv = res;
                needsOutputCleaning |= oconv != null;
            }
        } else if (line.startsWith(FULLSTRIP_KEY)) {
            fullStrip = true;
        } else if (line.startsWith(LANG_KEY)) {
            language = line.substring(LANG_KEY.length()).trim();
            alternateCasing = "tr_TR".equals(language) || "az_AZ".equals(language);
        }
    }
    this.prefixes = affixFST(prefixes);
    this.suffixes = affixFST(suffixes);
    int totalChars = 0;
    for (String strip : seenStrips.keySet()) {
        totalChars += strip.length();
    }
    stripData = new char[totalChars];
    stripOffsets = new int[seenStrips.size() + 1];
    int currentOffset = 0;
    int currentIndex = 0;
    for (String strip : seenStrips.keySet()) {
        stripOffsets[currentIndex++] = currentOffset;
        strip.getChars(0, strip.length(), stripData, currentOffset);
        currentOffset += strip.length();
    }
    assert currentIndex == seenStrips.size();
    stripOffsets[currentIndex] = currentOffset;
}
Also used : InputStreamReader(java.io.InputStreamReader) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) FST(org.apache.lucene.util.fst.FST) TreeMap(java.util.TreeMap) LinkedHashMap(java.util.LinkedHashMap) LineNumberReader(java.io.LineNumberReader) List(java.util.List) ArrayList(java.util.ArrayList) ParseException(java.text.ParseException)

Example 32 with LineNumberReader

use of java.io.LineNumberReader in project jackrabbit-oak by apache.

the class JsonIndexCommand method openScriptReader.

private LineNumberReader openScriptReader(String script) throws IOException {
    Reader reader;
    if ("-".equals(script)) {
        reader = new InputStreamReader(System.in);
        interactive = true;
    } else {
        reader = new FileReader(script);
    }
    return new LineNumberReader(new BufferedReader(reader));
}
Also used : InputStreamReader(java.io.InputStreamReader) BufferedReader(java.io.BufferedReader) LineNumberReader(java.io.LineNumberReader) Reader(java.io.Reader) JsopReader(org.apache.jackrabbit.oak.commons.json.JsopReader) InputStreamReader(java.io.InputStreamReader) BufferedReader(java.io.BufferedReader) FileReader(java.io.FileReader) FileReader(java.io.FileReader) LineNumberReader(java.io.LineNumberReader)

Example 33 with LineNumberReader

use of java.io.LineNumberReader in project jena by apache.

the class Parser method main.

public static void main(String[] args) throws IOException {
    LineNumberReader in = new LineNumberReader(new InputStreamReader(System.in));
    IRIImpl last = null;
    DEBUG = true;
    IRIFactory factory = IRIFactory.iriImplementation();
    while (true) {
        String s = in.readLine().trim();
        if (s.equals("quit"))
            return;
        IRIImpl iri = (IRIImpl) factory.create(s);
        show(iri);
        if (last != null) {
            IRI r = last.create(iri);
            System.out.println("Resolved: " + r.toString());
            show(r);
        }
        last = iri;
    }
}
Also used : InputStreamReader(java.io.InputStreamReader) LineNumberReader(java.io.LineNumberReader)

Example 34 with LineNumberReader

use of java.io.LineNumberReader in project intellij-community by JetBrains.

the class CodeStyleAbstractPanel method readFromFile.

public static String readFromFile(final Class resourceContainerClass, @NonNls final String fileName) {
    try {
        final InputStream stream = resourceContainerClass.getClassLoader().getResourceAsStream("codeStyle/preview/" + fileName);
        final InputStreamReader reader = new InputStreamReader(stream);
        final StringBuffer result;
        final LineNumberReader lineNumberReader = new LineNumberReader(reader);
        try {
            result = new StringBuffer();
            String line;
            while ((line = lineNumberReader.readLine()) != null) {
                result.append(line);
                result.append("\n");
            }
        } finally {
            lineNumberReader.close();
        }
        return result.toString();
    } catch (IOException e) {
        return "";
    }
}
Also used : InputStreamReader(java.io.InputStreamReader) InputStream(java.io.InputStream) IOException(java.io.IOException) LineNumberReader(java.io.LineNumberReader)

Example 35 with LineNumberReader

use of java.io.LineNumberReader in project AndResGuard by shwenzhang.

the class ResourceApkBuilder method generalRaw7zip.

private void generalRaw7zip() throws IOException, InterruptedException {
    String outPath = m7zipOutPutDir.getAbsoluteFile().getAbsolutePath();
    String path = outPath + File.separator + "*";
    //极限压缩
    String cmd = Utils.isPresent(config.m7zipPath) ? config.m7zipPath : TypedValue.COMMAND_7ZIP;
    ProcessBuilder pb = new ProcessBuilder(cmd, "a", "-tzip", mSignedWith7ZipApk.getAbsolutePath(), path, "-mx9");
    Process pro = pb.start();
    InputStreamReader ir = new InputStreamReader(pro.getInputStream());
    LineNumberReader input = new LineNumberReader(ir);
    //如果不读会有问题,被阻塞
    while (input.readLine() != null) {
    }
    //destroy the stream
    pro.waitFor();
    pro.destroy();
}
Also used : InputStreamReader(java.io.InputStreamReader) LineNumberReader(java.io.LineNumberReader)

Aggregations

LineNumberReader (java.io.LineNumberReader)401 IOException (java.io.IOException)203 InputStreamReader (java.io.InputStreamReader)167 FileReader (java.io.FileReader)102 File (java.io.File)79 InputStream (java.io.InputStream)64 StringReader (java.io.StringReader)64 ArrayList (java.util.ArrayList)54 FileInputStream (java.io.FileInputStream)33 BufferedReader (java.io.BufferedReader)27 PrintWriter (java.io.PrintWriter)25 HashMap (java.util.HashMap)22 FileNotFoundException (java.io.FileNotFoundException)20 BufferedWriter (java.io.BufferedWriter)16 FileWriter (java.io.FileWriter)16 Pattern (java.util.regex.Pattern)16 Test (org.junit.jupiter.api.Test)16 ByteArrayInputStream (java.io.ByteArrayInputStream)15 ByteArrayOutputStream (java.io.ByteArrayOutputStream)15 Reader (java.io.Reader)14