} // proc ParseConfiguration private void IncludeConfigTree(ParseContext context, XProcessingInstruction xPI) { if (xPI.Parent == null) { throw context.CreateConfigException(xPI, "It is not allowed to include to a root element."); } var xInc = context.LoadFile(xPI, xPI.Data).Root; if (xInc.Name == DEConfigurationConstants.xnInclude) { XNode xLast = xPI; // Copy the baseuri annotation var copy = new List <XElement>(); foreach (var xSrc in xInc.Elements()) { Procs.XCopyAnnotations(xSrc, xSrc); copy.Add(xSrc); } // Remove all elements from the source, that not get internal copied. xInc.RemoveAll(); xPI.AddAfterSelf(copy); } else { Procs.XCopyAnnotations(xInc, xInc); xInc.Remove(); xPI.AddAfterSelf(xInc); } } // proc IncludeConfigTree
} // proc IncludeConfigTree private void MergeConfigTree(ParseContext context, XProcessingInstruction xPI) { var xDoc = context.LoadFile(xPI, xPI.Data); // parse the loaded document var newFrame = context.PushFrame(xPI); if (xDoc.Root.Name != DEConfigurationConstants.xnFragment) { throw context.CreateConfigException(xDoc.Root, "<fragment> expected."); } ParseConfiguration(context, xDoc.Root); context.PopFrame(newFrame); // merge the parsed nodes MergeConfigTree(xPI.Document.Root, xDoc.Root); } // proc MergeConfigTree
/// <summary>Reads the configuration file and validates it agains the schema</summary> /// <returns></returns> public XElement ParseConfiguration() { // prepare arguments for the configuration var fileName = Path.GetFullPath(configurationFile); var context = new ParseContext(configurationProperties, Path.GetDirectoryName(fileName)); // read main file var doc = context.LoadFile(fileName); var frame = context.PushFrame(doc); try { if (doc.Root.Name != xnDes) { throw new InvalidDataException(String.Format("Configuration root node is invalid (expected: {0}).", xnDes)); } if (doc.Root.GetAttribute("version", String.Empty) != "330") { throw new InvalidDataException("Configuration version is invalid (expected: 330)."); } // parse the tree ParseConfiguration(context, doc, new XFileAnnotation()); context.PopFrame(frame); // remove all parse frames RemoveFileAnnotations(doc.Root); } catch (Exception e) { if (e is DEConfigurationStackException) { throw; } throw context.CreateConfigException(doc, "Could not parse configuration file.", e); } // check the schema doc.Validate(schema, ValidationEvent, false); return(doc.Root); } // func ParseConfiguration
} // proc ValidationEvent private void ParseConfiguration(ParseContext context, XContainer x) { var c = x.FirstNode; while (c != null) { var deleteMe = (XNode)null; var value = (string)null; if (c is XComment) { deleteMe = c; } else if (c is XProcessingInstruction) { ParseConfigurationPI(context, (XProcessingInstruction)c); deleteMe = c; } else { if (context.CurrentFrame.IsDeleteNodes) { deleteMe = c; } else if (c is XElement) { var xCur = (XElement)c; // Replace values in attributes foreach (var attr in xCur.Attributes()) { if (ChangeConfigurationValue(context, attr, attr.Value, out value)) { attr.Value = value; } } // Parse the current element var newFrame = context.PushFrame(xCur); ParseConfiguration(context, xCur); context.PopFrame(newFrame); // Load assemblies -> they preprocessor needs them if (xCur.Name == xnServer) { foreach (var cur in xCur.Elements()) { if (cur.Name == xnServerResolve) // resolve paths { if (ChangeConfigurationValue(context, cur, cur.Value, out value)) { cur.Value = value; } switch (cur.GetAttribute("type", "net")) { case "net": resolver?.AddPath(cur.Value); break; case "platform": resolver?.AddPath(cur.Value); if (IntPtr.Size == 4) // 32bit { DEServer.AddToProcessEnvironment(Path.Combine(cur.Value, "x86")); } else { DEServer.AddToProcessEnvironment(Path.Combine(cur.Value, "x64")); } break; case "envonly": DEServer.AddToProcessEnvironment(cur.Value); break; default: throw context.CreateConfigException(cur, "resolve @type has an invalid attribute value."); } } else if (cur.Name == xnServerLoad) { if (ChangeConfigurationValue(context, cur, cur.Value, out value)) { cur.Value = value; } try { UpdateSchema(Assembly.Load(cur.Value)); } catch (Exception e) { throw context.CreateConfigException(cur, String.Format("Failed to load assembly ({0}).", cur.Value), e); } } } } } else if (c is XText) { XText xText = (XText)c; if (ChangeConfigurationValue(context, xText, xText.Value, out value)) { xText.Value = value; } } } // Nächster Knoten c = c.NextNode; // Lösche den Knoten, sonst würde Next nicht funktionieren if (deleteMe != null) { deleteMe.Remove(); } } } // proc ParseConfiguration