directory-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From elecha...@apache.org
Subject svn commit: r159949 - directory/sandbox/trunk/asn1-new-codec/src/java/org/apache/asn1/ldap/codec/utils/LdapDN.java
Date Sun, 03 Apr 2005 18:20:04 GMT
Author: elecharny
Date: Sun Apr  3 11:20:03 2005
New Revision: 159949

URL: http://svn.apache.org/viewcvs?view=rev&rev=159949
Log:
- now returns a MutableString instead of a String
- correctly handle DN's Values (Unicode is allowed, it wasn't before)
- check that Attributes are ASCII, and not Unicode (cf RFC 2253)

Modified:
    directory/sandbox/trunk/asn1-new-codec/src/java/org/apache/asn1/ldap/codec/utils/LdapDN.java

Modified: directory/sandbox/trunk/asn1-new-codec/src/java/org/apache/asn1/ldap/codec/utils/LdapDN.java
URL: http://svn.apache.org/viewcvs/directory/sandbox/trunk/asn1-new-codec/src/java/org/apache/asn1/ldap/codec/utils/LdapDN.java?view=diff&r1=159948&r2=159949
==============================================================================
--- directory/sandbox/trunk/asn1-new-codec/src/java/org/apache/asn1/ldap/codec/utils/LdapDN.java
(original)
+++ directory/sandbox/trunk/asn1-new-codec/src/java/org/apache/asn1/ldap/codec/utils/LdapDN.java
Sun Apr  3 11:20:03 2005
@@ -17,6 +17,11 @@
 package org.apache.asn1.ldap.codec.utils;
 
 import org.apache.asn1.ldap.codec.DecoderException;
+import org.apache.asn1.util.MutableString;
+import org.apache.asn1.util.StringUtils;
+import org.apache.asn1.util.pools.LocalPoolManager;
+import org.apache.asn1.util.pools.PoolException;
+import org.apache.asn1.util.pools.PoolManager;
 
 
 /**
@@ -54,10 +59,10 @@
     //~ Static fields/initializers -----------------------------------------------------------------
 
     /** "oid." static */
-    private static final char[] OID_LOWER = new char[] { 'o', 'i', 'd', '.' };
+    private static final byte[] OID_LOWER = new byte[] { 'o', 'i', 'd', '.' };
 
     /** "OID." static */
-    private static final char[] OID_UPPER = new char[] { 'O', 'I', 'D', '.' };
+    private static final byte[] OID_UPPER = new byte[] { 'O', 'I', 'D', '.' };
 
     //~ Methods ------------------------------------------------------------------------------------
 
@@ -71,10 +76,10 @@
      * @param pos The current position in the byte buffer
      * @return The new position in the byte buffer
     */
-    private static int parseSpaces( char[] bytes, int pos )
+    private static int parseSpaces( byte[] bytes, int pos )
     {
 
-        while ( DNUtils.isChar( bytes, pos, ' ' ) )
+        while ( DNUtils.isCharASCII( bytes, pos, ' ' ) )
         {
             pos++;
         }
@@ -94,10 +99,9 @@
      * @param pos The current position in the byte buffer
      * @return The new position in the byte buffer, or -1 if the rule does not apply to the
byte buffer
      */
-    private static int parseAttributeValue( char[] bytes, int pos )
+    private static int parseAttributeValue( byte[] bytes, int pos )
     {
-
-        if ( DNUtils.isChar( bytes, pos, '#' ) )
+        if ( DNUtils.isCharASCII( bytes, pos, '#' ) )
         {
             pos++;
 
@@ -110,16 +114,16 @@
 
             return parseSpaces( bytes, pos );
         }
-        else if ( DNUtils.isChar( bytes, pos, '"' ) )
+        else if ( DNUtils.isCharASCII( bytes, pos, '"' ) )
         {
             pos++;
+            int nbBytes = 0;
 
             // <attributeValue>     ::= '"' <quotechar-or-pair> '"'
             // <quotechar-or-pairs>    ::= <quotechar> <quotechar-or-pairs>
| '\' <pairchar> <quotechar-or-pairs> | e
             while ( true )
             {
-
-                if ( DNUtils.isChar( bytes, pos, '\\' ) )
+                if ( DNUtils.isCharASCII( bytes, pos, '\\' ) )
                 {
                     pos++;
 
@@ -129,22 +133,20 @@
                     }
                     else
                     {
-
                         return -1;
                     }
                 }
-                else if ( DNUtils.isQuoteChar( bytes, pos ) )
+                else if ( (nbBytes = DNUtils.isQuoteChar( bytes, pos ) ) != -1 )
                 {
-                    pos++;
+                    pos += nbBytes;
                 }
                 else
                 {
-
                     break;
                 }
             }
 
-            if ( DNUtils.isChar( bytes, pos, '"' ) )
+            if ( DNUtils.isCharASCII( bytes, pos, '"' ) )
             {
                 pos++;
 
@@ -152,25 +154,20 @@
             }
             else
             {
-
                 return -1;
             }
         }
         else
         {
-
             while ( true )
             {
-
-                if ( DNUtils.isChar( bytes, pos, '\\' ) )
+                if ( DNUtils.isCharASCII( bytes, pos, '\\' ) )
                 {
-
                     // '\' <pairchar> <pairs-or-strings>
                     pos++;
 
                     if ( DNUtils.isPairChar( bytes, pos ) == false )
                     {
-
                         return -1;
                     }
                     else
@@ -180,35 +177,34 @@
                 }
                 else
                 {
-
+                    int nbBytes = 0;
+                    
                     // <stringchar> <pairs-or-strings>
-                    if ( DNUtils.isStringChar( bytes, pos ) )
+                    if ( (nbBytes = DNUtils.isStringChar( bytes, pos )) != -1)
                     {
-
                         // A special case : if we have some spaces before the '+' character,
                         // we MUST skip them.
                         int initPos = pos;
 
-                        if ( DNUtils.isChar( bytes, pos, ' ' ) )
+                        if ( DNUtils.isCharASCII( bytes, pos, ' ') )
                         {
                             pos = parseSpaces( bytes, pos );
 
-                            if ( ( DNUtils.isStringChar( bytes, pos ) == false ) &&
-                                    ( DNUtils.isChar( bytes, pos, '\\' ) == false ) )
+                            if ( ( DNUtils.isStringChar( bytes, pos ) == -1 ) &&
+                                    ( DNUtils.isCharASCII( bytes, pos, '\\' ) == false )
)
                             {
-
                                 // Ok, we are done with the stringchar.
                                 return pos;
                             }
                         }
                         else
                         {
-                            pos++;
+                            // An unicode char could be more than one byte long 
+                            pos += nbBytes;
                         }
                     }
                     else
                     {
-
                         return pos;
                     }
                 }
@@ -226,7 +222,7 @@
      * @param pos The current position in the byte buffer
      * @return The new position in the byte buffer, or -1 if the rule does not apply to the
byte buffer
      */
-    private static int parseOidPrefix( char[] bytes, int pos )
+    private static int parseOidPrefix( byte[] bytes, int pos )
     {
 
         if ( ( DNUtils.areEquals( bytes, pos, OID_LOWER ) == -1 ) &&
@@ -253,7 +249,7 @@
      * @param pos The current position in the byte buffer
      * @return The new position in the byte buffer, or -1 if the rule does not apply to the
byte buffer
      */
-    private static int parseOidValue(char[] bytes, int pos)
+    private static int parseOidValue(byte[] bytes, int pos)
     {
         // <attributType> ::= [0-9] <digits> <oids>
         if ( DNUtils.isDigit( bytes, pos ) == false )
@@ -274,7 +270,7 @@
             }
 
             // <oids> ::= '.' [0-9] <digits> <oids> | e
-            if ( DNUtils.isChar( bytes, pos, '.' ) == false )
+            if ( DNUtils.isCharASCII( bytes, pos, '.' ) == false )
             {
 
                 return pos;
@@ -301,7 +297,7 @@
                         }
                     }
                 }
-                while ( DNUtils.isChar( bytes, pos, '.' ) );
+                while ( DNUtils.isCharASCII( bytes, pos, '.' ) );
 
                 return pos;
             }
@@ -315,16 +311,18 @@
      * 							&lt;oidPrefix&gt; [0-9] &lt;digits&gt; &lt;oids&gt;
| [0-9] &lt;digits&gt; &lt;oids&gt;
      * </p>
      * 
+     * The string *MUST* be an ASCII string, not an unicode string.
+     * 
      * @param bytes The buffer to parse
      * @param pos The current position in the byte buffer
      * @return The new position in the byte buffer, or -1 if the rule does not apply to the
byte buffer
      */
-    private static int parseAttributeType( char[] bytes, int pos )
+    private static int parseAttributeType( byte[] bytes, int pos )
     {
 
         // <attributType> ::= [a-zA-Z] <keychars> | <oidPrefix> [0-9] <digits>
<oids> | [0-9] <digits> <oids>
     	
-        if ( DNUtils.isAlpha( bytes, pos ))
+        if ( DNUtils.isAlphaASCII( bytes, pos ))
         {
             // <attributType> ::= [a-zA-Z] <keychars> | <oidPrefix> [0-9]
<digits> <oids> 
 
@@ -337,12 +335,12 @@
 	        }
             else 
             {
-            	// It's not an oid, it's a String
+            	// It's not an oid, it's a String (ASCII)
                 // <attributType> ::= [a-zA-Z] <keychars>
                 // <keychars>       ::= [a-zA-Z] <keychar> | [0-9] <keychar>
| '-' <keychar> | e
                 pos = oldPos + 1;
 
-                while ( ( DNUtils.parseChar( bytes, pos ) != -1 ) )
+                while ( ( DNUtils.parseCharASCII( bytes, pos ) != -1 ) )
                 {
                     pos++;
                 }
@@ -369,14 +367,14 @@
      * @param pos The current position in the byte buffer
      * @return The new position in the byte buffer, or -1 if the rule does not apply to the
byte buffer
      */
-    private static int parseAttributeTypeAndValues( char[] bytes, int pos )
+    private static int parseAttributeTypeAndValues( byte[] bytes, int pos )
     {
 
         while ( true )
         {
             pos = parseSpaces( bytes, pos );
 
-            if ( DNUtils.isChar( bytes, pos, '+' ) )
+            if ( DNUtils.isCharASCII( bytes, pos, '+' ) )
             {
                 pos++;
             }
@@ -397,7 +395,7 @@
 
             pos = parseSpaces( bytes, pos );
 
-            if ( DNUtils.isChar( bytes, pos, '=' ) )
+            if ( DNUtils.isCharASCII( bytes, pos, '=' ) )
             {
                 pos++;
             }
@@ -423,7 +421,7 @@
      * @param pos The current position in the buffer
      * @return The new position in the byte buffer, or -1 if the rule does not apply to the
byte buffer 
      */
-    private static int parseNameComponent( char[] bytes, int pos )
+    private static int parseNameComponent( byte[] bytes, int pos )
     {
 
         if ( ( pos = parseAttributeType( bytes, pos ) ) == -1 )
@@ -434,7 +432,7 @@
 
         pos = parseSpaces( bytes, pos );
 
-        if ( DNUtils.isChar( bytes, pos, '=' ) == false )
+        if ( DNUtils.isCharASCII( bytes, pos, '=' ) == false )
         {
 
             return -1;
@@ -467,14 +465,13 @@
      * @return A String containing the DN. 
      * @exception A DecoderException is thrown if the buffer does not contains a valid DN.
      */
-    public static String parseDN( char[] bytes ) throws DecoderException
+    public static MutableString parseDN( PoolManager pool, byte[] bytes ) throws DecoderException
     {
 
         // <distinguishedName> ::= e
         if ( ( bytes == null ) || ( bytes.length == 0 ) )
         {
-
-            return "";
+            return MutableString.EMPTY_STRING;
         }
 
         int pos = 0;
@@ -487,8 +484,8 @@
             do
             {
 
-                if ( ( DNUtils.isChar( bytes, pos, ',' ) == false ) &&
-                        ( DNUtils.isChar( bytes, pos, ';' ) == false ) )
+                if ( ( DNUtils.isCharASCII( bytes, pos, ',' ) == false ) &&
+                        ( DNUtils.isCharASCII( bytes, pos, ';' ) == false ) )
                 {
 
                     break;
@@ -506,6 +503,19 @@
             throw new DecoderException( "Bad DN : " + new String( bytes) );
         }
 
-        return new String( bytes );
+        
+        int stringLength = StringUtils.countChars(bytes);
+        
+        try 
+        {
+            MutableString string = (MutableString)(((LocalPoolManager)pool).allocateString(stringLength));
+            string.setData(bytes);
+            
+            return string;
+        }
+        catch (PoolException pe) 
+        {
+            throw new DecoderException("Cannot allocate a MutableString");
+        }
     }
 }



Mime
View raw message