/*********************************************************************/ /* */ /* AgentService Framework */ /* www.agentservice.it */ /* */ /* Copyright (C) 2007 l.i.d.o. - DIST, University of Genoa, Italy */ /* GNU Lesser General Public License */ /* */ /* This library is free software; you can redistribute it and/or */ /* modify it under the terms of the GNU Lesser General Public */ /* License as published by the Free Software Foundation, */ /* version 2.1 of the License. */ /* */ /* This library is distributed in the hope that it will be useful, */ /* but WITHOUT ANY WARRANTY; without even the implied warranty of */ /* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU */ /* Lesser General Public License for more details. */ /* */ /* You should have received a copy of the GNU Lesser General Public */ /* License along with this library; if not, write to the */ /* Free Software Foundation, Inc., 59 Temple Place - Suite 330, */ /* Boston, MA 02111-1307, USA. */ /* */ /*********************************************************************/ using System; using System.Xml; using System.Xml.Serialization; namespace AgentService.Install.XmlDataModel { /// /// Class XmlPlatformInstallationInfo. Manages all the data /// that are used to install and then configure the /// AgentService platform. This class is an XML wrapper /// used to read and write AgentService XML installation /// files. /// // [XmlRoot(ElementName="installation")] [Serializable] public class XmlPlatformInstallationInfo : IXmlSerializable, ICloneable { /// /// AgentService installation root /// directory. /// protected string baseDir; /// /// Gets, sets the installation root /// directory of the AgentService /// platform. /// public string BaseDir { get { return this.baseDir; } set { this.baseDir = value; } } /// /// path to the repository file /// used by the installation program. /// protected string repository; /// /// Gets, sets the path to the /// repository file used by the /// installer of the platform. /// public string Repository { get { return this.repository; } set { this.repository = value; } } /// /// wraps the data defining the description /// of the platform. /// protected XmlPlatformDescriptionInfo description; /// /// Gets, sets the description of the platform. /// // [XmlElement(ElementName="description")] public XmlPlatformDescriptionInfo Description { get { return this.description; } set { this.description = value; } } /// /// system information. /// protected XmlSystemInfo systemInfo; /// /// Gets sets the system information /// data for the agent service platform. /// // [XmlElement(ElementName="system")] public XmlSystemInfo SystemInfo { get { return this.systemInfo; } set { this.systemInfo = value; } } /// /// core modules information /// // [XmlElement(ElementName="core")] protected XmlCoreInfo core; /// /// Gets, sets the core module /// information selected for /// the platform installation. /// public XmlCoreInfo Core { get { return this.core; } set { this.core = value; } } /// /// collection of additional modules information /// selected for the installation. /// protected XmlModuleInfoCollection additional; /// /// Gets, sets the collection of additional /// modules information selected for the /// installation. /// // [XmlElement(ElementName="additional")] public XmlModuleInfoCollection Additional { get { return this.additional; } set { this.additional = value; } } /// /// collection of key-value pair stored in the /// configuration file. /// protected XmlPropertyInfoCollection properties; /// /// Gets, sets the collection of key-value pair /// stored in the configuration files. /// public XmlPropertyInfoCollection Properties { get { return this.properties; } set { this.properties = value; } } /// /// Dummy default constructor used by /// the XML deserializer to create empty /// instances and the sets their properties /// reading them from the XML stream. /// public XmlPlatformInstallationInfo() { this.additional = new XmlModuleInfoCollection(); this.properties = new XmlPropertyInfoCollection(); this.description = new XmlPlatformDescriptionInfo(); this.core = new XmlCoreInfo(); this.systemInfo = new XmlSystemInfo(); this.repository = string.Empty; this.baseDir = string.Empty; } /// /// Constructs an instance of the XmlPlatformInstallationInfo with /// the given parameters. /// /// path to the root installation path /// name of the repository file /// description of the platform /// system information /// core module installation information /// additional modules installation information public XmlPlatformInstallationInfo(string baseDir, string repository, XmlPlatformDescriptionInfo description, XmlSystemInfo systemInfo, XmlCoreInfo core, XmlModuleInfoCollection additional) { this.additional = additional; this.description = description; this.core = core; this.systemInfo = systemInfo; this.repository = repository; this.baseDir = baseDir; this.properties = new XmlPropertyInfoCollection(); } #region IXmlSerializable Members public void WriteXml(System.Xml.XmlWriter writer) { XmlSerializationHelper.WriteTag("base-dir", "path", this.baseDir, writer); XmlSerializationHelper.WriteTag("repository", "file", this.repository, writer); XmlSerializer serializer = new XmlSerializer(typeof(XmlPlatformDescriptionInfo)); serializer.Serialize(writer,this.description); // backward compatibility: if the systemInfo // field is null avoids the serialization and // generate an old version xml installation // file. if (this.systemInfo != null) { serializer = new XmlSerializer(typeof(XmlSystemInfo)); serializer.Serialize(writer,this.systemInfo); } serializer = new XmlSerializer(typeof(XmlCoreInfo)); serializer.Serialize(writer,this.core); writer.WriteStartElement("additional"); this.additional.WriteXml(writer); writer.WriteStartElement("properties"); this.properties.WriteXml(writer); writer.WriteEndElement(); } public System.Xml.Schema.XmlSchema GetSchema() { // TODO: Add XmlPlatformInstallation.GetSchema implementation return null; } public void ReadXml(System.Xml.XmlReader reader) { reader.ReadStartElement("installation"); // reader.MoveToContent(); this.baseDir = XmlSerializationHelper.ReadTag("base-dir", "path", reader); this.repository = XmlSerializationHelper.ReadTag("repository", "file", reader); XmlSerializer serializer = new XmlSerializer(this.description.GetType()); object descXML = serializer.Deserialize(reader); this.description = descXML as XmlPlatformDescriptionInfo; // ok, just for backward compatibility // check for the presence of the // and eventually move next to the // tag. if (reader.LocalName == "platform") { XmlSerializer serializer2 = new XmlSerializer(typeof(XmlSystemInfo)); this.systemInfo = serializer2.Deserialize(reader) as XmlSystemInfo; } serializer = new XmlSerializer(typeof(XmlCoreInfo)); this.core = serializer.Deserialize(reader) as XmlCoreInfo; if (this.additional == null) { this.additional = new XmlModuleInfoCollection(); } this.additional.ReadXml(reader); if (this.properties == null) { this.properties = new XmlPropertyInfoCollection(); } this.properties.ReadXml(reader); reader.ReadEndElement(); } #endregion #region Read Write Operations /// /// Reads the XmlPlatformInstallationInfo instance from the given stream. /// /// source stream /// read instance public static XmlPlatformInstallationInfo FromStream(System.IO.Stream stream) { XmlTextReader reader = new XmlTextReader(stream); reader.WhitespaceHandling = WhitespaceHandling.Significant; XmlPlatformInstallationInfo info = new XmlPlatformInstallationInfo(); info.ReadXml(reader); reader.Close(); stream.Close(); return info; } /// /// Reads the XmlPlatformInstallationInfo instance from the given file. /// /// path to the file containing the XML document /// read instance public static XmlPlatformInstallationInfo FromFile(string path) { System.IO.FileStream stream = new System.IO.FileStream(path,System.IO.FileMode.Open); return XmlPlatformInstallationInfo.FromStream(stream); } /// /// Writes the XML representation of an XmlPlatformInstallationInfo /// to the given stream. /// /// instance to serialize /// destination stream public static void ToStream(XmlPlatformInstallationInfo installation, System.IO.Stream stream) { XmlTextWriter writer = new XmlTextWriter(stream, System.Text.Encoding.UTF8); writer.Formatting = Formatting.Indented; writer.Indentation = 4; writer.WriteStartDocument(); writer.WriteStartElement("installation"); installation.WriteXml(writer); writer.WriteEndDocument(); writer.Close(); } /// /// Writes the XML representation of the given XmlPlatformInstallationInfo /// to the file. If the file already exists it will be overwritten. /// /// instance of the XmlPlatformInstallationInfo class /// path to the destination file. public static void ToFile(XmlPlatformInstallationInfo installation, string path) { System.IO.FileStream stream = null; try { stream = new System.IO.FileStream(path, System.IO.FileMode.Create); XmlPlatformInstallationInfo.ToStream(installation,stream); } finally { if (stream != null) { stream.Close(); } } } #endregion #region ICloneable Members /// /// Clones this instance of the XmlPlatformInstallationInfo. /// /// identical copy of the instance public object Clone() { return new XmlPlatformInstallationInfo(this.baseDir.Clone() as string, this.repository.Clone() as string, this.description.Clone() as XmlPlatformDescriptionInfo, this.systemInfo.Clone() as XmlSystemInfo, this.core.Clone() as XmlCoreInfo, this.additional.Clone() as XmlModuleInfoCollection); } #endregion } }