//------------------------------------------------------------------------------
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//
// [....]
//------------------------------------------------------------------------------
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using System.Text;
using System.Xml;
namespace System.Xml.Xsl.Qil {
///
/// If an annotation implements this interface, then QilXmlWriter will call ToString() on the annotation
/// and serialize the result (if non-empty).
///
interface IQilAnnotation {
string Name { get; }
};
///
/// An example of QilVisitor. Prints the QilExpression tree as XML.
///
///
/// The QilXmlWriter Visits every node in the tree, printing out an XML representation of
/// each node. Several formatting options are available, including whether or not to include annotations
/// and type information. When full information is printed out, the graph can be reloaded from
/// its serialized form using QilXmlReader.
/// The XML format essentially uses one XML element for each node in the QIL graph.
/// Node properties such as type information are serialized as XML attributes.
/// Annotations are serialized as processing-instructions in front of a node.
/// Feel free to subclass this visitor to customize its behavior.
///
internal class QilXmlWriter : QilScopedVisitor {
protected XmlWriter writer;
protected Options options;
private NameGenerator ngen;
[Flags]
public enum Options {
None = 0, // No options selected
Annotations = 1, // Print annotations
TypeInfo = 2, // Print type information using "G" option
RoundTripTypeInfo = 4, // Print type information using "S" option
LineInfo = 8, // Print source line information
NodeIdentity = 16, // Print node identity (only works if QIL_TRACE_NODE_CREATION is defined)
NodeLocation = 32, // Print node creation location (only works if QIL_TRACE_NODE_CREATION is defined)
};
///
/// Construct a QilXmlWriter.
///
public QilXmlWriter(XmlWriter writer) : this(writer, Options.Annotations | Options.TypeInfo | Options.LineInfo | Options.NodeIdentity | Options.NodeLocation) {
}
///
/// Construct a QilXmlWriter.
///
public QilXmlWriter(XmlWriter writer, Options options) {
this.writer = writer;
this.ngen = new NameGenerator();
this.options = options;
}
///
/// Serialize a QilExpression graph as XML.
///
/// the QilExpression graph
public void ToXml(QilNode node) {
VisitAssumeReference(node);
}
//-----------------------------------------------
// QilXmlWrite methods
//-----------------------------------------------
///
/// Write all annotations as comments:
/// 1. string --
/// 2. IQilAnnotation --
/// 3. IList
protected virtual void WriteAnnotations(object ann) {
string s = null, name = null;
if (ann == null) {
return;
}
else if (ann is string) {
s = ann as string;
}
else if (ann is IQilAnnotation) {
// Get annotation's name and string value
IQilAnnotation qilann = ann as IQilAnnotation;
name = qilann.Name;
s = ann.ToString();
}
else if (ann is IList