예제 #1
0
        public void Save()
        {
            _projectXmlDocument.Load(GetType().Assembly.GetManifestResourceStream("Ssis2008Emitter.Ssis2008Emitter.Content.DataTransformationsProject.dtproj"));
            if (_projectXmlDocument != null)
            {
                XPathNavigator templateNav = _projectXmlDocument.CreateNavigator().SelectSingleNode("//Project");

                templateNav.SelectSingleNode("Database").AppendChildElement(String.Empty, "Name", String.Empty, "placeholder.database");
                templateNav.SelectSingleNode("Database").AppendChildElement(String.Empty, "FullPath", String.Empty, "placeholder.database");

                foreach (var package in _packages)
                {
                    XPathNavigator packageNav = templateNav.SelectSingleNode("DTSPackages");
                    packageNav.PrependChildElement(String.Empty, "DtsPackage", String.Empty, String.Empty);
                    packageNav.MoveToFirstChild();
                    packageNav.AppendChildElement(String.Empty, "Name", String.Empty, String.Format(CultureInfo.InvariantCulture, "{0}.{1}", package.Name, Resources.ExtensionDTSXPackageFile));
                    packageNav.AppendChildElement(String.Empty, "FullPath", String.Empty, String.Format(CultureInfo.InvariantCulture, "{0}.{1}", package.Name, Resources.ExtensionDTSXPackageFile));
                    packageNav.AppendChildElement(String.Empty, "References", String.Empty, String.Empty);
                }

                XPathNavigator miscFilesNav = templateNav.SelectSingleNode("Miscellaneous");
                foreach (string s in _miscFiles)
                {
                    miscFilesNav.PrependChildElement(String.Empty, "ProjectItem", String.Empty, String.Empty);
                    miscFilesNav.MoveToFirstChild();
                    miscFilesNav.AppendChildElement(String.Empty, "Name", String.Empty, s);
                    miscFilesNav.AppendChildElement(String.Empty, "FullPath", String.Empty, s);
                    miscFilesNav.MoveToParent();
                }

                _projectXmlDocument.Save(_projectPath);
            }
        }
예제 #2
0
        public void Save(string projectSubpath)
        {
            this._projectXmlDocument.Load(
                Settings.Default.DetegoProjectLocationRoot + Path.DirectorySeparatorChar + Settings.Default.SubpathDTProjectTemplateFile
                );
            XPathNavigator templateNav = _projectXmlDocument.CreateNavigator().SelectSingleNode("//Project");

            templateNav.SelectSingleNode("Database").AppendChildElement("", "Name", "", "placeholder");
            templateNav.SelectSingleNode("Database").AppendChildElement("", "FullPath", "", "placeholder");

            XPathNavigator packageNav = templateNav.SelectSingleNode("DTSPackages");

            packageNav.PrependChildElement("", "DtsPackage", "", "");
            packageNav.MoveToFirstChild();
            packageNav.AppendChildElement("", "Name", "", _projectName + Resources.ExtensionDTSXProjectFile);
            packageNav.AppendChildElement("", "FullPath", "", _projectName + Resources.ExtensionDTSXProjectFile);
            packageNav.AppendChildElement("", "References", "", "");

            XPathNavigator miscFilesNav = templateNav.SelectSingleNode("Miscellaneous");

            foreach (string s in _miscFiles)
            {
                miscFilesNav.PrependChildElement("", "ProjectItem", "", "");
                miscFilesNav.MoveToFirstChild();
                miscFilesNav.AppendChildElement("", "Name", "", s);
                miscFilesNav.AppendChildElement("", "FullPath", "", s);
                miscFilesNav.MoveToParent();
            }

            _projectXmlDocument.Save(_getProjectPath(projectSubpath));
        }
예제 #3
0
        public void Save()
        {
            string templatePath = PathManager.GetToolSubpath(Settings.Default.SubpathSsis2008EmitterContent);

            this._projectXmlDocument.Load(PathManager.AddSubpath(templatePath, Settings.Default.SubpathDTProjectTemplateFile));
            XPathNavigator templateNav = _projectXmlDocument.CreateNavigator().SelectSingleNode("//Project");

            templateNav.SelectSingleNode("Database").AppendChildElement("", "Name", "", "placeholder.database");
            templateNav.SelectSingleNode("Database").AppendChildElement("", "FullPath", "", "placeholder.database");

            XPathNavigator packageNav = templateNav.SelectSingleNode("DTSPackages");

            packageNav.PrependChildElement("", "DtsPackage", "", "");
            packageNav.MoveToFirstChild();
            packageNav.AppendChildElement("", "Name", "", String.Format("{0}.{1}", _package.Name, Resources.ExtensionDTSXPackageFile));
            packageNav.AppendChildElement("", "FullPath", "", String.Format("{0}.{1}", _package.Name, Resources.ExtensionDTSXPackageFile));
            packageNav.AppendChildElement("", "References", "", "");

            XPathNavigator miscFilesNav = templateNav.SelectSingleNode("Miscellaneous");

            foreach (string s in _miscFiles)
            {
                miscFilesNav.PrependChildElement("", "ProjectItem", "", "");
                miscFilesNav.MoveToFirstChild();
                miscFilesNav.AppendChildElement("", "Name", "", s);
                miscFilesNav.AppendChildElement("", "FullPath", "", s);
                miscFilesNav.MoveToParent();
            }

            _projectXmlDocument.Save(Path.ChangeExtension(_package.PackagePath, Resources.ExtensionDTProjectFile));
        }
예제 #4
0
        private static void MoveToOrCreateChildNode(XPathNavigator navigator, NamespaceAndName childNode)
        {
            if (navigator == null)
            {
                throw new ArgumentNullException("navigator");
            }
            if (childNode == null)
            {
                throw new ArgumentNullException("childNode");
            }
            if (childNode.Name == null)
            {
                throw new NullReferenceException("childNode.Name");
            }
            if (childNode.Namespace == null)
            {
                throw new NullReferenceException("childNode.Namespace");
            }

            if (MoveToChildNode(navigator, childNode))
            {
                return;
            }

            navigator.AppendChildElement(string.Empty, childNode.Name, childNode.Namespace, string.Empty);
            navigator.MoveToChild(childNode.Name, childNode.Namespace);
        }
예제 #5
0
 public XPathNavigator AppendElement(string name, string namespaceUri, XPathNavigator source)
 {
     name         = XmlConvert.EncodeLocalName(name);
     namespaceUri = GetEffectiveNamespace(namespaceUri);
     source.AppendChildElement(LookupPrefix(namespaceUri), name, namespaceUri, null);
     return(source.SelectSingleNode("*[position()=last()]"));
 }
예제 #6
0
        /// <summary>
        /// Moves the given runsettings file navigator to the DataCollectors node in the runsettings xml.
        /// Throws XmlException if it was unable to find the DataCollectors node.
        /// </summary>
        /// <param name="runSettingsNavigator">XPathNavigator for a runsettings xml document.</param>
        private static void MoveToDataCollectorsNode(ref XPathNavigator runSettingsNavigator)
        {
            runSettingsNavigator.MoveToRoot();
            if (!runSettingsNavigator.MoveToChild("RunSettings", string.Empty))
            {
                throw new XmlException(string.Format(CultureInfo.CurrentCulture, ObjectModelResources.CouldNotFindXmlNode, "RunSettings"));
            }

            if (!runSettingsNavigator.MoveToChild("DataCollectionRunSettings", string.Empty))
            {
                runSettingsNavigator.AppendChildElement(string.Empty, "DataCollectionRunSettings", string.Empty, string.Empty);
                runSettingsNavigator.MoveToChild("DataCollectionRunSettings", string.Empty);
            }

            if (!runSettingsNavigator.MoveToChild("DataCollectors", string.Empty))
            {
                runSettingsNavigator.AppendChildElement(string.Empty, "DataCollectors", string.Empty, string.Empty);
                runSettingsNavigator.MoveToChild("DataCollectors", string.Empty);
            }
        }
예제 #7
0
        public XPathNavigator AppendElement(string name, string namespaceUri, XPathNavigator source)
        {
            string prefix;

            name         = XmlConvert.EncodeLocalName(name);
            namespaceUri = GetEffectiveNamespace(namespaceUri);
            if (IsRootNamespace(namespaceUri, out prefix))
            {
                prefix = CreateNamespace(prefix, namespaceUri, source);
            }
            source.AppendChildElement(prefix ?? LookupPrefix(namespaceUri), name, namespaceUri, null);
            return(source.SelectSingleNode("*[position()=last()]"));
        }
예제 #8
0
    static void XPathNavigatorMethods_AppendChildElement()
    {
        // XPathNavigator.AppendChildElement(string, string, string, string)

        //<snippet5>
        XmlDocument document = new XmlDocument();

        document.Load("contosoBooks.xml");
        XPathNavigator navigator = document.CreateNavigator();

        navigator.MoveToChild("bookstore", "http://www.contoso.com/books");
        navigator.MoveToChild("book", "http://www.contoso.com/books");

        navigator.AppendChildElement(navigator.Prefix, "pages", navigator.LookupNamespace(navigator.Prefix), "100");

        Console.WriteLine(navigator.OuterXml);
        //</snippet5>
    }
예제 #9
0
        public void XPathNavigatorMembers(XPathNavigator nav, IXmlNamespaceResolver nsResolver, XPathExpression pathExpression)
        {
            const string constantPath = "/my/xpath/expression";
            string       path         = "variable path";

            // Should not raise for hard-coded paths
            nav.Compile(constantPath);
            nav.Evaluate("xpath-expression");
            nav.Select(constantPath);
            nav.Matches(FixedPath);

            // Should raise for variable paths
            nav.Compile(path);                              // Noncompliant

            nav.Evaluate(path);                             // Noncompliant
            nav.Evaluate(path, nsResolver);                 // Noncompliant
            nav.Evaluate(pathExpression);                   // Compliant - using path expression objects is ok
            nav.Evaluate(pathExpression, null);

            nav.Matches(path);                              // Noncompliant
            nav.Matches(pathExpression);

            // XPathNavigator selection methods
            nav.Select(path);                               // Noncompliant
            nav.Select(path, nsResolver);                   // Noncompliant
            nav.Select(pathExpression);

            nav.SelectSingleNode(path);                     // Noncompliant
            nav.SelectSingleNode(path, nsResolver);         // Noncompliant
            nav.SelectSingleNode(pathExpression);

            nav.SelectAncestors("name", "uri", false);
            nav.SelectChildren("name", "uri");
            nav.SelectDescendants("name", "uri", false);

            nav.AppendChild("newChild");
            nav.AppendChildElement("prefix", "localName", "uri", "value");
            nav.CheckValidity(null, null);

            var nav2 = nav.CreateNavigator();

            nav2.DeleteRange(nav);
        }
예제 #10
0
 /// <summary>
 /// Saves the collection to XML node.
 /// </summary>
 /// <param name="collection">The collection.</param>
 /// <param name="node">The node.</param>
 public static void SaveCollectionToXmlNode(NameValueCollection collection, IXPathNavigable node)
 {
     if (collection == null)
     {
         throw new ArgumentNullException("collection");
     }
     if (node == null)
     {
         throw new ArgumentNullException("node");
     }
     if (collection.AllKeys.Length > 0)
     {
         XPathNavigator navigator = node.CreateNavigator();
         foreach (string key in collection.AllKeys)
         {
             if (!string.IsNullOrEmpty(key))
             {
                 navigator.AppendChildElement(string.Empty, XmlConvert.EncodeName(key), string.Empty, collection[key]);
             }
         }
     }
 }
예제 #11
0
 private void SaveValue(object value, PropertyDescription property, XPathNavigator nav)
 {
     if (value == null)
     {
         value = "";
     }
     if (property.IsManyToManyRelation)
     {
         foreach (EntityBase e in value as IList)
         {
             nav.AppendChildElement("", "Item", "", e.ID.ToString());
         }
         return;
     }
     else if (property.IsOneToOneRelation)
     {
         value = (value as EntityBase).ID;
     }
     else
     {
         value = PrepareToSave(value);
     }
     nav.SetTypedValue(value);
 }
예제 #12
0
        void ScanPath(String path)
        {
            string[] files = Directory.GetFiles(path, "*.csproj", SearchOption.AllDirectories);
            foreach (String file in files)
            {
                bool        updated = false;
                XmlDocument xd      = new XmlDocument();
                try
                {
                    xd.Load(file);
                }
                catch
                {
                    continue;
                }
                XPathNavigator xn = xd.CreateNavigator();
                xn.MoveToFirst();
                String target_framework = null;
                bool   okay;
                okay = xn.MoveToFirstChild();
                while (xn.NodeType == XPathNodeType.Comment)
                {
                    okay = xn.MoveToNext();
                }
                for (okay = xn.MoveToFirstChild(); okay; okay = xn.MoveToNext())
                {
retry:
                    if (xn.Name == "PropertyGroup")
                    {
                        bool found_outpath     = false;
                        bool found_intermed_1  = false;
                        bool found_intermed_2  = false;
                        bool found_base_output = false;
                        bool found_framework   = false;
                        bool ok2;
                        bool ever_ok2 = false;

                        bool found_condition = false;
                        for (ok2 = xn.MoveToFirstAttribute(); ok2; ok2 = xn.MoveToNextAttribute())
                        {
                            ever_ok2 = true;
                            if (xn.Name == "Condition")
                            {
                                found_condition = true;
                                break;
                            }
                        }
                        if (ever_ok2)
                        {
                            xn.MoveToParent();
                        }

                        ever_ok2 = false;
                        for (ok2 = xn.MoveToFirstChild(); ok2; ok2 = xn.MoveToNext())
                        {
                            ever_ok2 = true;
                            if (xn.Name == "TargetFrameworkVersion")
                            {
                                found_framework  = true;
                                target_framework = xn.Value;
                            }
                            if (xn.Name == "BaseOutputPath")
                            {
                                if (found_base_output || found_condition)
                                {
                                    xn.DeleteSelf();
                                    xn.MoveToFirstChild();
                                    updated           = true;
                                    found_base_output = false;
                                    continue;
                                }
                                found_base_output = true;
                                if (String.Compare(xn.Value, "$(SolutionDir)/", true) != 0)
                                {
                                    xn.SetValue("$(SolutionDir)/");
                                    updated = true;
                                }
                            }
                            if (xn.Name == "OutputPath")
                            {
                                if (found_outpath || !found_condition)
                                {
                                    xn.DeleteSelf();
                                    xn.MoveToFirstChild();
                                    updated       = true;
                                    found_outpath = false;
                                    continue;
                                }
                                String old_value = xn.Value;
                                found_outpath = true;
                                if (String.Compare(old_value, "$(BaseOutputPath)/output/$(Configuration)/$(Platform)/" + target_framework, true) != 0)
                                {
                                    xn.SetValue("$(BaseOutputPath)/output/$(Configuration)/$(Platform)/" + target_framework);
                                    updated = true;
                                }
                            }
                            if (xn.Name == "BaseIntermediateOutputPath")
                            {
                                if (found_intermed_1 || found_condition)
                                {
                                    xn.DeleteSelf();
                                    xn.MoveToFirstChild();
                                    updated          = true;
                                    found_intermed_1 = false;

                                    continue;
                                }
                                String old_value = xn.Value;
                                found_intermed_1 = true;
                                if (String.Compare(old_value, "$(SolutionDir)/output/", true) != 0)
                                {
                                    xn.SetValue("$(SolutionDir)/output/");
                                    updated = true;
                                }
                            }
                            if (xn.Name == "IntermediateOutputPath")
                            {
                                if (found_intermed_2 || !found_condition)
                                {
                                    xn.DeleteSelf();
                                    xn.MoveToFirstChild();
                                    updated          = true;
                                    found_intermed_2 = false;
                                    continue;
                                }
                                String new_value = "$(BaseIntermediateOutputPath)obj/$(MSBuildProjectName)/$(Configuration)/$(Platform)/" + target_framework;
                                String old_value = xn.Value;
                                found_intermed_2 = true;
                                if (String.Compare(old_value, new_value, true) != 0)
                                {
                                    xn.SetValue(new_value);
                                    updated = true;
                                }
                            }
                        }
                        if (ever_ok2)
                        {
                            xn.MoveToParent();
                        }
                        if (!found_base_output && found_framework && !found_condition)
                        {
                            xn.AppendChildElement(null, "BaseOutputPath", null, "$(SolutionDir)/");
                            updated = true;
                        }
                        if (!found_outpath && found_condition)
                        {
                            xn.AppendChildElement(null, "OutputPath", null, "$(BaseOutputPath)/output/$(Configuration)/$(Platform)/" + target_framework);
                            updated = true;
                        }
                        if (!found_intermed_1 && !found_condition)
                        {
                            xn.AppendChildElement(null, "BaseIntermediateOutputPath", null, "$(SolutionDir)/output/");
                            updated = true;
                        }
                        if (!found_intermed_2 && found_condition)
                        {
                            String new_value = "$(BaseIntermediateOutputPath)obj/$(MSBuildProjectName)/$(Configuration)/$(Platform)/" + target_framework;
                            xn.AppendChildElement(null, "IntermediateOutputPath", null, new_value);
                            updated = true;
                        }
                    }
                }
                if (updated)
                {
                    listBox1.Items.Add(file);
                    if (!File.Exists(file + ".backup"))
                    {
                        File.Copy(file, file + ".backup");
                    }
                    xd.Save(file);
                }
                //else
                //    listBox1.Items.Add( "Ignore:"+file );
                listBox1.Refresh();
            }
        }
예제 #13
0
        static void ScanPath( String path )
        {
            string ] files = Directory.GetFiles( path, ".vcxproj", SearchOption.AllDirectories );
            foreach( String file in files )
            {

                bool updated = false;
                if( file.Contains( "INSTALL.vcxproj" ) )
                    continue;
                if( file.Contains( "ZERO_CHECK.vcxproj" ) )
                    continue;
                if( file.Contains( "ALL_BUILD.vcxproj" ) )
                    continue;
                if( file.Contains( "PACKAGE.vcxproj" ) )
                    continue;
                if( file.Contains( "bag.externals.vcxproj" ) )
                    continue;
                XmlDocument xd = new XmlDocument();
                try
                {
                    xd.Load( file );
                }
                catch
                {
                    continue;
                }
                XPathNavigator xn = xd.CreateNavigator();
                xn.MoveToFirst();
                String target_framework = null;
                bool okay;
                bool found_project_import = false;
                bool found_target = false;
                okay = xn.MoveToFirstChild();
                while( xn.NodeType == XPathNodeType.Comment )
                    okay = xn.MoveToNext();
                for( okay = xn.MoveToFirstChild(); okay; okay = xn.MoveToNext() )
                {
                    retry:
                    if( xn.Name == "Import" )
                    {
                        bool ok2;
                        bool ever_ok2 = false;

                        bool found_condition = false;
                        for( ok2 = xn.MoveToFirstAttribute(); ok2; ok2 = xn.MoveToNextAttribute() )
                        {
                            ever_ok2 = true;
                            if( xn.Name == "Project" && xn.Value == "$(VCTargetsPath)\\Microsoft.Cpp.targets" )
                            {
                                found_project_import = true;
                                found_target = false;
                            }
                        }
                        if( ever_ok2 )
                        {
                            xn.MoveToParent();
                        }
                    }
                    else if( xn.Name == "Target" )
                    {
                        found_target = true;
                    }
                    else if( xn.Name == "ImportGroup" )
                    {
                        if( found_project_import && !found_target )
                        {
                            xn.InsertElementBefore( null, "Target", null, null );
                            xn.MoveToPrevious();
                            xn.CreateAttribute( null, "Name", null, "GenerateTargetFrameworkMonikerAttribute" );
                            xn.MoveToNext();
                            updated = true;
                        }
                    }
                    else if( xn.Name == "PropertyGroup" )
                    {
                        bool found_outpath = false;
                        bool found_intermed_1 = false;
                        bool found_intermed_2 = false;
                        bool found_base_output = false;
                        bool found_framework = false;
                        bool ok2;
                        bool ever_ok2 = false;

                        bool found_condition = false;
                        for( ok2 = xn.MoveToFirstAttribute(); ok2; ok2 = xn.MoveToNextAttribute() )
                        {
                            ever_ok2 = true;
                            if( xn.Name == "Label" )
                            {
                                if( xn.Value == "Globals" )
                                    found_condition = true;
                                break;
                            }
                        }
                        if( ever_ok2 )
                            xn.MoveToParent();

                        bool found_support = false;
                        bool found_version = false;
                        ever_ok2 = false;
                        for( ok2 = xn.MoveToFirstChild(); ok2; ok2 = xn.MoveToNext() )
                        {
                            ever_ok2 = true;
                            if( xn.Name == "TargetFrameworkVersion" )
                            {
                                found_framework = true;
                                if( found_condition )
                                {
                                    xn.SetValue( "v4.0" );
                                    updated = true;
                                }
                            }
                            if( xn.Name == "CLRSupport" )
                            {
                                found_support = true;
                                if( found_condition )
                                {
                                    xn.SetValue( "true" );
                                    updated = true;
                                }
                                else
                                {
                                    xn.DeleteSelf();
                                    xn.MoveToFirstChild();
                                }
                            }
                            //    < TargetFrameworkVersion Condition = "'$(TargetFrameworkVersion)'==''" > v2.0 </ TargetFrameworkVersion >
                        }
                        if( ever_ok2 )
                        {
                            xn.MoveToParent();
                            if( found_condition )
                            {
                                if( !found_support )
                                {
                                    xn.AppendChildElement( null, "CLRSupport", null, "true" );
                                    updated = true;
                                }
                                if( !found_framework )
                                {
                                    xn.AppendChildElement( null, "TargetFrameworkVersion", null, "v4.0" );
                                    updated = true;
                                }
                            }
                        }
                        found_condition = false;

                    }
                }
                if( updated )
                {
                    //listBox1.Items.Add( file );
                    if( !File.Exists( file + ".backup" ) )
                        File.Copy( file, file + ".backup" );
                    xd.Save( file );
                }
                //else
                //    listBox1.Items.Add( "Ignore:"+file );
                //listBox1.Refresh();

            }

        }
예제 #14
0
        private void MakeNavigationFile(
            ContentURI uri, string packageFilePathName,
            IDictionary <string, string> args, ref string contentNewPath,
            ref string errorMsg)
        {
            string sPackagePartValue = Path.GetFileName(packageFilePathName);
            string sPartFullPath     = string.Empty;
            string sRelPartPath      = string.Empty;
            string sMimeType         = string.Empty;
            //need an editable navigator
            XmlDocument xmlNavigationDoc = new XmlDocument();
            XmlReader   reader
                = Helpers.FileStorageIO.GetXmlReader(uri, contentNewPath);

            if (reader != null)
            {
                using (reader)
                {
                    xmlNavigationDoc.Load(reader);
                }
                XPathNavigator navNavigationDoc = xmlNavigationDoc.CreateNavigator();
                //move to ncx
                navNavigationDoc.MoveToFirstChild();
                //move to navMap
                navNavigationDoc.MoveToFirstChild();
                //move to navPoint
                bool bHasMoved = navNavigationDoc.MoveToFirstChild();
                if (bHasMoved)
                {
                    string sKey   = string.Empty;
                    string sValue = string.Empty;
                    int    j      = 2;
                    foreach (KeyValuePair <string, string> kvp in args)
                    {
                        sKey   = kvp.Key;
                        sValue = kvp.Value;
                        if (sValue == sPackagePartValue)
                        {
                            if (string.IsNullOrEmpty(sKey) == false)
                            {
                                //clients use same relative paths as server
                                //convert the absolute path of partFullPath into a path relative to the package root (getcurrentdirectory)
                                sRelPartPath = AppSettings.ConvertAbsPathToRelPath(CurrentDirectory, sKey);
                                navNavigationDoc.InsertElementAfter(string.Empty, "navPoint", string.Empty, string.Empty);
                                navNavigationDoc.MoveToNext();
                                navNavigationDoc.CreateAttribute(string.Empty, "id", string.Empty, j.ToString());
                                navNavigationDoc.CreateAttribute(string.Empty, "playOrder", string.Empty, j.ToString());
                                navNavigationDoc.AppendChildElement(string.Empty, "navLabel", string.Empty, string.Empty);
                                navNavigationDoc.MoveToChild(XPathNodeType.Element);
                                navNavigationDoc.AppendChildElement(string.Empty, "text", string.Empty, Path.GetFileNameWithoutExtension(sRelPartPath));
                                navNavigationDoc.InsertElementAfter(string.Empty, "content", string.Empty, string.Empty);
                                navNavigationDoc.MoveToNext();
                                navNavigationDoc.CreateAttribute(string.Empty, "src", string.Empty, sRelPartPath);
                                //move to navPoint
                                navNavigationDoc.MoveToParent();
                                j++;
                            }
                        }
                    }
                }
                string sTocNewPath = Path.Combine(Path.GetDirectoryName(contentNewPath), NCX_FILE);
                Helpers.FileStorageIO.MoveURIs(uri, contentNewPath, sTocNewPath);
                contentNewPath = sTocNewPath;
            }
        }
예제 #15
0
        /// <summary>
        /// Kept separate so get good stack trace in case of an exception
        /// </summary>
        private void SetupForTraceImpl()
        {
            this.configFileState = File.Exists(this.configFileName) ? ConfigFileState.Existed : ConfigFileState.Missing;

            XmlDocument config = new XmlDocument();

            if (this.configFileState == ConfigFileState.Existed)
            {
                config.Load(this.configFileName);
            }

            XPathNavigator configNav = config.CreateNavigator();

            // Add <configuration> element if not already present
            XPathNavigator configurationElementNav = configNav.SelectSingleNode("/configuration");

            if (configurationElementNav == null)
            {
                configNav.AppendChildElement(null, "configuration", null, null);
                configurationElementNav = configNav.SelectSingleNode("/configuration");
            }

            // Add system.diagnostics section, replace any existing section
            XPathNavigator systemDiagnosticsElementNav = configNav.SelectSingleNode("/configuration/system.diagnostics");

            if (systemDiagnosticsElementNav != null)
            {
                systemDiagnosticsElementNav.ReplaceSelf(DiagnosticsSectionTemplate.Replace("#LOGFILE#", this.traceFileName));
            }
            else
            {
                configurationElementNav.PrependChild(DiagnosticsSectionTemplate.Replace("#LOGFILE#", this.traceFileName));
            }

            // Add <system.serviceModel> element if not already present
            XPathNavigator serviceModelNav = configNav.SelectSingleNode("/configuration/system.serviceModel");

            if (serviceModelNav == null)
            {
                configurationElementNav.AppendChildElement(null, "system.serviceModel", null, null);
                serviceModelNav = configNav.SelectSingleNode("/configuration/system.serviceModel");
            }

            // Add service model diagnostics section, replace any existing section
            XPathNavigator serviceModelDiagnosticsElementNav = configNav.SelectSingleNode("/configuration/system.serviceModel/diagnostics");

            if (serviceModelDiagnosticsElementNav != null)
            {
                serviceModelDiagnosticsElementNav.ReplaceSelf(ServiceModelSection);
            }
            else
            {
                serviceModelNav.PrependChild(ServiceModelSection);
            }

            // Write the new config file
            try
            {
                if (this.configFileState == ConfigFileState.Existed)
                {
                    this.renamedConfigFileName = this.configFileName + "._wcfunit";
                    File.Move(this.configFileName, this.renamedConfigFileName);
                    this.configFileState = ConfigFileState.Moved;
                    Trace.WriteLine(string.Format(CultureInfo.InvariantCulture, "Renamed application config file for the program under test to {0}", this.renamedConfigFileName));
                }

                XmlWriterSettings settings = new XmlWriterSettings();
                settings.Indent      = true;
                settings.CloseOutput = true;
                settings.Encoding    = Encoding.UTF8;
                using (XmlWriter xw = XmlWriter.Create(this.configFileName, settings))
                {
                    config.WriteTo(xw);
                }
            }
            catch (UnauthorizedAccessException uae)
            {
                if (this.configFileState == ConfigFileState.Moved)
                {
                    File.Move(this.renamedConfigFileName, this.configFileName);
                    this.configFileState = ConfigFileState.Unknown;
                }

                throw new UserException(Messages.SRMAccessDenied, uae);
            }
            catch (Exception)
            {
                // Make sure we put things back if we can whatever happens
                if (this.configFileState == ConfigFileState.Moved)
                {
                    File.Move(this.renamedConfigFileName, this.configFileName);
                    this.configFileState = ConfigFileState.Unknown;
                }

                throw;
            }
        }