~grubng-dev/grubng/tools-urlsdb

3 by thindil
added new version of config file
1
// Config.cs
2
// 
67 by thindil
added configuration for upload server
3
// Copyright (C) 2009,2010,2011 Bartek Jasicki
3 by thindil
added new version of config file
4
//
5
// This file is part of Grubng.
6
//
7
// Grubng is free software: you can redistribute it and/or modify
8
// it under the terms of the GNU General Public License as published by
9
// the Free Software Foundation, either version 3 of the License, or
10
// (at your option) any later version.
11
//
12
// This program is distributed in the hope that it will be useful,
13
// but WITHOUT ANY WARRANTY; without even the implied warranty of
14
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
// GNU General Public License for more details.
16
//
17
// You should have received a copy of the GNU General Public License
18
// along with this program.  If not, see <http://www.gnu.org/licenses/>.
19
//
20
21
/**@file Config.cs
22
 * Provide functions for config file.*/
23
24
using System;
25
using System.Xml;
26
27
namespace urlsdb
28
{
29
	
30
	/// <summary>
31
	/// Provide functions for config file.
32
	/// </summary>
33
	internal static class Config
34
	{
35
		
36
		/// <summary>
37
		/// Function create grub.xml file and write default configuration
38
		/// </summary>
39
		public static void CreateConfig()
40
		{
45 by thindil
some code optimization
41
			using (XmlTextWriter xmlw = new XmlTextWriter("grub.xml", null))
42
			{
43
				xmlw.Formatting = Formatting.Indented;
44
				xmlw.WriteStartDocument(false);
45
				xmlw.WriteStartElement("configuration");
46
				xmlw.WriteElementString("enablesolr", "Y");
47
				xmlw.WriteElementString("solraddress", "http://soap.grub.org:8180/solr/");
48
				xmlw.WriteElementString("solrusername", "user");
49
				xmlw.WriteElementString("solrpassword", "password");
67 by thindil
added configuration for upload server
50
				xmlw.WriteElementString("uploadaddress", "http://grub.silc.org.ua:8080");
68 by thindil
added automatic disabling upload server during maintenance work
51
				xmlw.WriteElementString("uploadusername", "user");
52
				xmlw.WriteElementString("uploadpassword", "password");
45 by thindil
some code optimization
53
				xmlw.WriteElementString("mysqlhost", "localhost");
54
				xmlw.WriteElementString("mysqldb", "grub");
55
				xmlw.WriteElementString("mysqluser", "user");
56
				xmlw.WriteElementString("mysqlpassword", "password");
57
				xmlw.WriteElementString("workunitsdirectory", "/home/thindil/workunits/wu/");
58
				xmlw.WriteElementString("workunitspassword", "password");
59
				xmlw.WriteElementString("useragent", "GrubNG 0.1 (http://grub.org)");
60
				xmlw.WriteElementString("urlsamount", "250");
61
				xmlw.WriteElementString("httpversion", "1.0");
62
				xmlw.WriteElementString("accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
63
				xmlw.WriteEndElement();
64
				xmlw.Flush();
65
				xmlw.Close();	
66
			}
3 by thindil
added new version of config file
67
		}
68
		
69
		/// <summary>
70
		/// Function read data from grub.xml file.
71
		/// </summary>
72
		/// <param name="node">
73
		/// A <see cref="System.String"/> XML node which been read
74
		/// </param>
75
		/// <returns>
76
		/// A <see cref="System.String"/> value of selected XML node
77
		/// </returns>
78
		public static string ReadConfig(string node)
79
		{
80
			XmlDocument doc = new XmlDocument();
81
			try
82
			{
83
				doc.Load("grub.xml");
84
			}
85
			catch (XmlException)
86
			{
87
				return "Invalid XML";
88
			}
89
			string answer = null;
90
			try
91
			{
92
				XmlNode noder = doc.SelectSingleNode(node);
93
				answer = noder.InnerText;
94
				doc = null;
95
			}
96
			catch (System.NullReferenceException)
97
			{
98
				answer = String.Empty;
99
			}
100
			return answer;
101
		}
102
		
103
		/// <summary>
104
		/// Function insert new data to grub.xml file.
105
		/// </summary>
106
		/// <param name="data">
107
		/// A <see cref="System.String"/> value which been inserted to file
108
		/// </param>
109
		/// <param name="name">
110
		/// A <see cref="System.String"/> name of XML Element which been inserted to file
111
		/// </param>
112
		static void InsertConfig(string data, string name)
113
		{
114
			XmlDocument doc = new XmlDocument();
115
			doc.Load("grub.xml");
116
			XmlNode nodew = doc.SelectSingleNode("/configuration");
117
			XmlNode childNode = doc.CreateNode(XmlNodeType.Element, name, String.Empty);
118
			childNode.InnerText = data;
119
			childNode = nodew.AppendChild(childNode);
120
			doc.Save("grub.xml");
121
			doc = null;
122
		}
123
		
124
		/// <summary>
125
		/// Function update config file.
126
		/// </summary>
127
		public static void UpdateConfig()
128
		{
129
			XmlDocument doc = new XmlDocument();
130
			doc.Load("grub.xml");
67 by thindil
added configuration for upload server
131
			string[] nodes = new string[17] {"enablesolr", "solraddress", "solrusername", 
68 by thindil
added automatic disabling upload server during maintenance work
132
				"solrpassword", "uploadaddress", "uploadusername", "uploadpassword", "mysqlhost", 
67 by thindil
added configuration for upload server
133
				"mysqldb", "mysqluser", "mysqlpassword", "workunitsdirectory", "workunitspassword", 
134
				"useragent", "urlsamount", "httpversion", "accept"};
135
			string[] values = new string[17] {"Y", "http://soap.grub.org:8180/solr/", 
136
				"user", "password", "http://grub.silc.org.ua:8080", "user", "password", "localhost", 
137
				"grub", "user", "password",	"/home/thindil/workunits/wu/", "password", 
138
				"GrubNG 0.1 (http://grub.org)", "250", "1.0", 
139
				"text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"};
3 by thindil
added new version of config file
140
			XmlNode noder;
141
			foreach (string node in nodes)
142
			{
143
				noder = doc.SelectSingleNode("/configuration/" + node);
144
				if (noder == null)
145
				{
146
					int index = Array.IndexOf(nodes, node);
147
					Config.InsertConfig(values[index], node);
148
				}
149
			}
150
			doc = null;
151
		}
152
	}
153
}