--- /james/server/trunk/src/java/org/apache/james/transport/mailets/UnwrapText.java 2006-09-24 15:20:31.000000000 +0200 +++ /james/server/trunk/src/java/org/apache/james/transport/mailets/UnwrapText.java 2006-09-24 15:25:22.000000000 +0200 @@ -22,9 +22,9 @@ * The value should be "WIDTH+X" if the original length is known, "-X" otherwise. * In the latter case the length of the longer line will be used. * X is the tollerance needed for the quoting chars: if the original width is known the suggested - * value for X is 2 (because of "> " prefix), otherwise it is suggested to increase it to a value + * value for X is 2 (because of "> " prefix), otherwise it is suggested to increase it to a value * like 10 (-10) - * + * * In summary, if the original wrap is known (for example 76, for flowed messages) * quotewidth = 78 * Otherwise @@ -32,12 +32,12 @@ */ public class UnwrapText extends GenericMailet { public final static String PARAMETER_NAME_QUOTEWIDTH = "quotewidth"; - + private int quotewidth; - + /** * returns a String describing this mailet. - * + * * @return A desciption of this mailet */ public String getMailetInfo() { @@ -51,10 +51,10 @@ public void service(Mail mail) throws MailetException { try { // TODO replace non standard quotes (at least "> " with ">", otherwise the widely used "> > >" will not work. - - if (FlowedMessageUtils.isFlowedTextMessage(mail.getMessage())) + + if (FlowedMessageUtils.isFlowedTextMessage(mail.getMessage())) { FlowedMessageUtils.deflowMessage(mail.getMessage()); - + } else { Object o = mail.getMessage().getContent(); if (o instanceof String) { @@ -63,39 +63,38 @@ mail.getMessage().saveChanges(); } } - - } catch (MessagingException e) { + } + catch (MessagingException e) { throw new MailetException("Could not unwrap message", e); - - } catch (IOException e) { + } + catch (IOException e) { throw new MailetException("Could not unwrap message", e); } - } - + public static String unwrap(String text) { return unwrap(text, - 10); } public static String unwrap(String text, int qwidth) { String[] lines = text.split("\r\n|\n", -1); - + //P1: Manage spaces without trims Pattern p1 = Pattern.compile("([> ]*)(.*[^ .?!][ ]*)$", 0); - - //P2: Quotation char at the begin of a line and the first word starts with a lowercase char or a number. The word ends with a space, a tab or a lineend. + + //P2: Quotation char at the begin of a line and the first word starts with a lowercase char or a number. The word ends with a space, a tab or a lineend. Pattern p2 = Pattern.compile("^([> ]*)(([a-zטיאשלע][^ \t\r\n]*|[0-9][0-9,.]*)([ \t].*$|$))", 0); - + // Width computation int width = 0; for (int i = 0; i < lines.length - 1; i++) { String l = lines[i].trim(); if (l.length() > width) width = l.length(); } - + if (width < 40) return text; if (qwidth < 0) qwidth = width - qwidth; - + StringBuffer result = new StringBuffer(); int prevWrapped = 0; for (int i = 0; i < lines.length; i++) { @@ -103,7 +102,9 @@ if (prevWrapped > 0 ) { if (result.charAt(result.length() - 1) != ' ') result.append(" "); } - else result.append("\r\n"); + else { + result.append("\r\n"); + } } String l = lines[i]; Matcher m1 = p1.matcher(l); @@ -118,13 +119,14 @@ // The following line has no quoting (while the previous yes) (!b && m2.group(1).trim().equals("") && (w = l.length() + m2.group(2).trim().length() + 1) > width && w <= qwidth) )) { - + if (b) { if (prevWrapped > 0 && m1.groupCount() >= 2) result.append(m1.group(2)); else result.append(l); prevWrapped = 1; - - } else { + + } + else { lines[i + 1] = l + (l.charAt(l.length() - 1) != ' ' ? " " : "") + m2.group(2).trim(); // Revert the previous append if (prevWrapped != 0) { @@ -132,16 +134,15 @@ else result.delete(result.length() - 2, result.length()); } } - - } else { + } + else { Matcher m3 = p2.matcher(l); if (prevWrapped > 0 && m3.matches()) result.append(m3.group(2)); else result.append(lines[i]); prevWrapped = -1; } } - return result.toString(); } - + }