001    /*
002     *  Licensed to the Apache Software Foundation (ASF) under one
003     *  or more contributor license agreements.  See the NOTICE file
004     *  distributed with this work for additional information
005     *  regarding copyright ownership.  The ASF licenses this file
006     *  to you under the Apache License, Version 2.0 (the
007     *  "License"); you may not use this file except in compliance
008     *  with the License.  You may obtain a copy of the License at
009     *  
010     *    http://www.apache.org/licenses/LICENSE-2.0
011     *  
012     *  Unless required by applicable law or agreed to in writing,
013     *  software distributed under the License is distributed on an
014     *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015     *  KIND, either express or implied.  See the License for the
016     *  specific language governing permissions and limitations
017     *  under the License. 
018     *  
019     */
020    package org.apache.directory.studio.apacheds.model;
021    
022    
023    import java.io.IOException;
024    import java.io.InputStream;
025    import java.io.OutputStream;
026    import java.util.ArrayList;
027    import java.util.Iterator;
028    import java.util.List;
029    
030    import org.dom4j.Attribute;
031    import org.dom4j.Document;
032    import org.dom4j.DocumentException;
033    import org.dom4j.DocumentHelper;
034    import org.dom4j.Element;
035    import org.dom4j.io.OutputFormat;
036    import org.dom4j.io.SAXReader;
037    import org.dom4j.io.XMLWriter;
038    
039    
040    /**
041     * This class is used to read/write the 'servers.xml' file.
042     *
043     * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
044     * @version $Rev$, $Date$
045     */
046    public class ServersHandlerIO
047    {
048        // XML tags and attributes
049        private static final String SERVERS_TAG = "servers"; //$NON-NLS-1$
050        private static final String SERVER_TAG = "server"; //$NON-NLS-1$
051        private static final String SERVER_ID_ATTRIBUTE = "id"; //$NON-NLS-1$
052        private static final String SERVER_NAME_ATTRIBUTE = "name"; //$NON-NLS-1$
053        private static final String SERVER_VERSION_ATTRIBUTE = "version"; //$NON-NLS-1$
054    
055    
056        /**
057         * Reads the given input stream.
058         *
059         * @param stream
060         *      the input stream
061         * @return
062         *      
063         * @throws ServersHandlerIOException
064         */
065        public static List<Server> read( InputStream stream ) throws ServersHandlerIOException
066        {
067            List<Server> servers = new ArrayList<Server>();
068    
069            SAXReader saxReader = new SAXReader();
070            Document document = null;
071    
072            try
073            {
074                document = saxReader.read( stream );
075            }
076            catch ( DocumentException e )
077            {
078                throw new ServersHandlerIOException( e.getMessage() );
079            }
080    
081            Element rootElement = document.getRootElement();
082            if ( !rootElement.getName().equals( SERVERS_TAG ) )
083            {
084                throw new ServersHandlerIOException( Messages.getString( "ServersHandlerIO.ErrorNotValidServersFile" ) ); //$NON-NLS-1$
085            }
086    
087            for ( Iterator<?> i = rootElement.elementIterator( SERVER_TAG ); i.hasNext(); )
088            {
089                servers.add( readServer( ( Element ) i.next() ) );
090            }
091    
092            return servers;
093        }
094    
095    
096        /**
097         * Reads a server element.
098         *
099         * @param element
100         *      the element
101         * @return
102         *      the corresponding {@link Server}
103         */
104        private static Server readServer( Element element )
105        {
106            Server server = new Server();
107    
108            // ID
109            Attribute idAttribute = element.attribute( SERVER_ID_ATTRIBUTE );
110            if ( idAttribute != null )
111            {
112                server.setId( idAttribute.getValue() );
113            }
114    
115            // Name
116            Attribute nameAttribute = element.attribute( SERVER_NAME_ATTRIBUTE );
117            if ( nameAttribute != null )
118            {
119                server.setName( nameAttribute.getValue() );
120            }
121    
122            // Version
123            Attribute versionAttribute = element.attribute( SERVER_VERSION_ATTRIBUTE );
124            if ( versionAttribute != null )
125            {
126                if ( versionAttribute.getValue().equalsIgnoreCase( "1.5.5" ) ) //$NON-NLS-1$
127                {
128                    server.setVersion( ServerVersion.VERSION_1_5_5 );
129                }
130                else if ( versionAttribute.getValue().equalsIgnoreCase( "1.5.4" ) ) //$NON-NLS-1$
131                {
132                    server.setVersion( ServerVersion.VERSION_1_5_4 );
133                }
134                else if ( versionAttribute.getValue().equalsIgnoreCase( "1.5.3" ) ) //$NON-NLS-1$
135                {
136                    server.setVersion( ServerVersion.VERSION_1_5_3 );
137                }
138                // <!> Compatibility mode <!>
139                // if the server does not have a version attribute, this means it's an
140                // Apache DS 1.5.3 server
141                else
142                {
143                    server.setVersion( ServerVersion.VERSION_1_5_3 );
144                }
145            }
146            // <!> Compatibility mode <!>
147            // if the server does not have a version attribute, this means it's an
148            // Apache DS 1.5.3 server
149            else
150            {
151                server.setVersion( ServerVersion.VERSION_1_5_3 );
152            }
153    
154            return server;
155        }
156    
157    
158        /**
159         * Writes the list of servers to the given stream.
160         *
161         * @param servers
162         *      the servers
163         * @param outputStream
164         *      the output stream
165         * @throws IOException
166         *      if an error occurs when writing to the stream
167         */
168        public static void write( List<Server> servers, OutputStream outputStream ) throws IOException
169        {
170            // Creating the Document
171            Document document = DocumentHelper.createDocument();
172    
173            // Creating the root element
174            Element root = document.addElement( SERVERS_TAG );
175    
176            if ( servers != null )
177            {
178                for ( Server server : servers )
179                {
180                    addServer( server, root );
181                }
182            }
183    
184            // Writing the file to the stream
185            OutputFormat outformat = OutputFormat.createPrettyPrint();
186            outformat.setEncoding( "UTF-8" ); //$NON-NLS-1$
187            XMLWriter writer = new XMLWriter( outputStream, outformat );
188            writer.write( document );
189            writer.flush();
190        }
191    
192    
193        /**
194         * Adds the XML representation of the server to the given parent. 
195         *
196         * @param server
197         *      the server
198         * @param parent
199         *      the parent element
200         */
201        private static void addServer( Server server, Element parent )
202        {
203            // Server element
204            Element serverElement = parent.addElement( SERVER_TAG );
205    
206            // ID
207            serverElement.addAttribute( SERVER_ID_ATTRIBUTE, server.getId() );
208    
209            // Name
210            serverElement.addAttribute( SERVER_NAME_ATTRIBUTE, server.getName() );
211    
212            // Version
213            serverElement.addAttribute( SERVER_VERSION_ATTRIBUTE, server.getVersion().toString() );
214        }
215    }