mirror of https://github.com/winsw/winsw
				
				
				
			Redundant codes removed.
							parent
							
								
									a2740d5eb9
								
							
						
					
					
						commit
						a56f2290e9
					
				| 
						 | 
				
			
			@ -67,7 +67,8 @@ namespace winsw
 | 
			
		|||
 | 
			
		||||
            try
 | 
			
		||||
            {
 | 
			
		||||
                ValidateAndLoadXmlSchema(GetXMLReader());
 | 
			
		||||
                string xmlstring = File.ReadAllText(BasePath + ".xml");
 | 
			
		||||
                dom = ServiceDescriptor.FromXML(xmlstring).dom;
 | 
			
		||||
            }
 | 
			
		||||
            catch (XmlException e)
 | 
			
		||||
            {
 | 
			
		||||
| 
						 | 
				
			
			@ -101,41 +102,6 @@ namespace winsw
 | 
			
		|||
            this.dom = dom;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        public void ValidateAndLoadXmlSchema(XmlReader reader)
 | 
			
		||||
        {
 | 
			
		||||
            XmlReaderSettings settings =  new XmlReaderSettings();
 | 
			
		||||
 | 
			
		||||
            settings.ValidationType = ValidationType.Schema;
 | 
			
		||||
            settings.ValidationEventHandler += new ValidationEventHandler(XmlValidationEventHandler);
 | 
			
		||||
 | 
			
		||||
            Assembly a = Assembly.GetExecutingAssembly();
 | 
			
		||||
 | 
			
		||||
            using (Stream schemaStream = a.GetManifestResourceStream("winsw.XMLSchema.xsd"))
 | 
			
		||||
            {
 | 
			
		||||
                using (XmlReader schemaReader = XmlReader.Create(schemaStream))
 | 
			
		||||
                {
 | 
			
		||||
                    settings.Schemas.Add(null, schemaReader);
 | 
			
		||||
                }
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
            var new_reader = XmlReader.Create(reader, settings);
 | 
			
		||||
 | 
			
		||||
            dom.Load(new_reader);
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        private XmlReader GetXMLReader()
 | 
			
		||||
        {
 | 
			
		||||
            return XmlReader.Create(BasePath + ".xml");
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        private void XmlValidationEventHandler(object sender, ValidationEventArgs e)
 | 
			
		||||
        {
 | 
			
		||||
            if (e.Severity == XmlSeverityType.Error)
 | 
			
		||||
            {
 | 
			
		||||
                throw new XmlException("[Error] XML validation - " + e.Message);
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        // ReSharper disable once InconsistentNaming
 | 
			
		||||
        public static ServiceDescriptor FromXML(string xml)
 | 
			
		||||
        {
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -87,21 +87,21 @@
 | 
			
		|||
        
 | 
			
		||||
        
 | 
			
		||||
        <!-- extensions -->
 | 
			
		||||
        <xs:element name="extensions">
 | 
			
		||||
        <xs:element name="extensions" minOccurs="0" maxOccurs="unbounded">
 | 
			
		||||
          <xs:complexType>
 | 
			
		||||
            <xs:choice maxOccurs="unbounded">
 | 
			
		||||
              
 | 
			
		||||
              <!-- extension -->
 | 
			
		||||
              <xs:element name="extension">
 | 
			
		||||
              <xs:element name="extension" minOccurs="0" maxOccurs="unbounded">
 | 
			
		||||
                <xs:complexType>
 | 
			
		||||
                  <xs:choice maxOccurs="unbounded">
 | 
			
		||||
                    <xs:element type="xs:string" name="pidfile"/>
 | 
			
		||||
                    <xs:element type="xs:short" name="stopTimeout"/>
 | 
			
		||||
                    <xs:element type="xs:string" name="stopParentFirst"/>
 | 
			
		||||
                    <xs:element type="xs:string" name="checkWinSWEnvironmentVariable"/>
 | 
			
		||||
                    <xs:element type="xs:string" name="pidfile" minOccurs="0" maxOccurs="unbounded"/>
 | 
			
		||||
                    <xs:element type="xs:short" name="stopTimeout" minOccurs="0" maxOccurs="unbounded"/>
 | 
			
		||||
                    <xs:element type="xs:string" name="stopParentFirst" minOccurs="0" maxOccurs="unbounded"/>
 | 
			
		||||
                    <xs:element type="xs:string" name="checkWinSWEnvironmentVariable" minOccurs="0" maxOccurs="unbounded"/>
 | 
			
		||||
 | 
			
		||||
                    <!-- mapping -->
 | 
			
		||||
                    <xs:element name="mapping">
 | 
			
		||||
                    <xs:element name="mapping" minOccurs="0" maxOccurs="unbounded">
 | 
			
		||||
                      <xs:complexType>
 | 
			
		||||
                        <xs:choice maxOccurs="unbounded">
 | 
			
		||||
                          <xs:element name="map" maxOccurs="unbounded" minOccurs="0">
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -446,22 +446,17 @@ $@"<service>
 | 
			
		|||
        }
 | 
			
		||||
 | 
			
		||||
        [Test]
 | 
			
		||||
        public void ValidateAndLoadXmlSchemaTest()
 | 
			
		||||
        public void FromXMLTest()
 | 
			
		||||
        {
 | 
			
		||||
            const string seedXml = @"<id>
 | 
			
		||||
                                        <id>myapp</id>
 | 
			
		||||
                                        <name>appname</name>
 | 
			
		||||
                                        <description>app description</description>
 | 
			
		||||
                                        <executable>jenkins</executable>
 | 
			
		||||
                                   </id>";
 | 
			
		||||
  <id>myapp</id>
 | 
			
		||||
  <name>MyApp Service (powered by WinSW)</name>
 | 
			
		||||
  <description>This service is a service created from a minimal configuration</description>
 | 
			
		||||
  <executable>%BASE%\myExecutable.exe</executable>
 | 
			
		||||
</id>
 | 
			
		||||
";
 | 
			
		||||
 | 
			
		||||
            XmlDocument dom = new XmlDocument();
 | 
			
		||||
            dom.LoadXml(seedXml);
 | 
			
		||||
 | 
			
		||||
            var serviceDescriptor = new ServiceDescriptor(dom);
 | 
			
		||||
 | 
			
		||||
            var reader = XmlReader.Create(new StringReader(seedXml));
 | 
			
		||||
            Assert.That(() => serviceDescriptor.ValidateAndLoadXmlSchema(reader), Throws.Exception.TypeOf<XmlException>());
 | 
			
		||||
            Assert.That(() => ServiceDescriptor.FromXML(seedXml), Throws.Exception.TypeOf<XmlException>());
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
		Reference in New Issue