Exemple #1
0
        /// <summary>
        /// This function will get the list/child corresponding the keys
        ///  (Keys can basically be a path [similar to a filesystem or XPath]
        ///    ex: "/root/child/subchild" or just an "element"/key)
        /// </summary>
        /// <param name="Keys">The path to the node/child</param>
        /// <param name="ReturnAll">Should we return all that matches?</param>
        public ktList Get(ktString Keys, bool ReturnAll)
        {
            // Init...
            ktString Key;
            ktString TmpKeys = Keys; // Temporary to save the given value

            //  Remove leading slashs (/)..
            Keys.Trim(ktStripType.leading, "/");

            if (Keys.Contains("/"))
            {
                // Get the first key
                Key = Keys.BeforeFirst('/');
                // Remove the first key
                Keys = Keys.AfterFirst('/');
            }
            else
            {
                Key = Keys;
            }

            // If the given key matches the key of "this node" (or we didn't get a key)
            if ((m_Node != null) && (m_Node.Check(Key)) || (Key == ""))
            {
                // Return "this"
                return this;
            }

            // No elements??
            if (m_Head == null)
            {
                // there's nothing to get
                throw new ktError("The list is empty (in ktList::Get( " + TmpKeys + ", bool ))", ktERR.EMPTY);
            }

            // Init...
            ktList TmpList = null;
            ktList AllMatches = null;
            bool GoOn = true;
            Reset();

            // Should we return all matches?
            if (ReturnAll)
            {
                AllMatches = new ktList();
            }

            // Go thru the list..
            while (MoveNext() && GoOn)
            {
                // Try it...
                try
                {
                    // Get the "sublist(s)"
                    TmpList = CurrentNode.Get(Keys, ReturnAll); // Key or Keys??

                    // If we should return all
                    if (ReturnAll)
                    {
                        // Add the found list... (and then go on)
                        AllMatches.Add(TmpList);
                    }
                    else
                    {
                        // Stop looking...
                        GoOn = false;
                    }
                    // Catch an error...
                }
                catch (ktError E)
                {
                    // If it was an error other than "Not found" or empty...
                    if ((E.ErrorNumber != ktERR._404) && (E.ErrorNumber != ktERR.EMPTY))
                    {
                        // Re-throw it
                        throw E;
                    } // If
                } // Catch
            } // While

            // If we should return all matches and
            //  we found something (The list isn't empty)
            if (ReturnAll && (!AllMatches.IsEmpty()))
            {
                // Set GoOn to false (as it marks a found node/list)
                GoOn = false;
            }

            // If we should get all matches but only found one
            if (ReturnAll && (AllMatches.GetCount() == 1))
            {
                // Get the first (and only) child
                TmpList = AllMatches.First;
                // Set "match/return all" to false
                ReturnAll = false;

                // Set tail and head to null...
                AllMatches.m_Head = AllMatches.m_Tail = null;

                // Dispose of the list...
                AllMatches.Dispose();
            }

            // If we didn't find a match
            if (GoOn)
            {
                // Throw an error
                throw new ktError("Couldn't find the list '" + TmpKeys + "' (in ktList::Get(  " + TmpKeys + " ))", ktERR._404);
                // If we should return all matches...
            }
            else if (ReturnAll)
            {
                // Return the list
                return AllMatches;
                // Found an match
            }
            else
            {
                // Return it..
                return TmpList;
            }
        }