directory-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From seelm...@apache.org
Subject svn commit: r1545954 - in /directory/studio/trunk/plugins/schemaeditor/src: main/java/org/apache/directory/studio/schemaeditor/model/io/ test/java/org/apache/directory/studio/schemaeditor/model/io/
Date Wed, 27 Nov 2013 07:31:36 GMT
Author: seelmann
Date: Wed Nov 27 07:31:36 2013
New Revision: 1545954

URL: http://svn.apache.org/r1545954
Log:
DIRSTUDIO-959 (Exporting schemas as OpenLDAP files does not use proper escaping):
* Use new SchemaObjectRenderer
* Remove redundant schema object render implementation

Added:
    directory/studio/trunk/plugins/schemaeditor/src/test/java/org/apache/directory/studio/schemaeditor/model/io/
    directory/studio/trunk/plugins/schemaeditor/src/test/java/org/apache/directory/studio/schemaeditor/model/io/OpenLdapSchemaFileExporterTest.java
Modified:
    directory/studio/trunk/plugins/schemaeditor/src/main/java/org/apache/directory/studio/schemaeditor/model/io/OpenLdapSchemaFileExporter.java

Modified: directory/studio/trunk/plugins/schemaeditor/src/main/java/org/apache/directory/studio/schemaeditor/model/io/OpenLdapSchemaFileExporter.java
URL: http://svn.apache.org/viewvc/directory/studio/trunk/plugins/schemaeditor/src/main/java/org/apache/directory/studio/schemaeditor/model/io/OpenLdapSchemaFileExporter.java?rev=1545954&r1=1545953&r2=1545954&view=diff
==============================================================================
--- directory/studio/trunk/plugins/schemaeditor/src/main/java/org/apache/directory/studio/schemaeditor/model/io/OpenLdapSchemaFileExporter.java
(original)
+++ directory/studio/trunk/plugins/schemaeditor/src/main/java/org/apache/directory/studio/schemaeditor/model/io/OpenLdapSchemaFileExporter.java
Wed Nov 27 07:31:36 2013
@@ -20,12 +20,9 @@
 package org.apache.directory.studio.schemaeditor.model.io;
 
 
-import java.util.List;
-
 import org.apache.directory.api.ldap.model.schema.AttributeType;
 import org.apache.directory.api.ldap.model.schema.ObjectClass;
-import org.apache.directory.api.ldap.model.schema.ObjectClassTypeEnum;
-import org.apache.directory.api.ldap.model.schema.UsageEnum;
+import org.apache.directory.api.ldap.model.schema.SchemaObjectRenderer;
 import org.apache.directory.studio.schemaeditor.model.Schema;
 
 
@@ -76,122 +73,7 @@ public class OpenLdapSchemaFileExporter
      */
     public static String toSourceCode( AttributeType at )
     {
-        StringBuffer sb = new StringBuffer();
-
-        // Opening the definition and OID
-        sb.append( "attributetype ( " + at.getOid() + " \n" ); //$NON-NLS-1$ //$NON-NLS-2$
-
-        // NAME(S)
-        List<String> names = at.getNames();
-        if ( ( names != null ) && ( names.size() > 0 ) )
-        {
-            sb.append( "\tNAME " ); //$NON-NLS-1$
-            if ( names.size() > 1 )
-            {
-                sb.append( "( " ); //$NON-NLS-1$
-                for ( String name : names )
-                {
-                    sb.append( "'" + name + "' " ); //$NON-NLS-1$ //$NON-NLS-2$
-                }
-                sb.append( ") \n" ); //$NON-NLS-1$
-            }
-            else
-            {
-                sb.append( "'" + names.get( 0 ) + "' \n" ); //$NON-NLS-1$ //$NON-NLS-2$
-            }
-        }
-
-        // DESC
-        if ( ( at.getDescription() != null ) && ( !at.getDescription().equals( ""
) ) ) //$NON-NLS-1$
-        {
-            sb.append( "\tDESC '" + at.getDescription() + "' \n" ); //$NON-NLS-1$ //$NON-NLS-2$
-        }
-
-        // OBSOLETE
-        if ( at.isObsolete() )
-        {
-            sb.append( "\tOBSOLETE \n" ); //$NON-NLS-1$
-        }
-
-        // SUP
-        if ( ( at.getSuperiorOid() != null ) && ( !at.getSuperiorOid().equals( ""
) ) ) //$NON-NLS-1$
-        {
-            sb.append( "\tSUP " + at.getSuperiorOid() + " \n" ); //$NON-NLS-1$ //$NON-NLS-2$
-        }
-
-        // EQUALITY
-        if ( ( at.getEqualityOid() != null ) && ( !at.getEqualityOid().equals( ""
) ) ) //$NON-NLS-1$
-        {
-            sb.append( "\tEQUALITY " + at.getEqualityOid() + " \n" ); //$NON-NLS-1$ //$NON-NLS-2$
-        }
-
-        // ORDERING
-        if ( ( at.getOrderingOid() != null ) && ( !at.getOrderingOid().equals( ""
) ) ) //$NON-NLS-1$
-        {
-            sb.append( "\tORDERING " + at.getOrderingOid() + " \n" ); //$NON-NLS-1$ //$NON-NLS-2$
-        }
-
-        // SUBSTR
-        if ( ( at.getSubstringOid() != null ) && ( !at.getSubstringOid().equals(
"" ) ) ) //$NON-NLS-1$
-        {
-            sb.append( "\tSUBSTR " + at.getSubstringOid() + " \n" ); //$NON-NLS-1$ //$NON-NLS-2$
-        }
-
-        // SYNTAX
-        if ( ( at.getSyntaxOid() != null ) && ( !at.getSyntaxOid().equals( "" ) )
) //$NON-NLS-1$
-        {
-            sb.append( "\tSYNTAX " + at.getSyntaxOid() ); //$NON-NLS-1$
-            if ( at.getSyntaxLength() > 0 )
-            {
-                sb.append( "{" + at.getSyntaxLength() + "}" ); //$NON-NLS-1$ //$NON-NLS-2$
-            }
-            sb.append( " \n" ); //$NON-NLS-1$
-        }
-
-        // SINGLE-VALUE
-        if ( at.isSingleValued() )
-        {
-            sb.append( "\tSINGLE-VALUE \n" ); //$NON-NLS-1$
-        }
-
-        // COLLECTIVE
-        if ( at.isCollective() )
-        {
-            sb.append( "\tCOLLECTIVE \n" ); //$NON-NLS-1$
-        }
-
-        // NO-USER-MODIFICATION
-        if ( !at.isUserModifiable() )
-        {
-            sb.append( "\tNO-USER-MODIFICATION \n" ); //$NON-NLS-1$
-        }
-
-        // USAGE
-        UsageEnum usage = at.getUsage();
-        if ( usage != null )
-        {
-            if ( usage == UsageEnum.DIRECTORY_OPERATION )
-            {
-                sb.append( "\tUSAGE directoryOperation \n" ); //$NON-NLS-1$
-            }
-            else if ( usage == UsageEnum.DISTRIBUTED_OPERATION )
-            {
-                sb.append( "\tUSAGE distributedOperation \n" ); //$NON-NLS-1$
-            }
-            else if ( usage == UsageEnum.DSA_OPERATION )
-            {
-                sb.append( "\tUSAGE dSAOperation \n" ); //$NON-NLS-1$
-            }
-            else if ( usage == UsageEnum.USER_APPLICATIONS )
-            {
-                // There's nothing to write, this is the default option
-            }
-        }
-
-        // Closing the definition
-        sb.append( " )\n" ); //$NON-NLS-1$
-
-        return sb.toString();
+        return SchemaObjectRenderer.OPEN_LDAP_SCHEMA_RENDERER.render( at );
     }
 
 
@@ -206,119 +88,6 @@ public class OpenLdapSchemaFileExporter
      */
     public static String toSourceCode( ObjectClass oc )
     {
-        StringBuffer sb = new StringBuffer();
-
-        // Opening the definition and OID
-        sb.append( "objectclass ( " + oc.getOid() + " \n" ); //$NON-NLS-1$ //$NON-NLS-2$
-
-        // NAME(S)
-        List<String> names = oc.getNames();
-        if ( ( names != null ) && ( names.size() > 0 ) )
-        {
-            sb.append( "\tNAME " ); //$NON-NLS-1$
-            if ( names.size() > 1 )
-            {
-                sb.append( "( " ); //$NON-NLS-1$
-                for ( String name : names )
-                {
-                    sb.append( "'" + name + "' " ); //$NON-NLS-1$ //$NON-NLS-2$
-                }
-                sb.append( ") \n" ); //$NON-NLS-1$
-            }
-            else
-            {
-                sb.append( "'" + names.get( 0 ) + "' \n" ); //$NON-NLS-1$ //$NON-NLS-2$
-            }
-        }
-
-        // DESC
-        if ( ( oc.getDescription() != null ) && ( !oc.getDescription().equals( ""
) ) ) //$NON-NLS-1$
-        {
-            sb.append( "\tDESC '" + oc.getDescription() + "' \n" ); //$NON-NLS-1$ //$NON-NLS-2$
-        }
-
-        // OBSOLETE
-        if ( oc.isObsolete() )
-        {
-            sb.append( "\tOBSOLETE \n" ); //$NON-NLS-1$
-        }
-
-        // SUP
-        List<String> superiors = oc.getSuperiorOids();
-        if ( ( superiors != null ) && ( superiors.size() != 0 ) )
-        {
-            if ( superiors.size() > 1 )
-            {
-                sb.append( "\tSUP (" + superiors.get( 0 ) ); //$NON-NLS-1$
-                for ( int i = 1; i < superiors.size(); i++ )
-                {
-                    sb.append( " $ " + superiors.get( i ) ); //$NON-NLS-1$
-                }
-                sb.append( ") \n" ); //$NON-NLS-1$
-            }
-            else
-            {
-                sb.append( "\tSUP " + superiors.get( 0 ) + " \n" ); //$NON-NLS-1$ //$NON-NLS-2$
-            }
-        }
-
-        // CLASSTYPE
-        ObjectClassTypeEnum classtype = oc.getType();
-        if ( classtype == ObjectClassTypeEnum.ABSTRACT )
-        {
-            sb.append( "\tABSTRACT \n" ); //$NON-NLS-1$
-        }
-        else if ( classtype == ObjectClassTypeEnum.AUXILIARY )
-        {
-            sb.append( "\tAUXILIARY \n" ); //$NON-NLS-1$
-        }
-        else if ( classtype == ObjectClassTypeEnum.STRUCTURAL )
-        {
-            sb.append( "\tSTRUCTURAL \n" ); //$NON-NLS-1$
-        }
-
-        // MUST
-        List<String> must = oc.getMustAttributeTypeOids();
-        if ( ( must != null ) && ( must.size() != 0 ) )
-        {
-            sb.append( "\tMUST " ); //$NON-NLS-1$
-            if ( must.size() > 1 )
-            {
-                sb.append( "( " + must.get( 0 ) + " " ); //$NON-NLS-1$ //$NON-NLS-2$
-                for ( int i = 1; i < must.size(); i++ )
-                {
-                    sb.append( "$ " + must.get( i ) + " " ); //$NON-NLS-1$ //$NON-NLS-2$
-                }
-                sb.append( ") \n" ); //$NON-NLS-1$
-            }
-            else if ( must.size() == 1 )
-            {
-                sb.append( must.get( 0 ) + " \n" ); //$NON-NLS-1$
-            }
-        }
-
-        // MAY
-        List<String> may = oc.getMayAttributeTypeOids();
-        if ( ( may != null ) && ( may.size() != 0 ) )
-        {
-            sb.append( "\tMAY " ); //$NON-NLS-1$
-            if ( may.size() > 1 )
-            {
-                sb.append( "( " + may.get( 0 ) + " " ); //$NON-NLS-1$ //$NON-NLS-2$
-                for ( int i = 1; i < may.size(); i++ )
-                {
-                    sb.append( "$ " + may.get( i ) + " " ); //$NON-NLS-1$ //$NON-NLS-2$
-                }
-                sb.append( ") \n" ); //$NON-NLS-1$
-            }
-            else if ( may.size() == 1 )
-            {
-                sb.append( may.get( 0 ) + " \n" ); //$NON-NLS-1$
-            }
-        }
-        // Closing the definition
-        sb.append( " )\n" ); //$NON-NLS-1$
-
-        return sb.toString();
+        return SchemaObjectRenderer.OPEN_LDAP_SCHEMA_RENDERER.render( oc );
     }
 }

Added: directory/studio/trunk/plugins/schemaeditor/src/test/java/org/apache/directory/studio/schemaeditor/model/io/OpenLdapSchemaFileExporterTest.java
URL: http://svn.apache.org/viewvc/directory/studio/trunk/plugins/schemaeditor/src/test/java/org/apache/directory/studio/schemaeditor/model/io/OpenLdapSchemaFileExporterTest.java?rev=1545954&view=auto
==============================================================================
--- directory/studio/trunk/plugins/schemaeditor/src/test/java/org/apache/directory/studio/schemaeditor/model/io/OpenLdapSchemaFileExporterTest.java
(added)
+++ directory/studio/trunk/plugins/schemaeditor/src/test/java/org/apache/directory/studio/schemaeditor/model/io/OpenLdapSchemaFileExporterTest.java
Wed Nov 27 07:31:36 2013
@@ -0,0 +1,102 @@
+package org.apache.directory.studio.schemaeditor.model.io;
+
+
+import static org.junit.Assert.assertEquals;
+
+import java.util.Arrays;
+import java.util.Collections;
+
+import org.apache.directory.api.ldap.model.schema.MutableAttributeType;
+import org.apache.directory.api.ldap.model.schema.MutableObjectClass;
+import org.apache.directory.api.ldap.model.schema.ObjectClassTypeEnum;
+import org.apache.directory.api.ldap.model.schema.SchemaObjectRenderer;
+import org.apache.directory.api.ldap.model.schema.UsageEnum;
+import org.junit.Before;
+import org.junit.Test;
+
+
+public class OpenLdapSchemaFileExporterTest
+{
+
+    private MutableObjectClass objectClassSimple;
+    private MutableObjectClass objectClassComplex;
+
+    private MutableAttributeType attributeTypeSimple;
+    private MutableAttributeType attributeTypeComplex;
+
+    @Before
+    public void setUp()
+    {
+        objectClassSimple = new MutableObjectClass( "1.2.3.4" );
+        objectClassSimple.setNames( "name0" );
+        objectClassSimple.setMustAttributeTypeOids( Arrays.asList( "att0" ) );
+        objectClassSimple.setSchemaName( "dummy" );
+
+        objectClassComplex = new MutableObjectClass( "1.2.3.4" );
+        objectClassComplex.setNames( "name1", "name2" );
+        objectClassComplex.setDescription( "description with 'quotes'" );
+        objectClassComplex.setObsolete( true );
+        objectClassComplex.setSuperiorOids( Collections.singletonList( "1.3.5.7" ) );
+        objectClassComplex.setType( ObjectClassTypeEnum.AUXILIARY );
+        objectClassComplex.setMustAttributeTypeOids( Arrays.asList( "att1", "att2" ) );
+        objectClassComplex.setMayAttributeTypeOids( Arrays.asList( "att3", "att4" ) );
+        objectClassComplex.setSchemaName( "dummy" );
+        
+        attributeTypeSimple = new MutableAttributeType( "1.2.3.4" );
+        attributeTypeSimple.setNames( "name0" );
+        attributeTypeSimple.setEqualityOid( "matchingRule0" );
+        attributeTypeSimple.setSyntaxOid( "2.3.4.5" );
+        attributeTypeSimple.setSyntaxLength( 512 );
+        attributeTypeSimple.setCollective( true );
+        attributeTypeSimple.setSchemaName( "dummy" );
+
+        attributeTypeComplex = new MutableAttributeType( "1.2.3.4" );
+        attributeTypeComplex.setNames( "name1", "name2" );
+        attributeTypeComplex.setDescription( "description with 'quotes'" );
+        attributeTypeComplex.setObsolete( true );
+        attributeTypeComplex.setSuperiorOid( "superAttr" );
+        attributeTypeComplex.setEqualityOid( "matchingRule1" );
+        attributeTypeComplex.setOrderingOid( "matchingRule2" );
+        attributeTypeComplex.setSubstringOid( "matchingRule3" );
+        attributeTypeComplex.setSingleValued( true );
+        attributeTypeComplex.setUserModifiable( false );
+        attributeTypeComplex.setUsage( UsageEnum.DIRECTORY_OPERATION );
+        attributeTypeComplex.setSchemaName( "dummy" );
+    }
+
+
+    @Test
+    public void testOpenLdapSchemaRendererObjectClassSimple()
+    {
+        String actual = OpenLdapSchemaFileExporter.toSourceCode( objectClassSimple );
+        String expected = "objectclass ( 1.2.3.4 NAME 'name0'\n\tSTRUCTURAL\n\tMUST att0\n)";
+        assertEquals( expected, actual );
+    }
+
+
+    @Test
+    public void testOpenLdapSchemaRendererObjectClassComplex()
+    {
+        String actual = OpenLdapSchemaFileExporter.toSourceCode( objectClassComplex );
+        String expected = "objectclass ( 1.2.3.4 NAME ( 'name1' 'name2' )\n\tDESC 'description
with \\27quotes\\27'\n\tOBSOLETE\n\tSUP 1.3.5.7\n\tAUXILIARY\n\tMUST ( att1 $ att2 )\n\tMAY
( att3 $ att4 )\n)";
+        assertEquals( expected, actual );
+    }
+    
+    @Test
+    public void testOpenLdapSchemaRendererAttributeTypeSimple()
+    {
+        String actual = OpenLdapSchemaFileExporter.toSourceCode( attributeTypeSimple );
+        String expected = "attributetype ( 1.2.3.4 NAME 'name0'\n\tEQUALITY matchingRule0\n\tSYNTAX
2.3.4.5{512}\n\tCOLLECTIVE\n\tUSAGE userApplications\n)";
+        assertEquals( expected, actual );
+    }
+
+
+    @Test
+    public void testOpenLdapSchemaRendererAttributeTypeComplex()
+    {
+        String actual = OpenLdapSchemaFileExporter.toSourceCode( attributeTypeComplex );
+        String expected = "attributetype ( 1.2.3.4 NAME ( 'name1' 'name2' )\n\tDESC 'description
with \\27quotes\\27'\n\tOBSOLETE\n\tSUP superAttr\n\tEQUALITY matchingRule1\n\tORDERING matchingRule2\n\tSUBSTR
matchingRule3\n\tSINGLE-VALUE\n\tNO-USER-MODIFICATION\n\tUSAGE directoryOperation\n)";
+        assertEquals( expected, actual );
+    }
+
+}



Mime
View raw message