You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@directory.apache.org by pa...@apache.org on 2007/07/16 17:08:10 UTC

svn commit: r556644 - in /directory/studio/trunk/studio-apacheds-schemaeditor/src/main/java/org/apache/directory/studio/apacheds/schemaeditor/model: ./ io/

Author: pamarcelot
Date: Mon Jul 16 08:08:05 2007
New Revision: 556644

URL: http://svn.apache.org/viewvc?view=rev&rev=556644
Log:
Added Project Importer and Exporter IO classes.

Added:
    directory/studio/trunk/studio-apacheds-schemaeditor/src/main/java/org/apache/directory/studio/apacheds/schemaeditor/model/io/ProjectsExporter.java
    directory/studio/trunk/studio-apacheds-schemaeditor/src/main/java/org/apache/directory/studio/apacheds/schemaeditor/model/io/ProjectsImportException.java
    directory/studio/trunk/studio-apacheds-schemaeditor/src/main/java/org/apache/directory/studio/apacheds/schemaeditor/model/io/ProjectsImporter.java
Modified:
    directory/studio/trunk/studio-apacheds-schemaeditor/src/main/java/org/apache/directory/studio/apacheds/schemaeditor/model/Project.java
    directory/studio/trunk/studio-apacheds-schemaeditor/src/main/java/org/apache/directory/studio/apacheds/schemaeditor/model/io/XMLSchemaFileImporter.java

Modified: directory/studio/trunk/studio-apacheds-schemaeditor/src/main/java/org/apache/directory/studio/apacheds/schemaeditor/model/Project.java
URL: http://svn.apache.org/viewvc/directory/studio/trunk/studio-apacheds-schemaeditor/src/main/java/org/apache/directory/studio/apacheds/schemaeditor/model/Project.java?view=diff&rev=556644&r1=556643&r2=556644
==============================================================================
--- directory/studio/trunk/studio-apacheds-schemaeditor/src/main/java/org/apache/directory/studio/apacheds/schemaeditor/model/Project.java (original)
+++ directory/studio/trunk/studio-apacheds-schemaeditor/src/main/java/org/apache/directory/studio/apacheds/schemaeditor/model/Project.java Mon Jul 16 08:08:05 2007
@@ -119,6 +119,18 @@
 
 
     /**
+     * Sets the type of the project.
+     *
+     * @param type
+     *      the type of the project
+     */
+    public void setType( ProjectType type )
+    {
+        this.type = type;
+    }
+
+
+    /**
      * Gets the name of the project.
      *
      * @return

Added: directory/studio/trunk/studio-apacheds-schemaeditor/src/main/java/org/apache/directory/studio/apacheds/schemaeditor/model/io/ProjectsExporter.java
URL: http://svn.apache.org/viewvc/directory/studio/trunk/studio-apacheds-schemaeditor/src/main/java/org/apache/directory/studio/apacheds/schemaeditor/model/io/ProjectsExporter.java?view=auto&rev=556644
==============================================================================
--- directory/studio/trunk/studio-apacheds-schemaeditor/src/main/java/org/apache/directory/studio/apacheds/schemaeditor/model/io/ProjectsExporter.java (added)
+++ directory/studio/trunk/studio-apacheds-schemaeditor/src/main/java/org/apache/directory/studio/apacheds/schemaeditor/model/io/ProjectsExporter.java Mon Jul 16 08:08:05 2007
@@ -0,0 +1,173 @@
+/*
+ *  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.apacheds.schemaeditor.model.io;
+
+
+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.studio.apacheds.schemaeditor.Activator;
+import org.apache.directory.studio.apacheds.schemaeditor.model.Project;
+import org.apache.directory.studio.apacheds.schemaeditor.model.Project.ProjectType;
+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 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";
+
+
+    /**
+     * Converts the given project to its code representation
+     * in XML file format.
+     *
+     * @param project
+     *      the project to convert
+     * @return
+     *      the corresponding code representation
+     */
+    public static String toSourceCode( Project project )
+    {
+        // Creating the Document
+        Document document = DocumentHelper.createDocument();
+
+        // Adding the project
+        addProject( project, document );
+
+        return styleDocument( document ).asXML();
+    }
+
+
+    /**
+     * Converts the given projects to their code representation
+     * in XML file format.
+     *
+     * @param projects
+     *      the projects to convert
+     * @return
+     *      the corresponding code representation
+     */
+    public static String toSourceCode( 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 styleDocument( document ).asXML();
+    }
+
+
+    /**
+     * 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 ( name != null )
+            {
+                element.addAttribute( TYPE_TAG, type.toString() );
+            }
+        }
+    }
+
+
+    /**
+     * 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;
+    }
+}

Added: directory/studio/trunk/studio-apacheds-schemaeditor/src/main/java/org/apache/directory/studio/apacheds/schemaeditor/model/io/ProjectsImportException.java
URL: http://svn.apache.org/viewvc/directory/studio/trunk/studio-apacheds-schemaeditor/src/main/java/org/apache/directory/studio/apacheds/schemaeditor/model/io/ProjectsImportException.java?view=auto&rev=556644
==============================================================================
--- directory/studio/trunk/studio-apacheds-schemaeditor/src/main/java/org/apache/directory/studio/apacheds/schemaeditor/model/io/ProjectsImportException.java (added)
+++ directory/studio/trunk/studio-apacheds-schemaeditor/src/main/java/org/apache/directory/studio/apacheds/schemaeditor/model/io/ProjectsImportException.java Mon Jul 16 08:08:05 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.apacheds.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 );
+    }
+}

Added: directory/studio/trunk/studio-apacheds-schemaeditor/src/main/java/org/apache/directory/studio/apacheds/schemaeditor/model/io/ProjectsImporter.java
URL: http://svn.apache.org/viewvc/directory/studio/trunk/studio-apacheds-schemaeditor/src/main/java/org/apache/directory/studio/apacheds/schemaeditor/model/io/ProjectsImporter.java?view=auto&rev=556644
==============================================================================
--- directory/studio/trunk/studio-apacheds-schemaeditor/src/main/java/org/apache/directory/studio/apacheds/schemaeditor/model/io/ProjectsImporter.java (added)
+++ directory/studio/trunk/studio-apacheds-schemaeditor/src/main/java/org/apache/directory/studio/apacheds/schemaeditor/model/io/ProjectsImporter.java Mon Jul 16 08:08:05 2007
@@ -0,0 +1,214 @@
+/*
+ *  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.apacheds.schemaeditor.model.io;
+
+
+import java.util.ArrayList;
+import java.util.Iterator;
+import java.util.List;
+
+import org.apache.directory.studio.apacheds.schemaeditor.model.Project;
+import org.apache.directory.studio.apacheds.schemaeditor.model.Project.ProjectType;
+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 TYPE_TAG = "type";
+
+
+    /**
+     * 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 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 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(); )
+        {
+            Project project = new Project();
+            readProject( rootElement, 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 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.getName() );
+        }
+
+        // Type
+        Attribute typeAttribute = element.attribute( TYPE_TAG );
+        if ( ( typeAttribute != null ) && ( !typeAttribute.getValue().equals( "" ) ) )
+        {
+            try
+            {
+                project.setType( ProjectType.valueOf( typeAttribute.getText() ) );
+            }
+            catch ( IllegalArgumentException e )
+            {
+                throw new ProjectsImportException( "The parser was not able to convert the type value of the project." );
+            }
+        }
+    }
+
+    /**
+     * 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." );
+        }
+    }
+}

Modified: directory/studio/trunk/studio-apacheds-schemaeditor/src/main/java/org/apache/directory/studio/apacheds/schemaeditor/model/io/XMLSchemaFileImporter.java
URL: http://svn.apache.org/viewvc/directory/studio/trunk/studio-apacheds-schemaeditor/src/main/java/org/apache/directory/studio/apacheds/schemaeditor/model/io/XMLSchemaFileImporter.java?view=diff&rev=556644&r1=556643&r2=556644
==============================================================================
--- directory/studio/trunk/studio-apacheds-schemaeditor/src/main/java/org/apache/directory/studio/apacheds/schemaeditor/model/io/XMLSchemaFileImporter.java (original)
+++ directory/studio/trunk/studio-apacheds-schemaeditor/src/main/java/org/apache/directory/studio/apacheds/schemaeditor/model/io/XMLSchemaFileImporter.java Mon Jul 16 08:08:05 2007
@@ -159,7 +159,7 @@
             throw new XMLSchemaFileImportException( "The file '" + path + "' does not seem to be a valid Schema file." );
         }
 
-        readSchema( document.getRootElement(), schema, path );
+        readSchema( rootElement, schema, path );
 
         return schema;
     }