~halega/+junk/sharpdevelop

« back to all changes in this revision

Viewing changes to src/AddIns/BackendBindings/WixBinding/Project/Src/WixDocument.cs

  • Committer: sk
  • Date: 2011-09-10 05:17:57 UTC
  • Revision ID: halega@halega.com-20110910051757-qfouz1llya9m6boy
4.1.0.7915 Release Candidate 1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// Copyright (c) AlphaSierraPapa for the SharpDevelop Team (for details please see \doc\copyright.txt)
 
2
// This code is distributed under the GNU LGPL (for details please see \doc\license.txt)
 
3
 
 
4
using System;
 
5
using System.Collections.Generic;
 
6
using System.Collections.ObjectModel;
 
7
using System.Drawing;
 
8
using System.IO;
 
9
using System.Text;
 
10
using System.Xml;
 
11
 
 
12
using ICSharpCode.Core;
 
13
using ICSharpCode.NRefactory;
 
14
using ICSharpCode.SharpDevelop.Dom;
 
15
using ICSharpCode.SharpDevelop.Editor;
 
16
using ICSharpCode.XmlEditor;
 
17
 
 
18
namespace ICSharpCode.WixBinding
 
19
{
 
20
        /// <summary>
 
21
        /// A Wix document (.wxs or .wxi).
 
22
        /// </summary>
 
23
        public class WixDocument : XmlDocument, IWixPropertyValueProvider
 
24
        {       
 
25
                WixNamespaceManager namespaceManager;
 
26
                IFileLoader fileLoader;
 
27
                WixProject project;
 
28
                string fileName = String.Empty;
 
29
                
 
30
                public WixDocument() 
 
31
                        : this((WixProject)null)
 
32
                {
 
33
                }
 
34
                
 
35
                public WixDocument(WixProject project) 
 
36
                        : this(project, new DefaultFileLoader())
 
37
                {
 
38
                }
 
39
                
 
40
                /// <summary>
 
41
                /// Creates a new instance of the WixDocument class but overrides the 
 
42
                /// default file loading functionality of the WixDocument. 
 
43
                /// </summary>
 
44
                public WixDocument(WixProject project, IFileLoader fileLoader)
 
45
                {
 
46
                        this.project = project;
 
47
                        this.fileLoader = fileLoader;
 
48
                        
 
49
                        if (fileLoader == null) {
 
50
                                throw new ArgumentNullException("fileLoader");
 
51
                        }
 
52
                        
 
53
                        namespaceManager = new WixNamespaceManager(NameTable);                  
 
54
                }
 
55
                
 
56
                public WixProject Project {
 
57
                        get { return project; }
 
58
                }
 
59
                
 
60
                public string FileName {
 
61
                        get { return fileName; }
 
62
                        set { fileName = value; }
 
63
                }
 
64
                
 
65
                /// <summary>
 
66
                /// Gets a WixDialog object for the specified dialog id.
 
67
                /// </summary>
 
68
                public WixDialog CreateWixDialog(string id, ITextFileReader reader)
 
69
                {
 
70
                        XmlElement dialogElement = GetDialogElement(id);
 
71
                        if (dialogElement != null) {
 
72
                                return new WixDialog(this, dialogElement, new WixBinaries(this, reader));
 
73
                        }
 
74
                        return null;
 
75
                }
 
76
                
 
77
                XmlElement GetDialogElement(string id)
 
78
                {
 
79
                        string xpath = GetXPath("//w:Dialog[@Id='{0}']", id);
 
80
                        return (XmlElement)SelectSingleElement(xpath);
 
81
                }
 
82
                
 
83
                string GetXPath(string xpathFormat, string arg)
 
84
                {
 
85
                        return String.Format(xpathFormat, XmlEncode(arg));
 
86
                }
 
87
                
 
88
                string GetXPath(string xpathFormat, string arg1, string arg2)
 
89
                {
 
90
                        return String.Format(xpathFormat, XmlEncode(arg1), XmlEncode(arg2));
 
91
                }
 
92
                
 
93
                string XmlEncode(string item)
 
94
                {
 
95
                        char quoteChar = '\'';
 
96
                        return XmlEncoder.Encode(item, quoteChar);
 
97
                }
 
98
                
 
99
                XmlElement SelectSingleElement(string xpath)
 
100
                {
 
101
                        return (XmlElement)SelectSingleNode(xpath, namespaceManager);
 
102
                }
 
103
                
 
104
                public Bitmap LoadBitmapWithId(string id)
 
105
                {
 
106
                        string bitmapFileName = GetBinaryFileName(id);
 
107
                        return LoadBitmapWithFileName(bitmapFileName);
 
108
                }
 
109
                
 
110
                public string GetBinaryFileName(string id)
 
111
                {
 
112
                        string xpath = GetXPath("//w:Binary[@Id='{0}']", id);
 
113
                        WixBinaryElement binaryElement = (WixBinaryElement)SelectSingleElement(xpath);
 
114
                        if (binaryElement != null) {
 
115
                                return binaryElement.GetFileName();
 
116
                        }
 
117
                        return null;
 
118
                }
 
119
                
 
120
                public Bitmap LoadBitmapWithFileName(string fileName)
 
121
                {
 
122
                        if (fileName != null) {
 
123
                                return fileLoader.LoadBitmap(fileName);
 
124
                        }
 
125
                        return null;
 
126
                }
 
127
                
 
128
                /// <summary>
 
129
                /// Gets a property value defined in the Wix document.
 
130
                /// </summary>
 
131
                public string GetProperty(string name)
 
132
                {
 
133
                        string xpath = GetXPath("//w:Property[@Id='{0}']", name);
 
134
                        XmlElement textStyleElement = SelectSingleElement(xpath);
 
135
                        if (textStyleElement != null) {
 
136
                                return textStyleElement.InnerText;
 
137
                        }
 
138
                        return String.Empty;
 
139
                }
 
140
                
 
141
                string IWixPropertyValueProvider.GetValue(string name)
 
142
                {
 
143
                        if (project != null) {
 
144
                                return project.GetPreprocessorVariableValue(name);
 
145
                        }
 
146
                        return null;
 
147
                }
 
148
                
 
149
                /// <summary>
 
150
                /// Gets the top SOURCEDIR directory.
 
151
                /// </summary>
 
152
                public WixDirectoryElement GetRootDirectory()
 
153
                {
 
154
                        string xpath = GetXPath("//w:Product/w:Directory[@Id='{0}']", WixDirectoryElement.RootDirectoryId);
 
155
                        return (WixDirectoryElement)SelectSingleElement(xpath);
 
156
                }
 
157
                
 
158
                /// <summary>
 
159
                /// Gets a reference to the root directory.
 
160
                /// </summary>
 
161
                public WixDirectoryRefElement GetRootDirectoryRef()
 
162
                {
 
163
                        string xpath = GetXPath("//w:DirectoryRef[@Id='{0}']", WixDirectoryElement.RootDirectoryId);
 
164
                        return (WixDirectoryRefElement)SelectSingleElement(xpath);
 
165
                }
 
166
                
 
167
                public bool HasProduct {
 
168
                        get { return GetProduct() != null; }
 
169
                }
 
170
                
 
171
                public string GetWixNamespacePrefix()
 
172
                {
 
173
                        XmlElement documentElement = DocumentElement;
 
174
                        if (documentElement != null) {
 
175
                                return documentElement.GetPrefixOfNamespace(WixNamespaceManager.Namespace);
 
176
                        }
 
177
                        return String.Empty;
 
178
                }
 
179
                
 
180
                public WixDirectoryElement AddRootDirectory()
 
181
                {
 
182
                        XmlElement productElement = GetProduct();
 
183
                        if (productElement == null) {
 
184
                                productElement = AddProduct();
 
185
                        }
 
186
                        return AddRootDirectoryToProduct(productElement);
 
187
                }
 
188
                
 
189
                XmlElement AddProduct()
 
190
                {
 
191
                        XmlElement productElement = CreateWixElement("Product");
 
192
                        DocumentElement.AppendChild(productElement);
 
193
                        return productElement;
 
194
                }
 
195
                
 
196
                WixDirectoryElement AddRootDirectoryToProduct(XmlElement parentProductElement)
 
197
                {
 
198
                        WixDirectoryElement rootDirectory = WixDirectoryElement.CreateRootDirectory(this);
 
199
                        return (WixDirectoryElement)parentProductElement.AppendChild(rootDirectory);
 
200
                }
 
201
                
 
202
                public XmlElement CreateWixElement(string name)
 
203
                {
 
204
                        return CreateElement(GetWixNamespacePrefix(), name, WixNamespaceManager.Namespace);
 
205
                }
 
206
                
 
207
                /// <summary>
 
208
                /// Creates custom Wix elements for certain elements such as Directory and File.
 
209
                /// </summary>
 
210
                public override XmlElement CreateElement(string prefix, string localName, string namespaceURI)
 
211
                {
 
212
                        if (namespaceURI == WixNamespaceManager.Namespace) {
 
213
                                switch (localName) {
 
214
                                        case WixDirectoryElement.DirectoryElementName:
 
215
                                                return new WixDirectoryElement(this);
 
216
                                        case WixComponentElement.ComponentElementName:
 
217
                                                return new WixComponentElement(this);
 
218
                                        case WixFileElement.FileElementName:
 
219
                                                return new WixFileElement(this);
 
220
                                        case WixDirectoryRefElement.DirectoryRefElementName:
 
221
                                                return new WixDirectoryRefElement(this);
 
222
                                        case WixBinaryElement.BinaryElementName:
 
223
                                                return new WixBinaryElement(this);
 
224
                                        case WixDialogElement.DialogElementName:
 
225
                                                return new WixDialogElement(this);
 
226
                                }
 
227
                        }
 
228
                        return base.CreateElement(prefix, localName, namespaceURI);
 
229
                }
 
230
                
 
231
                /// <summary>
 
232
                /// Checks to see if a File element exists with the specified id in this
 
233
                /// document.
 
234
                /// </summary>
 
235
                public bool FileIdExists(string id)
 
236
                {
 
237
                        return ElementIdExists(WixFileElement.FileElementName, id);
 
238
                }
 
239
                
 
240
                bool ElementIdExists(string elementName, string id)
 
241
                {
 
242
                        string xpath = GetXPath("//w:{0}[@Id='{1}']", elementName, id);
 
243
                        XmlNodeList nodes = SelectNodes(xpath, new WixNamespaceManager(NameTable));
 
244
                        return nodes.Count > 0;
 
245
                }
 
246
                
 
247
                /// <summary>
 
248
                /// Checks to see if a Component element exists with the specified id in this
 
249
                /// document.
 
250
                /// </summary>
 
251
                public bool ComponentIdExists(string id)
 
252
                {
 
253
                        return ElementIdExists(WixComponentElement.ComponentElementName, id);
 
254
                }
 
255
                
 
256
                public bool DirectoryIdExists(string id)
 
257
                {
 
258
                        return ElementIdExists(WixDirectoryElement.DirectoryElementName, id);
 
259
                }
 
260
                
 
261
                /// <summary>
 
262
                /// Returns the full path based on the location of 
 
263
                /// this WixDocument.
 
264
                /// </summary>
 
265
                public string GetFullPath(string relativePath)
 
266
                {
 
267
                        if (!String.IsNullOrEmpty(fileName)) {
 
268
                                string basePath = Path.GetDirectoryName(fileName);
 
269
                                return FileUtility.GetAbsolutePath(basePath, relativePath);
 
270
                        }
 
271
                        return relativePath;
 
272
                }
 
273
                
 
274
                /// <summary>
 
275
                /// Returns the relative path based on the location of 
 
276
                /// this WixDocument.
 
277
                /// </summary>
 
278
                public string GetRelativePath(string fullPath)
 
279
                {
 
280
                        if (!String.IsNullOrEmpty(fileName)) {
 
281
                                string basePath = Path.GetDirectoryName(fileName);
 
282
                                return FileUtility.GetRelativePath(basePath, fullPath);
 
283
                        }
 
284
                        return fullPath;
 
285
                }
 
286
                
 
287
                public XmlElement GetProduct()
 
288
                {
 
289
                        return SelectSingleElement("w:Wix/w:Product");
 
290
                }
 
291
                
 
292
                public WixBinaryElement[] GetBinaries()
 
293
                {
 
294
                        List<WixBinaryElement> binaries = new List<WixBinaryElement>();
 
295
                        foreach (WixBinaryElement element in SelectNodes("//w:Binary", namespaceManager)) {
 
296
                                binaries.Add(element);
 
297
                        }
 
298
                        return binaries.ToArray();
 
299
                }
 
300
        }
 
301
}