Forgot to bump FSQ version. Weekend syndrome.
[beagle.git] / Filters / FilterRPM.cs
blob585213d55580125a8f9bf0fe45a063c87f57de34
1 //
2 // FilterRPM.cs
3 //
4 // Copyright (C) 2006 Debajyoti Bera <dbera.web@gmail.com>
5 //
7 //
8 // Permission is hereby granted, free of charge, to any person obtaining a
9 // copy of this software and associated documentation files (the "Software"),
10 // to deal in the Software without restriction, including without limitation
11 // the rights to use, copy, modify, merge, publish, distribute, sublicense,
12 // and/or sell copies of the Software, and to permit persons to whom the
13 // Software is furnished to do so, subject to the following conditions:
15 // The above copyright notice and this permission notice shall be included in
16 // all copies or substantial portions of the Software.
18 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
23 // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
24 // DEALINGS IN THE SOFTWARE.
27 using System;
28 using System.IO;
29 using System.Text;
30 using System.Collections;
31 using System.Diagnostics;
32 using System.Xml;
34 using Beagle.Daemon;
35 using Beagle.Util;
37 namespace Beagle.Filters {
38 public class FilterRPM : Beagle.Daemon.Filter {
40 private class RpmPropertyInfo {
41 public string property_name;
42 public bool is_keyword;
44 public RpmPropertyInfo (string property_name, bool is_keyword)
46 this.property_name = property_name;
47 this.is_keyword = is_keyword;
51 private static Hashtable hash_property_list;
52 static FilterRPM ()
54 hash_property_list = new Hashtable ();
56 // mapping between rpm tagname and beagle property name
57 hash_property_list ["Name"] = new RpmPropertyInfo ("dc:title", false);
58 hash_property_list ["Version"] = new RpmPropertyInfo ("fixme:version", true);
59 hash_property_list ["Release"] = new RpmPropertyInfo ("fixme:release", true);
60 hash_property_list ["Summary"] = new RpmPropertyInfo ("dc:subject", false);
61 hash_property_list ["Description"] = new RpmPropertyInfo ("dc:description", false);
62 hash_property_list ["License"] = new RpmPropertyInfo ("dc:rights", false);
63 hash_property_list ["Group"] = new RpmPropertyInfo ("fixme:group", false);
64 hash_property_list ["Url"] = new RpmPropertyInfo ("dc:source", true);
65 hash_property_list ["Os"] = new RpmPropertyInfo ("fixme:os", false);
66 hash_property_list ["Arch"] = new RpmPropertyInfo ("fixme:arch", false);
67 hash_property_list ["Changelogtext"] = new RpmPropertyInfo ("fixme:changelog", false);
70 public FilterRPM ()
72 AddSupportedFlavor (FilterFlavor.NewFromMimeType ("application/x-rpm"));
75 protected override void DoPullProperties ()
77 Process pc = new Process ();
78 pc.StartInfo.FileName = "rpm";
79 pc.StartInfo.Arguments = " -qp --queryformat '[%{*:xml}\n]' \"" + FileInfo.FullName+"\"";
80 pc.StartInfo.RedirectStandardInput = false;
81 pc.StartInfo.RedirectStandardOutput = true;
82 pc.StartInfo.RedirectStandardError = true;
83 pc.StartInfo.UseShellExecute = false;
85 try {
86 pc.Start ();
87 } catch (System.ComponentModel.Win32Exception) {
88 Log.Warn ("Error: 'rpm' command not found or unable to run");
89 Error ();
90 return;
93 StreamReader pout = pc.StandardOutput;
94 XmlTextReader reader = new XmlTextReader (pout);
95 reader.WhitespaceHandling = WhitespaceHandling.None;
97 try {
98 ParseRpmTags (reader);
99 } catch (XmlException) {
100 Logger.Log.Debug ("FilterRPM: Error parsing output of rpmquery!");
101 Error ();
103 Finished ();
106 private void ParseRpmTags (XmlTextReader reader)
108 RpmPropertyInfo prop_info = null;
110 reader.Read ();
111 while (reader.Read ()) {
112 if (reader.IsEmptyElement || ! reader.IsStartElement ())
113 continue;
114 else if (reader.Name != "rpmTag") {
115 reader.Skip ();
116 continue;
118 string attr_name = reader ["name"];
119 //Logger.Log.Debug ("Read element:" + reader.Name + " - " + attr_name);
121 // store basenames values as Text - they are like the "text"-content of rpm files
122 if (attr_name == "Basenames") {
123 ReadStringValues (reader, true, null);
124 continue;
127 prop_info = (RpmPropertyInfo) hash_property_list [attr_name];
128 if (prop_info != null)
129 ReadStringValues (reader, false, prop_info);
133 private void ReadStringValues (XmlTextReader reader, bool store_as_text, RpmPropertyInfo prop_info)
135 reader.ReadStartElement ();
137 while (reader.IsStartElement ()) {
138 //Logger.Log.Debug (" Reading value for:" + reader.Name);
139 if (reader.IsEmptyElement)
140 reader.Skip ();
142 string content = HtmlAgilityPack.HtmlEntity.DeEntitize (reader.ReadInnerXml ());
143 //Logger.Log.Debug (prop_info.property_name
144 // + (prop_info.is_keyword ? " (keyword)" : " (text)")
145 // + " = [" + content + "]");
147 if (store_as_text) {
148 AppendText (content);
149 AppendWhiteSpace ();
150 continue;
153 if (prop_info.is_keyword)
154 AddProperty (Beagle.Property.New (prop_info.property_name, content));
155 else
156 AddProperty (Beagle.Property.NewUnsearched (prop_info.property_name, content));
159 //Logger.Log.Debug (" Done reading values. Now at " +
160 // (reader.IsStartElement () ? "" : "/") + reader.Name);