コード例 #1
0
ファイル: WixFiles.cs プロジェクト: wtf3505/WixEdit
        public void Save()
        {
            if (!IsNew && ReadOnly())
            {
                MessageBox.Show(String.Format("\"{0}\" is read-only, cannot save this file.", wxsFile.Name), "Read Only!", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }

            if (wxsWatcher != null)
            {
                wxsWatcher.EnableRaisingEvents = false;
            }

            UndoManager.DeregisterHandlers();

            XmlComment commentElement = null;

            if (projectSettings.IsEmpty() == false)
            {
                StringBuilder commentBuilder = new StringBuilder();
                commentBuilder.Append("\r\n");
                commentBuilder.Append("    # This comment is generated by WixEdit, the specific commandline\r\n");
                commentBuilder.Append("    # arguments for the WiX Toolset are stored here.\r\n\r\n");
                commentBuilder.AppendFormat("    candleArgs: {0}\r\n", projectSettings.CandleArgs);
                commentBuilder.AppendFormat("    lightArgs: {0}\r\n", projectSettings.LightArgs);

                commentElement = wxsDocument.CreateComment(commentBuilder.ToString());

                XmlNode firstElement = wxsDocument.FirstChild;
                if (firstElement != null)
                {
                    if (firstElement.NodeType == XmlNodeType.XmlDeclaration)
                    {
                        firstElement = wxsDocument.FirstChild.NextSibling;
                    }

                    wxsDocument.InsertBefore(commentElement, firstElement);
                }
                else
                {
                    wxsDocument.AppendChild(commentElement);
                }
            }

            // Handle extension namespaces. Remove all those which are not used in the file.
            foreach (string ext in xsdExtensionNames)
            {
                string theNodeNamespace = LookupExtensionNameReverse(ext);

                XmlNodeList list = wxsDocument.SelectNodes(String.Format("//{0}:*", theNodeNamespace), wxsNsmgr);
                if (list.Count == 0)
                {
                    // Error reports show that a NullReferenceException occurs on the next line now, how can this be?
                    // The wxsDocument.DocumentElement is null.
                    wxsDocument.DocumentElement.RemoveAttribute(String.Format("xmlns:{0}", theNodeNamespace));
                }
            }

            if (IncludeManager.HasIncludes)
            {
                IncludeManager.RemoveIncludes();

                ArrayList changedIncludes = UndoManager.ChangedIncludes;
                if (changedIncludes.Count > 0)
                {
                    string filesString = String.Join("\r\n\x2022 ", changedIncludes.ToArray(typeof(string)) as string[]);
                    if (DialogResult.Yes == MessageBox.Show(String.Format("Do you want to save the following changed include files?\r\n\r\n\x2022 {0}", filesString), "Save?", MessageBoxButtons.YesNo, MessageBoxIcon.Question))
                    {
                        IncludeManager.SaveIncludes(UndoManager.ChangedIncludes);
                    }
                }
            }

            FileMode mode = FileMode.OpenOrCreate;

            if (File.Exists(wxsFile.FullName))
            {
                mode = mode | FileMode.Truncate;
            }

            using (FileStream fs = new FileStream(wxsFile.FullName, mode))
            {
                XmlWriterSettings settings = new XmlWriterSettings();
                settings.Indent          = true;
                settings.IndentChars     = new string(' ', WixEditSettings.Instance.XmlIndentation);
                settings.NewLineHandling = NewLineHandling.None;
                settings.Encoding        = new System.Text.UTF8Encoding();
                XmlWriter writer = XmlWriter.Create(fs, settings);

                wxsDocument.Save(writer);

                writer.Close();
                fs.Close();
            }

            if (IncludeManager.HasIncludes)
            {
                // Remove nodes from main xml document
                IncludeManager.RestoreIncludes();
            }

            projectSettings.ChangesHasBeenSaved();

            if (commentElement != null)
            {
                wxsDocument.RemoveChild(commentElement);
            }

            if (wxsWatcher != null)
            {
                wxsWatcher.EnableRaisingEvents = true;
            }

            undoManager.DocumentIsSaved();
            UndoManager.RegisterHandlers();
        }
コード例 #2
0
ファイル: WixFiles.cs プロジェクト: wtf3505/WixEdit
        public void LoadNewWxsFile(string xml)
        {
            wxsFile       = new FileInfo(@"c:\foo.bar.baz");
            isTempNewFile = true;

            if (wxsDocument == null)
            {
                wxsDocument = new XmlDocument();
                wxsDocument.PreserveWhitespace = true;
            }

            wxsDocument.LoadXml(xml);

            if (wxsDocument.DocumentElement.GetAttribute("xmlns").ToLower() != WixNamespaceUri.ToLower())
            {
                string errorMessage = String.Format("\"{0}\" has the wrong namespace!\r\n\r\nFound namespace \"{1}\",\r\nbut WiX binaries version \"{2}\" require \"{3}\".\r\n\r\nYou can either convert the WiX source file to use the correct namespace (use WixCop.exe for upgrading from 2.0 to 3.0), or configure the correct version of the WiX binaries in the WixEdit settings.", wxsFile.Name, wxsDocument.DocumentElement.GetAttribute("xmlns"), WixEditSettings.Instance.WixBinariesVersion, WixNamespaceUri);
                throw new WixEditException(errorMessage);
            }

            XmlNode possibleComment = wxsDocument.FirstChild;

            if (possibleComment.NodeType == XmlNodeType.XmlDeclaration)
            {
                possibleComment = wxsDocument.FirstChild.NextSibling;
            }
            if (possibleComment != null && possibleComment.Name == "#comment")
            {
                string comment = possibleComment.Value;

                string candleArgs = String.Empty;
                string lightArgs  = String.Empty;
                bool   foundArg   = false;
                foreach (string fullLine in comment.Split('\r', '\n'))
                {
                    string line = fullLine.Trim();
                    if (line.Length == 0)
                    {
                        continue;
                    }

                    string candleStart = "candleargs:";
                    if (line.ToLower().StartsWith(candleStart))
                    {
                        candleArgs = line.Remove(0, candleStart.Length);
                        foundArg   = true;
                    }

                    string lightStart = "lightargs:";
                    if (line.ToLower().StartsWith(lightStart))
                    {
                        lightArgs = line.Remove(0, lightStart.Length);
                        foundArg  = true;
                    }
                }

                if (foundArg == true)
                {
                    wxsDocument.RemoveChild(possibleComment);
                }

                projectSettings = new ProjectSettings(candleArgs.Trim(), lightArgs.Trim());
            }
            else
            {
                projectSettings = new ProjectSettings(String.Empty, String.Empty);
            }

            xsdExtensionPrefixesMap        = new Hashtable();
            xsdExtensionPrefixesReverseMap = new Hashtable();
            foreach (XmlAttribute att in wxsDocument.DocumentElement.Attributes)
            {
                string attName = att.Name;
                if (attName.StartsWith("xmlns:"))
                {
                    if (xsdExtensionTargetNamespacesReverseMap.ContainsKey(att.Value))
                    {
                        string existingNamespaceName = (string)xsdExtensionTargetNamespacesReverseMap[att.Value];
                        string namespaceName         = attName.Substring(6);
                        if (namespaceName != existingNamespaceName)
                        {
                            xsdExtensionPrefixesMap.Add(namespaceName, existingNamespaceName);
                            xsdExtensionPrefixesReverseMap.Add(existingNamespaceName, namespaceName);
                        }
                    }
                }
            }

            foreach (DictionaryEntry entry in xsdExtensionTargetNamespaces)
            {
                if (xsdExtensionPrefixesMap.ContainsValue(entry.Key) == false)
                {
                    xsdExtensionPrefixesMap.Add(entry.Key, entry.Key);
                    xsdExtensionPrefixesReverseMap.Add(entry.Key, entry.Key);

                    wxsDocument.DocumentElement.SetAttribute("xmlns:" + (string)entry.Key, (string)entry.Value);
                }
            }

            wxsDocument.LoadXml(wxsDocument.OuterXml);

            wxsNsmgr = new XmlNamespaceManager(wxsDocument.NameTable);
            wxsNsmgr.AddNamespace("wix", wxsDocument.DocumentElement.NamespaceURI);
            foreach (DictionaryEntry entry in xsdExtensionTargetNamespaces)
            {
                wxsNsmgr.AddNamespace(LookupExtensionNameReverse((string)entry.Key), (string)entry.Value);
            }

            //init define manager to allow include manager to add dynamic includes
            defineManager = new DefineManager(this, wxsDocument);

            // Init IncludeManager after all doc.LoadXml(doc.OuterXml), because all references to nodes would dissapear!
            includeManager = new IncludeManager(this, wxsDocument);

            //re-init define manager using final includes
            defineManager = new DefineManager(this, wxsDocument);
        }
コード例 #3
0
ファイル: WixFiles.cs プロジェクト: xwiz/WixEdit
        public void LoadWxsFile()
        {
            if (wxsDocument == null)
            {
                wxsDocument = new XmlDocument();
            }

            FileMode mode = FileMode.Open;
            using (FileStream fs = new FileStream(wxsFile.FullName, mode, FileAccess.Read, FileShare.Read))
            {
                wxsDocument.Load(fs);
                fs.Close();
            }

            if (wxsDocument.DocumentElement.GetAttribute("xmlns").ToLower() != WixNamespaceUri.ToLower())
            {
                string errorMessage = String.Format("\"{0}\" has the wrong namespace!\r\n\r\nFound namespace \"{1}\",\r\nbut WiX binaries version \"{2}\" require \"{3}\".\r\n\r\nYou can either convert the WiX source file to use the correct namespace (use WixCop.exe for upgrading from 2.0 to 3.0), or configure the correct version of the WiX binaries in the WixEdit settings.", wxsFile.Name, wxsDocument.DocumentElement.GetAttribute("xmlns"), WixEditSettings.Instance.WixBinariesVersion, WixNamespaceUri);
                throw new WixEditException(errorMessage);
            }

            if (ReadOnly())
            {
                MessageBox.Show(String.Format("\"{0}\" is read-only.", wxsFile.Name), "Read Only!", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }

            XmlNode possibleComment = wxsDocument.FirstChild;
            if (possibleComment.NodeType == XmlNodeType.XmlDeclaration)
            {
                possibleComment = wxsDocument.FirstChild.NextSibling;
            }
            if (possibleComment != null && possibleComment.Name == "#comment")
            {
                string comment = possibleComment.Value;

                string candleArgs = String.Empty;
                string lightArgs = String.Empty;
                bool foundArg = false;
                foreach (string fullLine in comment.Split('\r', '\n'))
                {
                    string line = fullLine.Trim();
                    if (line.Length == 0)
                    {
                        continue;
                    }

                    string candleStart = "candleargs:";
                    if (line.ToLower().StartsWith(candleStart))
                    {
                        candleArgs = line.Remove(0, candleStart.Length);
                        foundArg = true;
                    }

                    string lightStart = "lightargs:";
                    if (line.ToLower().StartsWith(lightStart))
                    {
                        lightArgs = line.Remove(0, lightStart.Length);
                        foundArg = true;
                    }
                }

                if (foundArg == true)
                {
                    wxsDocument.RemoveChild(possibleComment);
                }

                projectSettings = new ProjectSettings(candleArgs.Trim(), lightArgs.Trim());
            }
            else
            {
                projectSettings = new ProjectSettings(String.Empty, String.Empty);
            }

            xsdExtensionPrefixesMap = new Hashtable();
            xsdExtensionPrefixesReverseMap = new Hashtable();
            foreach (XmlAttribute att in wxsDocument.DocumentElement.Attributes)
            {
                string attName = att.Name;
                if (attName.StartsWith("xmlns:"))
                {
                    if (xsdExtensionTargetNamespacesReverseMap.ContainsKey(att.Value))
                    {
                        string existingNamespaceName = (string)xsdExtensionTargetNamespacesReverseMap[att.Value];
                        string namespaceName = attName.Substring(6);
                        if (namespaceName != existingNamespaceName)
                        {
                            xsdExtensionPrefixesMap.Add(namespaceName, existingNamespaceName);
                            xsdExtensionPrefixesReverseMap.Add(existingNamespaceName, namespaceName);
                        }
                    }
                }
            }

            foreach (DictionaryEntry entry in xsdExtensionTargetNamespaces)
            {
                if (xsdExtensionPrefixesMap.ContainsValue(entry.Key) == false)
                {
                    xsdExtensionPrefixesMap.Add(entry.Key, entry.Key);
                    xsdExtensionPrefixesReverseMap.Add(entry.Key, entry.Key);

                    wxsDocument.DocumentElement.SetAttribute("xmlns:" + (string)entry.Key, (string)entry.Value);
                }
            }

            wxsDocument.LoadXml(wxsDocument.OuterXml);

            wxsNsmgr = new XmlNamespaceManager(wxsDocument.NameTable);
            wxsNsmgr.AddNamespace("wix", wxsDocument.DocumentElement.NamespaceURI);
            foreach (DictionaryEntry entry in xsdExtensionTargetNamespaces)
            {
                wxsNsmgr.AddNamespace(LookupExtensionNameReverse((string)entry.Key), (string)entry.Value);
            }

            //init define manager to allow include manager to add dynamic includes
            defineManager = new DefineManager(this, wxsDocument);

            // Init IncludeManager after all doc.LoadXml(doc.OuterXml), because all references to nodes would dissapear!
            includeManager = new IncludeManager(this, wxsDocument);

            //re-init define manager using final includes
            defineManager = new DefineManager(this, wxsDocument);
        }