You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@directory.apache.org by el...@apache.org on 2007/01/15 14:01:38 UTC
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
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 ) );
+ }
}