public ParserStackFrame Pop() { if (!IsEmpty()) { int count = m_array.Count; ParserStackFrame temp = (ParserStackFrame)m_array[count - 1]; m_array.RemoveAt(count - 1); return(temp); } else { throw new Exception(""); } }
private void ParseContents(SecurityElement e, bool restarted) { // // Iteratively collect stuff up until the next end-tag. // We've already seen the open-tag. // SecurityElementType lastType = SecurityElementType.Regular; ParserStack stack = new ParserStack(); ParserStackFrame firstFrame = new ParserStackFrame(); firstFrame.element = e; firstFrame.intag = false; stack.Push(firstFrame); bool needToBreak = false; bool needToPop = false; int i; do { ParserStackFrame locFrame = stack.Peek(); for (i = _t.NextTokenType(); i != -1; i = _t.NextTokenType()) { switch (i) { case Tokenizer.cstr: { if (locFrame.intag) { if (locFrame.type == SecurityElementType.Comment) { String appendString; if (locFrame.sawEquals) { appendString = "=\"" + _t.GetStringToken() + "\""; locFrame.sawEquals = false; } else { appendString = " " + _t.GetStringToken(); } // Always set this directly since comments are not subjected // to the same restraints as other element types. The strings // are all escaped so this shouldn't be a problem. locFrame.element.Tag = locFrame.element.Tag + appendString; } else { // We're in a regular tag, so we've found an attribute/value pair. if (locFrame.strValue == null) { // Found attribute name, save it for later. locFrame.strValue = _t.GetStringToken(); } else { // Found attribute text, add the pair to the current element. if (!locFrame.sawEquals) { throw new XmlSyntaxException(_t.LineNo); } locFrame.element.AddAttribute(locFrame.strValue, _t.GetStringToken()); locFrame.strValue = null; } } } else { // We're not in a tag, so we've found text between tags. if (locFrame.element.Text == null) { locFrame.element.Text = ""; } StringBuilder sb = new StringBuilder(locFrame.element.Text); // // Separate tokens with single spaces, collapsing whitespace // if (!locFrame.element.Text.Equals("")) { sb.Append(" "); } sb.Append(_t.GetStringToken()); locFrame.element.Text = sb.ToString(); } } break; case Tokenizer.bra: locFrame.intag = true; i = _t.NextTokenType(); if (i == Tokenizer.slash) { while (true) { // spin; don't care what's in here i = _t.NextTokenType(); if (i == Tokenizer.cstr) { continue; } else if (i == -1) { throw new Exception(""); } else { break; } } if (i != Tokenizer.ket) { throw new Exception(""); } locFrame.intag = false; // Found the end of this element lastType = stack.Peek().type; stack.Pop(); needToBreak = true; } else if (i == Tokenizer.cstr) { // Found a child ParserStackFrame newFrame = new ParserStackFrame(); newFrame.element = new SecurityElement(_t.GetStringToken()); if (locFrame.type != SecurityElementType.Regular) { throw new XmlSyntaxException(_t.LineNo); } locFrame.element.AddChild(newFrame.element); stack.Push(newFrame); needToBreak = true; } else if (i == Tokenizer.bang) { // Found a child that is a format node. Next up better be a cstr. ParserStackFrame newFrame = new ParserStackFrame(); newFrame.status = 1; do { i = _t.NextTokenType(); if (newFrame.status < 3) { if (i != Tokenizer.dash) { throw new XmlSyntaxException(_t.LineNo); } else { newFrame.status++; } } else { if (i != Tokenizer.cstr) { throw new XmlSyntaxException(_t.LineNo); } else { break; } } }while (true); newFrame.element = new SecurityElement(_t.GetStringToken()); newFrame.type = SecurityElementType.Comment; if (locFrame.type != SecurityElementType.Regular) { throw new XmlSyntaxException(_t.LineNo); } locFrame.element.AddChild(newFrame.element); stack.Push(newFrame); needToBreak = true; } else if (i == Tokenizer.quest) { // Found a child that is a format node. Next up better be a cstr. i = _t.NextTokenType(); if (i != Tokenizer.cstr) { throw new XmlSyntaxException(_t.LineNo); } ParserStackFrame newFrame = new ParserStackFrame(); newFrame.element = new SecurityElement(_t.GetStringToken()); newFrame.type = SecurityElementType.Format; if (locFrame.type != SecurityElementType.Regular) { throw new XmlSyntaxException(_t.LineNo); } locFrame.element.AddChild(newFrame.element); newFrame.status = 1; stack.Push(newFrame); needToBreak = true; } else { throw new Exception(""); } break; case Tokenizer.equals: locFrame.sawEquals = true; break; case Tokenizer.ket: if (locFrame.intag) { locFrame.intag = false; continue; } else { throw new Exception(""); } // not reachable case Tokenizer.slash: locFrame.element.Text = null; i = _t.NextTokenType(); if (i == Tokenizer.ket) { // Found the end of this element lastType = stack.Peek().type; stack.Pop(); needToBreak = true; } else { throw new Exception(""); } break; case Tokenizer.quest: if (locFrame.intag && locFrame.type == SecurityElementType.Format && locFrame.status == 1) { i = _t.NextTokenType(); if (i == Tokenizer.ket) { lastType = stack.Peek().type; stack.Pop(); needToBreak = true; } else { throw new Exception(""); } } else { throw new XmlSyntaxException(_t.LineNo); } break; case Tokenizer.dash: if (locFrame.intag && (locFrame.status > 0 && locFrame.status < 5) && locFrame.type == SecurityElementType.Comment) { locFrame.status++; if (locFrame.status == 5) { i = _t.NextTokenType(); if (i == Tokenizer.ket) { lastType = stack.Peek().type; stack.Pop(); needToBreak = true; } else { throw new Exception(""); } } } else { throw new XmlSyntaxException(_t.LineNo); } break; default: throw new XmlSyntaxException(_t.LineNo); } if (needToBreak) { needToBreak = false; needToPop = false; break; } else { needToPop = true; } } if (needToPop) { lastType = stack.Peek().type; stack.Pop(); } else if (i == -1 && stack.GetCount() != 1) { // This means that we still have items on the stack, but the end of our // stream has been reached. throw new Exception(""); } }while (stack.GetCount() > 1); SecurityElement topElement = this.GetTopElement(); if (lastType == SecurityElementType.Format) { if (restarted) { throw new XmlSyntaxException(_t.LineNo); } String format = topElement.Attribute("encoding"); if (format != null) { _t.ChangeFormat(System.Text.Encoding.GetEncoding(format)); } _ecurr = new SecurityElement("junk"); ParseContents(_ecurr, true); } }
public void Push(ParserStackFrame element) { m_array.Add(element); }