directory-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From elecha...@apache.org
Subject svn commit: r208918 - /directory/sandbox/trunk/asn1-new-codec/src/java/org/apache/asn1/ldap/codec/primitives/RelativeLdapDN.java
Date Sun, 03 Jul 2005 14:20:09 GMT
Author: elecharny
Date: Sun Jul  3 07:20:08 2005
New Revision: 208918

URL: http://svn.apache.org/viewcvs?rev=208918&view=rev
Log:
Added the RelativeLdapDN class.

Added:
    directory/sandbox/trunk/asn1-new-codec/src/java/org/apache/asn1/ldap/codec/primitives/RelativeLdapDN.java

Added: directory/sandbox/trunk/asn1-new-codec/src/java/org/apache/asn1/ldap/codec/primitives/RelativeLdapDN.java
URL: http://svn.apache.org/viewcvs/directory/sandbox/trunk/asn1-new-codec/src/java/org/apache/asn1/ldap/codec/primitives/RelativeLdapDN.java?rev=208918&view=auto
==============================================================================
--- directory/sandbox/trunk/asn1-new-codec/src/java/org/apache/asn1/ldap/codec/primitives/RelativeLdapDN.java
(added)
+++ directory/sandbox/trunk/asn1-new-codec/src/java/org/apache/asn1/ldap/codec/primitives/RelativeLdapDN.java
Sun Jul  3 07:20:08 2005
@@ -0,0 +1,496 @@
+/*
+ *   Copyright 2005 The Apache Software Foundation
+ *
+ *   Licensed under the Apache License, Version 2.0 (the "License");
+ *   you may not use this file except in compliance with the License.
+ *   You may obtain a copy of the License at
+ *
+ *       http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *   Unless required by applicable law or agreed to in writing, software
+ *   distributed under the License is distributed on an "AS IS" BASIS,
+ *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *   See the License for the specific language governing permissions and
+ *   limitations under the License.
+ *
+ */
+package org.apache.asn1.ldap.codec.primitives;
+
+import org.apache.asn1.DecoderException;
+import org.apache.asn1.ldap.codec.utils.DNUtils;
+import org.apache.asn1.util.MutableString;
+import org.apache.asn1.util.StringUtils;
+
+
+/**
+ * This class parses a relative LDdapDN, which is a name-component
+ * of the LdapDN grammar 
+ * 
+ * The relative DN MUST respect this  BNF grammar (a subset of the RFC2253, par. 3, and RFC1779,
fig. 1) <br>
+ * 
+ * <p>
+ *-    &lt;name-component&gt;         ::= &lt;attributeType&gt; &lt;spaces&gt;
'=' &lt;spaces&gt; &lt;attributeValue&gt; &lt;attributeTypeAndValues&gt;
<br>
+ *-    &lt;attributeTypeAndValues&gt; ::= &lt;spaces&gt; '+' &lt;spaces&gt;
&lt;attributeType&gt; &lt;spaces&gt; '=' &lt;spaces&gt; &lt;attributeValue&gt;
&lt;attributeTypeAndValues&gt; | e <br>
+ *-    &lt;attributeType&gt;          ::= [a-zA-Z] &lt;keychars&gt; | &lt;oidPrefix&gt;
[0-9] &lt;digits&gt; &lt;oids&gt; | [0-9] &lt;digits&gt; &lt;oids&gt;
<br>
+ *-    &lt;keychars&gt;               ::= [a-zA-Z] &lt;keychars&gt; | [0-9]
&lt;keychars&gt; | '-' &lt;keychars&gt; | e <br>
+ *-    &lt;oidPrefix&gt;              ::= 'OID.' | 'oid.' | e <br>
+ *-    &lt;oids&gt;                   ::= '.' [0-9] &lt;digits&gt; &lt;oids&gt;
| e <br>
+ *-    &lt;attributeValue&gt;         ::= &lt;pairs-or-strings&gt; | '#'
&lt;hexstring&gt; |'"' &lt;quotechar-or-pairs&gt; '"' <br>
+ *-    &lt;pairs-or-strings&gt;       ::= '\' &lt;pairchar&gt; &lt;pairs-or-strings&gt;
| &lt;stringchar&gt; &lt;pairs-or-strings&gt; | e <br>
+ *-    &lt;quotechar-or-pairs&gt;     ::= &lt;quotechar&gt; &lt;quotechar-or-pairs&gt;
| '\' &lt;pairchar&gt; &lt;quotechar-or-pairs&gt; | e <br>
+ *-    &lt;pairchar&gt;               ::= ',' | '=' | '+' | '&lt;' | '&gt;'
| '#' | ';' | '\' | '"' | [0-9a-fA-F] [0-9a-fA-F]  <br>
+ *-    &lt;hexstring&gt;              ::= [0-9a-fA-F] [0-9a-fA-F] &lt;hexpairs&gt;
<br>
+ *-    &lt;hexpairs&gt;               ::= [0-9a-fA-F] [0-9a-fA-F] &lt;hexpairs&gt;
| e <br>
+ *-    &lt;digits&gt;                 ::= [0-9] &lt;digits&gt; | e <br>
+ *-    &lt;stringchar&gt;             ::= [0x00-0xFF] - [,=+&lt;&gt;#;\"\n\r]
<br>
+ *-    &lt;quotechar&gt;              ::= [0x00-0xFF] - [\"] <br>
+ *-    &lt;spaces&gt;                 ::= ' ' &lt;spaces&gt; | e <br>
+ * </p>
+ *
+ * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
+ */
+public class RelativeLdapDN extends MutableString
+{
+    //~ Static fields/initializers -----------------------------------------------------------------
+
+    /** "oid." static */
+    private static final byte[] OID_LOWER = new byte[] { 'o', 'i', 'd', '.' };
+
+    /** "OID." static */
+    private static final byte[] OID_UPPER = new byte[] { 'O', 'I', 'D', '.' };
+
+    /** A null LdapDN */
+    public transient static final RelativeLdapDN EMPTY_STRING = new RelativeLdapDN();
+
+    //~ Methods ------------------------------------------------------------------------------------
+
+    /**
+     * Walk the buffer while the current char is a Space char
+     * <p>
+     * &lt;spaces&gt;    ::= ' ' &lt;spaces&gt; | e
+     * </p>
+     * 
+     * @param bytes The buffer to parse
+     * @param pos The current position in the byte buffer
+     * @return The new position in the byte buffer
+    */
+    protected static int parseSpaces( byte[] bytes, int pos )
+    {
+
+        while ( StringUtils.isCharASCII( bytes, pos, ' ' ) )
+        {
+            pos++;
+        }
+
+        return pos;
+    }
+
+    /**
+     * Parse this rule : <br>
+     * <p>
+     * &lt;attributeValue&gt;     ::= &lt;pairs-or-strings&gt; | '#' &lt;hexstring&gt;
|'"' &lt;quotechar-or-pairs&gt; '"' <br>
+     * &lt;pairs-or-strings&gt;    ::= '\' &lt;pairchar&gt; &lt;pairs-or-strings&gt;
| &lt;stringchar&gt; &lt;pairs-or-strings&gt; |  | e <br>
+     * &lt;quotechar-or-pairs&gt;    ::= &lt;quotechar&gt; &lt;quotechar-or-pairs&gt;
| '\' &lt;pairchar&gt; &lt;quotechar-or-pairs&gt; | e <br>
+     * </p>
+     * 
+     * @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
+     */
+    protected static int parseAttributeValue( byte[] bytes, int pos )
+    {
+        if ( StringUtils.isCharASCII( bytes, pos, '#' ) )
+        {
+            pos++;
+
+            // <attributeValue> ::= '#' <hexstring>
+            if ( ( pos = DNUtils.parseHexString( bytes, pos ) ) == -1 )
+            {
+
+                return -1;
+            }
+
+            return parseSpaces( bytes, pos );
+        }
+        else if ( StringUtils.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 ( StringUtils.isCharASCII( bytes, pos, '\\' ) )
+                {
+                    pos++;
+
+                    if ( DNUtils.isPairChar( bytes, pos ) )
+                    {
+                        pos++;
+                    }
+                    else
+                    {
+                        return -1;
+                    }
+                }
+                else if ( (nbBytes = DNUtils.isQuoteChar( bytes, pos ) ) != -1 )
+                {
+                    pos += nbBytes;
+                }
+                else
+                {
+                    break;
+                }
+            }
+
+            if ( StringUtils.isCharASCII( bytes, pos, '"' ) )
+            {
+                pos++;
+
+                return parseSpaces( bytes, pos );
+            }
+            else
+            {
+                return -1;
+            }
+        }
+        else
+        {
+            while ( true )
+            {
+                if ( StringUtils.isCharASCII( bytes, pos, '\\' ) )
+                {
+                    // '\' <pairchar> <pairs-or-strings>
+                    pos++;
+
+                    if ( DNUtils.isPairChar( bytes, pos ) == false )
+                    {
+                        return -1;
+                    }
+                    else
+                    {
+                        pos++;
+                    }
+                }
+                else
+                {
+                    int nbBytes = 0;
+                    
+                    // <stringchar> <pairs-or-strings>
+                    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 ( StringUtils.isCharASCII( bytes, pos, ' ') )
+                        {
+                            pos = parseSpaces( bytes, pos );
+
+                            if ( ( DNUtils.isStringChar( bytes, pos ) == -1 ) &&
+                                    ( StringUtils.isCharASCII( bytes, pos, '\\' ) == false
) )
+                            {
+                                // Ok, we are done with the stringchar.
+                                return pos;
+                            }
+                        }
+                        else
+                        {
+                            // An unicode char could be more than one byte long 
+                            pos += nbBytes;
+                        }
+                    }
+                    else
+                    {
+                        return pos;
+                    }
+                }
+            }
+        }
+    }
+
+    /**
+     * Parse this rule : <br>
+     * <p>
+     * &lt;oidPrefix&gt; ::= 'OID.' | 'oid.' | e
+     * </p>
+     * 
+     * @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
+     */
+    protected static int parseOidPrefix( byte[] bytes, int pos )
+    {
+
+        if ( ( StringUtils.areEquals( bytes, pos, OID_LOWER ) == -1 ) &&
+                ( StringUtils.areEquals( bytes, pos, OID_UPPER ) == -1 ) )
+        {
+
+            return -1;
+        }
+        else
+        {
+            pos += 4;
+
+            return pos;
+        }
+    }
+    
+    /**
+     * Parse this rule : <br>
+     * <p>
+     * &lt;oidValue&gt; ::= [0-9] &lt;digits&gt; &lt;oids&gt; 
+     * </p>
+     * 
+     * @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
+     */
+    protected static int parseOidValue(byte[] bytes, int pos)
+    {
+        // <attributType> ::= [0-9] <digits> <oids>
+        if ( StringUtils.isDigit( bytes, pos ) == false )
+        {
+
+            // Nope... An error
+            return -1;
+        }
+        else
+        {
+
+            // Let's process an oid
+            pos++;
+
+            while ( StringUtils.isDigit( bytes, pos ) )
+            {
+                pos++;
+            }
+
+            // <oids> ::= '.' [0-9] <digits> <oids> | e
+            if ( StringUtils.isCharASCII( bytes, pos, '.' ) == false )
+            {
+
+                return pos;
+            }
+            else
+            {
+
+                do
+                {
+                    pos++;
+
+                    if ( StringUtils.isDigit( bytes, pos ) == false )
+                    {
+
+                        return -1;
+                    }
+                    else
+                    {
+                        pos++;
+
+                        while ( StringUtils.isDigit( bytes, pos ) )
+                        {
+                            pos++;
+                        }
+                    }
+                }
+                while ( StringUtils.isCharASCII( bytes, pos, '.' ) );
+
+                return pos;
+            }
+        }
+    }
+
+    /**
+     * Parse this rule : <br>
+     * <p>
+     * &lt;attributType&gt; ::= [a-zA-Z] &lt;keychars&gt; | 
+     * 							&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
+     */
+    protected static int parseAttributeType( byte[] bytes, int pos )
+    {
+
+        // <attributType> ::= [a-zA-Z] <keychars> | <oidPrefix> [0-9] <digits>
<oids> | [0-9] <digits> <oids>
+    	
+        if ( StringUtils.isAlphaASCII( bytes, pos ))
+        {
+            // <attributType> ::= [a-zA-Z] <keychars> | <oidPrefix> [0-9]
<digits> <oids> 
+
+        	// We have got an Alpha char, it may be the begining of an OID ?
+            int oldPos = pos;
+        	
+            if ( ( pos = parseOidPrefix( bytes, oldPos ) ) != -1 ) 
+            {
+            	return parseOidValue(bytes, pos);
+	        }
+            else 
+            {
+            	// 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 ( StringUtils.isAlphaDigitMinus( bytes, pos ) )
+                {
+                    pos++;
+                }
+
+                return pos;
+            }
+        }
+        else
+        {
+
+            // An oid
+            // <attributType> ::= [0-9] <digits> <oids> 
+        	return parseOidValue(bytes, pos);
+        }
+    }
+
+    /**
+     * Parse this rule : <br>
+     * <p>
+     * &lt;attributeTypeAndValues&gt;    ::= &lt;spaces&gt; '+' &lt;spaces&gt;
&lt;attributeType&gt; &lt;spaces&gt; '=' &lt;spaces&gt; &lt;attributeValue&gt;
&lt;attributeTypeAndValues&gt; | e
+     * </p>
+     * 
+     * @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
+     */
+    protected static int parseAttributeTypeAndValues( byte[] bytes, int pos )
+    {
+
+        while ( true )
+        {
+            pos = parseSpaces( bytes, pos );
+
+            if ( StringUtils.isCharASCII( bytes, pos, '+' ) )
+            {
+                pos++;
+            }
+            else
+            {
+
+                // <attributeTypeAndValues> ::= e
+                return pos;
+            }
+
+            pos = parseSpaces( bytes, pos );
+
+            if ( ( pos = parseAttributeType( bytes, pos ) ) == -1 )
+            {
+
+                return -1;
+            }
+
+            pos = parseSpaces( bytes, pos );
+
+            if ( StringUtils.isCharASCII( bytes, pos, '=' ) )
+            {
+                pos++;
+            }
+            else
+            {
+
+                return -1;
+            }
+
+            pos = parseSpaces( bytes, pos );
+
+            return parseAttributeValue( bytes, pos );
+        }
+    }
+
+    /**
+     * Parse this rule : <br>
+     * <p>
+     * &lt;name-component&gt;    ::= &lt;attributeType&gt; &lt;spaces&gt;
'=' &lt;spaces&gt; &lt;attributeValue&gt; &lt;attributeTypeAndValues&gt;
+     * </p>
+     * 
+     * @param bytes The buffer to parse
+     * @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 
+     */
+    protected static int parseNameComponent( byte[] bytes, int pos )
+    {
+
+        if ( ( pos = parseAttributeType( bytes, pos ) ) == -1 )
+        {
+
+            return -1;
+        }
+
+        pos = parseSpaces( bytes, pos );
+
+        if ( StringUtils.isCharASCII( bytes, pos, '=' ) == false )
+        {
+
+            return -1;
+        }
+        else
+        {
+            pos++;
+        }
+
+        pos = parseSpaces( bytes, pos );
+
+        if ( ( pos = parseAttributeValue( bytes, pos ) ) == -1 )
+        {
+
+            return -1;
+        }
+
+        return parseAttributeTypeAndValues( bytes, pos );
+    }
+
+    /**
+     * Construct an empty LdapDN object
+     */
+    protected RelativeLdapDN(int length, boolean isStreamed)
+    {
+        super(length, isStreamed);
+    }
+    
+    /**
+     * Construct an empty LdapDN object
+     */
+    protected RelativeLdapDN()
+    {
+        super(0, false);
+    }
+    
+    /**
+     * Parse a buffer and checks that it is a valid relative DN <br>
+     * 
+     * @param bytes The byte buffer that contains the relative DN
+     * @exception A DecoderException is thrown if the buffer does not contains a valid relative
DN.
+     */
+    protected RelativeLdapDN( byte[] bytes ) throws DecoderException
+    {
+
+        if ( bytes == null || bytes.length == 0)
+        {
+            return;
+        }
+        
+        int pos = 0;
+
+        // Parse the name component
+        if ( ( pos = parseNameComponent( bytes, pos ) ) == -1 )
+        {
+            throw new DecoderException( "Bad relative DN : " + new String( bytes) );
+        }
+
+        int stringLength = StringUtils.countChars(bytes);
+        
+        setData(bytes);
+    }
+}



Mime
View raw message