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.4" ) ) //$NON-NLS-1$
127 {
128 server.setVersion( ServerVersion.VERSION_1_5_4 );
129 }
130 else if ( versionAttribute.getValue().equalsIgnoreCase( "1.5.3" ) ) //$NON-NLS-1$
131 {
132 server.setVersion( ServerVersion.VERSION_1_5_3 );
133 }
134 // <!> Compatibility mode <!>
135 // if the server does not have a version attribute, this means it's an
136 // Apache DS 1.5.3 server
137 else
138 {
139 server.setVersion( ServerVersion.VERSION_1_5_3 );
140 }
141 }
142 // <!> Compatibility mode <!>
143 // if the server does not have a version attribute, this means it's an
144 // Apache DS 1.5.3 server
145 else
146 {
147 server.setVersion( ServerVersion.VERSION_1_5_3 );
148 }
149
150 return server;
151 }
152
153
154 /**
155 * Writes the list of servers to the given stream.
156 *
157 * @param servers
158 * the servers
159 * @param outputStream
160 * the output stream
161 * @throws IOException
162 * if an error occurs when writing to the stream
163 */
164 public static void write( List<Server> servers, OutputStream outputStream ) throws IOException
165 {
166 // Creating the Document
167 Document document = DocumentHelper.createDocument();
168
169 // Creating the root element
170 Element root = document.addElement( SERVERS_TAG );
171
172 if ( servers != null )
173 {
174 for ( Server server : servers )
175 {
176 addServer( server, root );
177 }
178 }
179
180 // Writing the file to the stream
181 OutputFormat outformat = OutputFormat.createPrettyPrint();
182 outformat.setEncoding( "UTF-8" ); //$NON-NLS-1$
183 XMLWriter writer = new XMLWriter( outputStream, outformat );
184 writer.write( document );
185 writer.flush();
186 }
187
188
189 /**
190 * Adds the XML representation of the server to the given parent.
191 *
192 * @param server
193 * the server
194 * @param parent
195 * the parent element
196 */
197 private static void addServer( Server server, Element parent )
198 {
199 // Server element
200 Element serverElement = parent.addElement( SERVER_TAG );
201
202 // ID
203 serverElement.addAttribute( SERVER_ID_ATTRIBUTE, server.getId() );
204
205 // Name
206 serverElement.addAttribute( SERVER_NAME_ATTRIBUTE, server.getName() );
207
208 // Version
209 serverElement.addAttribute( SERVER_VERSION_ATTRIBUTE, server.getVersion().toString() );
210 }
211 }