~grubng-dev/grubng/tools-urlsdb

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
// Config.cs
// 
// Copyright (C) 2009,2010,2011 Bartek Jasicki
//
// This file is part of Grubng.
//
// Grubng is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program 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 General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program.  If not, see <http://www.gnu.org/licenses/>.
//

/**@file Config.cs
 * Provide functions for config file.*/

using System;
using System.Xml;

namespace urlsdb
{
	
	/// <summary>
	/// Provide functions for config file.
	/// </summary>
	internal static class Config
	{
		
		/// <summary>
		/// Function create grub.xml file and write default configuration
		/// </summary>
		public static void CreateConfig()
		{
			using (XmlTextWriter xmlw = new XmlTextWriter("grub.xml", null))
			{
				xmlw.Formatting = Formatting.Indented;
				xmlw.WriteStartDocument(false);
				xmlw.WriteStartElement("configuration");
				xmlw.WriteElementString("enablesolr", "Y");
				xmlw.WriteElementString("solraddress", "http://soap.grub.org:8180/solr/");
				xmlw.WriteElementString("solrusername", "user");
				xmlw.WriteElementString("solrpassword", "password");
				xmlw.WriteElementString("uploadaddress", "http://grub.silc.org.ua:8080");
				xmlw.WriteElementString("uploadusername", "user");
				xmlw.WriteElementString("uploadpassword", "password");
				xmlw.WriteElementString("mysqlhost", "localhost");
				xmlw.WriteElementString("mysqldb", "grub");
				xmlw.WriteElementString("mysqluser", "user");
				xmlw.WriteElementString("mysqlpassword", "password");
				xmlw.WriteElementString("workunitsdirectory", "/home/thindil/workunits/wu/");
				xmlw.WriteElementString("workunitspassword", "password");
				xmlw.WriteElementString("useragent", "GrubNG 0.1 (http://grub.org)");
				xmlw.WriteElementString("urlsamount", "250");
				xmlw.WriteElementString("httpversion", "1.0");
				xmlw.WriteElementString("accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
				xmlw.WriteEndElement();
				xmlw.Flush();
				xmlw.Close();	
			}
		}
		
		/// <summary>
		/// Function read data from grub.xml file.
		/// </summary>
		/// <param name="node">
		/// A <see cref="System.String"/> XML node which been read
		/// </param>
		/// <returns>
		/// A <see cref="System.String"/> value of selected XML node
		/// </returns>
		public static string ReadConfig(string node)
		{
			XmlDocument doc = new XmlDocument();
			try
			{
				doc.Load("grub.xml");
			}
			catch (XmlException)
			{
				return "Invalid XML";
			}
			string answer = null;
			try
			{
				XmlNode noder = doc.SelectSingleNode(node);
				answer = noder.InnerText;
				doc = null;
			}
			catch (System.NullReferenceException)
			{
				answer = String.Empty;
			}
			return answer;
		}
		
		/// <summary>
		/// Function insert new data to grub.xml file.
		/// </summary>
		/// <param name="data">
		/// A <see cref="System.String"/> value which been inserted to file
		/// </param>
		/// <param name="name">
		/// A <see cref="System.String"/> name of XML Element which been inserted to file
		/// </param>
		static void InsertConfig(string data, string name)
		{
			XmlDocument doc = new XmlDocument();
			doc.Load("grub.xml");
			XmlNode nodew = doc.SelectSingleNode("/configuration");
			XmlNode childNode = doc.CreateNode(XmlNodeType.Element, name, String.Empty);
			childNode.InnerText = data;
			childNode = nodew.AppendChild(childNode);
			doc.Save("grub.xml");
			doc = null;
		}
		
		/// <summary>
		/// Function update config file.
		/// </summary>
		public static void UpdateConfig()
		{
			XmlDocument doc = new XmlDocument();
			doc.Load("grub.xml");
			string[] nodes = new string[17] {"enablesolr", "solraddress", "solrusername", 
				"solrpassword", "uploadaddress", "uploadusername", "uploadpassword", "mysqlhost", 
				"mysqldb", "mysqluser", "mysqlpassword", "workunitsdirectory", "workunitspassword", 
				"useragent", "urlsamount", "httpversion", "accept"};
			string[] values = new string[17] {"Y", "http://soap.grub.org:8180/solr/", 
				"user", "password", "http://grub.silc.org.ua:8080", "user", "password", "localhost", 
				"grub", "user", "password",	"/home/thindil/workunits/wu/", "password", 
				"GrubNG 0.1 (http://grub.org)", "250", "1.0", 
				"text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"};
			XmlNode noder;
			foreach (string node in nodes)
			{
				noder = doc.SelectSingleNode("/configuration/" + node);
				if (noder == null)
				{
					int index = Array.IndexOf(nodes, node);
					Config.InsertConfig(values[index], node);
				}
			}
			doc = null;
		}
	}
}