Search in sources :

Example 76 with LineNumberReader

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

the class OpenTypeScript method parseScriptsFile.

private static void parseScriptsFile(InputStream inputStream) throws IOException {
    Map<int[], String> unicodeRanges = new TreeMap<>(new Comparator<int[]>() {

        @Override
        public int compare(int[] o1, int[] o2) {
            return Integer.compare(o1[0], o2[0]);
        }
    });
    try (LineNumberReader rd = new LineNumberReader(new InputStreamReader(inputStream))) {
        int[] lastRange = { Integer.MIN_VALUE, Integer.MIN_VALUE };
        String lastScript = null;
        do {
            String s = rd.readLine();
            if (s == null) {
                break;
            }
            // ignore comments
            int comment = s.indexOf('#');
            if (comment != -1) {
                s = s.substring(0, comment);
            }
            if (s.length() < 2) {
                continue;
            }
            StringTokenizer st = new StringTokenizer(s, ";");
            int nFields = st.countTokens();
            if (nFields < 2) {
                continue;
            }
            String characters = st.nextToken().trim();
            String script = st.nextToken().trim();
            int[] range = new int[2];
            int rangeDelim = characters.indexOf("..");
            if (rangeDelim == -1) {
                range[0] = range[1] = Integer.parseInt(characters, 16);
            } else {
                range[0] = Integer.parseInt(characters.substring(0, rangeDelim), 16);
                range[1] = Integer.parseInt(characters.substring(rangeDelim + 2), 16);
            }
            if (range[0] == lastRange[1] + 1 && script.equals(lastScript)) {
                // Combine with previous range
                lastRange[1] = range[1];
            } else {
                unicodeRanges.put(range, script);
                lastRange = range;
                lastScript = script;
            }
        } while (true);
    }
    unicodeRangeStarts = new int[unicodeRanges.size()];
    unicodeRangeScripts = new String[unicodeRanges.size()];
    int i = 0;
    for (Entry<int[], String> e : unicodeRanges.entrySet()) {
        unicodeRangeStarts[i] = e.getKey()[0];
        unicodeRangeScripts[i] = e.getValue();
        i++;
    }
}
Also used : StringTokenizer(java.util.StringTokenizer) InputStreamReader(java.io.InputStreamReader) TreeMap(java.util.TreeMap) LineNumberReader(java.io.LineNumberReader)

Example 77 with LineNumberReader

use of java.io.LineNumberReader in project epadd by ePADD.

the class JSONUtils method jsonForNewNewAlgResults.

public static String jsonForNewNewAlgResults(AddressBook ab, String resultsFile) throws IOException, JSONException {
    LineNumberReader lnr = new LineNumberReader(new InputStreamReader(new FileInputStream(resultsFile)));
    JSONObject result = new JSONObject();
    JSONArray groups = new JSONArray();
    int groupNum = 0;
    while (true) {
        String line = lnr.readLine();
        if (line == null)
            break;
        line = line.trim();
        // line: group 8, freq 49: seojiwon@gmail.com sseong@stanford.edu debangsu@cs.stanford.edu
        // ignore lines without a ':'
        int idx = line.indexOf(":");
        if (idx == -1)
            continue;
        String vars = line.substring(0, idx);
        // vars: freq=5 foo bar somevar=someval
        // we'll pick up all tokens with a = in them and assume they mean key=value
        StringTokenizer varsSt = new StringTokenizer(vars);
        JSONObject group = new JSONObject();
        while (varsSt.hasMoreTokens()) {
            String str = varsSt.nextToken();
            // str: freq=5
            int x = str.indexOf("=");
            if (x >= 0) {
                String key = str.substring(0, x);
                String value = "";
                // we should handle the case of key= (empty string)
                if (x < str.length() - 1)
                    value = str.substring(x + 1);
                group.put(key, value);
            }
        }
        String groupsStr = line.substring(idx + 1);
        // groupsStr: seojiwon@gmail.com sseong@stanford.edu debangsu@cs.stanford.edu
        StringTokenizer st = new StringTokenizer(groupsStr);
        JSONArray groupMembers = new JSONArray();
        int i = 0;
        while (st.hasMoreTokens()) {
            String s = st.nextToken();
            Contact ci = ab.lookupByEmail(s);
            if (ci == null) {
                System.out.println("WARNING: no contact info for email address: " + s);
                continue;
            }
            groupMembers.put(i++, ci.toJson());
        }
        group.put("members", groupMembers);
        groups.put(groupNum, group);
        groupNum++;
    }
    result.put("groups", groups);
    return result.toString();
}
Also used : StringTokenizer(java.util.StringTokenizer) InputStreamReader(java.io.InputStreamReader) JSONObject(org.json.JSONObject) JSONArray(org.json.JSONArray) FileInputStream(java.io.FileInputStream) LineNumberReader(java.io.LineNumberReader) Contact(edu.stanford.muse.AddressBookManager.Contact)

Example 78 with LineNumberReader

use of java.io.LineNumberReader in project epadd by ePADD.

the class WPMine method readTypes.

private static void readTypes() throws IOException {
    int count = 0;
    try {
        LineNumberReader lnr = new LineNumberReader(new FileReader(typesFile));
        while (true) {
            String line = lnr.readLine();
            if (line == null)
                break;
            count++;
            StringTokenizer st = new StringTokenizer(line);
            String r = st.nextToken().toLowerCase().trim();
            Info I = hitTitles.get(r);
            if (I == null)
                continue;
            I.type = st.nextToken();
        }
        lnr.close();
    } catch (Exception e) {
        err.println("Unable to read types file, err at line " + count);
        e.printStackTrace(err);
    }
}
Also used : StringTokenizer(java.util.StringTokenizer) FileReader(java.io.FileReader) IOException(java.io.IOException) LineNumberReader(java.io.LineNumberReader)

Example 79 with LineNumberReader

use of java.io.LineNumberReader in project vscrawler by virjar.

the class AwkParser method parse.

/**
 * Parse the script streamed by script_reader. Build and return the
 * root of the abstract syntax tree which represents the Jawk script.
 *
 * @param script_reader The Reader streaming the script to parse.
 *
 * @return The abstract syntax tree of this script.
 *
 * @throws IOException upon an IO error.
 */
public AwkSyntaxTree parse(List<ScriptSource> scriptSources) throws IOException {
    if ((scriptSources == null) || scriptSources.isEmpty()) {
        throw new IOException("No script sources supplied");
    }
    this.scriptSources = scriptSources;
    scriptSourcesCurrentIndex = 0;
    reader = new LineNumberReader(scriptSources.get(scriptSourcesCurrentIndex).getReader());
    read();
    lexer();
    return SCRIPT();
}
Also used : IOException(java.io.IOException) LineNumberReader(java.io.LineNumberReader)

Example 80 with LineNumberReader

use of java.io.LineNumberReader in project uavstack by uavorg.

the class TaildirLogComponent method writePosition.

private void writePosition(boolean isAppend) {
    File file = new File(positionFilePath);
    LineNumberReader reader = null;
    FileWriter writer = null;
    @SuppressWarnings("rawtypes") List<Map> json = Lists.newArrayList();
    if (!existingInodes.isEmpty()) {
        json.addAll(toPosInfoJson());
    }
    try {
        reader = new LineNumberReader(new FileReader(file));
        JSONArray array = Optional.fromNullable(JSONObject.parseArray(reader.readLine())).or(new JSONArray());
        for (int i = 0; i < array.size(); i++) {
            JSONObject posfile = array.getJSONObject(i);
            if (!existingInodes.contains(posfile.getLongValue("inode")))
                json.add(ImmutableMap.of("inode", posfile.getLongValue("inode"), "pos", posfile.getLongValue("pos"), "num", posfile.getLong("num"), "file", posfile.getString("file")));
        }
        writer = new FileWriter(file);
        writer.write(JSONObject.toJSONString(json));
    } catch (Throwable t) {
        log.err(this, "Failed writing positionFile", t);
    } finally {
        try {
            if (reader != null)
                reader.close();
        } catch (IOException e) {
            log.err(this, "Error: " + e.getMessage(), e);
        }
        try {
            if (writer != null)
                writer.close();
        } catch (IOException e) {
            log.err(this, "Error: " + e.getMessage(), e);
        }
    }
}
Also used : JSONObject(com.alibaba.fastjson.JSONObject) FileWriter(java.io.FileWriter) JSONArray(com.alibaba.fastjson.JSONArray) FileReader(java.io.FileReader) IOException(java.io.IOException) File(java.io.File) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap) 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