~cszikszoy/do-plugins/pastebin

« back to all changes in this revision

Viewing changes to OpenSearch/src/MozillaSearchFile.cs

  • Committer: Christopher Halse Rogers
  • Date: 2008-08-24 08:44:24 UTC
  • mfrom: (244.1.2 do-plugins)
  • Revision ID: raof@ubuntu.com-20080824084424-8gp5ff6v9nku9z21
Merge in the tip of 0.6

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
//  MozillaSearchFile.cs
2
 
//
3
 
//  GNOME Do is the legal property of its developers, whose names are too numerous
4
 
//  to list here.  Please refer to the COPYRIGHT file distributed with this
5
 
//  source distribution.
6
 
//
7
 
//  This program is free software: you can redistribute it and/or modify
8
 
//  it under the terms of the GNU General Public License as published by
9
 
//  the Free Software Foundation, either version 3 of the License, or
10
 
//  (at your option) any later version.
11
 
//
12
 
//  This program is distributed in the hope that it will be useful,
13
 
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
 
//  GNU General Public License for more details.
16
 
//
17
 
//  You should have received a copy of the GNU General Public License
18
 
//  along with this program.  If not, see <http://www.gnu.org/licenses/>.
19
 
 
20
 
using System.Text.RegularExpressions;
21
 
using System.Xml;
22
 
 
23
 
namespace Do.Plugins.OpenSearch
24
 
{
25
 
        public class MozillaSearchFile : IOpenSearchFile
26
 
        {
27
 
                private string path;
28
 
                
29
 
                public MozillaSearchFile (string path)
30
 
                {
31
 
                        this.path = path;
32
 
                }
33
 
                
34
 
                public OpenSearchItem CreateOpenSearchItem ()
35
 
                {
36
 
                        XmlDocument doc = new XmlDocument ();
37
 
                        doc.Load (path);                                                
38
 
                        XmlNamespaceManager namespaceManager = XmlNamespaceHelper.PopulateNamespaceManager (doc);
39
 
                        
40
 
                        XmlNode shortName = doc.SelectSingleNode ("//*/default:ShortName", namespaceManager);           
41
 
                        XmlNode description = doc.SelectSingleNode ("//*/default:Description", namespaceManager);       
42
 
                        XmlNode url = doc.SelectSingleNode ("//*/default:Url[@type='text/html' and @method='GET']", namespaceManager);
43
 
                        
44
 
                        if(shortName == null || description == null || url == null)
45
 
                                return null;
46
 
 
47
 
                        string templateUrl = url.Attributes["template"].Value + "?";
48
 
                                                
49
 
                        XmlNodeList paramList = url.ChildNodes;
50
 
                        foreach(XmlNode node in paramList)
51
 
                        {
52
 
                                if (node.Name != "Param")
53
 
                                        continue;
54
 
                                if (Regex.IsMatch(node.Attributes["value"].Value, "{.*}") && node.Attributes["value"].Value != "{searchTerms}")
55
 
                                        continue;
56
 
                                templateUrl += node.Attributes["name"].Value + "=" + node.Attributes["value"].Value + "&";
57
 
                        }
58
 
                                             
59
 
                        templateUrl = templateUrl.TrimEnd (new char [] {'&','?'});       
60
 
                        
61
 
                        return new OpenSearchItem (shortName.InnerText, description.InnerText, templateUrl);
62
 
                }
63
 
        }
64
 
}