2
// ProjectTemplateIndex.cs
5
// Michael Hutchinson <mhutchinson@novell.com>
7
// Copyright (c) 2011 Novell, Inc.
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:
16
// The above copyright notice and this permission notice shall be included in
17
// all copies or substantial portions of the Software.
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
28
using System.Collections.Generic;
29
using System.Xml.Linq;
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
//*********************************************************
37
namespace MonoDevelop.Ide.OnlineTemplates
39
class ProjectTemplateIndex : List<ProjectTemplateDescription>
41
const int FORMAT_VERSION = 1;
43
ProjectTemplateIndex ()
47
public ProjectTemplateIndex (string mdVersion)
49
this.MDVersion = mdVersion;
52
public string MDVersion { get; private set; }
54
public static ProjectTemplateIndex Load (string file)
56
var doc = XDocument.Load (file);
57
var idx = new ProjectTemplateIndex ();
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");
65
idx.MDVersion = (string) root.Attribute ("version");
70
public void Write (string file)
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 ());
81
class ProjectTemplateDescription
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; }
93
public ProjectTemplateDescription Read (XElement element)
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"),
107
public XElement Write ()
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)
123
class ProjectTemplateManifest
125
const int FORMAT_VERSION = 1;
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; }
136
public static ProjectTemplateManifest Load (XDocument doc)
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");
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"),
155
static string GetRequiredStringElement (XElement parent, string name)
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));