/// <summary> /// </summary> private static Node FromCGML_Legacy(string str) { if (string.IsNullOrEmpty(str)) { return(null); } Node root = null; bool inString = false; bool wasInString = false; bool inNode = false; bool inValue = false; bool inKey = false; bool inAttribute = false; char lastChar = (char)0; Node thisNode = null; StringBuilder thisKey = new StringBuilder(); StringBuilder thisValue = null; for (int i = 0; i <= str.Length; i++) { if (i < str.Length) { char c = str[i]; char nextChar = i < str.Length - 1 ? str[i + 1] : (char)0; if (inString) { if (c != CGML.STRING_BEGIN_END || (str[i - 1] == '\\' && str[i - 2] != '\\')) { thisValue.Append(c); } else { inString = false; wasInString = true; } } else { Node newNode = null; if (wasInString) { if (inNode) { if (inAttribute) { thisNode.Attributes.Set(new Attribute(thisKey.ToString(), thisValue.ToString())); thisValue = null; inAttribute = false; } } } switch (c) { // Ignore newlines case '\n': break; // Ignore carriage return case '\r': break; // Ignore tabs case '\t': break; case CGML.NODE_BEGIN: inNode = true; inKey = true; thisKey = new StringBuilder(); break; case CGML.NODE_END: newNode = new Node(thisKey.ToString()); inNode = false; inKey = false; inValue = false; inAttribute = false; break; case CGML.NODE_ENDMARK: thisNode = thisNode.Parent; break; case CGML.STRING_BEGIN_END: inString = true; thisValue = new StringBuilder(); break; case CGML.NODE_VALUE_BEGIN: inKey = false; inValue = true; break; case CGML.NODE_VALUE_END: inValue = false; break; case CGML.EQUAL_OPERATOR: inValue = true; break; case ' ': inKey = false; inAttribute = true; break; case (char)0: throw new System.Exception("[CGML] Could not parse string"); default: if (inNode) { if (inKey) { thisKey.Append(c); } else if (inValue) { thisValue.Append(c); } } break; } wasInString = false; if (newNode != null) { if (root == null) { root = newNode; } else { thisNode.Push(newNode); } thisNode = newNode; } } lastChar = c; } } return(root); }
// TODO: add comments (ez) // <! this is a comment !> /// <summary> /// </summary> private static Node FromCGML_0_4_0(string str) { if (string.IsNullOrEmpty(str)) { return(null); } Node root = null; ReadElement elementType = ReadElement.None; bool reading = false; ReadScope scope = ReadScope.Default; Node thisNode = null; StringBuilder element = new StringBuilder(); char lastChar = (char)0; Func <string> commitElement = new Func <string>(() => { string s = element.ToString(); element.Clear(); return(s); }); Action commitNodeKey = new Action(() => { Node newNode = new Node(commitElement()); if (root == null) { root = newNode; } else { thisNode.Push(newNode); } thisNode = newNode; }); Action commitAttributeKey = new Action(() => { thisNode.Attributes.Set(new Attribute(commitElement())); elementType = ReadElement.None; }); Action commitValue = new Action(() => { switch (scope) { case ReadScope.Node: { thisNode.Value = commitElement(); break; } case ReadScope.Attribute: { thisNode.Attributes[thisNode.Attributes.Count - 1].Value = commitElement(); break; } } elementType = ReadElement.None; }); for (int i = 0; i < str.Length; i++) { if (i < str.Length) { char c = str[i]; char nextChar = i < str.Length - 1 ? str[i + 1] : (char)0; if (reading) { switch (c) { case CGML.STRING_BEGIN_END: { if (lastChar == '\\') { break; } commitValue(); reading = false; break; } default: { element.Append(c); break; } } } else { switch (c) { // Ignore newlines case '\n': break; // Ignore carriage return case '\r': break; // Ignore tabs case '\t': break; case CGML.NODE_BEGIN: { scope = ReadScope.Node; elementType = ReadElement.Key; break; } case CGML.NODE_END: { scope = ReadScope.Default; elementType = ReadElement.None; break; } case CGML.STRING_BEGIN_END: { elementType = ReadElement.Value; reading = true; break; } case CGML.EQUAL_OPERATOR: { if (elementType == ReadElement.Key && scope == ReadScope.Attribute) { commitAttributeKey(); break; } break; } case ' ': { scope = ReadScope.Attribute; elementType = ReadElement.Key; break; } case CGML.NODE_ENDMARK: { thisNode = thisNode.Parent; break; } default: { if (elementType != ReadElement.None) { element.Append(c); } break; } } } if (scope == ReadScope.Node && elementType == ReadElement.Key) { if (nextChar == CGML.NODE_END || nextChar == CGML.EQUAL_OPERATOR || nextChar == ' ') { commitNodeKey(); } } lastChar = c; } } return(root); }