Example #1
0
        public static void ContractNSave(XmlNode xmlNode, string xmlFilePath)
        {
            ChoNestedDictionary <string, XmlNode> xmlFileList = ContractIncludes(xmlNode, xmlFilePath);

            foreach (string fileName in xmlFileList.Keys)
            {
                ChoXmlDocument.Save(xmlFileList[fileName], fileName, true);
            }
        }
Example #2
0
        public static XmlDocument Load(string filename, out ChoNestedDictionary <string, XmlNode> includeFileNodesMap)
        {
            XmlDocument doc = new XmlDocument(_nameTable);

            Load(doc, filename);

            includeFileNodesMap = ExpandIncludes(doc.DocumentElement, filename, _xmlnsManager);

            return(doc);
        }
Example #3
0
        public static ChoNestedDictionary <string, XmlNode> ExpandIncludes(string configPath, out XmlDocument xmlDocument)
        {
            xmlDocument = new XmlDocument(_nameTable);
            xmlDocument.Load(configPath);

            ChoNestedDictionary <string, XmlNode> parentFileList = new ChoNestedDictionary <string, XmlNode>();

            ExpandIncludes(xmlDocument.DocumentElement, Path.GetDirectoryName(configPath), parentFileList, _xmlnsManager);

            return(parentFileList);
        }
Example #4
0
        private void SaveInternal()
        {
            ChoNestedDictionary <string, XmlNode> xmlFileList = ContractIncludes(_xmlDocument, _filePath);

            foreach (string fileName in xmlFileList.Keys)
            {
                if (_lastWriteTimeCache.GetValue(fileName) <= File.GetLastWriteTime(fileName))
                {
                    ChoXmlDocument.Save(xmlFileList[fileName], fileName, true);
                }
            }
        }
        private void QueueMessageHandler(IChoQueuedMsgServiceObject <ChoStandardQueuedMsg <object> > msgObject)
        {
            if (msgObject == null)
            {
                return;
            }

            lock (_syncRoot)
            {
                if (msgObject.State.Msg == null)
                {
                    if (!_readOnly)
                    {
                        ChoNestedDictionary <string, XmlNode> xmlFileList = ContractIncludes(_xmlDocument, _filePath);

                        foreach (string fileName in xmlFileList.Keys)
                        {
                            if (_lastWriteTimeCache.GetValue(fileName) <= File.GetLastWriteTime(fileName))
                            {
                                ChoXmlDocument.Save(xmlFileList[fileName], fileName, true);
                            }
                        }
                    }
                }
                else if (msgObject.State.Msg is ChoConfigurationCompositeFileChangedEventArgs)
                {
                    ChoConfigurationCompositeFileChangedEventArgs e = msgObject.State.Msg as ChoConfigurationCompositeFileChangedEventArgs;
                    if (e != null)
                    {
                        try
                        {
                            ChoXmlDocumentChangingEventArgs xmlDocumentChangingEventArgs = new ChoXmlDocumentChangingEventArgs(_filePath, e.ModifiedIncludeFiles);
                            DocumentChanging.Raise(this, xmlDocumentChangingEventArgs);

                            if (xmlDocumentChangingEventArgs.IgnoreLoadDocument)
                            {
                                return;
                            }

                            _xmlDocument = null;
                            Load();

                            DocumentChanged.Raise(this, new ChoXmlDocumentChangedEventArgs(_filePath, e.ModifiedIncludeFiles));
                        }
                        catch
                        {
                            DocumentLoaded = false;
                            throw;
                        }
                    }
                }
            }
        }
Example #6
0
        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);
        }
Example #7
0
        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);
        }
Example #8
0
        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);
                }
            }
        }
Example #9
0
        private void RefreshDocument()
        {
            if (IsXmlFile)
            {
                _includeXmlFilePathNodesMap = ExpandIncludes(_xmlDocument, _filePath, _xmlnsManager);
            }
            else
            {
                _includeXmlFilePathNodesMap = ExpandIncludes(_xmlDocument, null, _xmlnsManager);
            }

            foreach (string includeFile in IncludeFiles)
            {
                _lastWriteTimeCache.SetValue(includeFile, File.GetLastWriteTime(includeFile));
            }

            if (_watchChange)
            {
                _configurationChangeWatcher = new ChoConfigurationChangeCompositeFileWatcher("ChoXmlDocument", _filePath, IncludeFiles);
                _configurationChangeWatcher.SetConfigurationChangedEventHandler(this, OnFileChanged);
            }
        }
Example #10
0
        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);
        }
Example #11
0
        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);
                }
            }
        }
Example #12
0
        /// <summary>
        /// Static method that calls XmlDocument.Load() but arranges to open
        /// the file with with FileShared.ReadWrite to keep from blocking others.
        /// </summary>
        public static XmlDocument Load(string filename)
        {
            ChoNestedDictionary <string, XmlNode> includeFileNodesMap = null;

            return(Load(filename, out includeFileNodesMap));
        }
Example #13
0
 private static bool IsCircularFileExists(ChoNestedDictionary <string, XmlNode> parentFileList, string xmlFilePath)
 {
     return(parentFileList.ContainsKey(xmlFilePath));            //.Exists(delegate(string item) { return String.Compare(item, xmlFilePath, true) == 0; });
 }