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 2006/12/23 18:06:52 UTC

svn commit: r489904 - in /directory/trunks/shared/ldap/src: main/java/org/apache/directory/shared/ldap/schema/syntax/ test/java/org/apache/directory/shared/ldap/schema/syntax/

Author: elecharny
Date: Sat Dec 23 09:06:51 2006
New Revision: 489904

URL: http://svn.apache.org/viewvc?view=rev&rev=489904
Log:
Added the TeletexTerminalIdentifier SC

Added:
    directory/trunks/shared/ldap/src/main/java/org/apache/directory/shared/ldap/schema/syntax/TeletexTerminalIdentifierSyntaxChecker.java
    directory/trunks/shared/ldap/src/test/java/org/apache/directory/shared/ldap/schema/syntax/TeletexTerminalIdentifierSyntaxCheckerTest.java

Added: directory/trunks/shared/ldap/src/main/java/org/apache/directory/shared/ldap/schema/syntax/TeletexTerminalIdentifierSyntaxChecker.java
URL: http://svn.apache.org/viewvc/directory/trunks/shared/ldap/src/main/java/org/apache/directory/shared/ldap/schema/syntax/TeletexTerminalIdentifierSyntaxChecker.java?view=auto&rev=489904
==============================================================================
--- directory/trunks/shared/ldap/src/main/java/org/apache/directory/shared/ldap/schema/syntax/TeletexTerminalIdentifierSyntaxChecker.java (added)
+++ directory/trunks/shared/ldap/src/main/java/org/apache/directory/shared/ldap/schema/syntax/TeletexTerminalIdentifierSyntaxChecker.java Sat Dec 23 09:06:51 2006
@@ -0,0 +1,212 @@
+/*
+ *  Licensed to the Apache Software Foundation (ASF) under one
+ *  or more contributor license agreements.  See the NOTICE file
+ *  distributed with this work for additional information
+ *  regarding copyright ownership.  The ASF licenses this file
+ *  to you 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.directory.shared.ldap.schema.syntax;
+
+
+import org.apache.directory.shared.ldap.util.StringTools;
+
+
+/**
+ * A SyntaxChecker which verifies that a value is a TeletexTerminalIdentifier according to 
+ * RFC 4517 :
+ * 
+ * teletex-id = ttx-term *(DOLLAR ttx-param)
+ * ttx-term   = PrintableString          ; terminal identifier
+ * ttx-param  = ttx-key COLON ttx-value  ; parameter
+ * ttx-key    = "graphic" | "control" | "misc" | "page" | "private"
+ * ttx-value  = *ttx-value-octet
+ *
+ * ttx-value-octet = %x00-23 | (%x5C "24") | %x25-5B | (%x5C "5C") | %x5D-FF
+ * 
+ * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
+ * @version $Rev$
+ */
+public class TeletexTerminalIdentifierSyntaxChecker extends AbstractSyntaxChecker
+{
+    /** The Syntax OID, according to RFC 4517 */
+    private static final String SC_OID = "1.3.6.1.4.1.1466.115.121.1.51";
+    
+    /**
+     * 
+     * Creates a new instance of TeletexTerminalIdentifier.
+     *
+     */
+    public TeletexTerminalIdentifierSyntaxChecker()
+    {
+        super( SC_OID );
+    }
+    
+    /**
+     * 
+     * Creates a new instance of TeletexTerminalIdentifier.
+     * 
+     * @param the oid to associate with this new SyntaxChecker
+     *
+     */
+    protected TeletexTerminalIdentifierSyntaxChecker( String oid )
+    {
+        super( oid );
+    }
+    
+    
+    /* (non-Javadoc)
+     * @see org.apache.directory.shared.ldap.schema.SyntaxChecker#isValidSyntax(java.lang.Object)
+     */
+    public boolean isValidSyntax( Object value )
+    {
+        String strValue;
+
+        if ( value == null )
+        {
+            return false;
+        }
+        
+        if ( value instanceof String )
+        {
+            strValue = ( String ) value;
+        }
+        else if ( value instanceof byte[] )
+        {
+            strValue = StringTools.utf8ToString( ( byte[] ) value ); 
+        }
+        else
+        {
+            strValue = value.toString();
+        }
+
+        if ( strValue.length() == 0 )
+        {
+            return false;
+        }
+
+        // Search for the first '$' separator
+        int dollar = strValue.indexOf( '$' );
+        
+        String terminalIdentifier = ( ( dollar == -1 ) ? strValue : strValue.substring( 0, dollar ) );
+        
+        if ( terminalIdentifier.length() == 0 )
+        {
+            // It should not be null
+            return false;
+        }
+        
+        if ( !StringTools.isPrintableString( terminalIdentifier ) )
+        {
+            // It's not a valid PrintableString 
+            return false;
+        }
+        
+        if ( dollar == -1 )
+        {
+            // No ttx-param : let's get out
+            return true;
+        }
+        
+        // Ok, now let's deal withh optional ttx-params
+        String[] ttxParams = strValue.substring( dollar + 1 ).split( "\\$" );
+        
+        if ( ttxParams.length == 0 )
+        {
+            return false;
+        }
+        
+        for ( String ttxParam:ttxParams )
+        {
+            int colon = ttxParam.indexOf( ':' );
+            
+            if ( colon == -1 )
+            {
+                // we must have a ':' separator
+                return false;
+            }
+            
+            String key = ttxParam.substring( 0, colon );
+            
+            if ( key.startsWith( "graphic" ) ||
+                 key.startsWith( "control" ) ||
+                 key.startsWith( "misc" ) ||
+                 key.startsWith( "page" ) ||
+                 key.startsWith( "private" ) )
+            {
+                if ( colon + 1 == ttxParam.length() )
+                {
+                    return false;
+                }
+                
+                boolean hasEsc = false;
+                
+                for ( byte b:StringTools.getBytesUtf8( ttxParam ) )
+                {
+                    switch ( b )
+                    {
+                        case 0x24 :
+                            // '$' is not accepted
+                            return false;
+                            
+                        case 0x5c :
+                            if ( hasEsc )
+                            {
+                                // two following \ are not accepted
+                                return false;
+                            }
+                            else
+                            {
+                                hasEsc = true;
+                            }
+                            
+                            continue;
+                        
+                        case '2' :
+                            continue;
+
+                        case '4' :
+                            // We have found a "\24"
+                            hasEsc = false;
+                            continue;
+                            
+                        case '5' :
+                            continue;
+
+                        case 'c' :
+                        case 'C' :
+                            // We have found a "\5c" or a "\5C"
+                            hasEsc = false;
+                            continue;
+                            
+                        default :
+                            if ( hasEsc )
+                            {
+                                // A \ should be followed by "24" or "5c" or "5C"
+                                return false;
+                            }
+                            
+                        continue;
+                    }
+                }
+            }
+            else
+            {
+                return false;
+            }
+        }
+        
+        return true;
+    }
+}

Added: directory/trunks/shared/ldap/src/test/java/org/apache/directory/shared/ldap/schema/syntax/TeletexTerminalIdentifierSyntaxCheckerTest.java
URL: http://svn.apache.org/viewvc/directory/trunks/shared/ldap/src/test/java/org/apache/directory/shared/ldap/schema/syntax/TeletexTerminalIdentifierSyntaxCheckerTest.java?view=auto&rev=489904
==============================================================================
--- directory/trunks/shared/ldap/src/test/java/org/apache/directory/shared/ldap/schema/syntax/TeletexTerminalIdentifierSyntaxCheckerTest.java (added)
+++ directory/trunks/shared/ldap/src/test/java/org/apache/directory/shared/ldap/schema/syntax/TeletexTerminalIdentifierSyntaxCheckerTest.java Sat Dec 23 09:06:51 2006
@@ -0,0 +1,78 @@
+/*
+ *  Licensed to the Apache Software Foundation (ASF) under one
+ *  or more contributor license agreements.  See the NOTICE file
+ *  distributed with this work for additional information
+ *  regarding copyright ownership.  The ASF licenses this file
+ *  to you 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.directory.shared.ldap.schema.syntax;
+
+import junit.framework.TestCase;
+
+/**
+ * Test cases for TeletexTerminalIdentifierSyntaxChecker.
+ *
+ * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
+ * @version $Rev$
+ */
+public class TeletexTerminalIdentifierSyntaxCheckerTest extends TestCase
+{
+    TeletexTerminalIdentifierSyntaxChecker checker = new TeletexTerminalIdentifierSyntaxChecker();
+
+
+    public void testNullString()
+    {
+        assertFalse( checker.isValidSyntax( null ) );
+    }
+
+
+    public void testEmptyString()
+    {
+        assertFalse( checker.isValidSyntax( "" ) );
+    }
+
+
+    public void testWrongCase() throws Exception
+    {
+        assertFalse( checker.isValidSyntax( "test$" ) );
+        assertFalse( checker.isValidSyntax( new String( new byte[]{ 't', 'e', 's', 't', 0x00, 0x7F, (byte)0x80, '$', 't', 'e', 's', 't' }, "UTF-8" ) ) );
+        assertFalse( checker.isValidSyntax( "test$$" ) );
+        assertFalse( checker.isValidSyntax( "test$a:b" ) );
+        assertFalse( checker.isValidSyntax( "test$misc" ) );
+        assertFalse( checker.isValidSyntax( "test$misc:" ) );
+        assertFalse( checker.isValidSyntax( "test$:" ) );
+        assertFalse( checker.isValidSyntax( "test$:abc" ) );
+        assertFalse( checker.isValidSyntax( "test$misc:a$b" ) );
+        assertFalse( checker.isValidSyntax( "test$misc:a\\b" ) );
+        assertFalse( checker.isValidSyntax( "test$misc:a\\2b" ) );
+        assertFalse( checker.isValidSyntax( "test$misc:a\\5b" ) );
+    }
+    
+    
+    public void testCorrectCase() throws Exception
+    {
+        assertTrue( checker.isValidSyntax( "test" ) );
+        assertTrue( checker.isValidSyntax( "test$graphic:abc" ) );
+        assertTrue( checker.isValidSyntax( "test$misc:abc" ) );
+        assertTrue( checker.isValidSyntax( "test$control:abc" ) );
+        assertTrue( checker.isValidSyntax( "test$page:abc" ) );
+        assertTrue( checker.isValidSyntax( "test$private:abc" ) );
+        assertTrue( checker.isValidSyntax( "test$private:abc$misc:def" ) );
+        assertTrue( checker.isValidSyntax( "test$misc:" + new String( new byte[]{ 't', 'e', 's', 't', 0x00, 0x7F, (byte)0xFF}, "UTF-8" ) ) );
+        assertTrue( checker.isValidSyntax( "test$misc:a\\5c" ) );
+        assertTrue( checker.isValidSyntax( "test$misc:a\\5C" ) );
+        assertTrue( checker.isValidSyntax( "test$misc:a\\24" ) );
+    }
+}