forked from ewilderj/doap
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathViewer.cs
190 lines (157 loc) · 4.93 KB
/
Viewer.cs
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
using System;
using System.IO;
using System.Text;
using System.Reflection;
using System.Collections;
using System.Xml;
using System.Xml.Xsl;
using Redland;
namespace Doap {
public class Viewer
{
static Model schemaModel;
static Storage schemaStorage;
Storage storage;
Model model;
Parser parser;
XmlDocument xml;
XslTransform style;
string text;
Redland.Uri DoapSchemaUri =
new Redland.Uri ("http://usefulinc.com/ns/doap");
public Viewer (string uri)
{
parser = new Parser ("raptor");
loadSchemas ();
if (uri != null) {
Load (uri);
}
}
private void loadSchemas ()
{
Assembly ass = Assembly.GetExecutingAssembly ();
if (schemaModel == null) {
System.IO.Stream s = ass.GetManifestResourceStream ("doap.rdf");
schemaStorage = new Storage ("memory", "schema", null);
schemaModel = new Model (schemaStorage);
Encoding e = Encoding.GetEncoding ("utf-8");
StreamReader r = new StreamReader (s, e);
string txt = r.ReadToEnd ();
parser.ParseStringIntoModel (txt, DoapSchemaUri, schemaModel);
}
if (style == null) {
style = new XslTransform ();
style.Load (new XmlTextReader (ass.GetManifestResourceStream ("style.xsl")),
new XmlUrlResolver (),
ass.Evidence);
}
}
public void Load (string uri)
{
string txt = null;
System.Uri u = new System.Uri (uri);
Util.Debug ("Fetching file from " + uri);
if (u.IsFile) {
txt = Util.LoadFile (u.LocalPath);
} else if (u.Scheme == System.Uri.UriSchemeHttp) {
txt = Util.FetchUrl (uri);
} else {
throw new Exception
("Unsupported URI scheme. Only file: and http: " +
"URIs understood.");
}
if (txt == null)
throw new Exception ("No content found.");
storage = new Storage ("memory", "data", null);
model = new Model (storage);
if (uri == null)
uri = "file:///tmp/stdin";
Redland.Uri baseuri = new Redland.Uri (uri);
parser.ParseStringIntoModel (txt, baseuri, model);
}
public void Serialize (System.IO.TextWriter writer)
{
DoapNs doap = new DoapNs ();
RdfNs rdf = new RdfNs ();
FoafNs foaf = new FoafNs ();
StringWriter sw = new StringWriter ();
XmlTextWriter w = new XmlTextWriter (sw);
string[] urltypes = new string[] { "homepage", "mailing-list",
"download-page", "download-mirror",
"wiki", "bug-database", "screenshots"
};
string[] literaltypes = new string[] {
"created", "os", "programming-language"
};
string[] peopletypes = new string [] {
"maintainer", "developer", "documenter",
"translator", "tester", "helper"
};
w.WriteStartDocument ();
w.WriteStartElement (null, "content", null);
foreach (Node proj in model.GetSources (rdf ["type"],
doap ["Project"])) {
w.WriteStartElement (null, "project", null);
Node name = model.GetTarget (proj, doap ["name"]);
w.WriteStartElement ("name");
w.WriteString (name.Literal);
w.WriteEndElement ();
Node description = model.GetTarget (proj, doap ["description"]);
w.WriteStartElement ("description");
w.WriteStartAttribute ("xml", "lang", null);
w.WriteString (description.Language);
w.WriteEndAttribute ();
w.WriteString (description.Literal);
w.WriteEndElement ();
Node shortdesc = model.GetTarget (proj, doap ["shortdesc"]);
w.WriteStartElement ("shortdesc");
w.WriteStartAttribute ("xml", "lang", null);
w.WriteString (shortdesc.Language);
w.WriteEndAttribute ();
w.WriteString (shortdesc.Literal);
w.WriteEndElement ();
foreach (string p in urltypes) {
Node r = model.GetTarget (proj, doap [p]);
if (r != null) {
w.WriteStartElement (p);
w.WriteStartAttribute (null, "href", null);
w.WriteString (r.Uri.ToString ());
w.WriteEndElement ();
}
}
foreach (string p in literaltypes) {
foreach (Node r in model.GetTargets (proj, doap [p])) {
w.WriteStartElement (p);
w.WriteString (r.Literal);
w.WriteEndElement ();
}
}
foreach (string p in peopletypes) {
foreach (Node r in model.GetTargets (proj, doap [p])) {
w.WriteStartElement ("person");
w.WriteStartAttribute (null, "role", null);
w.WriteString (p);
w.WriteEndAttribute ();
Node n = model.GetTarget (r, foaf ["name"]);
Node hp = model.GetTarget (r, foaf ["homepage"]);
w.WriteStartAttribute (null, "name", null);
w.WriteString (n.Literal);
w.WriteEndAttribute ();
if (hp != null) {
w.WriteStartAttribute (null, "homepage", null);
w.WriteString (hp.Uri.ToString ());
w.WriteEndAttribute ();
}
w.WriteEndElement ();
w.WriteEndElement ();
}
}
w.WriteEndElement ();
}
w.WriteEndDocument ();
// now do XML transform
System.Xml.XPath.XPathDocument doc = new System.Xml.XPath.XPathDocument (new XmlTextReader (new StringReader (sw.ToString ())));
style.Transform (doc, null, writer, null);
}
}
}