Return-Path: Delivered-To: apmail-directory-commits-archive@www.apache.org Received: (qmail 65262 invoked from network); 15 Jan 2007 13:02:43 -0000 Received: from hermes.apache.org (HELO mail.apache.org) (140.211.11.2) by minotaur.apache.org with SMTP; 15 Jan 2007 13:02:43 -0000 Received: (qmail 40630 invoked by uid 500); 15 Jan 2007 13:02:50 -0000 Delivered-To: apmail-directory-commits-archive@directory.apache.org Received: (qmail 40590 invoked by uid 500); 15 Jan 2007 13:02:49 -0000 Mailing-List: contact commits-help@directory.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@directory.apache.org Delivered-To: mailing list commits@directory.apache.org Received: (qmail 40579 invoked by uid 99); 15 Jan 2007 13:02:49 -0000 Received: from herse.apache.org (HELO herse.apache.org) (140.211.11.133) by apache.org (qpsmtpd/0.29) with ESMTP; Mon, 15 Jan 2007 05:02:49 -0800 X-ASF-Spam-Status: No, hits=-9.4 required=10.0 tests=ALL_TRUSTED,NO_REAL_NAME X-Spam-Check-By: apache.org Received: from [140.211.11.3] (HELO eris.apache.org) (140.211.11.3) by apache.org (qpsmtpd/0.29) with ESMTP; Mon, 15 Jan 2007 05:02:42 -0800 Received: by eris.apache.org (Postfix, from userid 65534) id EB4621A981A; Mon, 15 Jan 2007 05:01:38 -0800 (PST) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: svn commit: r496304 - in /directory/branches/shared/0.9.5/ldap/src: main/java/org/apache/directory/shared/ldap/ldif/LdifUtils.java test/java/org/apache/directory/shared/ldap/ldif/LdifUtilsTest.java Date: Mon, 15 Jan 2007 13:01:38 -0000 To: commits@directory.apache.org From: elecharny@apache.org X-Mailer: svnmailer-1.1.0 Message-Id: <20070115130138.EB4621A981A@eris.apache.org> X-Virus-Checked: Checked by ClamAV on apache.org Author: elecharny Date: Mon Jan 15 05:01:38 2007 New Revision: 496304 URL: http://svn.apache.org/viewvc?view=rev&rev=496304 Log: Fixed DIRSERVER-827 Modified: directory/branches/shared/0.9.5/ldap/src/main/java/org/apache/directory/shared/ldap/ldif/LdifUtils.java directory/branches/shared/0.9.5/ldap/src/test/java/org/apache/directory/shared/ldap/ldif/LdifUtilsTest.java Modified: directory/branches/shared/0.9.5/ldap/src/main/java/org/apache/directory/shared/ldap/ldif/LdifUtils.java URL: http://svn.apache.org/viewvc/directory/branches/shared/0.9.5/ldap/src/main/java/org/apache/directory/shared/ldap/ldif/LdifUtils.java?view=diff&rev=496304&r1=496303&r2=496304 ============================================================================== --- directory/branches/shared/0.9.5/ldap/src/main/java/org/apache/directory/shared/ldap/ldif/LdifUtils.java (original) +++ directory/branches/shared/0.9.5/ldap/src/main/java/org/apache/directory/shared/ldap/ldif/LdifUtils.java Mon Jan 15 05:01:38 2007 @@ -186,46 +186,62 @@ return sb.toString(); } - - /** - * Strips the String every n specified characters - * @param str the string to strip - * @param nbChars the number of characters - * @return the stripped String - */ - private static String stripLineToNChars( String str, int nbChars) - { - if ( str.length() <= nbChars ) - { - return str; - } - - StringBuffer sb = new StringBuffer(); - String substr; - int i = 0; - boolean firstPass = true; - - while ( i < (str.length() - nbChars) ) { - if ( firstPass ) - { - substr = str.substring( i, i + nbChars); - firstPass = false; - // Since we add a space at the beginning of the next line, - // we need to update nbChars - nbChars--; - } - else - { - substr = str.substring( i, i + nbChars ); - } - - sb.append( substr + "\n " ); - i = i + nbChars; - } - // Adding the last characters - sb.append( str.substring(i, str.length() ) ); - - return sb.toString(); - } + /** + * Strips the String every n specified characters + * @param str the string to strip + * @param nbChars the number of characters + * @return the stripped String + */ + public static String stripLineToNChars( String str, int nbChars) + { + int strLength = str.length(); + + if ( strLength <= nbChars ) + { + return str; + } + + if ( nbChars < 2 ) + { + throw new IllegalArgumentException( "The length of each line must be at least 2 chars long" ); + } + + // We will first compute the new size of the LDIF result + // It's at least nbChars chars plus one for \n + int charsPerLine = nbChars - 1; + + int remaining = ( strLength - nbChars ) % charsPerLine; + + int nbLines = 1 + ( ( strLength - nbChars ) / charsPerLine ) + + ( remaining == 0 ? 0 : 1 ); + + int nbCharsTotal = strLength + nbLines + nbLines - 2; + + char[] buffer = new char[ nbCharsTotal ]; + char[] orig = str.toCharArray(); + + int posSrc = 0; + int posDst = 0; + + System.arraycopy( orig, posSrc, buffer, posDst, nbChars ); + posSrc += nbChars; + posDst += nbChars; + + for ( int i = 0; i < nbLines - 2; i ++ ) + { + buffer[posDst++] = '\n'; + buffer[posDst++] = ' '; + + System.arraycopy( orig, posSrc, buffer, posDst, charsPerLine ); + posSrc += charsPerLine; + posDst += charsPerLine; + } + + buffer[posDst++] = '\n'; + buffer[posDst++] = ' '; + System.arraycopy( orig, posSrc, buffer, posDst, remaining == 0 ? charsPerLine : remaining ); + + return new String( buffer ); + } } Modified: directory/branches/shared/0.9.5/ldap/src/test/java/org/apache/directory/shared/ldap/ldif/LdifUtilsTest.java URL: http://svn.apache.org/viewvc/directory/branches/shared/0.9.5/ldap/src/test/java/org/apache/directory/shared/ldap/ldif/LdifUtilsTest.java?view=diff&rev=496304&r1=496303&r2=496304 ============================================================================== --- directory/branches/shared/0.9.5/ldap/src/test/java/org/apache/directory/shared/ldap/ldif/LdifUtilsTest.java (original) +++ directory/branches/shared/0.9.5/ldap/src/test/java/org/apache/directory/shared/ldap/ldif/LdifUtilsTest.java Mon Jan 15 05:01:38 2007 @@ -186,4 +186,43 @@ { assertTrue( LdifUtils.isLDIFSafe( testString ) ); } + + public void testStripLineToNChars() + { + String line = "abc"; + + try + { + LdifUtils.stripLineToNChars( line, 1 ); + fail(); + } + catch ( IllegalArgumentException iae ) + { + // This is correct + } + + String res = LdifUtils.stripLineToNChars( line, 2 ); + assertEquals( "ab\n c", res ); + assertEquals( "abc", LdifUtils.stripLineToNChars( line, 3 ) ); + } + + public void testStripLineTo5Chars() + { + assertEquals( "a", LdifUtils.stripLineToNChars( "a", 5 ) ); + assertEquals( "ab", LdifUtils.stripLineToNChars( "ab", 5 ) ); + assertEquals( "abc", LdifUtils.stripLineToNChars( "abc", 5 ) ); + assertEquals( "abcd", LdifUtils.stripLineToNChars( "abcd", 5 ) ); + assertEquals( "abcde", LdifUtils.stripLineToNChars( "abcde", 5 ) ); + assertEquals( "abcde\n f", LdifUtils.stripLineToNChars( "abcdef", 5 ) ); + assertEquals( "abcde\n fg", LdifUtils.stripLineToNChars( "abcdefg", 5 ) ); + assertEquals( "abcde\n fgh", LdifUtils.stripLineToNChars( "abcdefgh", 5 ) ); + assertEquals( "abcde\n fghi", LdifUtils.stripLineToNChars( "abcdefghi", 5 ) ); + assertEquals( "abcde\n fghi\n j", LdifUtils.stripLineToNChars( "abcdefghij", 5 ) ); + assertEquals( "abcde\n fghi\n jk", LdifUtils.stripLineToNChars( "abcdefghijk", 5 ) ); + assertEquals( "abcde\n fghi\n jkl", LdifUtils.stripLineToNChars( "abcdefghijkl", 5 ) ); + assertEquals( "abcde\n fghi\n jklm", LdifUtils.stripLineToNChars( "abcdefghijklm", 5 ) ); + assertEquals( "abcde\n fghi\n jklm\n n", LdifUtils.stripLineToNChars( "abcdefghijklmn", 5 ) ); + assertEquals( "abcde\n fghi\n jklm\n no", LdifUtils.stripLineToNChars( "abcdefghijklmno", 5 ) ); + assertEquals( "abcde\n fghi\n jklm\n nop", LdifUtils.stripLineToNChars( "abcdefghijklmnop", 5 ) ); + } }