Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/fsgateway/src/FsXml.cs
blob: b971a072bd8f809fd0e9e0b9f774e63854285cf6 (plain)
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
/* 
 * FsGateway - navigate a database structure as directory tree
 * Copyright (C) 2009-2010 Torello Querci <torello@torosoft.com>
 * 
 * This program 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 2 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, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

using System;
using Mono.Unix.Native;
using System.Collections;
using System.Collections.Generic;
using Mono.Fuse;
using System.Xml;
using System.Xml.XPath;

namespace FsGateway
{
	public class FsXml: IFsGateway
	{
		XmlDocument document=null;
		XmlNamespaceManager nsm;
		string defaultPrefix="";
		string fileXml=null;
		long timeval;
		uint uid,gid;

		public FsXml() 
		{
			document=null;
		}
		
		public FsXml(string fileXml, string[] args) 
		{
			analyzeXmlFile(fileXml);
		}
		
		public void Dispose() {
		}
		
		public bool Connect() {
			return false;
		}
		
		public bool Connect(string parameter) {

			analyzeXmlFile(parameter);
			return true;
		}

		private void analyzeXmlFile(string file) {

			fileXml=file;
			
			document=new XmlDocument();
			document.Load(file);
			
			Stat stat;
			Mono.Unix.Native.Syscall.stat(file,out stat);
			gid=stat.st_gid;
			uid=stat.st_uid;
			
			timeval=stat.st_mtime;			

			// Namespaces analyze
			nsm=new XmlNamespaceManager(document.NameTable);
			SortedDictionary<string,string> namespaces=new SortedDictionary<string, string>();
			string defaultNameSpace;
			defaultNameSpace=analyzeNamespaces(document.FirstChild, out namespaces);
			
			// Check for default namespace
			if (defaultNameSpace!=null && defaultNameSpace.Length>0) {
				
				int i=0;
				defaultPrefix="def";
				while (namespaces.ContainsKey(defaultPrefix)) {
					++i;
					defaultPrefix="def"+i.ToString();					
				};
				namespaces.Add(defaultPrefix,defaultNameSpace);
			}

			if (namespaces.Keys!=null) {
				IEnumerator<string> itKey=namespaces.Keys.GetEnumerator();
				while (itKey.MoveNext()) {
					nsm.AddNamespace(itKey.Current,namespaces[itKey.Current]);
				}
			}
		}
		
		public string connectionParameter {
			get {
				return fileXml;
			}
		}
		
		public void Unconnect() {
			fileXml=null;
			document=null;
		}
		
		public string Usage {
			get {
				return "You need to specify the XML file.";
			}
		}
		
		public string storageType {
			get {
				return "xmlfs";
			}
		}
		
		public bool isConnect {
			get {
				return true;
			}
		}
		
		public Errno OnReadHandle (string file
		                   ,OpenedPathInfo info
		                   ,byte[] buf
		                   ,long offset
		                   ,out int bytesWritten) {

			string data;
			Byte[] buffer=null;
			System.Text.UTF8Encoding encoder=new System.Text.UTF8Encoding();

			if (isXmlnsDeclaration(file)) {

				buffer = encoder.GetBytes(nsm.LookupNamespace(getXmlnsDeclaration(file)));
			} else {

				XmlNode	node=document.SelectSingleNode(file,nsm);
				if (node==null) {
					bytesWritten=0;
					return Errno.ENODATA;
				}

				data=node.Value;
				buffer=encoder.GetBytes(data);
			}

			buffer.CopyTo(buf,offset);
			bytesWritten=System.Math.Min(buf.Length,buffer.Length-(int)offset);

			return 0;
		}		

		private IEnumerable<DirectoryEntry> ListNames (IEnumerator<string> directories)
		{
			while (directories.MoveNext()) {
				yield return new DirectoryEntry (directories.Current);
			}
		}

		public Errno OnReadDirectory (string directory, OpenedPathInfo info,
		                                          out IEnumerable<DirectoryEntry> names)
		{
			XmlNode node=document.SelectSingleNode(directory,nsm);

			List<string> listPath=analyzeNode(node);
			IEnumerator<string> en=listPath.GetEnumerator();
			names=ListNames(en);

			return 0;
		}

		private bool isXmlnsDeclaration (string path) {
			// Check for specific attrib
			char[] pattern = { '/' };
			string[] pathSplitted = path.Split(pattern);
			
			// Check if the path is an attribute
			string lastTag=pathSplitted[pathSplitted.Length-1];
			if (lastTag.StartsWith("@xmlns")) {
				if (lastTag.Length==6)
					return true;
				if (lastTag.Substring(6,1).Equals(":"))
					return true;
			}
			
			return false;
		}

		private string getXmlnsDeclaration (string path) {
			// Check for specific attrib
			char[] pattern = { '/' };
			string[] pathSplitted = path.Split(pattern);
			
			// Check if the path is an attribute
			string lastTag=pathSplitted[pathSplitted.Length-1];
			if (lastTag.StartsWith("@xmlns")) {
				if (lastTag.Length==6)
					return this.defaultPrefix;
				if (lastTag.Substring(6,1).Equals(":"))
					return lastTag.Substring(7);
			}
			
			return null;
		}
		

		public Errno OnGetPathStatus (string path, out Stat stbuf)
		{
			stbuf = new Stat ();
			stbuf.st_uid = uid;
			stbuf.st_gid = gid;

			stbuf.st_mtime = timeval;

			if (isXmlnsDeclaration(path)) {
				stbuf.st_mode = NativeConvert.FromUnixPermissionString ("-r--r--r--");

				Byte[] buffer=null;
				System.Text.UTF8Encoding encoder=new System.Text.UTF8Encoding();
				stbuf.st_size=encoder.GetByteCount(nsm.LookupNamespace(getXmlnsDeclaration(path)));
			} else {
				
				XmlNode node=null;

				node=document.SelectSingleNode(path,nsm);

				if (node==null) {
					return Errno.ENOENT;
				}
	
				if (node.NodeType==System.Xml.XmlNodeType.Element || node.NodeType==System.Xml.XmlNodeType.Document) {
					stbuf.st_mode  = NativeConvert.FromUnixPermissionString ("dr-xr-xr-x");
					stbuf.st_nlink = 1;
				} else {
					stbuf.st_mode = NativeConvert.FromUnixPermissionString ("-r--r--r--");
	
					Byte[] buffer=null;
					System.Text.UTF8Encoding encoder=new System.Text.UTF8Encoding();
					stbuf.st_size=encoder.GetByteCount(node.Value);
				}
			}
			return 0;
		}

		List<string> analyzeNode(XmlNode reader) {
			string newPath="";
			List<string> listPath=new List<string>();

			if (reader==null) return listPath;

			XmlNode child=reader.FirstChild;

			if (reader.NodeType==XmlNodeType.Element || reader.NodeType==XmlNodeType.Document) {
				if (reader.Attributes !=null) {
					XmlAttributeCollection attributes=reader.Attributes;
					for (int j=0; j<attributes.Count; j++) {
						addPathToList(listPath,attributes.Item(j));
					}
				}

				if (child!=null) {
			
					do {
						addPathToList(listPath,child);
					}
					while ((child=child.NextSibling)!=null);
				}
			}
			
			return listPath;
		}

		private void addPathToList(List<string> list, XmlNode node) {
			string name="";
			if (node.NodeType==XmlNodeType.Attribute) {
				name = "@"+node.Name;
			} else {
			
				if (defaultPrefix!=null && defaultPrefix.Length>0 && (node.Prefix==null || node.Prefix.Length<1)) {
					name += defaultPrefix+":";
				}
				
				switch (node.NodeType) {
				case XmlNodeType.Text:
					name = "text()";
					break;
				case XmlNodeType.XmlDeclaration:
					name=null;
					break;
				default:
					name += node.Name;
					break;
				}
			}
			if (name != null) {
				addPathToList(list,name);
			}
		}

		private void addPathToList(List<string> list, string path) {
			
			// Check if the string is present in the list
			if (list.Contains(path)) {
				
				// There is the path string in the list so remove it and add two specify the index of the path
				list.Remove(path);
				list.Add(path+"[1]");
				list.Add(path+"[2]");
			} else if (list.Contains(path+"[1]")) {
				
				// The path is present in several copy so I need to find the first index empty
				// This implementation is not goot 'cause have a o(n2) complexity
				int i=2;
				for (; list.Contains(path+"["+i+"]"); ++i);
				path = path+"["+i+"]";
				list.Add(path);
				
			} else {
				
				// The name is not present
				list.Add(path);
			}
		}

		static string analyzeNamespaces(XmlNode reader,out SortedDictionary<string,string> ns) {

			char[] delimiters = { ':' };
			string defaultNameSpace=null;
			
			ns=new SortedDictionary<string,string>();
			if (reader==null) return defaultNameSpace;

			do {
				if (reader.Attributes!=null) {
					IEnumerator en=reader.Attributes.GetEnumerator();
					while (en.MoveNext()) {
						XmlNode node=(XmlNode)en.Current;
						string[] name=node.Name.Split(delimiters);
					
						if (name[0].Equals("xmlns")) {
							// Check for default namespace
							if (name.Length<2) {
								defaultNameSpace=node.Value;
							} else {
								ns.Add(name[1],node.Value);
							}
						}
					}
				}

				SortedDictionary<string,string> nns;
				string defaultNS;
				defaultNS=analyzeNamespaces(reader.FirstChild,out nns);
				
				// Check for default namespace
				if (defaultNameSpace==null || defaultNameSpace.Length<1) {
					defaultNameSpace=defaultNS;
				}
				
				// Merge the two sorted dictionary
				IEnumerator<string> enk=nns.Keys.GetEnumerator();
				while (enk.MoveNext()) {
					if (!ns.ContainsKey(enk.Current)) {
						ns.Add(enk.Current,nns[enk.Current]);
					} else {
						if (!nns[enk.Current].Equals(ns[enk.Current])) {
							Console.WriteLine("XML that use the same prefix "+enk.Current+" for different namespaces "+nns[enk.Current]+", "+ns[enk.Current]+".");
						}
					}
				}
			} while ((reader=reader.NextSibling)!=null);

			return defaultNameSpace;
		}

		public Errno OnReadSymbolicLink (string link, out string target) {
			target=link;
			return Errno.ENOENT;
		}
	}
}