Free cookie consent management tool by TermsFeed Policy Generator

source: branches/3.2/tools/ConfigMerger/ConfigMerger.cs @ 13612

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

updated ConfigMerger by merging behaviors separately #962

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