private void WriteDocument(Variant document) { if ((m_mode & XmlMode.NoHeader) == 0) { WriteHeader(); } if ((m_mode & XmlMode.Preserve) != 0) { string rootName = ""; if (document.Is(VariantBase.EnumType.Mapping)) { foreach (VariantItem item in document) { if (item.Key == XmlConst.Text) { throw new VariantException("Encountered text in document node"); } else if (item.Key == XmlConst.Attributes) { throw new VariantException("Encountered attributes in document node"); } else if (item.Key == XmlConst.Instruction) { WriteInstruction(item.Value); } else if (item.Key == XmlConst.Comment) { WriteComment(item.Value); } else { if (rootName.Length == 0) { rootName = item.Key; } else { throw new VariantException(string.Format("Illegal element {0} encountered in document, expecting single element {1} at root", item.Key, rootName)); } Push(rootName); WriteElement(item.Value); Pop(); } } } else { throw new VariantException("Invalid document structure, root node must be a Dictionary or Bag"); } } else { Push(); WriteVariant(document); Pop(); } }
private static void SelectImpl(Variant input, string path, Variant result) { string head = "", tail = ""; Match myMatch = Regex.Match(path, @"[^/]+"); if (myMatch.Success) { head = myMatch.Value; tail = path.Substring(myMatch.Index + myMatch.Length); } else { result.Add(input); return; } string node = "", pred_key = "", pred_val = ""; myMatch = Regex.Match(head, @"^(\*|\w+)(\[@?(\w+)=""?(\w+)""?\])?"); if (myMatch.Success) { node = myMatch.Groups[1].Value; pred_key = myMatch.Groups[3].Value; pred_val = myMatch.Groups[4].Value; } else { throw new VariantException("Select path has invalid syntax: " + head); } Variant nodes = new Variant(EnumType.List); if (node.Length == 0 || node == "*") { foreach (VariantItem item in input) { nodes.Add(item.Value); } } else if (input.Is(EnumType.Mapping)) { foreach (VariantItem item in input.Range(node)) { nodes.Add(item.Value); } } foreach (VariantItem item in nodes) { if (pred_key.Length == 0 || (item.Value.Is(EnumType.Mapping) && item.Value.ContainsKey(pred_key) && item.Value[pred_key].AnyCast().As <string>() == pred_val)) { SelectImpl(item.Value, tail, result); } } }
private void WriteInstruction(Variant instruction) { if (instruction.Is(VariantBase.EnumType.Mapping) && instruction.ContainsKey(XmlConst.Target) && instruction.ContainsKey(XmlConst.Data)) { m_writer.WriteProcessingInstruction(instruction[XmlConst.Target].As <string>(), instruction[XmlConst.Data].As <string>()); } else { throw new VariantException(string.Format("Expecting dictionary containing '{0}' and '{1}' for processing instruction", XmlConst.Target, XmlConst.Data)); } }