private static void ExpandIncludes(XmlNode section, string baseDirectory, ChoNestedDictionary <string, XmlNode> includeFileList, XmlNamespaceManager xmlnsManager) { XmlNodeList includeNodes = section.SelectNodes(".//cinchoo:include", xmlnsManager); if (includeNodes.Count > 0) { XmlDocument ownerDocument = section is XmlDocument ? section as XmlDocument : section.OwnerDocument; ChoNestedDictionary <string, XmlNode> includeSubFileList = new ChoNestedDictionary <string, XmlNode>(); foreach (XmlElement includeElement in includeNodes) { if (includeElement == null) { continue; } string includeFileName = includeElement.GetAttribute(PathToken); includeFileName = ExpandNow(section, baseDirectory, includeFileList, includeSubFileList, includeElement, includeFileName, xmlnsManager); } if (includeSubFileList.Length > 0) { includeFileList.Add(includeSubFileList); } } else if (section is XmlElement && ((XmlElement)section).Name == IncludeToken) { ChoNestedDictionary <string, XmlNode> includeSubFileList = new ChoNestedDictionary <string, XmlNode>(); XmlElement element = section as XmlElement; string includeFileName = element.Attributes != null && element.Attributes.Count > 0 && element.Attributes[PathToken] != null ? element.Attributes[PathToken].Value : null; includeFileName = ExpandNow(section, baseDirectory, includeFileList, includeSubFileList, element, includeFileName, xmlnsManager); if (includeSubFileList.Length > 0) { includeFileList.Add(includeSubFileList); } } }
public static ChoNestedDictionary <string, XmlNode> ExpandIncludes(XmlNode section, string baseDirectory, XmlNamespaceManager xmlnsManager) { ChoNestedDictionary <string, XmlNode> parentFileList = new ChoNestedDictionary <string, XmlNode>(); parentFileList.Add(baseDirectory, section); if (File.Exists(baseDirectory)) { baseDirectory = Path.GetDirectoryName(baseDirectory); } ExpandIncludes(section, baseDirectory, parentFileList, xmlnsManager); return(parentFileList); }
public static ChoNestedDictionary <string, XmlNode> ContractIncludes(XmlNode section, string filePath) { ChoGuard.ArgumentNotNull(section, "XmlNode"); if (filePath == null) { filePath = String.Empty; } ChoNestedDictionary <string, XmlNode> xmlDocumentList = new ChoNestedDictionary <string, XmlNode>(); xmlDocumentList.Add(filePath, section is XmlDocument ? section as XmlDocument : section.OwnerDocument); ContractIncludes(section, filePath, xmlDocumentList); return(xmlDocumentList); }
private static string ExpandNow(XmlNode section, string baseDirectory, ChoNestedDictionary <string, XmlNode> includeFileList, ChoNestedDictionary <string, XmlNode> includeSubFileList, XmlElement includeElement, string includeFileName, XmlNamespaceManager xmlnsManager) { if (String.IsNullOrEmpty(includeFileName)) { RemoveIncludeNode(includeElement); return(includeFileName); } else if (!Path.IsPathRooted(includeFileName) && !String.IsNullOrEmpty(baseDirectory)) { includeFileName = Path.Combine(baseDirectory, includeFileName); } //if (!File.Exists(includeFileName)) // RemoveIncludeNode(section, includeElement); if (IsCircularFileExists(includeFileList, includeFileName)) { throw new ChoXmlDocumentException(String.Format("Circular reference encountered on the {0} file.", baseDirectory)); } XmlNode[] newNodes = InsertNodes(includeElement, includeFileName, ExtractNodes(includeElement, includeFileName)); if (newNodes != null) { foreach (XmlNode newNode in newNodes) { if (!includeSubFileList.ContainsKey(includeFileName)) { includeSubFileList.Add(includeFileName, newNode); } baseDirectory = Path.GetDirectoryName(includeFileName); ExpandIncludes(newNode, baseDirectory, includeSubFileList, xmlnsManager); } } //includeSubFileList.Add(includeFileName, includeElement); return(includeFileName); }
private static void ContractIncludes(XmlNode section, string filePath, ChoNestedDictionary <string, XmlNode> xmlDocumentList) { if (section == null) { return; } section = section is XmlDocument ? ((XmlDocument)section).DocumentElement : section; bool beginIncludeTokenFound = false; string includeFilePath = String.Empty; List <XmlNode> childNodes = new List <XmlNode>(); XmlNode startCommentNode = null; foreach (XmlNode xmlNode in section.SelectNodes("child::node()")) { if (!beginIncludeTokenFound) { if (xmlNode is XmlComment && xmlNode.Value.StartsWith(BeginIncludeToken)) { includeFilePath = xmlNode.Value.Replace(BeginIncludeToken, String.Empty).Trim(); beginIncludeTokenFound = true; startCommentNode = xmlNode; continue; } else if (xmlNode is XmlElement) { ContractIncludes(xmlNode, filePath, xmlDocumentList); } } else { if (xmlNode is XmlComment && xmlNode.Value.StartsWith(EndIncludeToken) && xmlNode.Value.Replace(EndIncludeToken, String.Empty).Trim() == includeFilePath) { beginIncludeTokenFound = false; XmlNode includeXmlDocument = CreateIncludeDocument(childNodes); //Create INCLUDE node XmlElement includeElement = section.OwnerDocument.CreateElement(IncludePrefixToken, "include", CinchooNSURI); XmlAttribute pathAttribute = section.OwnerDocument.CreateAttribute(PathToken); if (Path.IsPathRooted(includeFilePath)) { if (filePath.IsNullOrEmpty() || Path.GetDirectoryName(includeFilePath) != Path.GetDirectoryName(filePath)) { pathAttribute.Value = includeFilePath; } else { pathAttribute.Value = Path.GetFileName(includeFilePath); } } else { pathAttribute.Value = ChoPath.GetRelativePath(Path.GetDirectoryName(filePath), includeFilePath); } includeElement.Attributes.Append(pathAttribute); section.InsertBefore(includeElement, startCommentNode); //Remove all the nodes section.RemoveChild(startCommentNode); foreach (XmlNode xmlChildNode in childNodes) { section.RemoveChild(xmlChildNode); } section.RemoveChild(xmlNode); ChoNestedDictionary <string, XmlNode> xmlSubDocumentList = new ChoNestedDictionary <string, XmlNode>(); xmlSubDocumentList.Add(includeFilePath, includeXmlDocument as XmlNode); ContractIncludes(includeXmlDocument, filePath, xmlSubDocumentList); if (xmlSubDocumentList.Count > 0) { xmlDocumentList.Add(xmlSubDocumentList); } childNodes.Clear(); continue; } childNodes.Add(xmlNode); } } }