~ubuntu-branches/ubuntu/oneiric/monodevelop/oneiric

« back to all changes in this revision

Viewing changes to src/core/MonoDevelop.Ide/MonoDevelop.Ide.OnlineTemplates/ProjectTemplateIndex.cs

  • Committer: Bazaar Package Importer
  • Author(s): Jo Shields
  • Date: 2011-06-27 17:03:13 UTC
  • mto: (1.8.1 upstream)
  • mto: This revision was merged to the branch mainline in revision 54.
  • Revision ID: james.westby@ubuntu.com-20110627170313-6cvz3s19x6e9hqe9
ImportĀ upstreamĀ versionĀ 2.5.92+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// 
 
2
// ProjectTemplateIndex.cs
 
3
//  
 
4
// Author:
 
5
//       Michael Hutchinson <mhutchinson@novell.com>
 
6
// 
 
7
// Copyright (c) 2011 Novell, Inc.
 
8
// 
 
9
// Permission is hereby granted, free of charge, to any person obtaining a copy
 
10
// of this software and associated documentation files (the "Software"), to deal
 
11
// in the Software without restriction, including without limitation the rights
 
12
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 
13
// copies of the Software, and to permit persons to whom the Software is
 
14
// furnished to do so, subject to the following conditions:
 
15
// 
 
16
// The above copyright notice and this permission notice shall be included in
 
17
// all copies or substantial portions of the Software.
 
18
// 
 
19
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 
20
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 
21
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 
22
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 
23
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 
24
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 
25
// THE SOFTWARE.
 
26
 
 
27
using System;
 
28
using System.Collections.Generic;
 
29
using System.Xml.Linq;
 
30
 
 
31
//************************ WARNING ************************
 
32
// This file should be kept in sync with MDData.Index
 
33
// and the index format should not be changed without
 
34
// bumping the version and maintaining the old format
 
35
//*********************************************************
 
36
 
 
37
namespace MonoDevelop.Ide.OnlineTemplates
 
38
{
 
39
        class ProjectTemplateIndex : List<ProjectTemplateDescription>
 
40
        {
 
41
                const int FORMAT_VERSION = 1;
 
42
                
 
43
                ProjectTemplateIndex ()
 
44
                {
 
45
                }
 
46
                
 
47
                public ProjectTemplateIndex (string mdVersion)
 
48
                {
 
49
                        this.MDVersion = mdVersion;
 
50
                }
 
51
                
 
52
                public string MDVersion { get; private set; }
 
53
                
 
54
                public static ProjectTemplateIndex Load (string file)
 
55
                {
 
56
                        var doc = XDocument.Load (file);
 
57
                        var idx = new ProjectTemplateIndex ();
 
58
                        
 
59
                        var root = doc.Root;
 
60
                        if (root.Name != "TemplateIndex")
 
61
                                throw new Exception (string.Format ("Root element was {0}, expected '{1}'", root.Name, "ProjectTemplate"));
 
62
                        if ((int) root.Attribute ("format") != FORMAT_VERSION)
 
63
                                throw new Exception ("Invalid format version");
 
64
                        
 
65
                        idx.MDVersion = (string) root.Attribute ("version");
 
66
                        
 
67
                        return idx;
 
68
                }
 
69
                
 
70
                public void Write (string file)
 
71
                {
 
72
                        var doc = new XDocument ();
 
73
                        doc.Add (new XElement ("TemplateIndex", new XAttribute ("format", FORMAT_VERSION)));
 
74
                        foreach (var item in this) {
 
75
                                doc.Root.Add (item.Write ());
 
76
                        }
 
77
                        doc.Save (file);
 
78
                }
 
79
        }
 
80
        
 
81
        class ProjectTemplateDescription
 
82
        {
 
83
                public string Name { get; set; }
 
84
                public string Summary { get; set; }
 
85
                public string Description { get; set; }
 
86
                public string Author { get; set; }
 
87
                public string Tags { get; set; }
 
88
                public string IconUrl { get; set; }
 
89
                public string ScreenshotUrl { get; set; }
 
90
                public string TemplateUrl { get; set; }
 
91
                public DateTime Modified { get; set; }
 
92
                
 
93
                public ProjectTemplateDescription Read (XElement element)
 
94
                {
 
95
                        return new ProjectTemplateDescription () {
 
96
                                Name = (string) element.Element ("Name"),
 
97
                                Summary = (string) element.Element ("Summary"),
 
98
                                Description = (string) element.Element ("Description"),
 
99
                                Author = (string) element.Element ("Author"),
 
100
                                Tags = (string) element.Element ("Tags"),
 
101
                                IconUrl = (string) element.Element ("IconUrl"),
 
102
                                TemplateUrl = (string) element.Element ("TemplateUrl"),
 
103
                                ScreenshotUrl = (string) element.Element ("ScreenshotUrl"),
 
104
                        };
 
105
                }
 
106
                
 
107
                public XElement Write ()
 
108
                {
 
109
                        return new XElement ("ProjectTemplate",
 
110
                                new XAttribute ("modified", Modified),
 
111
                                new XElement ("Name", Name),
 
112
                                new XElement ("Summary", Summary),
 
113
                                new XElement ("Description", Description),
 
114
                                new XElement ("Author", Author),
 
115
                                new XElement ("Tags", Tags),
 
116
                                new XElement ("IconUrl", IconUrl),
 
117
                                new XElement ("TemplateUrl", TemplateUrl),
 
118
                                new XElement ("ScreenshotUrl", ScreenshotUrl)
 
119
                        );
 
120
                }
 
121
        }
 
122
        
 
123
        class ProjectTemplateManifest
 
124
        {
 
125
                const int FORMAT_VERSION = 1;
 
126
                
 
127
                public string Name { get; set; }
 
128
                public string Summary { get; set; }
 
129
                public string Description { get; set; }
 
130
                public string Author { get; set; }
 
131
                public string Tags { get; set; }
 
132
                public string IconFile { get; set; }
 
133
                public string ScreenshotFile { get; set; }
 
134
                public string MinimumVersion { get; set; }
 
135
                
 
136
                public static ProjectTemplateManifest Load (XDocument doc)
 
137
                {
 
138
                        var root = doc.Root;
 
139
                        if (root.Name != "ProjectTemplate")
 
140
                                throw new Exception (string.Format ("Root element was {0}, expected '{1}'", root.Name, "ProjectTemplate"));
 
141
                        if ((int) root.Attribute ("format") != FORMAT_VERSION)
 
142
                                throw new Exception ("Invalid format version");
 
143
                        
 
144
                        return new ProjectTemplateManifest {
 
145
                                Name = GetRequiredStringElement (root, "Name"),
 
146
                                Summary = (string) root.Element ("Summary"),
 
147
                                Description = GetRequiredStringElement (root, "Description"),
 
148
                                Author = GetRequiredStringElement (root, "Author"),
 
149
                                Tags = GetRequiredStringElement (root, "Tags"),
 
150
                                IconFile = (string) root.Element ("IconFile"),
 
151
                                ScreenshotFile = (string) root.Element ("ScreenshotFile"),
 
152
                        };
 
153
                }
 
154
                
 
155
                static string GetRequiredStringElement (XElement parent, string name)
 
156
                {
 
157
                        string value = (string) parent.Element (name);
 
158
                        if (string.IsNullOrWhiteSpace (value))
 
159
                                throw new Exception (string.Format ("The '{0}' element cannot be empty", name));
 
160
                        return value;
 
161
                }
 
162
        }
 
163
}
 
164