Example #1
0
 public void RegisterCDataDelegate(string elems, EndElementDelegate f)
 {
     dict_CDataDelegate[elems] = f;
 }
Example #2
0
        // /> : start element end
        // </ : end element start

        // /> : start element end
        // </ : end element start
        bool ParseXMLString(Dictionary <string, string> processingInstruction, string src)
        {
            int i           = 0;
            int newline_cnt = 0;

            while (IsProcessingInstructionStart(src, i))
            {
                i += ProcessingInstructionStartLength;
                string prep = "";
                if (ReadProcessingInstructionValue(src, ref i, out prep, processingInstruction, ref newline_cnt))
                {
                    if (processingInstructionDelegate != null)
                    {
                        Dictionary <string, string> .Enumerator enumInstr = processingInstruction.GetEnumerator();

                        while (enumInstr.MoveNext())
                        {
                            processingInstructionDelegate(enumInstr.Current.Key, enumInstr.Current.Value);
                        }
                    }
                }
            }
            while (IsDocTypeStart(src, i))
            {
                i += DocTypeStartLength;
                string dest = "";
                ReadDocType(src, ref i, out dest);
            }

            Stack <string> StackElement = new Stack <string>();
            Stack <int>    StackLineNum = new Stack <int>();
            string         name         = "";
            string         val          = "";
            string         text         = "";

            while (i < src.Length)
            {
                text = "";
                while (IsContent(src, i))
                {
                    text += src[i];
                    ++i;
                }

                if (i >= src.Length)
                {
                    break;
                }

                if (IsCDataStart(src, i))
                {
                    i += CDataStartLength;
                    ReadCDataValue(src, ref i, out val);
                    if (StackElement.Count > 0)
                    {
                        string key = GetStackString(StackElement);
                        if (dict_CDataDelegate.ContainsKey(key))
                        {
                            EndElementDelegate del = dict_CDataDelegate[key];
                            del(val);
                        }
                    }
                }
                else if (IsCommentStart(src, i))
                {
                    i += CommentStartLength;
                    ReadCommentValue(src, ref i, out val);
                    if (StackElement.Count > 0)
                    {
                        string key = GetStackString(StackElement);
                        if (dict_CommentDelegate.ContainsKey(key))
                        {
                            EndElementDelegate del = dict_CommentDelegate[key];
                            del(val);
                        }
                    }
                }
                else if (src[i] == '<' && IsEndElementStart(src, i) == false) // is start element
                {
                    // read element
                    AfterElementName res = AfterElementName.None;
                    ++i;
                    if (ReadElementName(src, ref i, out name, out res, ref newline_cnt))
                    {
                        if (name == "")
                        {
                            throw new InvalidOperationException("empty name");
                        }
                        StackElement.Push(name);
                        StackLineNum.Push(newline_cnt + 1);

                        RawElement pRawElement = new RawElement(name);
                        if (res == AfterElementName.EncounterWhitespace)
                        {
                            bool isStartElementEnd = false;
                            while (i < src.Length)
                            {
                                while (IsWhitespace(src, i, ref newline_cnt))
                                {
                                    ++i;
                                }

                                isStartElementEnd = IsStartElementEnd(src, i);
                                if (isStartElementEnd)
                                {
                                    i += 1;
                                    string key = GetStackString(StackElement);
                                    if (dict_StartElemDelegate.ContainsKey(key))
                                    {
                                        StartElementDelegate del = dict_StartElemDelegate[key];
                                        Element elem             = new Element(pRawElement);
                                        del(elem);
                                    }
                                    else
                                    {
                                        pRawElement.Destroy();
                                        pRawElement = null;
                                    }
                                    if (dict_EndElemDelegate.ContainsKey(key))
                                    {
                                        EndElementDelegate del = dict_EndElemDelegate[key];
                                        del("");
                                    }

                                    StackElement.Pop();
                                    StackLineNum.Pop();
                                    goto breakout;
                                }

                                if (src[i] == '>')
                                {
                                    break;
                                }

                                ReadAttributeName(src, ref i, out name, ref newline_cnt);

                                while (IsWhitespace(src, i, ref newline_cnt) || src[i] == '=')
                                {
                                    ++i;
                                }

                                ReadAttributeValue(src, ref i, out val);

                                if (name != "")
                                {
                                    pRawElement.GetAttrs().Add(new KeyValuePair <string, string>(name, val));
                                }
                            }
                            string key1 = GetStackString(StackElement);
                            if (dict_StartElemDelegate.ContainsKey(key1))
                            {
                                StartElementDelegate del = dict_StartElemDelegate[key1];
                                Element elem             = new Element(pRawElement);
                                del(elem);
                            }
                            else
                            {
                                if (!isStartElementEnd)
                                {
                                    pRawElement.Destroy();
                                    pRawElement = null;
                                }
                            }
                        }
                        else if (res == AfterElementName.ElementEnded)
                        {
                            string key = GetStackString(StackElement);
                            if (dict_StartElemDelegate.ContainsKey(key))
                            {
                                StartElementDelegate del = dict_StartElemDelegate[key];
                                Element elem             = new Element(pRawElement);
                                del(elem);
                            }
                            else
                            {
                                pRawElement.Destroy();
                                pRawElement = null;
                            }
                            if (dict_EndElemDelegate.ContainsKey(key))
                            {
                                EndElementDelegate del = dict_EndElemDelegate[key];
                                del("");
                            }

                            StackElement.Pop();
                            StackLineNum.Pop();
                        }
                        else // NodeEnded
                        {
                            string key = GetStackString(StackElement);
                            if (dict_StartElemDelegate.ContainsKey(key))
                            {
                                StartElementDelegate del = dict_StartElemDelegate[key];
                                Element elem             = new Element(pRawElement);
                                del(elem);
                            }
                            else
                            {
                                pRawElement.Destroy();
                                pRawElement = null;
                            }
                        }
                    }
                    else // parser error
                    {
                        PrintStack(i, newline_cnt, "Start tag error", StackElement, StackLineNum);
                    }
                }
                else // is end element
                {
                    AfterElementName result;
                    i += 2;
                    if (ReadElementName(src, ref i, out name, out result, ref newline_cnt))
                    {
                        if (name == "")
                        {
                            PrintStack(i, newline_cnt, "The ending tag error", StackElement, StackLineNum);
                        }

                        if (result == AfterElementName.NodeEnded)
                        {
                            if (StackElement.Peek() != name)
                            {
                                // print error of different start and end names
                                string error = "Different start and end tag:";
                                error += "Expect <" + StackElement.Peek() + "> but received </" + name + ">";
                                PrintStack(i, newline_cnt, error, StackElement, StackLineNum);
                                return(false);
                            }
                            else
                            {
                                string key1 = GetStackString(StackElement);

                                if (dict_EndElemDelegate.ContainsKey(key1))
                                {
                                    EndElementDelegate del = dict_EndElemDelegate[key1];

                                    int newline_cnt1 = 0;
                                    if (IsWhitespace(text, ref newline_cnt1))
                                    {
                                        del("");
                                    }
                                    else
                                    {
                                        del(UnescapeXML(text));
                                    }
                                }
                                StackElement.Pop();
                                StackLineNum.Pop();
                            }
                        }
                    }
                    else
                    {
                        // print all stack names and element without a end element
                        PrintStack(i, newline_cnt, "The last element does not have a ending tag", StackElement, StackLineNum);
                    }
                }
breakout:
                ++i;
            }

            if (StackElement.Count > 0)
            {
                PrintStack(i, newline_cnt, "The element does not have a ending tag", StackElement, StackLineNum);
                return(false);
            }

            return(true);
        }