Free cookie consent management tool by TermsFeed Policy Generator

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

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

Fixed a bug in ConfigMerger when the bindings root node didn't exist (#858)

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