File: gapi-fixup.cs

package info (click to toggle)
gtk-sharp 1%3A1.0.10-5
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 18,416 kB
  • ctags: 4,465
  • sloc: xml: 224,361; cs: 17,961; sh: 8,422; ansic: 2,724; makefile: 1,604; perl: 1,089
file content (112 lines) | stat: -rw-r--r-- 3,545 bytes parent folder | download
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
// gapi-fixup.cs - xml alteration engine.
//
// Author: Mike Kestner <mkestner@speakeasy.net>
//
// Copyright (c) 2003 Mike Kestner
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of version 2 of the GNU General Public
// License as published by the Free Software Foundation.
//
// 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., 59 Temple Place - Suite 330,
// Boston, MA 02111-1307, USA.

namespace GtkSharp.Parsing {

	using System;
	using System.IO;
	using System.Xml;
	using System.Xml.XPath;

	public class Fixup  {

		public static int Main (string[] args)
		{
			if (args.Length != 2) {
				Console.WriteLine ("Usage: gapi-fixup --metadata=<filename> --api=<filename>");
				return 0;
			}

			string api_filename = "";
			XmlDocument api_doc = new XmlDocument ();
			XmlDocument meta_doc = new XmlDocument ();

			foreach (string arg in args) {

				if (arg.StartsWith("--metadata=")) {

					string meta_filename = arg.Substring (11);

					try {
						Stream stream = File.OpenRead (meta_filename);
						meta_doc.Load (stream);
						stream.Close ();
					} catch (XmlException e) {
						Console.WriteLine ("Invalid meta file.");
						Console.WriteLine (e);
						return 1;
					}

				} else if (arg.StartsWith ("--api=")) {

					api_filename = arg.Substring (6);

					try {
						Stream stream = File.OpenRead (api_filename);
						api_doc.Load (stream);
						stream.Close ();
					} catch (XmlException e) {
						Console.WriteLine ("Invalid api file.");
						Console.WriteLine (e);
						return 1;
					}

				} else {
					Console.WriteLine ("Usage: gapi-fixup --metadata=<filename> --api=<filename>");
					return 1;
				}
			}

			XPathNavigator meta_nav = meta_doc.CreateNavigator ();
			XPathNavigator api_nav = api_doc.CreateNavigator ();

			XPathNodeIterator attr_iter = meta_nav.Select ("/metadata/attr");
			while (attr_iter.MoveNext ()) {
				string path = attr_iter.Current.GetAttribute ("path", "");
				string attr_name = attr_iter.Current.GetAttribute ("name", "");
				XPathNodeIterator api_iter = api_nav.Select (path);
				while (api_iter.MoveNext ()) {
					XmlElement node = ((IHasXmlNode)api_iter.Current).GetNode () as XmlElement;
					node.SetAttribute (attr_name, attr_iter.Current.Value);
				}
			}

			XPathNodeIterator move_iter = meta_nav.Select ("/metadata/move-node");
			while (move_iter.MoveNext ()) {
				string path = move_iter.Current.GetAttribute ("path", "");
				XPathExpression expr = api_nav.Compile (path);
				string parent = move_iter.Current.Value;
				XPathNodeIterator parent_iter = api_nav.Select (parent);
				while (parent_iter.MoveNext ()) {
					XmlNode parent_node = ((IHasXmlNode)parent_iter.Current).GetNode ();
					XPathNodeIterator path_iter = parent_iter.Current.Clone ().Select (expr);
					while (path_iter.MoveNext ()) {
						XmlNode node = ((IHasXmlNode)path_iter.Current).GetNode ();
						parent_node.AppendChild (node.Clone ());
						node.ParentNode.RemoveChild (node);
					}
				}
			}

			api_doc.Save (api_filename);
			return 0;
		}
	}
}