avalon-cvs mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ymikul...@apache.org
Subject cvs commit: jakarta-avalon-excalibur/csframework/src/cs/Configuration DefaultConfigurationSerializer.cs
Date Fri, 23 Aug 2002 22:03:59 GMT
ymikulski    2002/08/23 15:03:59

  Added:       csframework/src/cs/Configuration
                        DefaultConfigurationSerializer.cs
  Log:
  no message
  
  Revision  Changes    Path
  1.1                  jakarta-avalon-excalibur/csframework/src/cs/Configuration/DefaultConfigurationSerializer.cs
  
  Index: DefaultConfigurationSerializer.cs
  ===================================================================
  ///
  /// Copyright (C) The Apache Software Foundation. All rights reserved.
  ///
  /// This software is published under the terms of the Apache Software License
  /// version 1.1, a copy of which has been included  with this distribution in
  /// the LICENSE.txt file.
  ///
  using System;
  using System.Collections;
  using System.IO;
  using System.Xml;
  
  namespace Apache.Avalon.Configuration
  {
  	/// <summary>
  	/// A Serializer of <see cref="DefaultConfiguration"/>.
  	/// </summary>
  	public class DefaultConfigurationSerializer
  	{
  
  		/// <summary>
  		/// Makes a serialization of a <see cref="DefaultConfiguration"/> instance.
  		/// </summary>
  		/// <param name="filename">
  		/// The File name where <see cref="DefaultConfiguration"/> instance will be
  		/// serialized to.
  		/// </param>
  		/// <param name="configuration">A <see cref="DefaultConfiguration"/> instance
to serialize</param>
  		public static void Serialize(string filename, DefaultConfiguration configuration)
  		{
  			XmlTextWriter writer = new XmlTextWriter(new StreamWriter(filename));
  
  			//Use indentation for readability.
  			writer.Formatting = Formatting.Indented;
  			writer.Indentation = 4;
  			
  			writer.WriteStartDocument(true);
  
  			Serialize(writer, configuration);				
  
  			writer.WriteEndDocument(); 
  			writer.Close(); 
  		}
  
  		public static void Serialize(XmlWriter writer, DefaultConfiguration configuration)
  		{
  			// serialize the configuration
  			writer.WriteStartElement(configuration.Prefix, configuration.Name, configuration.Namespace);

  			
  			if (configuration.Value != null)
  			{
  				writer.WriteString(configuration.Value.ToString());
  			}
  			
  			// attribute serialization
  			foreach (DictionaryEntry attr in configuration.Attributes) 
  			{
  				writer.WriteAttributeString(attr.Key.ToString(), attr.Value.ToString()); 
  			}  
  
  			// child serialization
  			foreach(IConfiguration child in configuration.Children)
  			{
  				Serialize(writer, (DefaultConfiguration) child);
  			}
  
  			writer.WriteEndElement();
  		}
  
  		/// <summary>
  		/// Makes a deserialization of a <see cref="DefaultConfiguration"/> instance.
  		/// </summary>
  		/// <param name="filename">The name of the file, containing the XML document to
deserialize.</param>
  		/// <returns>A deserialized <see cref="DefaultConfiguration"/>  instance.</returns>
  		public static DefaultConfiguration Deserialize(string fileName)
  		{
  			DefaultConfiguration configuration = null;
  
  			XmlDocument document = new XmlDocument();
  			document.Load(fileName);
  			
  			XmlNode root = document.DocumentElement; 
  
  			if (root != null)
  			{
  				configuration = Deserialize(root);
  			}
  
  			return configuration;
  		}
  
  		/// <summary>
  		/// Makes a deserialization of <see cref="System.Xml.XmlNode"/> instance.
  		/// </summary>
  		/// <param name="node">The node to deserialize.</param>
  		/// <returns>A deserialized <see cref="DefaultConfiguration"/>  instance.</returns>
  		public static DefaultConfiguration Deserialize(XmlNode node)
  		{
  			// node deserialization
  			DefaultConfiguration configuration =
  				 new DefaultConfiguration(node.LocalName, "-", node.NamespaceURI, node.Prefix);
  			
  			configuration.Value = node.Value;
  
  			// attribute deserialization
  			foreach (XmlAttribute attr in node.Attributes)
  			{
  				configuration.Attributes[attr.Name] = attr.Value;  
  			}
  
  			// child deserialization
  			foreach (XmlNode child in node.ChildNodes)
  			{
  				DefaultConfiguration childConfiguration = Deserialize(child);
  				configuration.Children.Add(childConfiguration); 
  			}
  
  			return configuration;
  		}
  	}
  }
  
  
  

--
To unsubscribe, e-mail:   <mailto:avalon-cvs-unsubscribe@jakarta.apache.org>
For additional commands, e-mail: <mailto:avalon-cvs-help@jakarta.apache.org>


Mime
View raw message