directory-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From fel...@apache.org
Subject svn commit: r592094 [10/35] - in /directory/sandbox/felixk/studio-schemaeditor: ./ META-INF/ src/ src/main/ src/main/java/ src/main/java/org/ src/main/java/org/apache/ src/main/java/org/apache/directory/ src/main/java/org/apache/directory/studio/ src/m...
Date Mon, 05 Nov 2007 17:15:02 GMT
Added: directory/sandbox/felixk/studio-schemaeditor/src/main/java/org/apache/directory/studio/schemaeditor/model/io/OpenLdapSchemaFileExporter.java
URL: http://svn.apache.org/viewvc/directory/sandbox/felixk/studio-schemaeditor/src/main/java/org/apache/directory/studio/schemaeditor/model/io/OpenLdapSchemaFileExporter.java?rev=592094&view=auto
==============================================================================
--- directory/sandbox/felixk/studio-schemaeditor/src/main/java/org/apache/directory/studio/schemaeditor/model/io/OpenLdapSchemaFileExporter.java (added)
+++ directory/sandbox/felixk/studio-schemaeditor/src/main/java/org/apache/directory/studio/schemaeditor/model/io/OpenLdapSchemaFileExporter.java Mon Nov  5 09:14:24 2007
@@ -0,0 +1,323 @@
+/*
+ *  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.studio.schemaeditor.model.io;
+
+
+import org.apache.directory.shared.ldap.schema.ObjectClassTypeEnum;
+import org.apache.directory.shared.ldap.schema.UsageEnum;
+import org.apache.directory.studio.schemaeditor.model.AttributeTypeImpl;
+import org.apache.directory.studio.schemaeditor.model.ObjectClassImpl;
+import org.apache.directory.studio.schemaeditor.model.Schema;
+
+
+/**
+ * This class is used to export a Schema file into the OpenLDAP Format.
+ *
+ * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
+ * @version $Rev$, $Date$
+ */
+public class OpenLdapSchemaFileExporter
+{
+    /**
+     * Converts the given schema to its source code representation
+     * in OpenLDAP Schema file format.
+     *
+     * @param schema
+     *      the schema to convert
+     * @return
+     *      the corresponding source code representation
+     */
+    public static String toSourceCode( Schema schema )
+    {
+        StringBuffer sb = new StringBuffer();
+
+        for ( AttributeTypeImpl at : schema.getAttributeTypes() )
+        {
+            sb.append( toSourceCode( at ) );
+            sb.append( "\n" );
+        }
+
+        for ( ObjectClassImpl oc : schema.getObjectClasses() )
+        {
+            sb.append( toSourceCode( oc ) );
+            sb.append( "\n" );
+        }
+
+        return sb.toString();
+    }
+
+
+    /**
+     * Converts the given attribute type to its source code representation
+     * in OpenLDAP Schema file format.
+     *
+     * @param at
+     *      the attribute type to convert
+     * @return
+     *      the corresponding source code representation
+     */
+    public static String toSourceCode( AttributeTypeImpl at )
+    {
+        StringBuffer sb = new StringBuffer();
+
+        // Opening the definition and OID
+        sb.append( "attributetype ( " + at.getOid() + " \n" ); //$NON-NLS-1$ //$NON-NLS-2$
+
+        // NAME(S)
+        String[] names = at.getNames();
+        if ( ( names != null ) && ( names.length > 0 ) )
+        {
+            sb.append( "\tNAME " ); //$NON-NLS-1$
+            if ( names.length > 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[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.getSuperiorName() != null ) && ( !at.getSuperiorName().equals( "" ) ) ) //$NON-NLS-1$
+        {
+            sb.append( "\tSUP " + at.getSuperiorName() + " \n" ); //$NON-NLS-1$ //$NON-NLS-2$
+        }
+
+        // EQUALITY
+        if ( ( at.getEqualityName() != null ) && ( !at.getEqualityName().equals( "" ) ) ) //$NON-NLS-1$
+        {
+            sb.append( "\tEQUALITY " + at.getEqualityName() + " \n" ); //$NON-NLS-1$ //$NON-NLS-2$
+        }
+
+        // ORDERING
+        if ( ( at.getOrderingName() != null ) && ( !at.getOrderingName().equals( "" ) ) ) //$NON-NLS-1$
+        {
+            sb.append( "\tORDERING " + at.getOrderingName() + " \n" ); //$NON-NLS-1$ //$NON-NLS-2$
+        }
+
+        // SUBSTR
+        if ( ( at.getSubstrName() != null ) && ( !at.getSubstrName().equals( "" ) ) ) //$NON-NLS-1$
+        {
+            sb.append( "\tSUBSTR " + at.getSubstrName() + " \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.getLength() > 0 )
+            {
+                sb.append( "{" + at.getLength() + "}" ); //$NON-NLS-1$ //$NON-NLS-2$
+            }
+            sb.append( " \n" ); //$NON-NLS-1$
+        }
+
+        // SINGLE-VALUE
+        if ( at.isSingleValue() )
+        {
+            sb.append( "\tSINGLE-VALUE \n" ); //$NON-NLS-1$
+        }
+
+        // COLLECTIVE
+        if ( at.isCollective() )
+        {
+            sb.append( "\tCOLLECTIVE \n" ); //$NON-NLS-1$
+        }
+
+        // NO-USER-MODIFICATION
+        if ( !at.isCanUserModify() )
+        {
+            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();
+    }
+
+
+    /**
+     * Converts the given object class to its source code representation
+     * in OpenLDAP Schema file format.
+     *
+     * @param at
+     *      the object class to convert
+     * @return
+     *      the corresponding source code representation
+     */
+    public static String toSourceCode( ObjectClassImpl oc )
+    {
+        StringBuffer sb = new StringBuffer();
+
+        // Opening the definition and OID
+        sb.append( "objectclass ( " + oc.getOid() + " \n" ); //$NON-NLS-1$ //$NON-NLS-2$
+
+        // NAME(S)
+        String[] names = oc.getNames();
+        if ( ( names != null ) && ( names.length > 0 ) )
+        {
+            sb.append( "\tNAME " ); //$NON-NLS-1$
+            if ( names.length > 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[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
+        String[] superiors = oc.getSuperClassesNames();
+        if ( ( superiors != null ) && ( superiors.length != 0 ) )
+        {
+            if ( superiors.length > 1 )
+            {
+                sb.append( "\tSUP (" + superiors[0] ); //$NON-NLS-1$
+                for ( int i = 1; i < superiors.length; i++ )
+                {
+                    sb.append( " $ " + superiors[i] ); //$NON-NLS-1$
+                }
+                sb.append( ") \n" ); //$NON-NLS-1$
+            }
+            else
+            {
+                sb.append( "\tSUP " + superiors[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
+        String[] must = oc.getMustNamesList();
+        if ( ( must != null ) && ( must.length != 0 ) )
+        {
+            sb.append( "\tMUST " ); //$NON-NLS-1$
+            if ( must.length > 1 )
+            {
+                sb.append( "( " + must[0] + " " ); //$NON-NLS-1$ //$NON-NLS-2$
+                for ( int i = 1; i < must.length; i++ )
+                {
+                    sb.append( "$ " + must[i] + " " ); //$NON-NLS-1$ //$NON-NLS-2$
+                }
+                sb.append( ") \n" ); //$NON-NLS-1$
+            }
+            else if ( must.length == 1 )
+            {
+                sb.append( must[0] + " \n" ); //$NON-NLS-1$
+            }
+        }
+
+        // MAY
+        String[] may = oc.getMayNamesList();
+        if ( ( may != null ) && ( may.length != 0 ) )
+        {
+            sb.append( "\tMAY " ); //$NON-NLS-1$
+            if ( may.length > 1 )
+            {
+                sb.append( "( " + may[0] + " " ); //$NON-NLS-1$ //$NON-NLS-2$
+                for ( int i = 1; i < may.length; i++ )
+                {
+                    sb.append( "$ " + may[i] + " " ); //$NON-NLS-1$ //$NON-NLS-2$
+                }
+                sb.append( ") \n" ); //$NON-NLS-1$
+            }
+            else if ( may.length == 1 )
+            {
+                sb.append( may[0] + " \n" ); //$NON-NLS-1$
+            }
+        }
+        // Closing the definition
+        sb.append( " )\n" ); //$NON-NLS-1$
+
+        return sb.toString();
+    }
+}

Propchange: directory/sandbox/felixk/studio-schemaeditor/src/main/java/org/apache/directory/studio/schemaeditor/model/io/OpenLdapSchemaFileExporter.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: directory/sandbox/felixk/studio-schemaeditor/src/main/java/org/apache/directory/studio/schemaeditor/model/io/OpenLdapSchemaFileImportException.java
URL: http://svn.apache.org/viewvc/directory/sandbox/felixk/studio-schemaeditor/src/main/java/org/apache/directory/studio/schemaeditor/model/io/OpenLdapSchemaFileImportException.java?rev=592094&view=auto
==============================================================================
--- directory/sandbox/felixk/studio-schemaeditor/src/main/java/org/apache/directory/studio/schemaeditor/model/io/OpenLdapSchemaFileImportException.java (added)
+++ directory/sandbox/felixk/studio-schemaeditor/src/main/java/org/apache/directory/studio/schemaeditor/model/io/OpenLdapSchemaFileImportException.java Mon Nov  5 09:14:24 2007
@@ -0,0 +1,44 @@
+/*
+ *  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.studio.schemaeditor.model.io;
+
+
+/**
+ * This class represents the OpenLdapSchemaFileImportException.
+ *
+ * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
+ * @version $Rev$, $Date$
+ */
+public class OpenLdapSchemaFileImportException extends Exception
+{
+    private static final long serialVersionUID = 1L;
+
+
+    /**
+     * Creates a new instance of OpenLdapSchemaFileImportException.
+     *
+     * @param message
+     *      the message
+     */
+    public OpenLdapSchemaFileImportException( String message )
+    {
+        super( message );
+    }
+}

Propchange: directory/sandbox/felixk/studio-schemaeditor/src/main/java/org/apache/directory/studio/schemaeditor/model/io/OpenLdapSchemaFileImportException.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: directory/sandbox/felixk/studio-schemaeditor/src/main/java/org/apache/directory/studio/schemaeditor/model/io/OpenLdapSchemaFileImporter.java
URL: http://svn.apache.org/viewvc/directory/sandbox/felixk/studio-schemaeditor/src/main/java/org/apache/directory/studio/schemaeditor/model/io/OpenLdapSchemaFileImporter.java?rev=592094&view=auto
==============================================================================
--- directory/sandbox/felixk/studio-schemaeditor/src/main/java/org/apache/directory/studio/schemaeditor/model/io/OpenLdapSchemaFileImporter.java (added)
+++ directory/sandbox/felixk/studio-schemaeditor/src/main/java/org/apache/directory/studio/schemaeditor/model/io/OpenLdapSchemaFileImporter.java Mon Nov  5 09:14:24 2007
@@ -0,0 +1,204 @@
+/*
+ *  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.studio.schemaeditor.model.io;
+
+
+import java.io.File;
+import java.io.IOException;
+import java.io.InputStream;
+import java.net.MalformedURLException;
+import java.text.ParseException;
+import java.util.List;
+
+import org.apache.directory.server.core.tools.schema.AttributeTypeLiteral;
+import org.apache.directory.server.core.tools.schema.ObjectClassLiteral;
+import org.apache.directory.server.core.tools.schema.OpenLdapSchemaParser;
+import org.apache.directory.studio.schemaeditor.model.AttributeTypeImpl;
+import org.apache.directory.studio.schemaeditor.model.ObjectClassImpl;
+import org.apache.directory.studio.schemaeditor.model.Schema;
+import org.apache.directory.studio.schemaeditor.model.SchemaImpl;
+
+
+/**
+ * This class is used to import a Schema file in the OpenLDAP Format.
+ *
+ * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
+ * @version $Rev$, $Date$
+ */
+public class OpenLdapSchemaFileImporter
+{
+    /**
+     * Extracts the Schema from the given path.
+     *
+     * @param path
+     *      the path of the file.
+     * @return
+     *      the corresponding schema
+     * @throws OpenLdapSchemaFileImportException
+     *      if an error occurrs when importing the schema
+     */
+    public static Schema getSchema( String path ) throws OpenLdapSchemaFileImportException
+    {
+        File file = new File( path );
+
+        // Checking the file properties
+        if ( !file.exists() )
+        {
+            throw new OpenLdapSchemaFileImportException( "The file '" + path + "' does not exist." );
+        }
+        else if ( !file.canRead() )
+        {
+            throw new OpenLdapSchemaFileImportException( "The file '" + path + "' can not be read." );
+        }
+
+        InputStream in = null;
+        try
+        {
+            in = file.toURL().openStream();
+        }
+        catch ( MalformedURLException e )
+        {
+            throw new OpenLdapSchemaFileImportException( "The file '" + path + "' can not be read correctly." );
+        }
+        catch ( IOException e )
+        {
+            throw new OpenLdapSchemaFileImportException( "The file '" + path + "' can not be read correctly." );
+        }
+
+        OpenLdapSchemaParser parser = null;
+        try
+        {
+            parser = new OpenLdapSchemaParser();
+        }
+        catch ( IOException e )
+        {
+            throw new OpenLdapSchemaFileImportException( "The file '" + path + "' can not be read correctly." );
+        }
+
+        try
+        {
+            parser.parse( in );
+        }
+        catch ( IOException e )
+        {
+            throw new OpenLdapSchemaFileImportException( "The file '" + path + "' can not be read correctly." );
+        }
+        catch ( ParseException e )
+        {
+            throw new OpenLdapSchemaFileImportException( "The file '" + path + "' can not be read correctly." );
+        }
+
+        String schemaName = getNameFromPath( path );
+
+        Schema schema = new SchemaImpl( schemaName );
+
+        List<?> ats = parser.getAttributeTypes();
+        for ( int i = 0; i < ats.size(); i++ )
+        {
+            AttributeTypeImpl at = convertAttributeType( ( AttributeTypeLiteral ) ats.get( i ) );
+            at.setSchema( schemaName );
+            schema.addAttributeType( at );
+        }
+
+        List<?> ocs = parser.getObjectClassTypes();
+        for ( int i = 0; i < ocs.size(); i++ )
+        {
+            ObjectClassImpl oc = convertObjectClass( ( ObjectClassLiteral ) ocs.get( i ) );
+            oc.setSchema( schemaName );
+            schema.addObjectClass( oc );
+        }
+
+        return schema;
+    }
+
+
+    /**
+     * Gets the name of the file.
+     *
+     * @param path
+     *      the path
+     * @return
+     *      the name of the file.
+     */
+    private static final String getNameFromPath( String path )
+    {
+        File file = new File( path );
+        String fileName = file.getName();
+        if ( fileName.endsWith( ".schema" ) ) //$NON-NLS-1$
+        {
+            String[] fileNameSplitted = fileName.split( "\\." ); //$NON-NLS-1$
+            return fileNameSplitted[0];
+        }
+
+        return fileName;
+    }
+
+
+    /**
+     * Convert the given AttributeTypeLiteral into its AttributeTypeImpl representation.
+     *
+     * @param at
+     *      the AttributeTypeLiteral
+     * @return
+     *      the corresponding AttributeTypeImpl
+     */
+    private static final AttributeTypeImpl convertAttributeType( AttributeTypeLiteral at )
+    {
+        AttributeTypeImpl newAT = new AttributeTypeImpl( at.getOid() );
+        newAT.setNames( at.getNames() );
+        newAT.setDescription( at.getDescription() );
+        newAT.setSuperiorName( at.getSuperior() );
+        newAT.setUsage( at.getUsage() );
+        newAT.setSyntaxOid( at.getSyntax() );
+        newAT.setLength( at.getLength() );
+        newAT.setObsolete( at.isObsolete() );
+        newAT.setSingleValue( at.isSingleValue() );
+        newAT.setCollective( at.isCollective() );
+        newAT.setCanUserModify( !at.isNoUserModification() );
+        newAT.setEqualityName( at.getEquality() );
+        newAT.setOrderingName( at.getOrdering() );
+        newAT.setSubstrName( at.getSubstr() );
+
+        return newAT;
+    }
+
+
+    /**
+     * Convert the given ObjectClassLiteral into its ObjectClassImpl representation.
+     *
+     * @param oc
+     *      the ObjectClassLiteral
+     * @return
+     *      the corresponding ObjectClassImpl
+     */
+    private static final ObjectClassImpl convertObjectClass( ObjectClassLiteral oc )
+    {
+        ObjectClassImpl newOC = new ObjectClassImpl( oc.getOid() );
+        newOC.setNames( oc.getNames() );
+        newOC.setDescription( oc.getDescription() );
+        newOC.setSuperClassesNames( oc.getSuperiors() );
+        newOC.setType( oc.getClassType() );
+        newOC.setObsolete( oc.isObsolete() );
+        newOC.setMustNamesList( oc.getMust() );
+        newOC.setMayNamesList( oc.getMay() );
+
+        return newOC;
+    }
+}

Propchange: directory/sandbox/felixk/studio-schemaeditor/src/main/java/org/apache/directory/studio/schemaeditor/model/io/OpenLdapSchemaFileImporter.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: directory/sandbox/felixk/studio-schemaeditor/src/main/java/org/apache/directory/studio/schemaeditor/model/io/ProjectsExporter.java
URL: http://svn.apache.org/viewvc/directory/sandbox/felixk/studio-schemaeditor/src/main/java/org/apache/directory/studio/schemaeditor/model/io/ProjectsExporter.java?rev=592094&view=auto
==============================================================================
--- directory/sandbox/felixk/studio-schemaeditor/src/main/java/org/apache/directory/studio/schemaeditor/model/io/ProjectsExporter.java (added)
+++ directory/sandbox/felixk/studio-schemaeditor/src/main/java/org/apache/directory/studio/schemaeditor/model/io/ProjectsExporter.java Mon Nov  5 09:14:24 2007
@@ -0,0 +1,153 @@
+/*
+ *  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.studio.schemaeditor.model.io;
+
+
+import java.util.List;
+
+import org.apache.directory.studio.schemaeditor.model.Project;
+import org.apache.directory.studio.schemaeditor.model.ProjectType;
+import org.apache.directory.studio.schemaeditor.model.Schema;
+import org.dom4j.Branch;
+import org.dom4j.Document;
+import org.dom4j.DocumentHelper;
+import org.dom4j.Element;
+
+
+/**
+ * This class is used to export Project(s) into the XML Format.
+ *
+ * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
+ * @version $Rev$, $Date$
+ */
+public class ProjectsExporter
+{
+    // The tags
+    private static final String PROJECT_TAG = "project";
+    private static final String PROJECTS_TAG = "projects";
+    private static final String NAME_TAG = "name";
+    private static final String TYPE_TAG = "type";
+    private static final String CONNECTION_TAG = "connection";
+    private static final String SCHEMA_CONNECTOR_TAG = "schemaConnector";
+    private static final String SCHEMA_BACKUP_TAG = "schemaBackup";
+
+
+    /**
+     * Converts the given project to its representation
+     * in Dom4J Document.
+     * 
+     * @param project
+     *      the project to convert
+     * @return
+     *      the corresponding Dom4j Document representation
+     */
+    public static Document toDocument( Project project )
+    {
+        // Creating the Document
+        Document document = DocumentHelper.createDocument();
+
+        // Adding the project
+        addProject( project, document );
+
+        return document;
+    }
+
+
+    /**
+     * Converts the given projects to their representation
+     * in Dom4J Document.
+     *
+     * @param projects
+     *      the projects to convert
+     * @return
+     *      the corresponding Dom4j Document representation
+     */
+    public static Document toDocument( Project[] projects )
+    {
+        // Creating the Document
+        Document document = DocumentHelper.createDocument();
+        Element projectsElement = document.addElement( PROJECTS_TAG );
+
+        if ( projects != null )
+        {
+            for ( Project project : projects )
+            {
+                addProject( project, projectsElement );
+            }
+        }
+
+        return document;
+    }
+
+
+    /**
+     * Add the XML representation of the given project
+     * to the given branch
+     *
+     * @param project
+     *      the project
+     * @param branch
+     *      the branch
+     */
+    private static void addProject( Project project, Branch branch )
+    {
+        Element element = branch.addElement( PROJECT_TAG );
+
+        if ( project != null )
+        {
+            // Name 
+            String name = project.getName();
+            if ( ( name != null ) && ( !name.equals( "" ) ) )
+            {
+                element.addAttribute( NAME_TAG, name );
+            }
+
+            // Type
+            ProjectType type = project.getType();
+            if ( type != null )
+            {
+                element.addAttribute( TYPE_TAG, type.toString() );
+            }
+
+            // If project is an Online Schema Project
+            if ( type.equals( ProjectType.ONLINE ) )
+            {
+                // Connection Name
+                element.addAttribute( CONNECTION_TAG, project.getConnection().getId() );
+
+                // Connection Name
+                element.addAttribute( SCHEMA_CONNECTOR_TAG, project.getSchemaConnector().getId() );
+
+                // Schema Backup
+                Element schemaBackupElement = element.addElement( SCHEMA_BACKUP_TAG );
+                List<Schema> backupSchemas = project.getSchemaBackup();
+                if ( backupSchemas != null )
+                {
+                    XMLSchemaFileExporter.addSchemas( backupSchemas.toArray( new Schema[0] ), schemaBackupElement );
+                }
+
+            }
+
+            // Schemas
+            XMLSchemaFileExporter
+                .addSchemas( project.getSchemaHandler().getSchemas().toArray( new Schema[0] ), element );
+        }
+    }
+}

Propchange: directory/sandbox/felixk/studio-schemaeditor/src/main/java/org/apache/directory/studio/schemaeditor/model/io/ProjectsExporter.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: directory/sandbox/felixk/studio-schemaeditor/src/main/java/org/apache/directory/studio/schemaeditor/model/io/ProjectsImportException.java
URL: http://svn.apache.org/viewvc/directory/sandbox/felixk/studio-schemaeditor/src/main/java/org/apache/directory/studio/schemaeditor/model/io/ProjectsImportException.java?rev=592094&view=auto
==============================================================================
--- directory/sandbox/felixk/studio-schemaeditor/src/main/java/org/apache/directory/studio/schemaeditor/model/io/ProjectsImportException.java (added)
+++ directory/sandbox/felixk/studio-schemaeditor/src/main/java/org/apache/directory/studio/schemaeditor/model/io/ProjectsImportException.java Mon Nov  5 09:14:24 2007
@@ -0,0 +1,44 @@
+/*
+ *  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.studio.schemaeditor.model.io;
+
+
+/**
+ * This class represents the ProjectsImportException.
+ *
+ * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
+ * @version $Rev$, $Date$
+ */
+public class ProjectsImportException extends Exception
+{
+    private static final long serialVersionUID = 1L;
+
+
+    /**
+     * Creates a new instance of ProjectsImportException.
+     *
+     * @param message
+     *      the message
+     */
+    public ProjectsImportException( String message )
+    {
+        super( message );
+    }
+}

Propchange: directory/sandbox/felixk/studio-schemaeditor/src/main/java/org/apache/directory/studio/schemaeditor/model/io/ProjectsImportException.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: directory/sandbox/felixk/studio-schemaeditor/src/main/java/org/apache/directory/studio/schemaeditor/model/io/ProjectsImporter.java
URL: http://svn.apache.org/viewvc/directory/sandbox/felixk/studio-schemaeditor/src/main/java/org/apache/directory/studio/schemaeditor/model/io/ProjectsImporter.java?rev=592094&view=auto
==============================================================================
--- directory/sandbox/felixk/studio-schemaeditor/src/main/java/org/apache/directory/studio/schemaeditor/model/io/ProjectsImporter.java (added)
+++ directory/sandbox/felixk/studio-schemaeditor/src/main/java/org/apache/directory/studio/schemaeditor/model/io/ProjectsImporter.java Mon Nov  5 09:14:24 2007
@@ -0,0 +1,299 @@
+/*
+ *  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.studio.schemaeditor.model.io;
+
+
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Iterator;
+import java.util.List;
+
+import org.apache.directory.studio.schemaeditor.PluginUtils;
+import org.apache.directory.studio.schemaeditor.model.Project;
+import org.apache.directory.studio.schemaeditor.model.ProjectType;
+import org.apache.directory.studio.schemaeditor.model.Schema;
+import org.dom4j.Attribute;
+import org.dom4j.Document;
+import org.dom4j.DocumentException;
+import org.dom4j.Element;
+import org.dom4j.io.SAXReader;
+
+
+/**
+ * This class is used to import a Project file.
+ *
+ * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
+ * @version $Rev$, $Date$
+ */
+public class ProjectsImporter
+{
+    // The tags
+    private static final String PROJECT_TAG = "project";
+    private static final String PROJECTS_TAG = "projects";
+    private static final String NAME_TAG = "name";
+    private static final String SCHEMAS_TAG = "schemas";
+    private static final String TYPE_TAG = "type";
+    private static final String CONNECTION_TAG = "connection";
+    private static final String SCHEMA_CONNECTOR_TAG = "schemaConnector";
+    private static final String SCHEMA_BACKUP_TAG = "schemaBackup";
+
+
+    /**
+     * Extract the project from the given path
+     *
+     * @param path
+     *      the path of the file
+     * @return
+     *      the corresponding project
+     * @throws ProjectsImportException 
+     *      if an error occurs when importing the project
+     */
+    public static Project getProject( String path ) throws ProjectsImportException
+    {
+        Project project = new Project();
+
+        SAXReader reader = new SAXReader();
+        Document document = null;
+        try
+        {
+            document = reader.read( path );
+        }
+        catch ( DocumentException e )
+        {
+            throw new ProjectsImportException( "The file '" + path + "' can not be read correctly." );
+        }
+
+        Element rootElement = document.getRootElement();
+        if ( !rootElement.getName().equals( PROJECT_TAG ) )
+        {
+            throw new ProjectsImportException( "The file '" + path + "' does not seem to be a valid project file." );
+        }
+
+        readProject( rootElement, project, path );
+
+        return project;
+    }
+
+
+    /**
+     * Extract the projects from the given path
+     *
+     * @param path
+     *      the path of the file
+     * @return
+     *      the corresponding projects
+     * @throws ProjectsImportException 
+     *      if an error occurs when importing the project
+     */
+    public static Project[] getProjects( String path ) throws ProjectsImportException
+    {
+        List<Project> projects = new ArrayList<Project>();
+
+        SAXReader reader = new SAXReader();
+        Document document = null;
+        try
+        {
+            document = reader.read( path );
+        }
+        catch ( DocumentException e )
+        {
+            throw new ProjectsImportException( "The file '" + path + "' can not be read correctly." );
+        }
+
+        Element rootElement = document.getRootElement();
+        if ( !rootElement.getName().equals( PROJECTS_TAG ) )
+        {
+            throw new ProjectsImportException( "The file '" + path + "' does not seem to be a valid project file." );
+        }
+
+        for ( Iterator<?> i = rootElement.elementIterator( PROJECT_TAG ); i.hasNext(); )
+        {
+            Element projectElement = ( Element ) i.next();
+            Project project = new Project();
+            readProject( projectElement, project, path );
+            projects.add( project );
+        }
+
+        return projects.toArray( new Project[0] );
+    }
+
+
+    /**
+     * Reads a project.
+     *
+     * @param element
+     *      the element
+     * @param project
+     *      the project
+     * @param path
+     *      the path
+     * @throws ProjectsImportException 
+     *      if an error occurs when importing the project
+     */
+    private static void readProject( Element element, Project project, String path ) throws ProjectsImportException
+    {
+        // Name
+        Attribute nameAttribute = element.attribute( NAME_TAG );
+        if ( ( nameAttribute != null ) && ( !nameAttribute.getValue().equals( "" ) ) )
+        {
+            project.setName( nameAttribute.getValue() );
+        }
+
+        // Type
+        Attribute typeAttribute = element.attribute( TYPE_TAG );
+        if ( ( typeAttribute != null ) && ( !typeAttribute.getValue().equals( "" ) ) )
+        {
+            try
+            {
+                project.setType( ProjectType.valueOf( typeAttribute.getValue() ) );
+            }
+            catch ( IllegalArgumentException e )
+            {
+                throw new ProjectsImportException( "The parser was not able to convert the type value of the project." );
+            }
+        }
+
+        // If project is an Online Schema Project
+        if ( project.getType().equals( ProjectType.ONLINE ) )
+        {
+            // Connection
+            Attribute connectionAttribute = element.attribute( CONNECTION_TAG );
+            if ( ( connectionAttribute != null ) && ( !connectionAttribute.getValue().equals( "" ) ) )
+            {
+                project.setConnection( PluginUtils.getConnection( connectionAttribute.getValue() ) );
+            }
+
+            // Schema Connector
+            Attribute schemaConnectorAttribute = element.attribute( SCHEMA_CONNECTOR_TAG );
+            if ( ( schemaConnectorAttribute != null ) && ( !schemaConnectorAttribute.getValue().equals( "" ) ) )
+            {
+                String schemaConnectorId = schemaConnectorAttribute.getValue();
+
+                SchemaConnector schemaConnector = null;
+                List<SchemaConnector> schemaConnectors = PluginUtils.getSchemaConnectors();
+                for ( SchemaConnector sc : schemaConnectors )
+                {
+                    if ( sc.getId().equalsIgnoreCase( schemaConnectorId ) )
+                    {
+                        schemaConnector = sc;
+                    }
+                }
+
+                if ( schemaConnector == null )
+                {
+                    throw new ProjectsImportException( "The parser was not able to find the SchemaConnector with ID :"
+                        + schemaConnectorId + "." );
+                }
+
+                project.setSchemaConnector( schemaConnector );
+            }
+
+            // SchemaBackup
+            Element schemaBackupElement = element.element( SCHEMA_BACKUP_TAG );
+            if ( schemaBackupElement != null )
+            {
+                Element schemasElement = schemaBackupElement.element( SCHEMAS_TAG );
+                if ( schemasElement != null )
+                {
+                    Schema[] schemas = null;
+                    try
+                    {
+                        schemas = XMLSchemaFileImporter.readSchemas( schemasElement, path );
+                    }
+                    catch ( XMLSchemaFileImportException e )
+                    {
+                        throw new ProjectsImportException(
+                            "The parser was not able to convert the schemas of the project." );
+                    }
+
+                    project.setSchemaBackup( Arrays.asList( schemas ) );
+                }
+            }
+        }
+
+        // Schemas
+        Element schemasElement = element.element( SCHEMAS_TAG );
+        if ( schemasElement != null )
+        {
+            Schema[] schemas = null;
+            try
+            {
+                schemas = XMLSchemaFileImporter.readSchemas( schemasElement, path );
+            }
+            catch ( XMLSchemaFileImportException e )
+            {
+                throw new ProjectsImportException( "The parser was not able to convert the schemas of the project." );
+            }
+            for ( Schema schema : schemas )
+            {
+                project.getSchemaHandler().addSchema( schema );
+            }
+        }
+    }
+
+    /**
+     * This enum represents the different types of project files.
+     *
+     * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
+     * @version $Rev$, $Date$
+     */
+    public enum ProjectFileType
+    {
+        SINGLE, MULTIPLE
+    }
+
+
+    /**
+     * Gets the type of file.
+     *
+     * @param path
+     *      the path of the file
+     * @return
+     *      the type of the file
+     * @throws ProjectsImportException
+     */
+    public static ProjectFileType getProjectFileType( String path ) throws ProjectsImportException
+    {
+        SAXReader reader = new SAXReader();
+        Document document = null;
+        try
+        {
+            document = reader.read( path );
+        }
+        catch ( DocumentException e )
+        {
+            throw new ProjectsImportException( "The file '" + path + "' can not be read correctly." );
+        }
+
+        Element rootElement = document.getRootElement();
+        if ( rootElement.getName().equals( PROJECT_TAG ) )
+        {
+            return ProjectFileType.SINGLE;
+        }
+        else if ( rootElement.getName().equals( PROJECTS_TAG ) )
+        {
+            return ProjectFileType.MULTIPLE;
+        }
+        else
+        {
+            throw new ProjectsImportException( "The file '" + path + "' does not seem to be a valid project file." );
+        }
+    }
+}

Propchange: directory/sandbox/felixk/studio-schemaeditor/src/main/java/org/apache/directory/studio/schemaeditor/model/io/ProjectsImporter.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: directory/sandbox/felixk/studio-schemaeditor/src/main/java/org/apache/directory/studio/schemaeditor/model/io/SchemaConnector.java
URL: http://svn.apache.org/viewvc/directory/sandbox/felixk/studio-schemaeditor/src/main/java/org/apache/directory/studio/schemaeditor/model/io/SchemaConnector.java?rev=592094&view=auto
==============================================================================
--- directory/sandbox/felixk/studio-schemaeditor/src/main/java/org/apache/directory/studio/schemaeditor/model/io/SchemaConnector.java (added)
+++ directory/sandbox/felixk/studio-schemaeditor/src/main/java/org/apache/directory/studio/schemaeditor/model/io/SchemaConnector.java Mon Nov  5 09:14:24 2007
@@ -0,0 +1,138 @@
+/*
+ *  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.studio.schemaeditor.model.io;
+
+
+import java.util.List;
+
+import org.apache.directory.studio.connection.core.Connection;
+import org.apache.directory.studio.connection.core.StudioProgressMonitor;
+import org.apache.directory.studio.schemaeditor.model.Schema;
+
+
+/**
+ * This interface defines a SchemaConnector.
+ *
+ * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
+ * @version $Rev$, $Date$
+ */
+public interface SchemaConnector
+{
+    /**
+     * Indicates whether the SchemaConnector is suitable for use with the
+     * given connection.
+     *
+     * @param connection
+     *      the connection
+     * @param monitor
+     *      the progress monitor
+     * @return
+     *      true if the SchemaConnector is suitable for the given connection,
+     *      false if not
+     */
+    /**
+     * TODO isSuitableConnector.
+     *
+     * @param connection
+     * @param monitor
+     * @return
+     */
+    public boolean isSuitableConnector( Connection connection, StudioProgressMonitor monitor );
+
+
+    /**
+     * Imports the Schema of the LDAP Server using the given connection and
+     * progress monitor.
+     *
+     * @param connection
+     *      the connection
+     * @param monitor
+     *      the progress monitor
+     * @return
+     *      the list of schemas of the LDAP Server
+     */
+    public List<Schema> importSchema( Connection connection, StudioProgressMonitor monitor );
+
+
+    /**
+     * Exports the Schema to the LDAP Server using the given connection and
+     * progress monitor.
+     *
+     * @param connection
+     *      the connection
+     * @param monitor
+     *      the progress monitor
+     */
+    public void exportSchema( Connection connection, StudioProgressMonitor monitor );
+
+
+    /**
+     * Gets the name.
+     *
+     * @return
+     *      the name
+     */
+    public String getName();
+
+
+    /**
+     * Sets the name.
+     *
+     * @param name
+     *      the name
+     */
+    public void setName( String name );
+
+
+    /**
+     * Gets the ID.
+     *
+     * @return
+     *      the ID
+     */
+    public String getId();
+
+
+    /**
+     * Sets the ID.
+     *
+     * @param id
+     *      the ID
+     */
+    public void setId( String id );
+
+
+    /**
+     * Gets the description.
+     *
+     * @return
+     *      the description
+     */
+    public String getDescription();
+
+
+    /**
+     * Sets the description.
+     *
+     * @param description
+     *      the description
+     */
+    public void setDescription( String description );
+}

Propchange: directory/sandbox/felixk/studio-schemaeditor/src/main/java/org/apache/directory/studio/schemaeditor/model/io/SchemaConnector.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: directory/sandbox/felixk/studio-schemaeditor/src/main/java/org/apache/directory/studio/schemaeditor/model/io/XMLSchemaFileExporter.java
URL: http://svn.apache.org/viewvc/directory/sandbox/felixk/studio-schemaeditor/src/main/java/org/apache/directory/studio/schemaeditor/model/io/XMLSchemaFileExporter.java?rev=592094&view=auto
==============================================================================
--- directory/sandbox/felixk/studio-schemaeditor/src/main/java/org/apache/directory/studio/schemaeditor/model/io/XMLSchemaFileExporter.java (added)
+++ directory/sandbox/felixk/studio-schemaeditor/src/main/java/org/apache/directory/studio/schemaeditor/model/io/XMLSchemaFileExporter.java Mon Nov  5 09:14:24 2007
@@ -0,0 +1,596 @@
+/*
+ *  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.studio.schemaeditor.model.io;
+
+
+import java.util.List;
+
+import javax.xml.transform.Transformer;
+import javax.xml.transform.TransformerConfigurationException;
+import javax.xml.transform.TransformerException;
+import javax.xml.transform.TransformerFactory;
+import javax.xml.transform.stream.StreamSource;
+
+import org.apache.directory.shared.ldap.schema.ObjectClassTypeEnum;
+import org.apache.directory.shared.ldap.schema.UsageEnum;
+import org.apache.directory.studio.schemaeditor.Activator;
+import org.apache.directory.studio.schemaeditor.model.AttributeTypeImpl;
+import org.apache.directory.studio.schemaeditor.model.MatchingRuleImpl;
+import org.apache.directory.studio.schemaeditor.model.ObjectClassImpl;
+import org.apache.directory.studio.schemaeditor.model.Schema;
+import org.apache.directory.studio.schemaeditor.model.SyntaxImpl;
+import org.dom4j.Branch;
+import org.dom4j.Document;
+import org.dom4j.DocumentHelper;
+import org.dom4j.Element;
+import org.dom4j.io.DocumentResult;
+import org.dom4j.io.DocumentSource;
+
+
+/**
+ * This class is used to export a Schema file into the XML Format.
+ *
+ * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
+ * @version $Rev$, $Date$
+ */
+public class XMLSchemaFileExporter
+{
+    // The Tags
+    private static final String ALIAS_TAG = "alias";
+    private static final String ALIASES_TAG = "aliases";
+    private static final String ATTRIBUTE_TYPE_TAG = "attributetype";
+    private static final String ATTRIBUTE_TYPES_TAG = "attributetypes";
+    private static final String BOOLEAN_FALSE = "false";
+    private static final String BOOLEAN_TRUE = "true";
+    private static final String COLLECTIVE_TAG = "collective";
+    private static final String DESCRIPTION_TAG = "description";
+    private static final String EQUALITY_TAG = "equality";
+    private static final String HUMAN_READABLE_TAG = "humanreadable";
+    private static final String MANDATORY_TAG = "mandatory";
+    private static final String MATCHING_RULE_TAG = "matchingrule";
+    private static final String MATCHING_RULES_TAG = "matchingrules";
+    private static final String NAME_TAG = "name";
+    private static final String NO_USER_MODIFICATION_TAG = "nousermodification";
+    private static final String OBJECT_CLASS_TAG = "objectclass";
+    private static final String OBJECT_CLASSES_TAG = "objectclasses";
+    private static final String OBSOLETE_TAG = "obsolete";
+    private static final String OID_TAG = "oid";
+    private static final String OPTIONAL_TAG = "optional";
+    private static final String ORDERING_TAG = "ordering";
+    private static final String SCHEMA_TAG = "schema";
+    private static final String SCHEMAS_TAG = "schemas";
+    private static final String SINGLE_VALUE_TAG = "singlevalue";
+    private static final String SUBSTRING_TAG = "substring";
+    private static final String SUPERIOR_TAG = "superior";
+    private static final String SUPERIORS_TAG = "superiors";
+    private static final String SYNTAX_LENGTH_TAG = "syntaxlength";
+    private static final String SYNTAX_OID_TAG = "syntaxoid";
+    private static final String SYNTAX_TAG = "syntax";
+    private static final String SYNTAXES_TAG = "syntaxes";
+    private static final String TYPE_TAG = "type";
+    private static final String USAGE_TAG = "usage";
+
+
+    /**
+     * Converts the given schema to its source code representation
+     * in XML file format.
+     *
+     * @param schema
+     *      the schema to convert
+     * @return
+     *      the corresponding source code representation
+     */
+    public static String toXml( Schema schema )
+    {
+        // Creating the Document
+        Document document = DocumentHelper.createDocument();
+
+        // Adding the schema
+        addSchema( schema, document );
+
+        return styleDocument( document ).asXML();
+    }
+
+
+    /**
+     * Converts the given schemas to their source code representation
+     * in one XML file format.
+     *
+     * @param schemas
+     *      the array of schemas to convert
+     * @return
+     *      the corresponding source code representation
+     */
+    public static String toXml( Schema[] schemas )
+    {
+        // Creating the Document and the 'root' Element
+        Document document = DocumentHelper.createDocument();
+
+        addSchemas( schemas, document );
+
+        return styleDocument( document ).asXML();
+    }
+
+
+    /**
+     * Add the XML representation of the given schemas
+     * to the given branch.
+     *
+     * @param schemas
+     *      the schemas
+     * @param branch
+     *      the branch
+     */
+    public static void addSchemas( Schema[] schemas, Branch branch )
+    {
+        Element element = branch.addElement( SCHEMAS_TAG );
+
+        if ( schemas != null )
+        {
+            for ( Schema schema : schemas )
+            {
+                addSchema( schema, element );
+            }
+        }
+    }
+
+
+    /**
+     * Add the XML representation of the given schema
+     * to the given branch.
+     *
+     * @param schema
+     *      the schema
+     * @param branch
+     *      the branch
+     */
+    public static void addSchema( Schema schema, Branch branch )
+    {
+        Element element = branch.addElement( SCHEMA_TAG );
+        if ( schema != null )
+        {
+            // Name 
+            String name = schema.getName();
+            if ( ( name != null ) && ( !name.equals( "" ) ) )
+            {
+                element.addAttribute( NAME_TAG, name );
+            }
+
+            // Attribute Types
+            List<AttributeTypeImpl> ats = schema.getAttributeTypes();
+            if ( ( ats != null ) && ( ats.size() >= 1 ) )
+            {
+                Element attributeTypesNode = element.addElement( ATTRIBUTE_TYPES_TAG );
+                for ( AttributeTypeImpl at : ats )
+                {
+                    toXml( at, attributeTypesNode );
+                }
+            }
+
+            // Object Classes
+            List<ObjectClassImpl> ocs = schema.getObjectClasses();
+            if ( ( ocs != null ) && ( ocs.size() >= 1 ) )
+            {
+                Element objectClassesNode = element.addElement( OBJECT_CLASSES_TAG );
+                for ( ObjectClassImpl oc : ocs )
+                {
+                    toXml( oc, objectClassesNode );
+                }
+            }
+
+            // Matching Rules
+            List<MatchingRuleImpl> mrs = schema.getMatchingRules();
+            if ( ( mrs != null ) && ( mrs.size() >= 1 ) )
+            {
+                Element matchingRulesNode = element.addElement( MATCHING_RULES_TAG );
+                for ( MatchingRuleImpl mr : mrs )
+                {
+                    toXml( mr, matchingRulesNode );
+                }
+            }
+
+            // Syntaxes
+            List<SyntaxImpl> syntaxes = schema.getSyntaxes();
+            if ( ( syntaxes != null ) && ( syntaxes.size() >= 1 ) )
+            {
+                Element syntaxesNode = element.addElement( SYNTAXES_TAG );
+                for ( SyntaxImpl syntax : syntaxes )
+                {
+                    toXml( syntax, syntaxesNode );
+                }
+            }
+        }
+    }
+
+
+    /**
+     * Adds the given attribute type to its root Element.
+     *
+     * @param at
+     *      the attribute type
+     * @param root
+     *      the root Element
+     */
+    private static void toXml( AttributeTypeImpl at, Element root )
+    {
+        Element atNode = root.addElement( ATTRIBUTE_TYPE_TAG );
+
+        // OID
+        String oid = at.getOid();
+        if ( ( oid != null ) && ( !oid.equals( "" ) ) )
+        {
+            atNode.addAttribute( OID_TAG, oid );
+        }
+
+        // Aliases
+        String[] aliases = at.getNames();
+        if ( ( aliases != null ) && ( aliases.length >= 1 ) )
+        {
+            Element aliasesNode = atNode.addElement( ALIASES_TAG );
+            for ( String alias : aliases )
+            {
+                aliasesNode.addElement( ALIAS_TAG ).setText( alias );
+            }
+        }
+
+        // Description
+        String description = at.getDescription();
+        if ( ( description != null ) && ( !description.equals( "" ) ) )
+        {
+            atNode.addElement( DESCRIPTION_TAG ).setText( description );
+        }
+
+        // Superior
+        String superior = at.getSuperiorName();
+        if ( ( superior != null ) && ( !superior.equals( "" ) ) )
+        {
+            atNode.addElement( SUPERIOR_TAG ).setText( superior );
+        }
+
+        // Usage
+        UsageEnum usage = at.getUsage();
+        if ( usage != null )
+        {
+            atNode.addElement( USAGE_TAG ).setText( usage.toString() );
+        }
+
+        // Syntax
+        String syntax = at.getSyntaxOid();
+        if ( ( syntax != null ) && ( !syntax.equals( "" ) ) )
+        {
+            atNode.addElement( SYNTAX_TAG ).setText( syntax );
+        }
+
+        // Syntax Length
+        int syntaxLength = at.getLength();
+        if ( syntaxLength > 0 )
+        {
+            atNode.addElement( SYNTAX_LENGTH_TAG ).setText( "" + syntaxLength );
+        }
+
+        // Obsolete
+        if ( at.isObsolete() )
+        {
+            atNode.addAttribute( OBSOLETE_TAG, BOOLEAN_TRUE );
+        }
+        else
+        {
+            atNode.addAttribute( OBSOLETE_TAG, BOOLEAN_FALSE );
+        }
+
+        // Single Value
+        if ( at.isSingleValue() )
+        {
+            atNode.addAttribute( SINGLE_VALUE_TAG, BOOLEAN_TRUE );
+        }
+        else
+        {
+            atNode.addAttribute( SINGLE_VALUE_TAG, BOOLEAN_FALSE );
+        }
+
+        // Collective
+        if ( at.isCollective() )
+        {
+            atNode.addAttribute( COLLECTIVE_TAG, BOOLEAN_TRUE );
+        }
+        else
+        {
+            atNode.addAttribute( COLLECTIVE_TAG, BOOLEAN_FALSE );
+        }
+
+        // No User Modification
+        if ( at.isCanUserModify() )
+        {
+            atNode.addAttribute( NO_USER_MODIFICATION_TAG, BOOLEAN_FALSE );
+        }
+        else
+        {
+            atNode.addAttribute( NO_USER_MODIFICATION_TAG, BOOLEAN_TRUE );
+        }
+
+        // Equality
+        String equality = at.getEqualityName();
+        if ( ( equality != null ) && ( !equality.equals( "" ) ) )
+        {
+            atNode.addElement( EQUALITY_TAG ).setText( equality );
+        }
+
+        // Ordering
+        String ordering = at.getOrderingName();
+        if ( ( ordering != null ) && ( !ordering.equals( "" ) ) )
+        {
+            atNode.addElement( ORDERING_TAG ).setText( ordering );
+        }
+
+        // Substring
+        String substring = at.getSubstrName();
+        if ( ( substring != null ) && ( !substring.equals( "" ) ) )
+        {
+            atNode.addElement( SUBSTRING_TAG ).setText( substring );
+        }
+    }
+
+
+    /**
+     * Adds the given object class to its root Element.
+     *
+     * @param oc
+     *      the object class to convert
+     * @param root
+     *      the root Element
+     */
+    private static void toXml( ObjectClassImpl oc, Element root )
+    {
+        Element ocNode = root.addElement( OBJECT_CLASS_TAG );
+
+        // OID
+        String oid = oc.getOid();
+        if ( ( oid != null ) && ( !oid.equals( "" ) ) )
+        {
+            ocNode.addAttribute( OID_TAG, oid );
+        }
+
+        // Aliases
+        String[] aliases = oc.getNames();
+        if ( ( aliases != null ) && ( aliases.length >= 1 ) )
+        {
+            Element aliasesNode = ocNode.addElement( ALIASES_TAG );
+            for ( String alias : aliases )
+            {
+                aliasesNode.addElement( ALIAS_TAG ).setText( alias );
+            }
+        }
+
+        // Description
+        String description = oc.getDescription();
+        if ( ( description != null ) && ( !description.equals( "" ) ) )
+        {
+            ocNode.addElement( DESCRIPTION_TAG ).setText( description );
+        }
+
+        // Superiors
+        String[] superiors = oc.getSuperClassesNames();
+        if ( ( superiors != null ) && ( superiors.length >= 1 ) )
+        {
+            Element superiorsNode = ocNode.addElement( SUPERIORS_TAG );
+            for ( String superior : superiors )
+            {
+                superiorsNode.addElement( SUPERIOR_TAG ).setText( superior );
+            }
+        }
+
+        // Type
+        ObjectClassTypeEnum type = oc.getType();
+        if ( type != null )
+        {
+            ocNode.addElement( TYPE_TAG ).setText( type.toString() );
+        }
+
+        // Obsolete
+        if ( oc.isObsolete() )
+        {
+            ocNode.addAttribute( OBSOLETE_TAG, BOOLEAN_TRUE );
+        }
+        else
+        {
+            ocNode.addAttribute( OBSOLETE_TAG, BOOLEAN_FALSE );
+        }
+
+        // Mandatory Attribute Types
+        String[] mandatoryATs = oc.getMustNamesList();
+        if ( ( mandatoryATs != null ) && ( mandatoryATs.length >= 1 ) )
+        {
+            Element mandatoryNode = ocNode.addElement( MANDATORY_TAG );
+            for ( String mandatoryAT : mandatoryATs )
+            {
+                mandatoryNode.addElement( ATTRIBUTE_TYPE_TAG ).setText( mandatoryAT );
+            }
+        }
+
+        // Optional Attribute Types
+        String[] optionalATs = oc.getMayNamesList();
+        if ( ( optionalATs != null ) && ( optionalATs.length >= 1 ) )
+        {
+            Element optionalNode = ocNode.addElement( OPTIONAL_TAG );
+            for ( String optionalAT : optionalATs )
+            {
+                optionalNode.addElement( ATTRIBUTE_TYPE_TAG ).setText( optionalAT );
+            }
+        }
+    }
+
+
+    /**
+     * Adds the given matching rule to its root Element.
+     *
+     * @param mr
+     *      the matching rule to convert
+     * @param root
+     *      the root Element
+     */
+    private static void toXml( MatchingRuleImpl mr, Element root )
+    {
+        Element mrNode = root.addElement( MATCHING_RULE_TAG );
+
+        // OID
+        String oid = mr.getOid();
+        if ( ( oid != null ) && ( !oid.equals( "" ) ) )
+        {
+            mrNode.addAttribute( OID_TAG, oid );
+        }
+
+        // Aliases
+        String[] aliases = mr.getNames();
+        if ( ( aliases != null ) && ( aliases.length >= 1 ) )
+        {
+            Element aliasesNode = mrNode.addElement( ALIASES_TAG );
+            for ( String alias : aliases )
+            {
+                aliasesNode.addElement( ALIAS_TAG ).setText( alias );
+            }
+        }
+
+        // Description
+        String description = mr.getDescription();
+        if ( ( description != null ) && ( !description.equals( "" ) ) )
+        {
+            mrNode.addElement( DESCRIPTION_TAG ).setText( description );
+        }
+
+        // Obsolete
+        if ( mr.isObsolete() )
+        {
+            mrNode.addAttribute( OBSOLETE_TAG, BOOLEAN_TRUE );
+        }
+        else
+        {
+            mrNode.addAttribute( OBSOLETE_TAG, BOOLEAN_FALSE );
+        }
+
+        // Syntax OID
+        String syntaxOid = mr.getSyntaxOid();
+        if ( ( syntaxOid != null ) && ( !syntaxOid.equals( "" ) ) )
+        {
+            mrNode.addElement( SYNTAX_OID_TAG ).setText( syntaxOid );
+        }
+    }
+
+
+    /**
+     * Converts the given syntax to its source code representation
+     * in XML file format.
+     *
+     * @param syntax
+     *      the syntax to convert
+     * @param root
+     *      the root Element
+     * @return
+     *      the corresponding source code representation
+     */
+    private static void toXml( SyntaxImpl syntax, Element root )
+    {
+        Element syntaxNode = root.addElement( SYNTAX_TAG );
+
+        // OID
+        String oid = syntax.getOid();
+        if ( ( oid != null ) && ( !oid.equals( "" ) ) )
+        {
+            syntaxNode.addAttribute( OID_TAG, oid );
+        }
+
+        // Aliases
+        String[] aliases = syntax.getNames();
+        if ( ( aliases != null ) && ( aliases.length >= 1 ) )
+        {
+            Element aliasesNode = syntaxNode.addElement( ALIASES_TAG );
+            for ( String alias : aliases )
+            {
+                aliasesNode.addElement( ALIAS_TAG ).setText( alias );
+            }
+        }
+
+        // Description
+        String description = syntax.getDescription();
+        if ( ( description != null ) && ( !description.equals( "" ) ) )
+        {
+            syntaxNode.addElement( DESCRIPTION_TAG ).setText( description );
+        }
+
+        // Obsolete
+        if ( syntax.isObsolete() )
+        {
+            syntaxNode.addAttribute( OBSOLETE_TAG, BOOLEAN_TRUE );
+        }
+        else
+        {
+            syntaxNode.addAttribute( OBSOLETE_TAG, BOOLEAN_FALSE );
+        }
+
+        // Human Readible
+        if ( syntax.isHumanReadable() )
+        {
+            syntaxNode.addAttribute( HUMAN_READABLE_TAG, BOOLEAN_TRUE );
+        }
+        else
+        {
+            syntaxNode.addAttribute( HUMAN_READABLE_TAG, BOOLEAN_FALSE );
+        }
+    }
+
+
+    /**
+     * XML Pretty Printer XSLT Transformation
+     * 
+     * @param document
+     *      the Dom4j Document
+     * @return
+     */
+    private static Document styleDocument( Document document )
+    {
+        // load the transformer using JAXP
+        TransformerFactory factory = TransformerFactory.newInstance();
+        Transformer transformer = null;
+        try
+        {
+            transformer = factory.newTransformer( new StreamSource( Activator.class
+                .getResourceAsStream( "XmlFileFormat.xslt" ) ) );
+        }
+        catch ( TransformerConfigurationException e1 )
+        {
+            // Will never occur
+        }
+
+        // now lets style the given document
+        DocumentSource source = new DocumentSource( document );
+        DocumentResult result = new DocumentResult();
+        try
+        {
+            transformer.transform( source, result );
+        }
+        catch ( TransformerException e )
+        {
+            // Will never occur
+        }
+
+        // return the transformed document
+        Document transformedDoc = result.getDocument();
+        return transformedDoc;
+    }
+}

Propchange: directory/sandbox/felixk/studio-schemaeditor/src/main/java/org/apache/directory/studio/schemaeditor/model/io/XMLSchemaFileExporter.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: directory/sandbox/felixk/studio-schemaeditor/src/main/java/org/apache/directory/studio/schemaeditor/model/io/XMLSchemaFileImportException.java
URL: http://svn.apache.org/viewvc/directory/sandbox/felixk/studio-schemaeditor/src/main/java/org/apache/directory/studio/schemaeditor/model/io/XMLSchemaFileImportException.java?rev=592094&view=auto
==============================================================================
--- directory/sandbox/felixk/studio-schemaeditor/src/main/java/org/apache/directory/studio/schemaeditor/model/io/XMLSchemaFileImportException.java (added)
+++ directory/sandbox/felixk/studio-schemaeditor/src/main/java/org/apache/directory/studio/schemaeditor/model/io/XMLSchemaFileImportException.java Mon Nov  5 09:14:24 2007
@@ -0,0 +1,44 @@
+/*
+ *  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.studio.schemaeditor.model.io;
+
+
+/**
+ * This class represents the XMLSchemaFileImportException.
+ *
+ * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
+ * @version $Rev$, $Date$
+ */
+public class XMLSchemaFileImportException extends Exception
+{
+    private static final long serialVersionUID = 1L;
+
+
+    /**
+     * Creates a new instance of XMLSchemaFileImportException.
+     *
+     * @param message
+     *      the message
+     */
+    public XMLSchemaFileImportException( String message )
+    {
+        super( message );
+    }
+}

Propchange: directory/sandbox/felixk/studio-schemaeditor/src/main/java/org/apache/directory/studio/schemaeditor/model/io/XMLSchemaFileImportException.java
------------------------------------------------------------------------------
    svn:eol-style = native



Mime
View raw message