configurationutility.cs :  » Development » ScintillaNET » Scintilla » Configuration » C# / CSharp Open Source

Home
C# / CSharp Open Source
1.2.6.4 mono .net core
2.2.6.4 mono core
3.Aspect Oriented Frameworks
4.Bloggers
5.Build Systems
6.Business Application
7.Charting Reporting Tools
8.Chat Servers
9.Code Coverage Tools
10.Content Management Systems CMS
11.CRM ERP
12.Database
13.Development
14.Email
15.Forum
16.Game
17.GIS
18.GUI
19.IDEs
20.Installers Generators
21.Inversion of Control Dependency Injection
22.Issue Tracking
23.Logging Tools
24.Message
25.Mobile
26.Network Clients
27.Network Servers
28.Office
29.PDF
30.Persistence Frameworks
31.Portals
32.Profilers
33.Project Management
34.RSS RDF
35.Rule Engines
36.Script
37.Search Engines
38.Sound Audio
39.Source Control
40.SQL Clients
41.Template Engines
42.Testing
43.UML
44.Web Frameworks
45.Web Service
46.Web Testing
47.Wiki Engines
48.Windows Presentation Foundation
49.Workflows
50.XML Parsers
C# / C Sharp
C# / C Sharp by API
C# / CSharp Tutorial
C# / CSharp Open Source » Development » ScintillaNET 
ScintillaNET » Scintilla » Configuration » configurationutility.cs
using System;
using System.IO;
using System.Reflection;
using System.Windows.Forms;
using System.Xml.Serialization;

namespace Scintilla.Configuration{
    public class ConfigurationUtility
    {
        protected Assembly _assembly;

        protected virtual byte[] LoadFile(string filename, ConfigFile parent)
        {
      System.IO.Stream res;
      byte[] buf;
      // check for local file.
      res = OpenFile( filename, parent );
      if( res != null)
      {
        buf = new byte[res.Length];
        res.Read( buf , 0 , buf.Length );
        return buf;
      }
      return null;
    }

        protected virtual Stream OpenFile(string filename, ConfigFile parent)
        {
      System.IO.Stream res;
      // check for local file.
      if( System.IO.File.Exists( filename ) )
      {  
        String fn = filename.Replace( "$$USERDIR$$" , Application.UserAppDataPath );
        res = new System.IO.FileStream( filename , System.IO.FileMode.Open );
      }
      else
      {
        res = _assembly.GetManifestResourceStream(String.Format( "{0}.{1}" , _assembly.GetName().Name, filename.Replace("\\" , "." ) ));
      }
      if( res==null && parent !=null && parent.filename != null )
      {
        // try by prepending the path from the parent to the path.
        int p = parent.filename.LastIndexOf('\\');
        if( p > 0 )
          return OpenFile( String.Format( "{0}\\{1}" , parent.filename.Substring(0 , p) , filename ) , null );
      }

      return res;
    }

        protected object Deserialize(TextReader reader, Type aType)
        {
            XmlSerializer xmlSerializer = null;
            object local = null;
            xmlSerializer = new XmlSerializer(aType);
            local = xmlSerializer.Deserialize(reader);
            reader.Close();
            return local;
        }

        public ConfigurationUtility(Assembly assembly)
        {
            _assembly = assembly;
        }

        public virtual object LoadConfiguration(ConfigFile parent)
        {
            return LoadConfiguration(typeof(Scintilla), "ScintillaNET.xml", parent);
        }

        public virtual object LoadConfiguration(string filename, ConfigFile parent)
        {
            return LoadConfiguration(typeof(Scintilla), filename, parent);
        }

        public virtual object LoadConfiguration(Type configType, ConfigFile parent)
        {
            return LoadConfiguration(configType, "ScintillaNET.xml", parent);
        }

        public virtual object LoadConfiguration(Type configType, string filename, ConfigFile parent)
        {
            ConfigFile configFile = null;
            Stream stream = null;
            TextReader textReader = null;
            configFile = null;
            if (typeof(ConfigFile).IsAssignableFrom(configType))
            {
                stream = OpenFile(filename, parent);
                if (stream != null)
                {
                    textReader = new StreamReader(stream);
                    configFile = Deserialize(textReader, configType) as ConfigFile;
                    configFile.filename = filename;
                    configFile.init(this, parent);
                }
            }
            return configFile;
        }

        public virtual object LoadConfiguration()
        {
            return LoadConfiguration(typeof(Scintilla), "ScintillaNET.xml", null);
        }

        public virtual object LoadConfiguration(string filename)
        {
            return LoadConfiguration(typeof(Scintilla), filename, null);
        }

        public virtual object LoadConfiguration(Type configType)
        {
            return LoadConfiguration( configType , "ScintillaNET.xml", null );
        }

        public virtual object LoadConfiguration(Type configType, string filename)
        {
            return LoadConfiguration( configType , filename, null );
        }
    }

}
www.java2v.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.