Example #1
0
 /// <summary>
 /// Write the XML Configuration Header.
 /// </summary>
 /// <param name="writer">XML Writer</param>
 /// <param name="header">Cofniguration Header</param>
 private void WriteHeader(XmlWriter writer, ConfigurationHeader header)
 {
     writer.WriteStartElement(ConstXmlConfigHeader.XML_CONFIG_NODE_HEADER);
     {
         writer.WriteAttributeString(ConstXmlConfigHeader.XML_CONFIG_HEADER_ATTR_ID, header.Id);
         writer.WriteAttributeString(ConstXmlConfigHeader.XML_CONFIG_HEADER_ATTR_GROUP, header.ApplicationGroup);
         writer.WriteAttributeString(ConstXmlConfigHeader.XML_CONFIG_HEADER_ATTR_APP, header.Application);
         writer.WriteAttributeString(ConstXmlConfigHeader.XML_CONFIG_HEADER_ATTR_NAME, header.Name);
         writer.WriteAttributeString(ConstXmlConfigHeader.XML_CONFIG_HEADER_ATTR_VERSION, header.Version.ToString());
         if (!String.IsNullOrWhiteSpace(header.Description))
         {
             writer.WriteStartElement(ConstXmlConfigHeader.XML_CONFIG_HEADER_DESC);
             writer.WriteString(header.Description);
             writer.WriteEndElement();
         }
         if (header.CreatedBy != null)
         {
             WriteModifiedBy(writer, ConstXmlConfigHeader.XML_CONFIG_HEADER_CREATED_BY, header.CreatedBy);
         }
         if (header.ModifiedBy != null)
         {
             WriteModifiedBy(writer, ConstXmlConfigHeader.XML_CONFIG_HEADER_UPDATED_BY, header.ModifiedBy);
         }
     }
     writer.WriteEndElement();
 }
Example #2
0
 /// <summary>
 /// Parse the configuration header information.
 /// </summary>
 /// <param name="name">Configuration name</param>
 /// <param name="version">Expected configuration version</param>
 /// <param name="elem">Header XML element</param>
 private void ParseHeader(string name, Version version, XmlElement elem, string passwrod)
 {
     if (elem.HasAttributes)
     {
         ConfigurationHeader header = new ConfigurationHeader();
         string attr = elem.GetAttribute(ConstXmlConfigHeader.XML_CONFIG_HEADER_ATTR_GROUP);
         if (!String.IsNullOrWhiteSpace(attr))
         {
             header.ApplicationGroup = attr;
         }
         attr = elem.GetAttribute(ConstXmlConfigHeader.XML_CONFIG_HEADER_ATTR_ID);
         if (!String.IsNullOrWhiteSpace(attr))
         {
             header.Id = attr;
         }
         attr = elem.GetAttribute(ConstXmlConfigHeader.XML_CONFIG_HEADER_ATTR_APP);
         if (!String.IsNullOrWhiteSpace(attr))
         {
             header.Application = attr;
         }
         attr = elem.GetAttribute(ConstXmlConfigHeader.XML_CONFIG_HEADER_ATTR_NAME);
         if (!String.IsNullOrWhiteSpace(attr))
         {
             header.Name = attr;
         }
         attr = elem.GetAttribute(ConstXmlConfigHeader.XML_CONFIG_HEADER_ATTR_VERSION);
         if (!String.IsNullOrWhiteSpace(attr))
         {
             Version ver = Version.Parse(attr);
             if (!ver.Equals(version))
             {
                 throw new ConfigurationException(String.Format("Configuration Version mis-match: [expected={0}][actual={1}]", version.ToString(), ver.ToString()));
             }
             header.Version = ver;
         }
         if (name != header.Name)
         {
             throw new ConfigurationException(String.Format("Invalid Configuration: Name mis-match. [expected={0}][actual={1}]", name, header.Name));
         }
         attr = elem.GetAttribute(ConstXmlConfigHeader.XML_CONFIG_HEADER_ATTR_HASH);
         if (!String.IsNullOrWhiteSpace(attr))
         {
             header.EncryptionHash = attr;
             if (String.IsNullOrWhiteSpace(passwrod))
             {
                 throw new ConfigurationException(String.Format("Configuration has encryption, but no password specified. [name={0}]", header.Name));
             }
             if (!CryptoUtils.CompareHash(header.EncryptionHash, passwrod))
             {
                 throw new ConfigurationException(String.Format("Password doesn't match the hash specified in the configuration. [name={0}]", header.Name));
             }
         }
         if (elem.HasChildNodes)
         {
             foreach (XmlNode node in elem.ChildNodes)
             {
                 if (node.NodeType == XmlNodeType.Element)
                 {
                     if (node.Name == ConstXmlConfigHeader.XML_CONFIG_HEADER_CREATED_BY)
                     {
                         ModifiedBy mb = ParseModifiedBy((XmlElement)node);
                         if (mb != null)
                         {
                             header.CreatedBy = mb;
                         }
                     }
                     else if (node.Name == ConstXmlConfigHeader.XML_CONFIG_HEADER_UPDATED_BY)
                     {
                         ModifiedBy mb = ParseModifiedBy((XmlElement)node);
                         if (mb != null)
                         {
                             header.ModifiedBy = mb;
                         }
                     }
                     else if (IsTextNode((XmlElement)node))
                     {
                         if (node.Name == ConstXmlConfigHeader.XML_CONFIG_HEADER_DESC)
                         {
                             header.Description = node.InnerText;
                         }
                     }
                 }
             }
         }
         header.Timestamp = DateTime.Now.Ticks;
         LogUtils.Debug(String.Format("Loaded Header: [name={0}]", header.Name), header);
         configuration.Header = header;
     }
     else
     {
         throw new ConfigurationException(String.Format("Error loading configuration header: No attributes defined. [name={0}]", name));
     }
 }