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/14 21:08:14 UTC

svn commit: r496122 - in /directory/trunks/shared/convert/src/main/java/org/apache/directory/shared/converter/schema: AttributeTypeHolder.java ObjectClassHolder.java Schema.java SchemaElement.java SchemaElementImpl.java SchemaParser.java SchemaToLdif.java

Author: elecharny
Date: Sun Jan 14 12:08:13 2007
New Revision: 496122

URL: http://svn.apache.org/viewvc?view=rev&rev=496122
Log:
Fixed bugs and added javadoc

Modified:
    directory/trunks/shared/convert/src/main/java/org/apache/directory/shared/converter/schema/AttributeTypeHolder.java
    directory/trunks/shared/convert/src/main/java/org/apache/directory/shared/converter/schema/ObjectClassHolder.java
    directory/trunks/shared/convert/src/main/java/org/apache/directory/shared/converter/schema/Schema.java
    directory/trunks/shared/convert/src/main/java/org/apache/directory/shared/converter/schema/SchemaElement.java
    directory/trunks/shared/convert/src/main/java/org/apache/directory/shared/converter/schema/SchemaElementImpl.java
    directory/trunks/shared/convert/src/main/java/org/apache/directory/shared/converter/schema/SchemaParser.java
    directory/trunks/shared/convert/src/main/java/org/apache/directory/shared/converter/schema/SchemaToLdif.java

Modified: directory/trunks/shared/convert/src/main/java/org/apache/directory/shared/converter/schema/AttributeTypeHolder.java
URL: http://svn.apache.org/viewvc/directory/trunks/shared/convert/src/main/java/org/apache/directory/shared/converter/schema/AttributeTypeHolder.java?view=diff&rev=496122&r1=496121&r2=496122
==============================================================================
--- directory/trunks/shared/convert/src/main/java/org/apache/directory/shared/converter/schema/AttributeTypeHolder.java (original)
+++ directory/trunks/shared/convert/src/main/java/org/apache/directory/shared/converter/schema/AttributeTypeHolder.java Sun Jan 14 12:08:13 2007
@@ -20,6 +20,8 @@
 package org.apache.directory.shared.converter.schema;
 
 
+import javax.naming.NamingException;
+
 import org.apache.directory.shared.ldap.schema.UsageEnum;
 
 
@@ -32,25 +34,46 @@
  */
 public class AttributeTypeHolder extends SchemaElementImpl
 {
+    /** A flag for single valued attributes. Default to false */
     private boolean singleValue = false;
+    
+    /** A flag for collective attribute. Default to false */
     private boolean collective = false;
+    
+    /** A flaf for immutable attribue. Default to false */
     private boolean noUserModification = false;
 
+    /** The optional superior */
     private String superior;
+    
+    /** The equality matching rule */
     private String equality;
+
+    /** The ordering matching rule */
     private String ordering;
+
+    /** The substring matching rule */
     private String substr;
+
+    /** The syntax this attribute respects */
     private String syntax;
 
-    private UsageEnum usage = UsageEnum.USER_APPLICATIONS;
+    /** The optional length for this attribute */
+    private int oidLen = -1;
 
-    private int length = -1;
+    /** The attribute uase. Default to userApplication */
+    private UsageEnum usage = UsageEnum.USER_APPLICATIONS;
 
 
     // ------------------------------------------------------------------------
     // C O N S T R U C T O R S
     // ------------------------------------------------------------------------
 
+    /**
+     * Create an instance of an attributeType
+     * 
+     * @param The attributeType's OID
+     */
     public AttributeTypeHolder( String oid )
     {
         this.oid = oid;
@@ -61,189 +84,272 @@
     // Accessors and mutators
     // ------------------------------------------------------------------------
 
-    public boolean isObsolete()
-    {
-        return obsolete;
-    }
-
-
-    public void setObsolete( boolean obsolete )
-    {
-        this.obsolete = obsolete;
-    }
-
-
+    /**
+     * Tells if the attribute is single-valued
+     * @return true if the attribute is single-valued, false otherwise
+     */
     public boolean isSingleValue()
     {
         return singleValue;
     }
 
-
+    /**
+     * Set the attributeType singleValue flag
+     * @param singleValue The value for this flag
+     */
     public void setSingleValue( boolean singleValue )
     {
         this.singleValue = singleValue;
     }
 
-
+    /**
+     * Tells if the attributeType is collectove or not
+     * @return True if the attributeType is collective, false otherwise
+     */
     public boolean isCollective()
     {
         return collective;
     }
 
-
+    /**
+     * Set the attributeType collective flag
+     * @param collective The value for this flag
+     */
     public void setCollective( boolean collective )
     {
         this.collective = collective;
     }
 
-
+    /**
+     * Tellse if the attributeType is mutable or not
+     * @return True if the attributeType is immutable, false otherwise
+     */
     public boolean isNoUserModification()
     {
         return noUserModification;
     }
 
-
+    /**
+     * Set the attributeType noUserModification flag
+     * @param noUserModification The value for this flag
+     */
     public void setNoUserModification( boolean noUserModification )
     {
         this.noUserModification = noUserModification;
     }
 
-
-    public String getOid()
-    {
-        return oid;
-    }
-
-
-    public String getDescription()
-    {
-        return description;
-    }
-
-
-    public void setDescription( String description )
-    {
-        this.description = description;
-    }
-
-
+    /**
+     * Get the optional attributeType's superior
+     * @return The attributeType's superior, if any
+     */
     public String getSuperior()
     {
         return superior;
     }
 
-
+    /**
+     * Set the attributeType's superior
+     * @param superior The attributeType's superior
+     */
     public void setSuperior( String superior )
     {
         this.superior = superior;
     }
 
-
+    /**
+     * Get the equality Matching Rule
+     * @return The equality matchingRule
+     */    
     public String getEquality()
     {
         return equality;
     }
 
-
+    /**
+     * Set the equality Matching Rule
+     * @param equality The equality Matching Rule
+     */
     public void setEquality( String equality )
     {
         this.equality = equality;
     }
 
-
+    /**
+     * Get the ordering Matching Rule
+     * @return The ordering matchingRule
+     */    
     public String getOrdering()
     {
         return ordering;
     }
 
-
+    /**
+     * Set the ordering Matching Rule
+     * @param ordering The ordering Matching Rule
+     */
     public void setOrdering( String ordering )
     {
         this.ordering = ordering;
     }
 
-
+    /**
+     * Get the substring Matching Rule
+     * @return The substring matchingRule
+     */    
     public String getSubstr()
     {
         return substr;
     }
 
-
+    /**
+     * Set the substring Matching Rule
+     * @param substring The substring Matching Rule
+     */
     public void setSubstr( String substr )
     {
         this.substr = substr;
     }
 
-
+    /**
+     * Get the attributeType's syntax
+     * @return The attributeType's syntax
+     */
     public String getSyntax()
     {
         return syntax;
     }
 
-
+    /**
+     * Set the attributeType's syntax
+     * @param syntax The attributeType's syntax
+     */
     public void setSyntax( String syntax )
     {
         this.syntax = syntax;
     }
 
-
+    /**
+     * Get the attributeType's usage
+     * @return The attributeType's usage
+     */
     public UsageEnum getUsage()
     {
         return usage;
     }
 
-
+    /**
+     * Set the attributeType's usage 
+     * @param usage The attributeType's usage
+     */
     public void setUsage( UsageEnum usage )
     {
         this.usage = usage;
     }
 
     /**
-    public void setNames( String[] names )
-    {
-        this.names = names;
-    }
-    */
-
-    public int getLength()
-    {
-        return length;
-    }
-
-
-    public void setLength( int length )
+     * Get the attributeType's syntax length
+     * @return The attributeType's syntax length
+     */
+    public int getOidLen()
     {
-        this.length = length;
+        return oidLen;
     }
 
-
-    // ------------------------------------------------------------------------
-    // Object overrides
-    // ------------------------------------------------------------------------
-
-    public int hashCode()
+    /**
+     * Set the attributeType's syntax length
+     * @param oidLen The attributeType's syntax length
+     */
+    public void setOidLen( int oidLen )
     {
-        return getOid().hashCode();
+        this.oidLen = oidLen;
     }
 
+    /**
+     * Convert this attributeType to a Ldif string
+     * 
+     * @param schemaName The name of the schema file containing this attributeType
+     * @return A ldif formatted string
+     */
+    public String toLdif( String schemaName ) throws NamingException
+    {
+        StringBuilder sb = new StringBuilder();
+        
+        sb.append( schemaToLdif( schemaName, "metaAttributeType" ) );
 
-    public boolean equals( Object obj )
-    {
-        if ( this == obj )
+        // The superior
+        if ( superior != null )
         {
-            return true;
+            sb.append( "m-supAttributeType: " ).append( superior ).append( '\n' );
         }
-
-        if ( ( obj == null ) || !( obj instanceof AttributeTypeHolder ) )
+        
+        // The equality matching rule
+        if ( equality != null )
         {
-            return false;
+            sb.append( "m-equality: " ).append( equality ).append( '\n' );
+        }
+        
+        // The ordering matching rule
+        if ( ordering != null )
+        {
+            sb.append( "m-ordering: " ).append( ordering ).append( '\n' );
+        }
+        
+        //  The substrings matching rule
+        if ( substr != null )
+        {
+            sb.append( "m-substr: " ).append( substr ).append( '\n' );
+        }
+        
+        // The value syntax
+        if ( syntax != null )
+        {
+            sb.append( "m-syntax: " ).append( syntax );
+            
+            if ( oidLen != -1 )
+            {
+                sb.append( '{' ).append( oidLen ).append( '}' );
+            }
+            
+            sb.append( '\n' );
+        }
+        
+        // The single value flag
+        if ( singleValue )
+        {
+            sb.append( "m-singleValue: true\n" );
+        }
+        
+        // The collective flag
+        if ( collective )
+        {
+            sb.append( "m-collective: true\n" );
+        }
+        
+        // The not user modifiable flag
+        if ( noUserModification )
+        {
+            sb.append( "m-noUserModification: true\n" );
+        }
+        
+        // The usage value
+        if ( usage != UsageEnum.USER_APPLICATIONS )
+        {
+            sb.append( "m-usage: " ).append( usage.render() ).append( '\n' );
+        }
+        
+        // The extensions
+        if ( extensions.size() != 0 )
+        {
+            extensionsToLdif( "m-extensionAttributeType" );
         }
 
-
-        return getOid().equals( ( ( AttributeTypeHolder ) obj ).getOid() );
+        return sb.toString();
+        
     }
 
-
+    /**
+     * Return a String representing this AttributeType.
+     */
     public String toString()
     {
         return getOid();

Modified: directory/trunks/shared/convert/src/main/java/org/apache/directory/shared/converter/schema/ObjectClassHolder.java
URL: http://svn.apache.org/viewvc/directory/trunks/shared/convert/src/main/java/org/apache/directory/shared/converter/schema/ObjectClassHolder.java?view=diff&rev=496122&r1=496121&r2=496122
==============================================================================
--- directory/trunks/shared/convert/src/main/java/org/apache/directory/shared/converter/schema/ObjectClassHolder.java (original)
+++ directory/trunks/shared/convert/src/main/java/org/apache/directory/shared/converter/schema/ObjectClassHolder.java Sun Jan 14 12:08:13 2007
@@ -20,8 +20,12 @@
 package org.apache.directory.shared.converter.schema;
 
 
+import java.util.ArrayList;
+import java.util.List;
+
+import javax.naming.NamingException;
+
 import org.apache.directory.shared.ldap.schema.ObjectClassTypeEnum;
-import org.apache.directory.shared.ldap.util.ArrayUtils;
 
 
 /**
@@ -33,138 +37,152 @@
  */
 public class ObjectClassHolder extends SchemaElementImpl
 {
+    /** The list of superiors */
+    private List<String> superiors = new ArrayList<String>();
+    
+    /** The list of mandatory attributes */
+    private List<String> must = new ArrayList<String>();
+    
+    /** The list of optional attributes */
+    private List<String> may = new ArrayList<String>();
 
-    private String[] superiors = ArrayUtils.EMPTY_STRING_ARRAY;
-    private String[] must = ArrayUtils.EMPTY_STRING_ARRAY;
-    private String[] may = ArrayUtils.EMPTY_STRING_ARRAY;
-
+    /** The ObjectClass type */
     private ObjectClassTypeEnum classType = ObjectClassTypeEnum.STRUCTURAL;
 
-
-    // ------------------------------------------------------------------------
-    // C O N S T R U C T O R S
-    // ------------------------------------------------------------------------
-
-    public ObjectClassHolder(String oid)
+    /**
+     * Create an instance of ObjectClass element
+     */
+    public ObjectClassHolder( String oid )
     {
         this.oid = oid;
     }
 
-
-    // ------------------------------------------------------------------------
-    // Accessors and mutators
-    // ------------------------------------------------------------------------
-
-    public boolean isObsolete()
-    {
-        return obsolete;
-    }
-
-
-    public void setObsolete( boolean obsolete )
-    {
-        this.obsolete = obsolete;
-    }
-
-
-    public String getOid()
-    {
-        return oid;
-    }
-
-
-    public void setOid( String oid )
-    {
-        this.oid = oid;
-    }
-
-
-    public String getDescription()
-    {
-        return description;
-    }
-
-
-    public void setDescription( String description )
-    {
-        this.description = description;
-    }
-
-
-    public String[] getSuperiors()
+    /**
+     * Get the list of superior for this objectClass
+     * @return A list of all inherited objectClasses 
+     */
+    public List<String> getSuperiors()
     {
         return superiors;
     }
 
-
-    public void setSuperiors( String[] superiors )
+    /**
+     * Set the list of inherited objectClasses
+     * @param superiors The list of inherited objectClasses
+     */
+    public void setSuperiors( List<String> superiors )
     {
         this.superiors = superiors;
     }
 
-
-    public String[] getMust()
+    /**
+     * @return The list of mandatory attributes
+     */
+    public List<String> getMust()
     {
         return must;
     }
 
-
-    public void setMust( String[] must )
+    /**
+     * Set the list of mandatory attributes
+     * @param must The list of mandatory attributes
+     */
+    public void setMust( List<String> must )
     {
         this.must = must;
     }
 
-
-    public String[] getMay()
+    /**
+     * @return The list of optional attributes
+     */
+    public List<String> getMay()
     {
         return may;
     }
 
-
-    public void setMay( String[] may )
+    /**
+     * Set the list of optional attributes
+     * @param must The list of optional attributes
+     */
+    public void setMay( List<String> may )
     {
         this.may = may;
     }
 
-
+    /**
+     * @return The objectClass type
+     */
     public ObjectClassTypeEnum getClassType()
     {
         return classType;
     }
 
-
+    /**
+     * Set the objectClass type. 
+     * @param classType The objectClass type. 
+     */
     public void setClassType( ObjectClassTypeEnum classType )
     {
         this.classType = classType;
     }
 
-
-    // ------------------------------------------------------------------------
-    // Object overrides
-    // ------------------------------------------------------------------------
-
-    public int hashCode()
-    {
-        return getOid().hashCode();
-    }
-
-
-    public boolean equals( Object obj )
-    {
-        if ( this == obj )
+    /**
+     * Convert this objectClass to a Ldif string
+     * 
+     * @param schemaName The name of the schema file containing this objectClass
+     * @return A ldif formatted string
+     */
+    public String toLdif( String schemaName ) throws NamingException
+    {
+        StringBuilder sb = new StringBuilder();
+        
+        sb.append( schemaToLdif( schemaName, "metaObjectclass" ) );
+        
+        // The superiors
+        if ( superiors.size() != 0 )
         {
-            return true;
+            for ( String superior:superiors )
+            {
+                sb.append( "m-supObjectClass: " ).append( superior ).append( '\n' );
+            }
+        }
+        
+        // The kind of class
+        if ( classType != ObjectClassTypeEnum.STRUCTURAL )
+        {
+            sb.append( "m-typeObjectClass: " ).append( classType ).append( '\n' );
+        }
+        
+        // The 'must'
+        if ( must.size() != 0 )
+        {
+            for ( String attr:must )
+            {
+                sb.append( "m-must: " ).append( attr ).append( '\n' );
+            }
         }
 
-        if ( !( obj instanceof ObjectClassHolder ) )
+        // The 'may'
+        if ( may.size() != 0 )
         {
-            return false;
+            for ( String attr:may )
+            {
+                sb.append( "m-may: " ).append( attr ).append( '\n' );
+            }
+        }
+        
+        // The extensions
+        if ( extensions.size() != 0 )
+        {
+            extensionsToLdif( "m-extensionObjectClass" );
         }
 
-        return getOid().equals( ( ( ObjectClassHolder ) obj ).getOid() );
+        return sb.toString();
     }
 
-
+    /**
+     * Return a String representing this ObjectClass.
+     */
     public String toString()
     {
         return getOid();

Modified: directory/trunks/shared/convert/src/main/java/org/apache/directory/shared/converter/schema/Schema.java
URL: http://svn.apache.org/viewvc/directory/trunks/shared/convert/src/main/java/org/apache/directory/shared/converter/schema/Schema.java?view=diff&rev=496122&r1=496121&r2=496122
==============================================================================
--- directory/trunks/shared/convert/src/main/java/org/apache/directory/shared/converter/schema/Schema.java (original)
+++ directory/trunks/shared/convert/src/main/java/org/apache/directory/shared/converter/schema/Schema.java Sun Jan 14 12:08:13 2007
@@ -23,80 +23,74 @@
 import java.io.Writer;
 
 
+/**
+ * A bean used to hold a schema. We keep its name and we associate whith this
+ * object an inputStream mapped on the OpenLdap schema to read, and a writer
+ * in which the ldif file will be dumped.
+ *
+ * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
+ */
 public class Schema
 {
+    /** The schema name */
     private String name;
-    private String[] dependencies;
-    private String pkg;
-    private String owner;
+    
+    /** The inputStream mapped on the file to read */
     private InputStream in; 
+    
+    /** The writer where we dump the ldif lines */
     private Writer out;
 
-
-    public void setDependencies( String[] dependencies )
-    {
-        this.dependencies = dependencies;
-    }
-
-
-    public String[] getDependencies()
-    {
-        return dependencies;
-    }
-
-
-    public void setPkg( String pkg )
-    {
-        this.pkg = pkg;
-    }
-
-
-    public String getPkg()
-    {
-        return pkg;
-    }
-
-
-    public void setOwner( String owner )
-    {
-        this.owner = owner;
-    }
-
-
-    public String getOwner()
-    {
-        return owner;
-    }
-
-
+    /**
+     * Set the schema name to parse. This name is the prefix of the
+     * schema file, which postifx is '.schema'.
+     * 
+     * For instance, 'test.schema' being the file to parse, its name 
+     * will be 'test'
+     * @param name
+     */
     public void setName( String name )
     {
         this.name = name;
     }
 
-
+    /**
+     * @return The schema name.
+     */
     public String getName()
     {
         return name;
     }
     
+    /**
+     * Set the inputStream mapped on the schema file
+     * @param in The InputStream mapped on the schema file
+     */
     public void setInput( InputStream in )
     {
         this.in = in;
     }
     
+    /**
+     * @return The InputStream mapped on the schema file
+     */
     public InputStream getInput()
     {
         return in;
     }
 
-
+    /**
+     * @return The writer in which the ldif lines will be dumped
+     */
     public Writer getOutput()
     {
         return out;
     }
 
-
+    /**
+     * Set a writer to dump the ldif files
+     * @param out The writer 
+     */
     public void setOutput( Writer out )
     {
         this.out = out;

Modified: directory/trunks/shared/convert/src/main/java/org/apache/directory/shared/converter/schema/SchemaElement.java
URL: http://svn.apache.org/viewvc/directory/trunks/shared/convert/src/main/java/org/apache/directory/shared/converter/schema/SchemaElement.java?view=diff&rev=496122&r1=496121&r2=496122
==============================================================================
--- directory/trunks/shared/convert/src/main/java/org/apache/directory/shared/converter/schema/SchemaElement.java (original)
+++ directory/trunks/shared/convert/src/main/java/org/apache/directory/shared/converter/schema/SchemaElement.java Sun Jan 14 12:08:13 2007
@@ -1,14 +1,95 @@
-
+/*
+ *  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.converter.schema;
 
 import java.util.List;
 
+import javax.naming.NamingException;
+
+/**
+ * An interface defining the methods to be implemented by the SchemaElement 
+ * classes
+ *
+ * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
+ * @version $Rev: 437016 $
+ */
 public interface SchemaElement
 {
+    /**
+     * Tells if the attributeType is obsolete
+     * 
+     * @return true if the schema element is obsolete, folse otherwise
+     */
     boolean isObsolete();
+    
+    /**
+     * Set the obsolete flag
+     * 
+     * @param obsolete The value to be set
+     */
+    void setObsolete( boolean isObsolete );
+
+    /**
+     * Returns the schema element's OID
+     */
     String getOid();
+    
+    /**
+     * @return Return the schema element description
+     */
     String getDescription();
+    
+    /**
+     * Set the schema element's description
+     * @param description The schema element's description
+     */
+    void setDescription( String description );
+    
+    /**
+     * @return The list of names for the schemaElement
+     */
     List<String> getNames();
+    
+    /**
+     * Set a list of names for a schemaElement
+     * @param names The list of names of this schemaElement
+     */
     void setNames( List<String> names );
-    String getShortAlias();
+    
+    /**
+     * @return The list of extensions for the schemaElement
+     */
+    List<String> getExtensions();
+    
+    /**
+     * Set a list of extensions for a schemaElement
+     * @param names The list of extensions of this schemaElement
+     */
+    void setExtensions( List<String> extensions );
+
+    /**
+     * Generate a String representation of this schemaElement, formated
+     * as a ldif string 
+     * @param schemaName The schema from which is extracted this schemaElement
+     * @return A string representing the schemaElement as a Ldif formated  String 
+     * @throws NamingException If any error occurs.
+     */
+    String toLdif( String schemaName ) throws NamingException;
 }

Modified: directory/trunks/shared/convert/src/main/java/org/apache/directory/shared/converter/schema/SchemaElementImpl.java
URL: http://svn.apache.org/viewvc/directory/trunks/shared/convert/src/main/java/org/apache/directory/shared/converter/schema/SchemaElementImpl.java?view=diff&rev=496122&r1=496121&r2=496122
==============================================================================
--- directory/trunks/shared/convert/src/main/java/org/apache/directory/shared/converter/schema/SchemaElementImpl.java (original)
+++ directory/trunks/shared/convert/src/main/java/org/apache/directory/shared/converter/schema/SchemaElementImpl.java Sun Jan 14 12:08:13 2007
@@ -1,46 +1,270 @@
-
+/*
+ *  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.converter.schema;
 
 import java.util.ArrayList;
 import java.util.List;
 
-public class SchemaElementImpl implements SchemaElement
+import javax.naming.NamingException;
+import javax.naming.directory.Attribute;
+import javax.naming.directory.Attributes;
+
+import org.apache.directory.shared.ldap.ldif.LdifUtils;
+import org.apache.directory.shared.ldap.message.AttributeImpl;
+import org.apache.directory.shared.ldap.message.AttributesImpl;
+import org.apache.directory.shared.ldap.name.Rdn;
+import org.apache.directory.shared.ldap.util.StringTools;
+
+/**
+ * An abstract SchemaElement implementation. It contains shared
+ * elements from AttributeType and ObjectClass, like obsolete, oid, 
+ * description, names and extensions (not implemented)
+ *
+ * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
+ */
+public abstract class SchemaElementImpl implements SchemaElement
 {
-    protected boolean obsolete = false;
+    /** The schema element oid */
     protected String oid;
+    
+    /** The schema element description */
     protected String description;
+    
+    /** The list of names for this schemaElements */
     protected List<String> names = new ArrayList<String>();
+
+    /** The obsolete flag */
+    protected boolean obsolete = false;
+    
+    /** The optional list of extensions */
+    protected List<String> extensions = new ArrayList<String>();
     
+    /**
+     * @see SchemaElement#isObsolete()
+     */
     public boolean isObsolete()
     {
         return obsolete;
     }
-    
+
+    /**
+     * @see SchemaElement#setObsolete(boolean)
+     */
+    public void setObsolete( boolean obsolete )
+    {
+        this.obsolete = obsolete;
+    }
+
+    /**
+     * @see SchemaElement#getOid()
+     */
     public String getOid()
     {
         return oid;
     }
     
+    /**
+     * @see SchemaElement#getDescription()
+     */
     public String getDescription()
     {
         return description;
     }
     
+    /**
+     * @see SchemaElement#setDescription(String)
+     */
+    public void setDescription( String description )
+    {
+        this.description = description;
+    }
+    
+    /**
+     * @see SchemaElement#getNames()
+     */
     public List<String> getNames()
     {
         return names;
     }
     
+    /**
+     * @see SchemaElement#setNames(List)
+     */
     public void setNames( List<String> names )
     {
         this.names = names;
     }
 
+    /**
+     * @see SchemaElement#getExtensions()
+     */
+    public List<String> getExtensions()
+    {
+        return extensions;
+    }
+
+    /**
+     * @see SchemaElement#setExtensions(List)
+     */
+    public void setExtensions( List<String> extensions )
+    {
+        this.extensions = extensions;
+    }
+    
+    /**
+     * @return The OID as a Ldif line 
+     */
+    private String oidToLdif()
+    {
+        return "m-oid: " + oid + '\n';
+    }
+    
+    /**
+     * @return the Names as Ldif lines
+     */
+    private String nameToLdif() throws NamingException
+    {
+        if ( names.size() == 0 )
+        {
+            return "";
+        }
+        else
+        {
+            Attributes attributes = new AttributesImpl();
+            Attribute attribute = new AttributeImpl( "m-name" );
+            
+            for ( String name:names )
+            {
+                attribute.add( name );
+            }
+            
+            attributes.put( attribute );
+            
+            return LdifUtils.convertToLdif( attributes );
+        }
+    }
+    
+    /**
+     * @return The description as a ldif line
+     */
+    private String descToLdif() throws NamingException
+    {
+        if ( StringTools.isEmpty( description ) )
+        {
+            return "";
+        }
+        else
+        {
+            Attributes attributes = new AttributesImpl();
+            Attribute attribute = new AttributeImpl( "m-description", description );
+            attributes.put( attribute );
+            
+            return LdifUtils.convertToLdif( attributes );
+        }
+    }
+    
+    /**
+     * @return The dn as a ldif line
+     */
+    private String dnToLdif( String schemaName ) throws NamingException
+    {
+        StringBuilder sb = new StringBuilder();
+        
+        String name = null;
+
+        // We may have no names. In this case, we use the OID
+        if ( names.size() == 0 )
+        {
+            name = oid;
+        }
+        else
+        {
+            name = names.get( 0 );
+        }
 
+        String dn = "m-name=" + Rdn.escapeValue( name ) + 
+                    ", ou=" + Rdn.escapeValue( schemaName ) + 
+                    ", ou=schema";
 
+        // First dump the DN only
+        Attributes attributes = new AttributesImpl();
+        Attribute attribute = new AttributeImpl( "dn", dn );
 
-    public String getShortAlias()
+        attributes.put( attribute );
+        sb.append( LdifUtils.convertToLdif( attributes ) );
+        
+        return sb.toString();
+    }
+    
+    /**
+     * Return the extensions formated as Ldif lines
+     * @param ID The attributeId : can be m-objectClassExtension or
+     * m-attributeTypeExtension
+     * 
+     * @return The extensions formated as ldif lines
+     * @throws NamingException If the conversion goes wrong
+     */
+    protected String extensionsToLdif( String ID ) throws NamingException
     {
-        return ( names.size() == 0 ? "" : names.get( 0 ) );
+        StringBuilder sb = new StringBuilder();
+        
+        Attributes attributes = new AttributesImpl();
+        Attribute attribute = new AttributeImpl( ID ); 
+
+        for ( String extension:extensions )
+        {
+            attribute.add( extension );
+        }
+
+        sb.append( LdifUtils.convertToLdif( attributes ) );
+        
+        return sb.toString();
+    }
+
+    protected String schemaToLdif( String schemaName, String type ) throws NamingException
+    {
+        StringBuilder sb = new StringBuilder();
+        
+        // The DN
+        sb.append( dnToLdif( schemaName ) );
+
+        // ObjectClasses
+        sb.append( "objectclass: " ).append( type ).append( '\n' );
+        sb.append( "objectclass: metaTop\n" );
+        sb.append( "objectClass: top\n" );
+
+        // The oid
+        sb.append( oidToLdif() );
+
+        // The name
+        sb.append( nameToLdif() );
+        
+        // The desc
+        sb.append( descToLdif() );
+        
+        // The obsolete flag, only if "true"
+        if ( obsolete )
+        {
+            sb.append( "m-obsolete: true\n" );
+        }
+        
+        return sb.toString();
     }
 }

Modified: directory/trunks/shared/convert/src/main/java/org/apache/directory/shared/converter/schema/SchemaParser.java
URL: http://svn.apache.org/viewvc/directory/trunks/shared/convert/src/main/java/org/apache/directory/shared/converter/schema/SchemaParser.java?view=diff&rev=496122&r1=496121&r2=496122
==============================================================================
--- directory/trunks/shared/convert/src/main/java/org/apache/directory/shared/converter/schema/SchemaParser.java (original)
+++ directory/trunks/shared/convert/src/main/java/org/apache/directory/shared/converter/schema/SchemaParser.java Sun Jan 14 12:08:13 2007
@@ -43,7 +43,6 @@
  * A reusable wrapper for antlr generated schema parsers.
  *
  * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
- * @version $Rev: 494164 $
  */
 public class SchemaParser
 {
@@ -53,10 +52,13 @@
     /** A pipe into the parser */
     private PipedOutputStream parserIn = null;
 
+    /** A temporary buffer storing the read schema bytes */
     byte[] buf = new byte[128];
 
+    /** The inputStream mapped over the schema file to parse */
     private InputStream schemaIn;
     
+    /** The thread used to read the schema */
     private Thread producerThread;
 
     /**
@@ -69,7 +71,6 @@
         init();
     }
 
-
     /**
      * Initializes a parser and its plumbing.
      *
@@ -84,7 +85,9 @@
         parser = new antlrSchemaParser( lexer );
     }
 
-
+    /**
+     * Clear the parser.
+     */
     public synchronized void clear()
     {
         parser.clear();
@@ -97,12 +100,12 @@
      */
     public synchronized List<SchemaElement> parse( String schemaObject ) throws IOException, ParseException
     {
-        if ( schemaObject == null || schemaObject.trim().equals( "" ) )
+        if ( ( schemaObject == null ) || ( schemaObject.trim().equals( "" ) ) )
         {
-            throw new ParseException( "The schemaObject is either null or is " + "the empty String!", 0 );
+            throw new ParseException( "The schemaObject is either null or is empty!", 0 );
         }
 
-        this.schemaIn = new ByteArrayInputStream( schemaObject.getBytes() );
+        schemaIn = new ByteArrayInputStream( schemaObject.getBytes() );
 
         if ( producerThread == null )
         {
@@ -113,8 +116,15 @@
         return invokeParser( schemaObject );
     }
 
-
-    private List<SchemaElement> invokeParser( String subject ) throws IOException, ParseException
+    /**
+     * Invoke the parser
+     * @param schemaName The schema to be parsed
+     * @return A list of schema elements 
+     * 
+     * @throws IOException If the inputStream can't be read
+     * @throws ParseException If the parser encounter an error
+     */
+    private List<SchemaElement> invokeParser( String schemaName ) throws IOException, ParseException
     {
         try
         {
@@ -122,17 +132,17 @@
             
             return parser.getSchemaElements();
         }
-        catch ( RecognitionException e )
+        catch ( RecognitionException re )
         {
-            String msg = "Parser failure on:\n\t" + subject;
-            msg += "\nAntlr exception trace:\n" + ExceptionUtils.getFullStackTrace( e );
+            String msg = "Parser failure on:\n\t" + schemaName;
+            msg += "\nAntlr exception trace:\n" + ExceptionUtils.getFullStackTrace( re );
             init();
-            throw new ParseException( msg, e.getColumn() );
+            throw new ParseException( msg, re.getColumn() );
         }
-        catch ( TokenStreamException e2 )
+        catch ( TokenStreamException tse )
         {
-            String msg = "Parser failure on:\n\t" + subject;
-            msg += "\nAntlr exception trace:\n" + ExceptionUtils.getFullStackTrace( e2 );
+            String msg = "Parser failure on:\n\t" + schemaName;
+            msg += "\nAntlr exception trace:\n" + ExceptionUtils.getFullStackTrace( tse );
             init();
             throw new ParseException( msg, 0 );
         }
@@ -176,7 +186,10 @@
         invokeParser( "schema file ==> " + schemaFile.getAbsolutePath() );
     }
 
-
+    /**
+     * The thread which read the schema files and fill the
+     * temporaru buffer used by the lexical analyzer.
+     */
     class DataProducer implements Runnable
     {
         public void run()

Modified: directory/trunks/shared/convert/src/main/java/org/apache/directory/shared/converter/schema/SchemaToLdif.java
URL: http://svn.apache.org/viewvc/directory/trunks/shared/convert/src/main/java/org/apache/directory/shared/converter/schema/SchemaToLdif.java?view=diff&rev=496122&r1=496121&r2=496122
==============================================================================
--- directory/trunks/shared/convert/src/main/java/org/apache/directory/shared/converter/schema/SchemaToLdif.java (original)
+++ directory/trunks/shared/convert/src/main/java/org/apache/directory/shared/converter/schema/SchemaToLdif.java Sun Jan 14 12:08:13 2007
@@ -1,21 +1,38 @@
-
+/*
+ *  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.converter.schema;
 
 import java.io.InputStream;
 import java.io.Writer;
 import java.util.List;
 
-import javax.naming.directory.Attribute;
-import javax.naming.directory.Attributes;
-
 import org.apache.directory.shared.converter.schema.Schema;
-import org.apache.directory.shared.ldap.ldif.LdifUtils;
-import org.apache.directory.shared.ldap.message.AttributeImpl;
-import org.apache.directory.shared.ldap.message.AttributesImpl;
-import org.apache.directory.shared.ldap.name.Rdn;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
+/**
+ * A class used to translate a OpenLdap schema file to a Ldif file compatible
+ * with the Apache DS meta schema format
+ *
+ * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
+ */
 public class SchemaToLdif
 {
     private static final String HEADER = 
@@ -43,11 +60,16 @@
     /** The logger */
     private static Logger log = LoggerFactory.getLogger( SchemaToLdif.class );
 
-
-    public void transform( List<Schema> schemas ) throws ParserException
+    /**
+     * This method takes a list of schema and transform them to Ldif files 
+     * 
+     * @param schemas The list of schema to be transformed
+     * @throws ParserException If we get an error while converting the schemas
+     */
+    public static void transform( List<Schema> schemas ) throws ParserException
     {
         // Bypass if no schemas have yet been defined 
-        if ( schemas == null || schemas.size() == 0 )
+        if ( ( schemas == null ) || ( schemas.size() == 0 ) )
         {
             log.warn( "No schemas defined!" );
             return;
@@ -61,7 +83,7 @@
         {
             if ( schema.getName() == null )
             {
-                String msg = i + "th schema configuration element must specify a name.";
+                String msg = i + "the schema configuration element must specify a name.";
                 log.error( msg );
                 throw new ParserException( msg );
             }
@@ -73,7 +95,7 @@
         {
             try
             {
-                log.info( "Generating " + schema.getName() + " schema." );
+                log.info( "Generating {} schema.", schema.getName() );
                 generate( schema );
             }
             catch ( Exception e )
@@ -84,7 +106,14 @@
         }
     }
     
-    private void generate( Schema schema ) throws Exception
+    /**
+     * Generate the ldif from a schema. The schema contains the inputStream
+     * and Writer.
+     * 
+     * @param schema The schema to transfom
+     * @throws Exception If the conversion fails
+     */
+    private static void generate( Schema schema ) throws Exception
     {
         if ( schema == null )
         {
@@ -95,62 +124,25 @@
         InputStream in = schema.getInput();
         Writer out = schema.getOutput();
         
+        // First parse the schema
         SchemaParser parser = new SchemaParser();
         List<SchemaElement> elements = parser.parse( in, out );
         
+        // Start with the header (apache licence)
         out.write( HEADER );
         
+        // Iterate through each schema elemnts
         for ( SchemaElement element:elements )
         {
-            Attributes attributes = new AttributesImpl();
-            
-            StringBuilder sb = new StringBuilder();
-            String dn = "m-name=" + Rdn.escapeValue( element.getShortAlias() ) + ", ou=" + Rdn.escapeValue( schema.getName() ) + ", ou=schema";
-            
-            // First dump the DN only
-            Attribute attribute = new AttributeImpl( "dn", dn );
-            attributes.put( attribute );
-            sb.append( LdifUtils.convertToLdif( attributes ) );
-            
-            if ( element instanceof ObjectClassHolder )
-            {
-                sb.append( "objectclass: MetaObjectClass\n" );
-            }
-            else
-            {
-                sb.append( "objectclass: MetaAttribute\n" );
-            }
-            
-            sb.append( "objectclass: MetaTop\n" );
-            sb.append( "objectClass: top\n" );
-            
-            sb.append( "m-oid: " ).append( element.getOid() ).append( '\n' );
-            sb.append( "m-name: " ).append( element.getShortAlias() ).append( '\n' );
+            out.write(  element.toLdif( schema.getName() ) );
             
-            attributes = new AttributesImpl();
-            attribute = new AttributeImpl( "m-desc" );
-            attribute.add( element.getDescription() );
-            attributes.put( attribute );
-            
-            if ( element.isObsolete() )
-            {
-                sb.append( "m-obsolete: true\n" );
-            }
-            
-            sb.append( LdifUtils.convertToLdif( attributes ) ).append( '\n' );
-
-            if ( element instanceof ObjectClassHolder )
-            {
-                ObjectClassHolder objectClass = (ObjectClassHolder)element; 
-            }
-            else
-            {
-                AttributeTypeHolder attributeType = (AttributeTypeHolder)element; 
-            }
-
-            out.write(  sb.toString() );
+            out.write( '\n' );
         }
         
+        // Done. Flush the result and close the reader and writer
         out.flush();
+        
+        out.close();
+        in.close();
     }
 }