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)
5
using System.Collections.Generic;
6
using System.Collections.ObjectModel;
12
using ICSharpCode.Core;
13
using ICSharpCode.NRefactory;
14
using ICSharpCode.SharpDevelop.Dom;
15
using ICSharpCode.SharpDevelop.Editor;
16
using ICSharpCode.XmlEditor;
18
namespace ICSharpCode.WixBinding
21
/// A Wix document (.wxs or .wxi).
23
public class WixDocument : XmlDocument, IWixPropertyValueProvider
25
WixNamespaceManager namespaceManager;
26
IFileLoader fileLoader;
28
string fileName = String.Empty;
31
: this((WixProject)null)
35
public WixDocument(WixProject project)
36
: this(project, new DefaultFileLoader())
41
/// Creates a new instance of the WixDocument class but overrides the
42
/// default file loading functionality of the WixDocument.
44
public WixDocument(WixProject project, IFileLoader fileLoader)
46
this.project = project;
47
this.fileLoader = fileLoader;
49
if (fileLoader == null) {
50
throw new ArgumentNullException("fileLoader");
53
namespaceManager = new WixNamespaceManager(NameTable);
56
public WixProject Project {
57
get { return project; }
60
public string FileName {
61
get { return fileName; }
62
set { fileName = value; }
66
/// Gets a WixDialog object for the specified dialog id.
68
public WixDialog CreateWixDialog(string id, ITextFileReader reader)
70
XmlElement dialogElement = GetDialogElement(id);
71
if (dialogElement != null) {
72
return new WixDialog(this, dialogElement, new WixBinaries(this, reader));
77
XmlElement GetDialogElement(string id)
79
string xpath = GetXPath("//w:Dialog[@Id='{0}']", id);
80
return (XmlElement)SelectSingleElement(xpath);
83
string GetXPath(string xpathFormat, string arg)
85
return String.Format(xpathFormat, XmlEncode(arg));
88
string GetXPath(string xpathFormat, string arg1, string arg2)
90
return String.Format(xpathFormat, XmlEncode(arg1), XmlEncode(arg2));
93
string XmlEncode(string item)
95
char quoteChar = '\'';
96
return XmlEncoder.Encode(item, quoteChar);
99
XmlElement SelectSingleElement(string xpath)
101
return (XmlElement)SelectSingleNode(xpath, namespaceManager);
104
public Bitmap LoadBitmapWithId(string id)
106
string bitmapFileName = GetBinaryFileName(id);
107
return LoadBitmapWithFileName(bitmapFileName);
110
public string GetBinaryFileName(string id)
112
string xpath = GetXPath("//w:Binary[@Id='{0}']", id);
113
WixBinaryElement binaryElement = (WixBinaryElement)SelectSingleElement(xpath);
114
if (binaryElement != null) {
115
return binaryElement.GetFileName();
120
public Bitmap LoadBitmapWithFileName(string fileName)
122
if (fileName != null) {
123
return fileLoader.LoadBitmap(fileName);
129
/// Gets a property value defined in the Wix document.
131
public string GetProperty(string name)
133
string xpath = GetXPath("//w:Property[@Id='{0}']", name);
134
XmlElement textStyleElement = SelectSingleElement(xpath);
135
if (textStyleElement != null) {
136
return textStyleElement.InnerText;
141
string IWixPropertyValueProvider.GetValue(string name)
143
if (project != null) {
144
return project.GetPreprocessorVariableValue(name);
150
/// Gets the top SOURCEDIR directory.
152
public WixDirectoryElement GetRootDirectory()
154
string xpath = GetXPath("//w:Product/w:Directory[@Id='{0}']", WixDirectoryElement.RootDirectoryId);
155
return (WixDirectoryElement)SelectSingleElement(xpath);
159
/// Gets a reference to the root directory.
161
public WixDirectoryRefElement GetRootDirectoryRef()
163
string xpath = GetXPath("//w:DirectoryRef[@Id='{0}']", WixDirectoryElement.RootDirectoryId);
164
return (WixDirectoryRefElement)SelectSingleElement(xpath);
167
public bool HasProduct {
168
get { return GetProduct() != null; }
171
public string GetWixNamespacePrefix()
173
XmlElement documentElement = DocumentElement;
174
if (documentElement != null) {
175
return documentElement.GetPrefixOfNamespace(WixNamespaceManager.Namespace);
180
public WixDirectoryElement AddRootDirectory()
182
XmlElement productElement = GetProduct();
183
if (productElement == null) {
184
productElement = AddProduct();
186
return AddRootDirectoryToProduct(productElement);
189
XmlElement AddProduct()
191
XmlElement productElement = CreateWixElement("Product");
192
DocumentElement.AppendChild(productElement);
193
return productElement;
196
WixDirectoryElement AddRootDirectoryToProduct(XmlElement parentProductElement)
198
WixDirectoryElement rootDirectory = WixDirectoryElement.CreateRootDirectory(this);
199
return (WixDirectoryElement)parentProductElement.AppendChild(rootDirectory);
202
public XmlElement CreateWixElement(string name)
204
return CreateElement(GetWixNamespacePrefix(), name, WixNamespaceManager.Namespace);
208
/// Creates custom Wix elements for certain elements such as Directory and File.
210
public override XmlElement CreateElement(string prefix, string localName, string namespaceURI)
212
if (namespaceURI == WixNamespaceManager.Namespace) {
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);
228
return base.CreateElement(prefix, localName, namespaceURI);
232
/// Checks to see if a File element exists with the specified id in this
235
public bool FileIdExists(string id)
237
return ElementIdExists(WixFileElement.FileElementName, id);
240
bool ElementIdExists(string elementName, string id)
242
string xpath = GetXPath("//w:{0}[@Id='{1}']", elementName, id);
243
XmlNodeList nodes = SelectNodes(xpath, new WixNamespaceManager(NameTable));
244
return nodes.Count > 0;
248
/// Checks to see if a Component element exists with the specified id in this
251
public bool ComponentIdExists(string id)
253
return ElementIdExists(WixComponentElement.ComponentElementName, id);
256
public bool DirectoryIdExists(string id)
258
return ElementIdExists(WixDirectoryElement.DirectoryElementName, id);
262
/// Returns the full path based on the location of
263
/// this WixDocument.
265
public string GetFullPath(string relativePath)
267
if (!String.IsNullOrEmpty(fileName)) {
268
string basePath = Path.GetDirectoryName(fileName);
269
return FileUtility.GetAbsolutePath(basePath, relativePath);
275
/// Returns the relative path based on the location of
276
/// this WixDocument.
278
public string GetRelativePath(string fullPath)
280
if (!String.IsNullOrEmpty(fileName)) {
281
string basePath = Path.GetDirectoryName(fileName);
282
return FileUtility.GetRelativePath(basePath, fullPath);
287
public XmlElement GetProduct()
289
return SelectSingleElement("w:Wix/w:Product");
292
public WixBinaryElement[] GetBinaries()
294
List<WixBinaryElement> binaries = new List<WixBinaryElement>();
295
foreach (WixBinaryElement element in SelectNodes("//w:Binary", namespaceManager)) {
296
binaries.Add(element);
298
return binaries.ToArray();