Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/tools/ConfigMerger/ConfigMerger.cs @ 2692

Last change on this file since 2692 was 2692, checked in by gkronber, 15 years ago

Fixed #855 (Configuration for SQL Compact DbProviderFactory should be merged from a local app.config for the SqlServerCompact assembly)

File size: 6.5 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2008 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
4 *
5 * This file is part of HeuristicLab.
6 *
7 * HeuristicLab is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * HeuristicLab is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
19 */
20#endregion
21
22using System;
23using System.Collections.Generic;
24using System.Linq;
25using System.Text;
26using System.Xml;
27
28namespace HeuristicLab.Tools.ConfigMerger {
29  public class ConfigMerger {
30    public static void Main(string[] args) {
31      try {
32        Merge(args[0], args[1]);
33      }
34      catch (Exception ex) {
35        Console.Out.WriteLine(BuildErrorMessage(ex));
36      }
37    }
38
39    public static void Merge(string sourceFile, string destinationFile) {
40      XmlDocument source = new XmlDocument();
41      source.Load(sourceFile);
42      XmlDocument destination = new XmlDocument();
43      destination.Load(destinationFile);
44
45      XmlNode sourceNode;
46      XmlNode destinationNode;
47
48      sourceNode = source.SelectSingleNode("/configuration/system.serviceModel/behaviors");
49      destinationNode = destination.SelectSingleNode("/configuration/system.serviceModel/behaviors");
50      Merge(sourceNode, destinationNode, destination, "/configuration/system.serviceModel");
51
52      sourceNode = source.SelectSingleNode("/configuration/system.serviceModel/services");
53      destinationNode = destination.SelectSingleNode("/configuration/system.serviceModel/services");
54      Merge(sourceNode, destinationNode, destination, "/configuration/system.serviceModel");
55
56      sourceNode = source.SelectSingleNode("/configuration/system.serviceModel/bindings");
57      destinationNode = destination.SelectSingleNode("/configuration/system.serviceModel/bindings");
58      Merge(sourceNode, destinationNode, destination, "/configuration/system.serviceModel");
59
60      sourceNode = source.SelectSingleNode("/configuration/system.serviceModel/client");
61      destinationNode = destination.SelectSingleNode("/configuration/system.serviceModel/client");
62      Merge(sourceNode, destinationNode, destination, "/configuration/system.serviceModel");
63
64      sourceNode = source.SelectSingleNode("/configuration/configSections/sectionGroup[@name='applicationSettings']");
65      destinationNode = destination.SelectSingleNode("/configuration/configSections/sectionGroup[@name='applicationSettings']");
66      Merge(sourceNode, destinationNode, destination, "/configuration/configSections");
67
68      sourceNode = source.SelectSingleNode("/configuration/configSections/sectionGroup[@name='userSettings']");
69      destinationNode = destination.SelectSingleNode("/configuration/configSections/sectionGroup[@name='userSettings']");
70      Merge(sourceNode, destinationNode, destination, "/configuration/configSections");
71
72      sourceNode = source.SelectSingleNode("/configuration/applicationSettings");
73      destinationNode = destination.SelectSingleNode("/configuration/applicationSettings");
74      Merge(sourceNode, destinationNode, destination, "/configuration");
75
76      sourceNode = source.SelectSingleNode("/configuration/userSettings");
77      destinationNode = destination.SelectSingleNode("/configuration/userSettings");
78      Merge(sourceNode, destinationNode, destination, "/configuration");
79
80      sourceNode = source.SelectSingleNode("/configuration/connectionStrings");
81      destinationNode = destination.SelectSingleNode("/configuration/connectionStrings");
82      Merge(sourceNode, destinationNode, destination, "/configuration");
83
84      sourceNode = source.SelectSingleNode("/configuration/system.data/DbProviderFactories");
85      destinationNode = destination.SelectSingleNode("/configuration/system.data/DbProviderFactories");
86      Merge(sourceNode, destinationNode, destination, "/configuration");
87
88      destination.Save(destinationFile);
89    }
90
91    private static void Merge(XmlNode source, XmlNode destination, XmlDocument document, string root) {
92      try {
93        if (source != null) {
94          if (destination == null) {
95            XmlNode newNode = document.ImportNode(source, true);
96            document.SelectSingleNode(root).AppendChild(newNode);
97          } else {
98            foreach (XmlNode node in source.ChildNodes) {
99              XmlNode newNode = document.ImportNode(node, true);
100              XmlNode oldNode = destination.SelectSingleNode(BuildXPathString(newNode));
101              if (oldNode != null)
102                destination.ReplaceChild(newNode, oldNode);
103              else
104                destination.AppendChild(newNode);
105            }
106          }
107        }
108      }
109      catch (Exception ex) {
110        StringBuilder sb = new StringBuilder();
111        sb.Append("Error while merging node \"").Append(source.Name).Append("\"");
112        throw new Exception(sb.ToString(), ex);
113      }
114    }
115
116    private static string BuildXPathString(XmlNode node) {
117      StringBuilder builder = new StringBuilder();
118      builder.Append(node.Name);
119      if (node.Attributes.Count > 0) {
120        XmlAttribute attrib = node.Attributes[0];
121        builder.Append("[");
122        builder.Append("@" + attrib.Name + "='" + attrib.Value + "'");
123        for (int i = 1; i < node.Attributes.Count; i++) {
124          attrib = node.Attributes[i];
125          builder.Append(" and @" + attrib.Name + "='" + attrib.Value + "'");
126        }
127        builder.Append("]");
128      }
129      return builder.ToString();
130    }
131
132    private static string BuildErrorMessage(Exception ex) {
133      StringBuilder sb = new StringBuilder();
134      sb.Append("\n\n");
135      sb.Append("### ConfigMerger ERROR ###########################################\n" + ex.Message + "\n" + ex.StackTrace + "\n");
136      while (ex.InnerException != null) {
137        ex = ex.InnerException;
138        sb.Append("-----\n" + ex.Message + "\n" + ex.StackTrace + "\n");
139      }
140      sb.Append("##################################################################\n\n");
141      return sb.ToString();
142    }
143  }
144}
Note: See TracBrowser for help on using the repository browser.