class POTResource extends ResourceFilter {

    @Override
    public void merge(InputStream baseStream, OutputStream outStream, LanguageBundle languageBundle,
            FilterOptions options) throws IOException, ResourceFilterException {
        // put res data into a map for easier searching
        Map<String, String> kvMap = Utils.createKeyValueMap(languageBundle.getResourceStrings());

        BufferedReader reader = new BufferedReader(new InputStreamReader(baseStream, CHAR_SET));
        BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(outStream, CHAR_SET));

        BreakIterator brkItr = Utils.getWordBreakIterator(options);

        String line;
        while ((line = reader.readLine()) != null) {
            // if the line is a msgid, extract the value and check if the key is
            // in the resMap
            if (line.startsWith(UNTRANSLATED_STRING_PREFIX)) {
                String key = extractMessage(line, reader);

                // write the msgid
                do {
                    writer.write(line);
                    writer.newLine();
                } while ((line = reader.readLine()) != null && line.trim().startsWith("\""));

                // write msgstr
                if (!line.startsWith(TRANSLATED_STRING_PREFIX) || key == null || key.isEmpty()
                        || !kvMap.containsKey(key) || kvMap.get(key) == null) {
                    // key not found, write msgstr as-is
                    do {
                        writer.write(line);
                        writer.newLine();
                    } while ((line = reader.readLine()) != null && !line.isEmpty());

                    if (line != null && line.isEmpty()) {
                        writer.newLine();
                    }
                } else {
                    // key found, write new msgstr
                    writer.write(formatMessage(TRANSLATED_STRING_PREFIX, kvMap.get(key), brkItr));
                    writer.newLine();

                    // skip the old msgstr in the input stream
                    while ((line = reader.readLine()) != null && !line.isEmpty())
                        ;
                }
            } else {
                // write line as-is
                writer.write(line);
                writer.newLine();
            }
        }

        writer.flush();
    }

}