Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 2697 was 2697, checked in by abeham, 15 years ago

Changed ConfigMerger to merge each possible binding in system.serviceModel/bindings separately (#858)

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