Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 5469 was 5163, checked in by swagner, 14 years ago

Reintegrated .NET 4.0 migration branch (#1350)

File size: 14.2 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      Console.Out.WriteLine("ConfigMerger: Merge started ...");
41      Console.Out.WriteLine("ConfigMerger: Merge source:      \"" + sourceFile + "\"");
42      Console.Out.WriteLine("ConfigMerger: Merge destination: \"" + destinationFile + "\"");
43
44      XmlDocument source = new XmlDocument();
45      source.Load(sourceFile);
46      XmlDocument destination = new XmlDocument();
47      destination.Load(destinationFile);
48
49      XmlNode sourceNode;
50      XmlNode destinationNode;
51
52      #region Merge 'system.serviceModel/behaviors/*'
53      destinationNode = destination.SelectSingleNode("/configuration/system.serviceModel/behaviors");
54      if (destinationNode == null) {
55        destinationNode = destination.CreateElement("behaviors");
56        destination.SelectSingleNode("/configuration/system.serviceModel").AppendChild(destinationNode);
57      }
58
59      sourceNode = source.SelectSingleNode("/configuration/system.serviceModel/behaviors/endpointBehaviors");
60      destinationNode = destination.SelectSingleNode("/configuration/system.serviceModel/behaviors/endpointBehaviors");
61      Merge(sourceNode, destinationNode, destination, "/configuration/system.serviceModel/behaviors");
62
63      sourceNode = source.SelectSingleNode("/configuration/system.serviceModel/behaviors/serviceBehaviors");
64      destinationNode = destination.SelectSingleNode("/configuration/system.serviceModel/behaviors/serviceBehaviors");
65      Merge(sourceNode, destinationNode, destination, "/configuration/system.serviceModel/behaviors");
66      #endregion
67
68      sourceNode = source.SelectSingleNode("/configuration/system.serviceModel/services");
69      destinationNode = destination.SelectSingleNode("/configuration/system.serviceModel/services");
70      Merge(sourceNode, destinationNode, destination, "/configuration/system.serviceModel");
71     
72      #region Merge 'system.serviceModel/bindings/*'
73      destinationNode = destination.SelectSingleNode("/configuration/system.serviceModel/bindings");
74      if (destinationNode == null) {
75        destinationNode = destination.CreateElement("bindings");
76        destination.SelectSingleNode("/configuration/system.serviceModel").AppendChild(destinationNode);
77      }
78      sourceNode = source.SelectSingleNode("/configuration/system.serviceModel/bindings/basicHttpBinding");
79      destinationNode = destination.SelectSingleNode("/configuration/system.serviceModel/bindings/basicHttpBinding");
80      Merge(sourceNode, destinationNode, destination, "/configuration/system.serviceModel/bindings");
81
82      sourceNode = source.SelectSingleNode("/configuration/system.serviceModel/bindings/basicHttpContextBinding");
83      destinationNode = destination.SelectSingleNode("/configuration/system.serviceModel/bindings/basicHttpContextBinding");
84      Merge(sourceNode, destinationNode, destination, "/configuration/system.serviceModel/bindings");
85
86      sourceNode = source.SelectSingleNode("/configuration/system.serviceModel/bindings/customBinding");
87      destinationNode = destination.SelectSingleNode("/configuration/system.serviceModel/bindings/customBinding");
88      Merge(sourceNode, destinationNode, destination, "/configuration/system.serviceModel/bindings");
89
90      sourceNode = source.SelectSingleNode("/configuration/system.serviceModel/bindings/mexHttpBinding");
91      destinationNode = destination.SelectSingleNode("/configuration/system.serviceModel/bindings/mexHttpBinding");
92      Merge(sourceNode, destinationNode, destination, "/configuration/system.serviceModel/bindings");
93
94      sourceNode = source.SelectSingleNode("/configuration/system.serviceModel/bindings/mexHttpsBinding");
95      destinationNode = destination.SelectSingleNode("/configuration/system.serviceModel/bindings/mexHttpsBinding");
96      Merge(sourceNode, destinationNode, destination, "/configuration/system.serviceModel/bindings");
97
98      sourceNode = source.SelectSingleNode("/configuration/system.serviceModel/bindings/mexNamedPipeBinding");
99      destinationNode = destination.SelectSingleNode("/configuration/system.serviceModel/bindings/mexNamedPipeBinding");
100      Merge(sourceNode, destinationNode, destination, "/configuration/system.serviceModel/bindings");
101
102      sourceNode = source.SelectSingleNode("/configuration/system.serviceModel/bindings/mexTcpBinding");
103      destinationNode = destination.SelectSingleNode("/configuration/system.serviceModel/bindings/mexTcpBinding");
104      Merge(sourceNode, destinationNode, destination, "/configuration/system.serviceModel/bindings");
105
106      sourceNode = source.SelectSingleNode("/configuration/system.serviceModel/bindings/msmqIntegrationBinding");
107      destinationNode = destination.SelectSingleNode("/configuration/system.serviceModel/bindings/msmqIntegrationBinding");
108      Merge(sourceNode, destinationNode, destination, "/configuration/system.serviceModel/bindings");
109
110      sourceNode = source.SelectSingleNode("/configuration/system.serviceModel/bindings/netMsmqBinding");
111      destinationNode = destination.SelectSingleNode("/configuration/system.serviceModel/bindings/netMsmqBinding");
112      Merge(sourceNode, destinationNode, destination, "/configuration/system.serviceModel/bindings");
113
114      sourceNode = source.SelectSingleNode("/configuration/system.serviceModel/bindings/netNamedPipeBinding");
115      destinationNode = destination.SelectSingleNode("/configuration/system.serviceModel/bindings/netNamedPipeBinding");
116      Merge(sourceNode, destinationNode, destination, "/configuration/system.serviceModel/bindings");
117
118      sourceNode = source.SelectSingleNode("/configuration/system.serviceModel/bindings/netPeerTcpBinding");
119      destinationNode = destination.SelectSingleNode("/configuration/system.serviceModel/bindings/netPeerTcpBinding");
120      Merge(sourceNode, destinationNode, destination, "/configuration/system.serviceModel/bindings");
121
122      sourceNode = source.SelectSingleNode("/configuration/system.serviceModel/bindings/netTcpBinding");
123      destinationNode = destination.SelectSingleNode("/configuration/system.serviceModel/bindings/netTcpBinding");
124      Merge(sourceNode, destinationNode, destination, "/configuration/system.serviceModel/bindings");
125
126      sourceNode = source.SelectSingleNode("/configuration/system.serviceModel/bindings/netTcpContextBinding");
127      destinationNode = destination.SelectSingleNode("/configuration/system.serviceModel/bindings/netTcpContextBinding");
128      Merge(sourceNode, destinationNode, destination, "/configuration/system.serviceModel/bindings");
129
130      sourceNode = source.SelectSingleNode("/configuration/system.serviceModel/bindings/webHttpBinding");
131      destinationNode = destination.SelectSingleNode("/configuration/system.serviceModel/bindings/webHttpBinding");
132      Merge(sourceNode, destinationNode, destination, "/configuration/system.serviceModel/bindings");
133
134      sourceNode = source.SelectSingleNode("/configuration/system.serviceModel/bindings/ws2007FederationHttpBinding");
135      destinationNode = destination.SelectSingleNode("/configuration/system.serviceModel/bindings/ws2007FederationHttpBinding");
136      Merge(sourceNode, destinationNode, destination, "/configuration/system.serviceModel/bindings");
137
138      sourceNode = source.SelectSingleNode("/configuration/system.serviceModel/bindings/ws2007HttpBinding");
139      destinationNode = destination.SelectSingleNode("/configuration/system.serviceModel/bindings/ws2007HttpBinding");
140      Merge(sourceNode, destinationNode, destination, "/configuration/system.serviceModel/bindings");
141
142      sourceNode = source.SelectSingleNode("/configuration/system.serviceModel/bindings/wsDualHttpBinding");
143      destinationNode = destination.SelectSingleNode("/configuration/system.serviceModel/bindings/wsDualHttpBinding");
144      Merge(sourceNode, destinationNode, destination, "/configuration/system.serviceModel/bindings");
145
146      sourceNode = source.SelectSingleNode("/configuration/system.serviceModel/bindings/wsFederationHttpBinding");
147      destinationNode = destination.SelectSingleNode("/configuration/system.serviceModel/bindings/wsFederationHttpBinding");
148      Merge(sourceNode, destinationNode, destination, "/configuration/system.serviceModel/bindings");
149
150      sourceNode = source.SelectSingleNode("/configuration/system.serviceModel/bindings/wsHttpBinding");
151      destinationNode = destination.SelectSingleNode("/configuration/system.serviceModel/bindings/wsHttpBinding");
152      Merge(sourceNode, destinationNode, destination, "/configuration/system.serviceModel/bindings");
153
154      sourceNode = source.SelectSingleNode("/configuration/system.serviceModel/bindings/wsHttpContextBinding");
155      destinationNode = destination.SelectSingleNode("/configuration/system.serviceModel/bindings/wsHttpContextBinding");
156      Merge(sourceNode, destinationNode, destination, "/configuration/system.serviceModel/bindings");
157      #endregion
158
159      sourceNode = source.SelectSingleNode("/configuration/system.serviceModel/client");
160      destinationNode = destination.SelectSingleNode("/configuration/system.serviceModel/client");
161      Merge(sourceNode, destinationNode, destination, "/configuration/system.serviceModel");
162
163      sourceNode = source.SelectSingleNode("/configuration/configSections/sectionGroup[@name='applicationSettings']");
164      destinationNode = destination.SelectSingleNode("/configuration/configSections/sectionGroup[@name='applicationSettings']");
165      Merge(sourceNode, destinationNode, destination, "/configuration/configSections");
166
167      sourceNode = source.SelectSingleNode("/configuration/configSections/sectionGroup[@name='userSettings']");
168      destinationNode = destination.SelectSingleNode("/configuration/configSections/sectionGroup[@name='userSettings']");
169      Merge(sourceNode, destinationNode, destination, "/configuration/configSections");
170
171      sourceNode = source.SelectSingleNode("/configuration/applicationSettings");
172      destinationNode = destination.SelectSingleNode("/configuration/applicationSettings");
173      Merge(sourceNode, destinationNode, destination, "/configuration");
174
175      sourceNode = source.SelectSingleNode("/configuration/userSettings");
176      destinationNode = destination.SelectSingleNode("/configuration/userSettings");
177      Merge(sourceNode, destinationNode, destination, "/configuration");
178
179      sourceNode = source.SelectSingleNode("/configuration/connectionStrings");
180      destinationNode = destination.SelectSingleNode("/configuration/connectionStrings");
181      Merge(sourceNode, destinationNode, destination, "/configuration");
182
183      sourceNode = source.SelectSingleNode("/configuration/system.data/DbProviderFactories");
184      destinationNode = destination.SelectSingleNode("/configuration/system.data/DbProviderFactories");
185      Merge(sourceNode, destinationNode, destination, "/configuration");
186
187      destination.Save(destinationFile);
188
189      Console.Out.WriteLine("ConfigMerger: Merge successfully finished!");
190    }
191
192    private static void Merge(XmlNode source, XmlNode destination, XmlDocument document, string root) {
193      try {
194        if (source != null) {
195          if (destination == null) {
196            XmlNode newNode = document.ImportNode(source, true);
197            document.SelectSingleNode(root).AppendChild(newNode);
198          } else {
199            foreach (XmlNode node in source.ChildNodes) {
200              XmlNode newNode = document.ImportNode(node, true);
201              XmlNode oldNode = destination.SelectSingleNode(BuildXPathString(newNode));
202              if (oldNode != null)
203                destination.ReplaceChild(newNode, oldNode);
204              else
205                destination.AppendChild(newNode);
206            }
207          }
208        }
209      }
210      catch (Exception ex) {
211        StringBuilder sb = new StringBuilder();
212        sb.Append("Error while merging node \"").Append(source.Name).Append("\"");
213        throw new Exception(sb.ToString(), ex);
214      }
215    }
216
217    private static string BuildXPathString(XmlNode node) {
218      StringBuilder builder = new StringBuilder();
219      builder.Append(node.Name);
220      if (node.Attributes.Count > 0) {
221        XmlAttribute attrib = node.Attributes[0];
222        builder.Append("[");
223        builder.Append("@" + attrib.Name + "='" + attrib.Value + "'");
224        for (int i = 1; i < node.Attributes.Count; i++) {
225          attrib = node.Attributes[i];
226          builder.Append(" and @" + attrib.Name + "='" + attrib.Value + "'");
227        }
228        builder.Append("]");
229      }
230      return builder.ToString();
231    }
232
233    private static string BuildErrorMessage(Exception ex) {
234      StringBuilder sb = new StringBuilder();
235      sb.Append("\n\n");
236      sb.Append("### ConfigMerger ERROR ###########################################\n" + ex.Message + "\n" + ex.StackTrace + "\n");
237      while (ex.InnerException != null) {
238        ex = ex.InnerException;
239        sb.Append("-----\n" + ex.Message + "\n" + ex.StackTrace + "\n");
240      }
241      sb.Append("##################################################################\n\n");
242      return sb.ToString();
243    }
244  }
245}
Note: See TracBrowser for help on using the repository browser.