GoDigram for .NET Framework and .NET Core
Northwoods.Go.Xml Namespace / GoXmlTransformer Class
Members Example
In This Topic
    GoXmlTransformer Class
    In This Topic
    This abstract class holds methods called by GoXmlWriter and GoXmlReader, used to generate or parse XML for objects of a particular class.
    Syntax
    public abstract class GoXmlTransformer : IGoXmlTransformer  
    Remarks

    When you define transformers for your application's object classes, you will want to inherit from this class. This provides standard implementations for all of the IGoXmlTransformer methods, which basically just call the same method on the base type's transformer.

    To make it easier to access the Writer's and Reader's properties and methods, a number of their properties and methods are provided here also, whose implementations just delegate to the writer or reader. An instance of this class can only be used for one GoXmlWriter or GoXmlReader at a time, not for both at once.

    Example
    This continues the examples given with the description of GoXmlWriter and GoXmlReader, defining simple transformers for GoBasicNode and GoLabeledLink.
    public class SimpleXmlTransformBasicNode : GoXmlTransformer {
      public SimpleXmlTransformBasicNode() {
        this.TransformerType = typeof(GoBasicNode); 
        this.ElementName = "node";
        this.IdAttributeUsedForSharedObjects = true;
      }
                
      public override void GenerateAttributes(Object obj) {
        base.GenerateAttributes(obj);
        GoBasicNode n = (GoBasicNode)obj;
        WriteAttrVal("label", n.Text);
        WriteAttrVal("shapebounds", n.SelectionObject.Bounds);
      }
      
      public override Object Allocate() {
        GoBasicNode n = new GoBasicNode();
        // you might want to do other common initialization here
        return n;
      }
      public override void ConsumeAttributes(Object obj) {
        base.ConsumeAttributes(obj);
        GoBasicNode n = (GoBasicNode)obj;
        n.Text = StringAttr("label", "");
        n.SelectionObject.Bounds = RectangleFAttr("shapebounds", new RectangleF(100, 100, 40, 50));
      }
    }
                  
    public class SimpleXmlTransformLink : GoXmlTransformer {
      public SimpleXmlTransformLink() {
        this.TransformerType = typeof(GoLabeledLink);
        this.ElementName = "link";
      }
      
      public override void GenerateAttributes(Object obj) {
        base.GenerateAttributes(obj);
        GoLabeledLink link = (GoLabeledLink)obj;
        GoNode n = link.FromNode as GoNode;
        WriteAttrVal("from", this.Writer.FindShared(n));
        n = link.ToNode as GoNode;
        WriteAttrVal("to", this.Writer.FindShared(n));
        GoText lab = link.MidLabel as GoText;
        if (lab != null)
          WriteAttrVal("label", lab.Text);
      }
      
      public override Object Allocate() {
        GoLabeledLink ll = new GoLabeledLink();
        GoText lab = new GoText();
        lab.Selectable = false;
        ll.MidLabel = lab;
        // you might want to do other common initialization here
        return ll;
      }
      public override void ConsumeAttributes(Object obj) {
        base.ConsumeAttributes(obj);
        GoLabeledLink link = (GoLabeledLink)obj;
        String fromid = StringAttr("from", null);
        GoBasicNode from = this.Reader.FindShared(fromid) as GoBasicNode;
        if (from != null) {
          link.FromPort = from.Port;
        }
        String toid = StringAttr("to", null);
        GoBasicNode to = this.Reader.FindShared(toid) as GoBasicNode;
        if (to != null) {
          link.ToPort = to.Port;
        }
        GoText lab = link.MidLabel as GoText;
        if (lab != null && IsAttrPresent("label")) {
          lab.Text = StringAttr("label", lab.Text);
        }
      }
    }
    See Also