Changeset 277

Show
Ignore:
Timestamp:
08/26/08 08:06:55 PM (4 months ago)
Author:
octorian
Message:

Fix for quoted-printable decoding errors (#98)

Location:
branches/LogicMail-1.0
Files:
2 modified

Legend:

Unmodified
Added
Removed
  • branches/LogicMail-1.0/LogicMail/src/org/logicprobe/LogicMail/util/StringParser.java

    r274 r277  
    732732                    break; 
    733733                } 
    734                 try { 
    735                     int charVal = Integer.parseInt(text.substring(index+1, index+3), 16); 
    736                     buffer.append((char)charVal); 
    737                 } catch (NumberFormatException exp) { } 
    738                 index += 3; 
     734                else { 
     735                    char ch1 = text.charAt(index+1); 
     736                    char ch2 = text.charAt(index+2); 
     737                    if(ch1 == '\r' && ch2 == '\n') { 
     738                        index += 3; 
     739                    } 
     740                    else if(ch1 == '\n') { 
     741                        index += 2; 
     742                    } 
     743                    else { 
     744                        try { 
     745                            int charVal = Integer.parseInt(text.substring(index+1, index+3), 16); 
     746                            buffer.append((char)charVal); 
     747                        } catch (NumberFormatException exp) { } 
     748                        index += 3; 
     749                    } 
     750                } 
    739751            } 
    740752            else { 
     
    753765        char ch; 
    754766        String charStr; 
    755         for(int i=0; i<text.length(); i++) { 
     767        int length = text.length(); 
     768        for(int i=0; i<length; i++) { 
    756769            ch = text.charAt(i); 
    757             if(ch < 128) { 
     770            if(ch != '\t' && ch != ' ' && ch < 128) { 
     771                if(buffer.length() == 75 && i < length - 1) { 
     772                    buffer.append("=\r\n"); 
     773                } 
    758774                buffer.append(ch); 
    759775            } 
    760776            else { 
    761                 charStr = Integer.toHexString((int)ch); 
     777                if(buffer.length() == 73 && i < length - 3) { 
     778                    buffer.append("=\r\n"); 
     779                } 
     780                charStr = Integer.toHexString((int)ch).toUpperCase(); 
    762781                buffer.append('='); 
    763782                if(charStr.length() == 1) { 
     
    767786            } 
    768787        } 
     788         
    769789        return buffer.toString(); 
    770790    } 
  • branches/LogicMail-1.0/LogicMailTests/src/org/logicprobe/LogicMail/util/StringParserTest.java

    r197 r277  
    649649    /** 
    650650     * Test of decodeQuotedPrintable method, of class org.logicprobe.LogicMail.util.StringParser. 
    651      */ 
    652     public void testDecodeQuotedPrintable() { 
     651     * Basic test. 
     652     */ 
     653    public void testDecodeQuotedPrintable1() { 
    653654        System.out.println("decodeQuotedPrintable"); 
    654655        String text = "=A1Hol=E1 Se=F1or!"; 
     
    659660 
    660661    /** 
     662     * Test of decodeQuotedPrintable method, of class org.logicprobe.LogicMail.util.StringParser. 
     663     * Soft line-break test. 
     664     */ 
     665    public void testDecodeQuotedPrintable2() { 
     666        System.out.println("decodeQuotedPrintable"); 
     667        String text = "=A1Hol=E1 Se=F1or!=20=20H=\n"+ 
     668                      "ow=20are=20you=20today?"; 
     669        String expectedResult = "¡Holá Señor!  How are you today?"; 
     670        String result = StringParser.decodeQuotedPrintable(text); 
     671        assertEquals(expectedResult, result); 
     672    } 
     673 
     674    /** 
    661675     * Test of encodeQuotedPrintable method, of class org.logicprobe.LogicMail.util.StringParser. 
    662      */ 
    663     public void testEncodeQuotedPrintable() { 
     676     * Basic test. 
     677     */ 
     678    public void testEncodeQuotedPrintable1() { 
    664679        System.out.println("encodeQuotedPrintable"); 
    665680        String text = "¡Holá Señor!"; 
    666         String expectedResult = "=A1Hol=E1 Se=F1or!".toLowerCase(); 
    667         String result = StringParser.encodeQuotedPrintable(text).toLowerCase(); 
     681        String expectedResult = "=A1Hol=E1=20Se=F1or!"; 
     682        String result = StringParser.encodeQuotedPrintable(text); 
     683        assertEquals(expectedResult, result); 
     684    } 
     685 
     686    /** 
     687     * Test of encodeQuotedPrintable method, of class org.logicprobe.LogicMail.util.StringParser. 
     688     * Soft line-break test. 
     689     */ 
     690    public void testEncodeQuotedPrintable2() { 
     691        System.out.println("encodeQuotedPrintable"); 
     692        String text = "¡Holá Señor! ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789 ¡Holá Señor!"; 
     693        String expectedResult = "=A1Hol=E1=20Se=F1or!=20ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz=\r\n0123456789=20=A1Hol=E1=20Se=F1or!"; 
     694        String result = StringParser.encodeQuotedPrintable(text); 
    668695        assertEquals(expectedResult, result); 
    669696    } 
     
    723750//                ((StringParserTest)tc).testReadWholeStream(); 
    724751//        }})); 
    725         testSuite.addTest(new StringParserTest("decodeQuotedPrintable", new TestMethod() { 
    726             public void run(TestCase tc) { 
    727                 ((StringParserTest)tc).testDecodeQuotedPrintable(); 
    728         }})); 
    729         testSuite.addTest(new StringParserTest("encodeQuotedPrintable", new TestMethod() { 
    730             public void run(TestCase tc) { 
    731                 ((StringParserTest)tc).testEncodeQuotedPrintable(); 
     752        testSuite.addTest(new StringParserTest("decodeQuotedPrintable1", new TestMethod() { 
     753            public void run(TestCase tc) { 
     754                ((StringParserTest)tc).testDecodeQuotedPrintable1(); 
     755        }})); 
     756        testSuite.addTest(new StringParserTest("decodeQuotedPrintable2", new TestMethod() { 
     757            public void run(TestCase tc) { 
     758                ((StringParserTest)tc).testDecodeQuotedPrintable2(); 
     759        }})); 
     760        testSuite.addTest(new StringParserTest("encodeQuotedPrintable1", new TestMethod() { 
     761            public void run(TestCase tc) { 
     762                ((StringParserTest)tc).testEncodeQuotedPrintable1(); 
     763        }})); 
     764        testSuite.addTest(new StringParserTest("encodeQuotedPrintable2", new TestMethod() { 
     765            public void run(TestCase tc) { 
     766                ((StringParserTest)tc).testEncodeQuotedPrintable2(); 
    732767        }})); 
    733768        return testSuite;