예제 #1
0
		void PopulateDocument (string name, FeatureTarget target, XmlDocument doc, DefaultConfigFile dcf,
				       IDefaultContainer[] defaults)
		{
			List <Section> children = dcf.Sections != null ? dcf.Sections.Children : null;
			if (children == null || children.Count == 0)
				return;

			PopulateDocument (name, target, doc, doc, defaults, children);
		}
예제 #2
0
		void PopulateDocument (string name, FeatureTarget target, XmlDocument doc, XmlNode parent,
				       IDefaultContainer[] defaults, List <Section> children)
		{
			if (defaults == null || defaults.Length == 0)
				return;
			
			XmlNode node;
			XmlDocument tmp;
			
			foreach (Section s in children) {
				tmp = Helpers.FindDefault (defaults, s.DefaultBlockName, target);
				if (tmp == null)
					continue;
				
				node = doc.ImportNode (tmp.DocumentElement.FirstChild, true);
				try {
					PopulateDocument (name, target, doc, node, defaults, s.Children);
				} catch (Exception ex) {
					throw new ApplicationException (
						String.Format ("Error building default config file '{0}'", name),
						ex);
				}

				parent.AppendChild (node);
			}
		}
예제 #3
0
		public void WriteDefaultConfigFile (string name, FeatureTarget target, string path, IDefaultContainer[] defaults)
		{
			AssertStorage ();

			DefaultConfigFile dcf;
			if (!storage.ContainsKey (name) || (dcf = storage [name]) == null)
				throw new ApplicationException (
					String.Format ("Definition of the '{0}' default config file not found.", name));

			if (target != FeatureTarget.Any && dcf.Target != target)
				throw new ApplicationException (
					String.Format ("Config file '{0}' can be generated only for the '{1}' target",
						       name, target));

			string targetFile = Path.Combine (path, dcf.FileName);
			if (File.Exists (targetFile)) {
				OverwriteFileEventArgs args = new OverwriteFileEventArgs (
					dcf.FileName,
					path,
					target,
					true
				);

				OnOverwriteFile (args);
				if (!args.Overwrite)
					return;
			}

			try {
				if (!Directory.Exists (path))
					Directory.CreateDirectory (path);
			} catch (Exception ex) {
				throw new ApplicationException (
					String.Format ("Could not create directory '{0}'", path),
					ex);
			}

			XmlDocument doc = new XmlDocument ();
			PopulateDocument (name, target, doc, dcf, defaults);
			Helpers.SaveXml (doc, targetFile);
		}
예제 #4
0
		XmlNode FindNodeOrAddDefault (XmlDocument doc, string nodeName, string nodePath, IDefaultContainer[] defaults)
		{
			XmlNode ret = doc.SelectSingleNode (nodePath);

			if (ret != null)
				return ret;
			
			XmlDocument defDoc = Helpers.FindDefault (defaults, nodeName, FeatureTarget.Any);
			if (defDoc == null)
				throw new ApplicationException (
					String.Format ("Document doesn't contain node '{0}' and no default can be found",
						       nodePath));

			return doc.ImportNode (defDoc.DocumentElement.FirstChild, true);
		}
예제 #5
0
		void ProcessSections (XmlDocument doc, XmlNode parent, string topPath, Section top, IDefaultContainer[] defaults,
				      string blockName, ref XmlNode attachPoint)
		{
			List <Section> topChildren, children;
			if (top == null || (topChildren = top.Children) == null)
				return;
			
			XmlNode node;
			string curPath;

			foreach (Section s in topChildren) {
				curPath = String.Format ("{0}/{1}", topPath, s.Name);
				
				node = FindNodeOrAddDefault (doc, s.DefaultBlockName, curPath, defaults);
				if (node != null && s.AttachPoint) {
					if (attachPoint != null)
						throw new ApplicationException (
							String.Format ("Config block '{0}' has more than one attachment point",
								       blockName));
					attachPoint = node;
				}
				parent.AppendChild (node);
				
				if ((children = s.Children) != null && children.Count > 0)
					ProcessSections (doc, node, curPath, s, defaults, blockName, ref attachPoint);
			}

			return;
		}
예제 #6
0
		void AddFeatureBlock (XmlDocument doc, FeatureBlock block, FeatureTarget target, IDefaultContainer[] defaults,
				      IConfigBlockContainer[] configBlocks)
		{
			if (target != FeatureTarget.Any && block.Target != target)
				return;

			ConfigBlockBlock configBlock = Helpers.FindConfigBlock (configBlocks, block.Name);
			if (configBlock == null)
				throw new ApplicationException (String.Format ("Config block '{0}' cannot be found", block.Name));

			XmlNode attachPoint = null;

			ProcessSections (doc, doc, "/", configBlock.Requires, defaults, configBlock.Name, ref attachPoint);
			if (attachPoint == null)
				attachPoint = FindDefaultAttachPoint (doc, configBlock.Requires);
			if (attachPoint == null)
				throw new ApplicationException (
					String.Format ("Missing attachment point for block '{0}'", configBlock.Name));
			
			XmlDocument contents = new XmlDocument ();
			contents.LoadXml (String.Format ("<{0}>{1}</{0}>", Helpers.FakeRootName, configBlock.Contents));
			AddFeatureRecursively (doc, attachPoint, contents.DocumentElement);
		}
예제 #7
0
		public void AddFeature (string configFilePath, string featureName, FeatureTarget target,
					IDefaultContainer[] defaults, IConfigBlockContainer[] configBlocks)
		{
			AssertStorage ();

			FeatureNode fn;
			
			if (!storage.ContainsKey (featureName) || (fn = storage [featureName]) == null)
				throw new ApplicationException (String.Format ("Missing definition of feature '{0}'", featureName));
				
			List <FeatureBlock> blocks = fn.Blocks;
			if (blocks == null || blocks.Count == 0)
				throw new ApplicationException (String.Format ("Definition of feature '{0}' is empty", featureName));

			RunActions (fn.ActionsBefore);
			XmlDocument doc = new XmlDocument ();

			if (File.Exists (configFilePath))
				doc.Load (configFilePath);

			foreach (FeatureBlock block in blocks)
				AddFeatureBlock (doc, block, target, defaults, configBlocks);
			
			Helpers.SaveXml (doc, configFilePath);
			RunActions (fn.ActionsAfter);
		}