@Test public void testStripQuoted() throws Exception { StringWriter writer = new StringWriter(); MailOptions options = new MailOptions(); options.setSeparator(":::"); options.setCharset(Charsets.UTF_8); options.setPatternsToMatch(new Pattern[]{ MailProcessor.SUBJECT_PREFIX}); options.setInput(new File(System.getProperty("user.dir"))); options.setIncludeBody(true); MailProcessor proc = new MailProcessor(options, "", writer); URL url = MailProcessorTest.class.getClassLoader().getResource("test.mbox"); File file = new File(url.toURI()); long count = proc.parseMboxLineByLine(file); assertEquals(7, count); assertTrue(writer.getBuffer().toString().contains("> Cocoon Cron Block Configurable Clustering")); writer = new StringWriter(); proc = new MailProcessor(options, "", writer); options.setStripQuotedText(true); count = proc.parseMboxLineByLine(file); assertEquals(7, count); assertFalse(writer.getBuffer().toString().contains("> Cocoon Cron Block Configurable Clustering")); }
public long parseMboxLineByLine(File mboxFile) throws IOException { long messageCount = 0; try { StringBuilder contents = new StringBuilder(); StringBuilder body = new StringBuilder(); Matcher messageIdMatcher = MESSAGE_ID_PREFIX.matcher(""); Matcher messageBoundaryMatcher = MESSAGE_START.matcher(""); String[] patternResults = new String[options.getPatternsToMatch().length]; Matcher[] matchers = new Matcher[options.getPatternsToMatch().length]; for (int i = 0; i < matchers.length; i++) { matchers[i] = options.getPatternsToMatch()[i].matcher(""); } String messageId = null; boolean inBody = false; Pattern quotedTextPattern = options.getQuotedTextPattern(); for (String nextLine : new FileLineIterable(mboxFile, options.getCharset(), false)) { if (options.isStripQuotedText() && quotedTextPattern.matcher(nextLine).find()) { continue; } for (int i = 0; i < matchers.length; i++) { Matcher matcher = matchers[i]; matcher.reset(nextLine); if (matcher.matches()) { patternResults[i] = matcher.group(1); } } if (messageId != null) { messageBoundaryMatcher.reset(nextLine); if (messageBoundaryMatcher.matches()) { String key = generateKey(mboxFile, prefix, messageId); writeContent(options.getSeparator(), contents, body, patternResults); writer.write(key, contents.toString()); contents.setLength(0); body.setLength(0); messageId = null; inBody = false; } else { if (inBody && options.isIncludeBody()) { if (!nextLine.isEmpty()) { body.append(nextLine).append(options.getBodySeparator()); } } else { inBody = nextLine.isEmpty(); } } } else { if (nextLine.length() > 14) { messageIdMatcher.reset(nextLine); if (messageIdMatcher.matches()) { messageId = messageIdMatcher.group(1); ++messageCount; } } } } if (messageId != null) { String key = generateKey(mboxFile, prefix, messageId); writeContent(options.getSeparator(), contents, body, patternResults); writer.write(key, contents.toString()); contents.setLength(0); } } catch (FileNotFoundException e) { log.warn("Unable to process non-existing file", e); } return messageCount; }
MailProcessor { public long parseMboxLineByLine(File mboxFile) throws IOException { long messageCount = 0; try { StringBuilder contents = new StringBuilder(); StringBuilder body = new StringBuilder(); Matcher messageIdMatcher = MESSAGE_ID_PREFIX.matcher(""); Matcher messageBoundaryMatcher = MESSAGE_START.matcher(""); String[] patternResults = new String[options.getPatternsToMatch().length]; Matcher[] matchers = new Matcher[options.getPatternsToMatch().length]; for (int i = 0; i < matchers.length; i++) { matchers[i] = options.getPatternsToMatch()[i].matcher(""); } String messageId = null; boolean inBody = false; Pattern quotedTextPattern = options.getQuotedTextPattern(); for (String nextLine : new FileLineIterable(mboxFile, options.getCharset(), false)) { if (options.isStripQuotedText() && quotedTextPattern.matcher(nextLine).find()) { continue; } for (int i = 0; i < matchers.length; i++) { Matcher matcher = matchers[i]; matcher.reset(nextLine); if (matcher.matches()) { patternResults[i] = matcher.group(1); } } if (messageId != null) { messageBoundaryMatcher.reset(nextLine); if (messageBoundaryMatcher.matches()) { String key = generateKey(mboxFile, prefix, messageId); writeContent(options.getSeparator(), contents, body, patternResults); writer.write(key, contents.toString()); contents.setLength(0); body.setLength(0); messageId = null; inBody = false; } else { if (inBody && options.isIncludeBody()) { if (!nextLine.isEmpty()) { body.append(nextLine).append(options.getBodySeparator()); } } else { inBody = nextLine.isEmpty(); } } } else { if (nextLine.length() > 14) { messageIdMatcher.reset(nextLine); if (messageIdMatcher.matches()) { messageId = messageIdMatcher.group(1); ++messageCount; } } } } if (messageId != null) { String key = generateKey(mboxFile, prefix, messageId); writeContent(options.getSeparator(), contents, body, patternResults); writer.write(key, contents.toString()); contents.setLength(0); } } catch (FileNotFoundException e) { log.warn("Unable to process non-existing file", e); } return messageCount; } }
MailProcessor { public long parseMboxLineByLine(File mboxFile) throws IOException { long messageCount = 0; try { StringBuilder contents = new StringBuilder(); StringBuilder body = new StringBuilder(); Matcher messageIdMatcher = MESSAGE_ID_PREFIX.matcher(""); Matcher messageBoundaryMatcher = MESSAGE_START.matcher(""); String[] patternResults = new String[options.getPatternsToMatch().length]; Matcher[] matchers = new Matcher[options.getPatternsToMatch().length]; for (int i = 0; i < matchers.length; i++) { matchers[i] = options.getPatternsToMatch()[i].matcher(""); } String messageId = null; boolean inBody = false; Pattern quotedTextPattern = options.getQuotedTextPattern(); for (String nextLine : new FileLineIterable(mboxFile, options.getCharset(), false)) { if (options.isStripQuotedText() && quotedTextPattern.matcher(nextLine).find()) { continue; } for (int i = 0; i < matchers.length; i++) { Matcher matcher = matchers[i]; matcher.reset(nextLine); if (matcher.matches()) { patternResults[i] = matcher.group(1); } } if (messageId != null) { messageBoundaryMatcher.reset(nextLine); if (messageBoundaryMatcher.matches()) { String key = generateKey(mboxFile, prefix, messageId); writeContent(options.getSeparator(), contents, body, patternResults); writer.write(key, contents.toString()); contents.setLength(0); body.setLength(0); messageId = null; inBody = false; } else { if (inBody && options.isIncludeBody()) { if (!nextLine.isEmpty()) { body.append(nextLine).append(options.getBodySeparator()); } } else { inBody = nextLine.isEmpty(); } } } else { if (nextLine.length() > 14) { messageIdMatcher.reset(nextLine); if (messageIdMatcher.matches()) { messageId = messageIdMatcher.group(1); ++messageCount; } } } } if (messageId != null) { String key = generateKey(mboxFile, prefix, messageId); writeContent(options.getSeparator(), contents, body, patternResults); writer.write(key, contents.toString()); contents.setLength(0); } } catch (FileNotFoundException e) { log.warn("Unable to process non-existing file", e); } return messageCount; } MailProcessor(MailOptions options, String prefix, Writer writer);  MailProcessor(MailOptions options, String prefix, ChunkedWriter writer); }
MailProcessor { public long parseMboxLineByLine(File mboxFile) throws IOException { long messageCount = 0; try { StringBuilder contents = new StringBuilder(); StringBuilder body = new StringBuilder(); Matcher messageIdMatcher = MESSAGE_ID_PREFIX.matcher(""); Matcher messageBoundaryMatcher = MESSAGE_START.matcher(""); String[] patternResults = new String[options.getPatternsToMatch().length]; Matcher[] matchers = new Matcher[options.getPatternsToMatch().length]; for (int i = 0; i < matchers.length; i++) { matchers[i] = options.getPatternsToMatch()[i].matcher(""); } String messageId = null; boolean inBody = false; Pattern quotedTextPattern = options.getQuotedTextPattern(); for (String nextLine : new FileLineIterable(mboxFile, options.getCharset(), false)) { if (options.isStripQuotedText() && quotedTextPattern.matcher(nextLine).find()) { continue; } for (int i = 0; i < matchers.length; i++) { Matcher matcher = matchers[i]; matcher.reset(nextLine); if (matcher.matches()) { patternResults[i] = matcher.group(1); } } if (messageId != null) { messageBoundaryMatcher.reset(nextLine); if (messageBoundaryMatcher.matches()) { String key = generateKey(mboxFile, prefix, messageId); writeContent(options.getSeparator(), contents, body, patternResults); writer.write(key, contents.toString()); contents.setLength(0); body.setLength(0); messageId = null; inBody = false; } else { if (inBody && options.isIncludeBody()) { if (!nextLine.isEmpty()) { body.append(nextLine).append(options.getBodySeparator()); } } else { inBody = nextLine.isEmpty(); } } } else { if (nextLine.length() > 14) { messageIdMatcher.reset(nextLine); if (messageIdMatcher.matches()) { messageId = messageIdMatcher.group(1); ++messageCount; } } } } if (messageId != null) { String key = generateKey(mboxFile, prefix, messageId); writeContent(options.getSeparator(), contents, body, patternResults); writer.write(key, contents.toString()); contents.setLength(0); } } catch (FileNotFoundException e) { log.warn("Unable to process non-existing file", e); } return messageCount; } MailProcessor(MailOptions options, String prefix, Writer writer);  MailProcessor(MailOptions options, String prefix, ChunkedWriter writer); long parseMboxLineByLine(File mboxFile); String getPrefix(); MailOptions getOptions(); }
MailProcessor { public long parseMboxLineByLine(File mboxFile) throws IOException { long messageCount = 0; try { StringBuilder contents = new StringBuilder(); StringBuilder body = new StringBuilder(); Matcher messageIdMatcher = MESSAGE_ID_PREFIX.matcher(""); Matcher messageBoundaryMatcher = MESSAGE_START.matcher(""); String[] patternResults = new String[options.getPatternsToMatch().length]; Matcher[] matchers = new Matcher[options.getPatternsToMatch().length]; for (int i = 0; i < matchers.length; i++) { matchers[i] = options.getPatternsToMatch()[i].matcher(""); } String messageId = null; boolean inBody = false; Pattern quotedTextPattern = options.getQuotedTextPattern(); for (String nextLine : new FileLineIterable(mboxFile, options.getCharset(), false)) { if (options.isStripQuotedText() && quotedTextPattern.matcher(nextLine).find()) { continue; } for (int i = 0; i < matchers.length; i++) { Matcher matcher = matchers[i]; matcher.reset(nextLine); if (matcher.matches()) { patternResults[i] = matcher.group(1); } } if (messageId != null) { messageBoundaryMatcher.reset(nextLine); if (messageBoundaryMatcher.matches()) { String key = generateKey(mboxFile, prefix, messageId); writeContent(options.getSeparator(), contents, body, patternResults); writer.write(key, contents.toString()); contents.setLength(0); body.setLength(0); messageId = null; inBody = false; } else { if (inBody && options.isIncludeBody()) { if (!nextLine.isEmpty()) { body.append(nextLine).append(options.getBodySeparator()); } } else { inBody = nextLine.isEmpty(); } } } else { if (nextLine.length() > 14) { messageIdMatcher.reset(nextLine); if (messageIdMatcher.matches()) { messageId = messageIdMatcher.group(1); ++messageCount; } } } } if (messageId != null) { String key = generateKey(mboxFile, prefix, messageId); writeContent(options.getSeparator(), contents, body, patternResults); writer.write(key, contents.toString()); contents.setLength(0); } } catch (FileNotFoundException e) { log.warn("Unable to process non-existing file", e); } return messageCount; } MailProcessor(MailOptions options, String prefix, Writer writer);  MailProcessor(MailOptions options, String prefix, ChunkedWriter writer); long parseMboxLineByLine(File mboxFile); String getPrefix(); MailOptions getOptions(); static final Pattern SUBJECT_PREFIX; static final Pattern FROM_PREFIX; static final Pattern REFS_PREFIX; static final Pattern TO_PREFIX; }