public override XmlParserContext TryRecreateState(XObject xobject, int position) { if (xobject is XText text && position >= text.Span.Start && position < text.Span.End) { // truncate to length var sb = new System.Text.StringBuilder(text.Text); sb.Length = position - text.Span.Start; var parents = NodeStack.FromParents(text); if (sb.Length > 0) { parents.Push(new XText(text.Span.Start)); } return(new XmlParserContext { CurrentState = this, Position = position, PreviousState = Parent, CurrentStateLength = sb.Length, KeywordBuilder = sb, Nodes = parents, StateTag = position }); } return(null); }
public override XmlParserContext TryRecreateState(XObject xobject, int position) { if (xobject is XComment comment && position >= comment.Span.Start + STARTOFFSET && position < comment.Span.End) { var parents = NodeStack.FromParents(comment); var length = position - comment.Span.Start + STARTOFFSET; if (length > 0) { parents.Push(new XComment(comment.Span.Start)); } return(new XmlParserContext { CurrentState = this, Position = position, PreviousState = Parent, CurrentStateLength = length, KeywordBuilder = new System.Text.StringBuilder(), StateTag = position == comment.Span.End - 3 ? SINGLE_DASH : (position == comment.Span.End - 2 ? DOUBLE_DASH: NOMATCH), Nodes = parents }); } return(null); }
static List <XObject> ToNodePath(this NodeStack stack) { var path = new List <XObject> (stack); path.Reverse(); return(path); }
public override XmlParserContext TryRecreateState(XObject xobject, int position) { if (xobject is XProcessingInstruction pi && position >= pi.Span.Start + STARTOFFSET && position < pi.Span.End) { var parents = NodeStack.FromParents(pi); var length = position - pi.Span.Start + STARTOFFSET; if (length > 0) { parents.Push(new XProcessingInstruction(pi.Span.Start)); } return(new XmlParserContext { CurrentState = this, Position = position, PreviousState = Parent, CurrentStateLength = length, KeywordBuilder = new System.Text.StringBuilder(), StateTag = position == pi.Span.End - 2? QUESTION : NOMATCH, Nodes = parents }); } return(null); }
public void Reset() { CurrentState = RootState; previousState = RootState; Position = 0; stateTag = 0; keywordBuilder = new StringBuilder(); CurrentStateLength = 0; Nodes = new NodeStack(); Nodes.Push(RootState.CreateDocument()); }
internal static NodeStack FromParents(XObject fromObject) { var newStack = new NodeStack(); DepthFirstAddParentsToStack(fromObject); void DepthFirstAddParentsToStack(XObject o) { if (o.Parent is XObject parent) { DepthFirstAddParentsToStack(parent); newStack.Push(parent.ShallowCopy()); } } return(newStack); }
XmlParser(XmlParser copyFrom) { BuildTree = false; RootState = copyFrom.RootState; CurrentState = copyFrom.CurrentState; previousState = copyFrom.previousState; Position = copyFrom.Position; stateTag = copyFrom.stateTag; keywordBuilder = new StringBuilder(copyFrom.keywordBuilder.ToString()); CurrentStateLength = copyFrom.CurrentStateLength; //clone the node stack var l = new List <XObject> (CopyXObjects(copyFrom.Nodes)); l.Reverse(); Nodes = new NodeStack(l); }
public override XmlParserContext TryRecreateState(XObject xobject, int position) { return (TagState.TryRecreateState(xobject, position) ?? ClosingTagState.TryRecreateState(xobject, position) ?? CommentState.TryRecreateState(xobject, position) ?? CDataState.TryRecreateState(xobject, position) ?? DocTypeState.TryRecreateState(xobject, position) ?? ProcessingInstructionState.TryRecreateState(xobject, position) ?? TextState.TryRecreateState(xobject, position) ?? new XmlParserContext { CurrentState = this, Position = xobject.Span.Start, PreviousState = Parent, CurrentStateLength = 0, KeywordBuilder = new System.Text.StringBuilder(), Nodes = NodeStack.FromParents(xobject), StateTag = FREE }); }
XmlParser(XmlParser copyFrom) { buildTree = false; rootState = copyFrom.rootState; currentState = copyFrom.currentState; previousState = copyFrom.previousState; position = copyFrom.position; previousLineEnd = copyFrom.location; location = copyFrom.location; stateTag = copyFrom.stateTag; keywordBuilder = new StringBuilder(copyFrom.keywordBuilder.ToString()); currentStateLength = copyFrom.currentStateLength; //clone the node stack var l = new List <XObject> (CopyXObjects(copyFrom.nodes)); l.Reverse(); nodes = new NodeStack(l); }
public void Reset() { currentState = rootState; previousState = rootState; position = 0; stateTag = 0; location = new DocumentLocation(1, 1); previousLineEnd = DocumentLocation.Empty; keywordBuilder = new StringBuilder(); currentStateLength = 0; nodes = new NodeStack(); nodes.Push(rootState.CreateDocument()); if (buildTree) { errors = new List <Error> (); } else { errors = null; } }
public override XmlParserContext TryRecreateState(XObject xobject, int position) { var fromAtt = AttributeState.TryRecreateState(xobject, position); if (fromAtt != null) { return(fromAtt); } // we can also recreate state for attributes within the tag, if the attribute state didn't var el = xobject as XElement; if (el == null && xobject is XAttribute a) { el = (XElement)a.Parent; } if (el != null && position >= el.Span.Start && position < el.Span.End) { // recreating name builder and value builder state is a pain to get right // for now, let parent recreate state at start of tag if (position <= el.NameSpan.End) { return(null); } // if there are attributes, then at the start of an attribute is also a pretty safe place to recreate state // but if not, let parent recreate state at start of tag if (el.Attributes.First == null) { return(null); } var newEl = new XElement(el.Span.Start, el.Name); int prevStateEnd = el.NameSpan.End; XmlParserState prevState = NameState; foreach (var att in el.Attributes) { if (att.Span.End < position) { prevStateEnd = att.Span.End; prevState = AttributeState; //spine parser is currently expected to have attributes newEl.Attributes.AddAttribute((XAttribute)att.ShallowCopy()); continue; } if (att.Span.End > position) { position = att.Span.Start; break; } } var parents = NodeStack.FromParents(el); parents.Push(newEl); return(new XmlParserContext { CurrentState = this, Position = position, PreviousState = prevState, CurrentStateLength = position - prevStateEnd, KeywordBuilder = new StringBuilder(), Nodes = parents, StateTag = FREE }); } return(null); }