void WriteNodeDeep(EmberNode frame) { var nodeStack = new Stack <StackElem>(); nodeStack.Push(new StackElem(frame, 1)); Console.WriteLine(frame.ToString()); while (nodeStack.Count > 0) { var elem = nodeStack.Peek(); var node = elem.Node; var position = elem.Position; var indent = elem.Indent; var isPushed = false; while (position.MoveNext()) { var current = position.Current; Console.WriteLine(new String(' ', indent * 3) + current.ToString()); if (current is EmberContainer) { nodeStack.Push(new StackElem(current, indent + 1)); isPushed = true; break; } } if (isPushed == false) { nodeStack.Pop(); } } }
/// <summary> /// Gets the integer value of the child leaf with the specified tag. /// Throws an exception if this EmberNode is not derived from EmberContainer. /// </summary> /// <param name="tag">The tag of the leaf whose value to return.</param> /// <param name="value">Received the value of the child node with the specified tag /// if a child leaf of type int or long exists.</param> /// <returns>True if a fitting child leaf exists and <paramref name="value"/> /// has received its value.</returns> public static bool GetInteger(this EmberNode node, BerTag tag, out long value) { var child = node[tag]; if (child != null && child.BerTypeNumber == BerType.Integer) { var integerLeaf = child as EmberLeaf <int>; if (integerLeaf != null) { value = integerLeaf.Value; return(true); } var longLeaf = child as EmberLeaf <long>; if (longLeaf != null) { value = longLeaf.Value; return(true); } } value = 0; return(false); }
public static GlowValue GetValue(EmberNode leaf) { var value = null as GlowValue; switch (leaf.BerTypeNumber) { case BerType.Integer: value = new GlowValue(GetIntegerNodeValue(leaf)); break; case BerType.Real: value = new GlowValue(((RealEmberLeaf)leaf).Value); break; case BerType.UTF8String: value = new GlowValue(((StringEmberLeaf)leaf).Value); break; case BerType.Boolean: value = new GlowValue(((BooleanEmberLeaf)leaf).Value); break; case BerType.OctetString: value = new GlowValue(((OctetStringEmberLeaf)leaf).Value); break; case BerType.Null: value = GlowValue.Null; break; } return(value); }
EmberNode Convert_Recurse(EmberNode parent) { var tagName = _reader.Name; var isEmpty = _reader.IsEmptyElement; var typeName = _reader["type"]; _reader.ReadStartElement(); var node = CreateNode(tagName, typeName); if(node != null) { if(node is EmberContainer) { while(_reader.IsStartElement()) Convert_Recurse(node); } if(parent != null) parent.InsertChildNode(node); } if(isEmpty == false) _reader.ReadEndElement(); return node; }
EmberNode Convert_Recurse(EmberNode parent) { var tagName = _reader.Name; var isEmpty = _reader.IsEmptyElement; var typeName = _reader["type"]; _reader.ReadStartElement(); var node = CreateNode(tagName, typeName); if (node != null) { if (node is EmberContainer) { while (_reader.IsStartElement()) { Convert_Recurse(node); } } if (parent != null) { parent.InsertChildNode(node); } } if (isEmpty == false) { _reader.ReadEndElement(); } return(node); }
/// <summary> /// Overriden to check if inserted GlowElements have the required tag: GlowTags.CollectionItem /// </summary> protected override void InsertChildNode(EmberNode node) { if(node is GlowElement && node.Tag != GlowTags.CollectionItem) throw new ArgumentException("When inserted into a GlowElementCollection, the GlowElement must have the ElementDefaultTag"); base.InsertChildNode(node); }
/// <summary> /// Override to check if inserted StringIntegerPairs have the tag: GlowTags.StringIntegerCollection.StringIntegerPair. /// </summary> protected override void InsertChildNode(EmberNode node) { if(node is GlowStringIntegerPair && node.Tag != GlowTags.StringIntegerCollection.StringIntegerPair) throw new ArgumentException("When inserted into a StringIntegerCollection, the StringIntegerPair must have the default tag!"); base.InsertChildNode(node); }
static long GetIntegerNodeValue(EmberNode node) { if (node is LongEmberLeaf) { return(((LongEmberLeaf)node).Value); } return(((IntegerEmberLeaf)node).Value); }
/// <summary> /// Converts <paramref name="node"/> to XML and writes the XML to <paramref name="writer"/>. /// </summary> public static void Export(EmberNode node, XmlWriter writer) { var export = new XmlExport(); var state = new XmlExportState(writer); node.Accept(export, state); writer.WriteWhitespace(Environment.NewLine); }
/// <summary> /// Override to check if inserted GlowStreamEntries have the tag: GlowTags.StreamCollection.StreamEntry /// </summary> protected override void InsertChildNode(EmberNode node) { if (node is GlowStreamEntry && node.Tag != GlowTags.StreamCollection.StreamEntry) { throw new ArgumentException("When inserted into a StreamCollection, the StreamEntry must have the ElementDefaultTag"); } base.InsertChildNode(node); }
/// <summary> /// Overriden to check if inserted GlowElements have the required tag: GlowTags.CollectionItem /// </summary> protected override void InsertChildNode(EmberNode node) { if (node is GlowElement && node.Tag != GlowTags.CollectionItem) { throw new ArgumentException("When inserted into a GlowElementCollection, the GlowElement must have the ElementDefaultTag"); } base.InsertChildNode(node); }
/// <summary> /// Override to check if inserted StringIntegerPairs have the tag: GlowTags.StringIntegerCollection.StringIntegerPair. /// </summary> protected override void InsertChildNode(EmberNode node) { if (node is GlowStringIntegerPair && node.Tag != GlowTags.StringIntegerCollection.StringIntegerPair) { throw new ArgumentException("When inserted into a StringIntegerCollection, the StringIntegerPair must have the default tag!"); } base.InsertChildNode(node); }
/// <summary> /// Gets the value of the child leaf with the specified tag. /// Throws an exception if this EmberNode is not derived from EmberContainer or /// if no child node of type EmberLeaf<TValue> with the specified tag exists. /// It is strongly recommended to use type specific methods such as GetInteger(), /// GetDateTime() etc. Only use this method if there is no specialized /// method for the type you want to get. /// </summary> /// <typeparam name="TValue">The type of the leaf value to get.</typeparam> /// <param name="tag">The tag of the leaf whose value to return.</param> /// <returns>The value of the child node with the specified tag.</returns> public static TValue Get <TValue>(this EmberNode node, BerTag tag) { var leaf = node[tag] as EmberLeaf <TValue>; if (leaf != null) { return(leaf.Value); } throw new BerException(5, "Leaf not found or type mismatch"); }
/// <summary> /// Gets the integer value of the child leaf with the specified tag. /// Throws an exception if this EmberNode is not derived from EmberContainer or /// if no child node of type EmberLeaf<int> or EmberLeaf<long> with the specified tag exists. /// </summary> /// <param name="tag">The tag of the leaf whose value to return.</param> /// <returns>The value of the child node with the specified tag.</returns> public static long GetInteger(this EmberNode node, BerTag tag) { long value; if (GetInteger(node, tag, out value)) { return(value); } throw new BerException(5, "Leaf not found or type mismatch"); }
/// <summary> /// Gets the RELATIVE OBJECT value of the child leaf with the specified tag. /// Throws an exception if this EmberNode is not derived from EmberContainer or /// if no child node of type EmberLeaf<int[]> with the specified tag exists. /// </summary> /// <param name="tag">The tag of the leaf whose value to return.</param> /// <returns>The value of the child node with the specified tag.</returns> public static int[] GetRelativeOid(this EmberNode node, BerTag tag) { int[] value; if (GetRelativeOid(node, tag, out value)) { return(value); } throw new BerException(5, "leaf not found or leaf type mismatch"); }
string DumpXml(EmberNode node) { var xmlBuffer = new StringBuilder(); var xmlSettings = new XmlWriterSettings { OmitXmlDeclaration = true }; using(var writer = XmlWriter.Create(xmlBuffer, xmlSettings)) XmlExport.Export(node, writer); var str = xmlBuffer.ToString(); Console.WriteLine(str); return str; }
protected internal override void InsertChildNode(EmberNode node) { if (node.Tag == CrcTag && node.BerTypeNumber == BerType.OctetString) { _crcNode = node; } else { base.InsertChildNode(node); } }
void Run(string path) { using (var stream = File.OpenRead(path)) { var reader = new EmberReader(new BerStreamInput(stream)); var app = new GlowApplicationInterface(); _glow = (GlowContainer)EmberNode.Decode(reader, app); } //var settings = new XmlWriterSettings //{ // Indent = true, // IndentChars = " ", // OmitXmlDeclaration = true, //}; //using(var writer = XmlWriter.Create(Console.Out, settings)) // XmlExport.Export(_glow, writer); //var children = _glow[GlowTags.CollectionItem][GlowTags.Node.Children]; //for(int index = 0; index < 2000; index++) //{ // var node = new GlowNode(5 + index) // { // Identifier = "abc" + index, // Description = "Hallo ich bin Node " + index, // }; // children.Insert(node); //} //using(var output = new BerStreamOutput(File.Create("big.ember"))) // _glow.Encode(output); var listener = new TcpListener(IPAddress.Any, 9097); listener.Start(); listener.BeginAcceptSocket(AcceptCallback, listener); Console.WriteLine("Press Enter to quit..."); Console.ReadLine(); lock (_sync) { foreach (var client in _clients) { client.Close(); } } listener.Stop(); }
void Test_InteropDom() { Console.WriteLine("\r\n------------------------ Interop DOM"); var testFilePath = @"N:\Temp\test.ber"; using (var stream = File.OpenRead(testFilePath)) { var input = new BerStreamInput(stream); var reader = new EmberReader(input); var root = EmberNode.Decode(reader, this); Console.WriteLine(GetXml(root)); } }
string DumpXml(EmberNode node) { var xmlBuffer = new StringBuilder(); var xmlSettings = new XmlWriterSettings { OmitXmlDeclaration = true }; using (var writer = XmlWriter.Create(xmlBuffer, xmlSettings)) XmlExport.Export(node, writer); var str = xmlBuffer.ToString(); Console.WriteLine(str); return(str); }
/// <summary> /// Gets the value of the child leaf with the specified tag. /// Throws an exception if this EmberNode is not derived from EmberContainer. /// </summary> /// <typeparam name="TValue">The type of the leaf value to get.</typeparam> /// <param name="tag">The tag of the leaf whose value to return.</param> /// <param name="value">Received the value of the child node with the specified tag /// if a child leaf of type TValue exists.</param> /// <returns>True if a fitting child leaf exists and <paramref name="value"/> /// has received its value.</returns> public static bool Get <TValue>(this EmberNode node, BerTag tag, out TValue value) { var leaf = node[tag] as EmberLeaf <TValue>; if (leaf != null) { value = leaf.Value; return(true); } else { value = default(TValue); return(false); } }
void AssertCodecSanity(EmberNode ember) { var originalXml = GetXml(ember); var output = new BerMemoryOutput(); ember.Encode(output); var input = new BerMemoryInput(output.Memory); var reader = new EmberReader(input); var decoded = EmberNode.Decode(reader, new GlowApplicationInterface()); var decodedXml = GetXml(decoded); if(originalXml != decodedXml) throw new Exception("Codec error!"); }
void AssertCodecSanity(EmberNode ember) { var originalXml = GetXml(ember); var output = new BerMemoryOutput(); ember.Encode(output); var input = new BerMemoryInput(output.Memory); var reader = new EmberReader(input); var decoded = EmberNode.Decode(reader, new GlowApplicationInterface()); var decodedXml = GetXml(decoded); if (originalXml != decodedXml) { throw new Exception("Codec error!"); } }
/// <summary> /// Gets the RELATIVE OBJECT value of the child leaf with the specified tag. /// Throws an exception if this EmberNode is not derived from EmberContainer. /// </summary> /// <param name="tag">The tag of the leaf whose value to return.</param> /// <param name="value">Receives the value of the child node with the specified tag /// if a child leaf of type RelativeOidEmberLeaf exists.</param> /// <returns>True if a fitting child leaf exists and <paramref name="value"/> /// has received its value.</returns> public static bool GetRelativeOid(this EmberNode node, BerTag tag, out int[] value) { var child = node[tag]; if (child != null && child.BerTypeNumber == BerType.RelativeOid) { var leaf = child as RelativeOidEmberLeaf; if (leaf != null) { value = leaf.Value; return(true); } } value = null; return(false); }
void loadButton_Click(object sender, RoutedEventArgs e) { var dialog = new OpenFileDialog { Filter = EmberFileDialogFilter, }; if (dialog.ShowDialog() == true) { using (var stream = dialog.File.OpenRead()) { var input = new BerStreamInput(stream); var reader = new EmberReader(input); var glow = EmberNode.Decode(reader, new GlowApplicationInterface()) as GlowContainer; if (glow != null) { DecodeGlow(glow); } } } }
protected internal override void InsertChildNode(EmberNode node) { if(IsMapUsed) { var tag = node.Tag; var nodesMap = NodesMap; if(nodesMap.ContainsKey(tag)) { IsMapUsed = false; nodesMap.Clear(); BerTypeNumber = ChangeToSequence(); } else { nodesMap[tag] = node; } } InsertChildNodeBase(node); }
protected internal override void InsertChildNode(EmberNode node) { if (IsMapUsed) { var tag = node.Tag; var nodesMap = NodesMap; if (nodesMap.ContainsKey(tag)) { IsMapUsed = false; nodesMap.Clear(); BerTypeNumber = ChangeToSequence(); } else { nodesMap[tag] = node; } } InsertChildNodeBase(node); }
/// <summary> /// Overriden to check if inserted GlowElements have the required tag: GlowTags.CollectionItem /// </summary> protected override void InsertChildNode(EmberNode node) { if (node is GlowElement && node.Tag != GlowTags.CollectionItem) { throw new ArgumentException("When inserted into a GlowElementCollection, the GlowElement must have the ElementDefaultTag"); } switch (node.BerTypeNumber) { case GlowType.Command: case GlowType.Parameter: case GlowType.Node: case GlowType.Matrix: case GlowType.Function: break; default: throw new ArgumentException("A GlowElementCollection must not contain this node type!"); } base.InsertChildNode(node); }
/// <summary> /// Inserts a new REAL leaf into this node's collection of children. /// </summary> /// <param name="tag">The tag of the leaf to create and insert.</param> /// <param name="value">The value of the leaf to create and insert.</param> public static void Insert(this EmberNode node, BerTag tag, double value) { node.Insert(new RealEmberLeaf(tag, value)); }
/// <summary> /// Inserts a new INTEGER (32bit) leaf into this node's collection of children. /// </summary> /// <param name="tag">The tag of the leaf to create and insert.</param> /// <param name="value">The value of the leaf to create and insert.</param> public static void Insert(this EmberNode node, BerTag tag, int value) { node.Insert(new IntegerEmberLeaf(tag, value)); }
protected internal override void InsertChildNode(EmberNode node) { if(node.Tag == CrcTag && node.BerTypeNumber == BerType.OctetString) _crcNode = node; else base.InsertChildNode(node); }
/// <summary> /// Inserts a new boolean leaf into this node's collection of children. /// </summary> /// <param name="tag">The tag of the leaf to create and insert.</param> /// <param name="value">The value of the leaf to create and insert.</param> public static void Insert(this EmberNode node, BerTag tag, bool value) { node.Insert(new BooleanEmberLeaf(tag, value)); }
/// <summary> /// Called when a TLV (either container or primitive) has been decoded. /// The read TLV can be examined through <paramref name="node"/>. /// </summary> /// <param name="node">An EmberNode object containing the decoded information. /// This object is ready for use, all significant properties (like Tag, Parent, Type) /// have valid values. /// You can use the EmberNode.Accept() method to pass an IEmberVisitor implementation to /// examine the node.</param> protected virtual void OnItemReady(EmberNode node) { }
/// <summary> /// Returns the root node of the decoded tree and resets the reader. /// </summary> /// <returns>The root node of the decoded tree if IsRootReady is true, otherwise null.</returns> public EmberNode DetachRoot() { if(IsRootReady) { var root = _rootNode; _rootNode = null; _currentNode = null; IsRootReady = false; return root; } return null; }
/// <summary> /// Inserts a new OBJECT IDENTIFIER leaf into this node's collection of children. /// </summary> /// <param name="tag">The tag of the leaf to create and insert.</param> /// <param name="values">The value of the leaf to create and insert.</param> public static void InsertOid(this EmberNode node, BerTag tag, int[] values) { node.Insert(new ObjectIdentifierEmberLeaf(tag, values)); }
/// <summary> /// Resets the reader state. Overridden to free all memory assigned for /// DOM nodes being decoded. /// </summary> public override void Reset() { base.Reset(); _rootNode = null; _currentNode = null; IsRootReady = false; }
void RaiseItemReady(EmberNode node) { node.ValidateAfterDecode(); OnItemReady(node); }
string GetXml(EmberNode ember) { return(GetXmlBody(writer => XmlExport.Export(ember, writer))); }
/// <summary> /// Called when the opening tag and length of a new container have been /// read. /// The opened container can be accessed through <paramref name="node" />. /// </summary> /// <param name="node">The newly created EmberNode to hold the container's children. /// The children will not be included in the node until OnItemReady(EmberNode) is /// called. /// You can use the EmberNode.Accept() method to pass an IEmberVisitor implementation to /// examine the node.</param> protected virtual void OnNewContainer(EmberNode node) { }
public ValidationErrorArgs(EmberNode node, string message) { Node = node; Message = message; }
/// <summary> /// Overridden to create DOM nodes from the decoded TLV. /// In derived classes, override the OnItemReady(EmberNode) /// overload. /// </summary> protected override sealed void OnItemReady() { Debug.Assert(_rootNode != null); Debug.Assert(_currentNode != null); if(IsContainer) { EmberNode readyNode; if(_currentNode == _rootNode) { IsRootReady = true; OnRootReady(new RootReadyArgs(_currentNode)); readyNode = _currentNode; _currentNode = null; } else { readyNode = _currentNode; _currentNode = _currentNode.Parent; } RaiseItemReady(readyNode); } else { var node = EmberNode.FromReader(this, _application); if(node != null) { _currentNode.InsertChildNode(node); RaiseItemReady(node); } } }
/// <summary> /// Inserts a new UTF8String leaf into this node's collection of children. /// </summary> /// <param name="tag">The tag of the leaf to create and insert.</param> /// <param name="value">The value of the leaf to create and insert.</param> public static void Insert(this EmberNode node, BerTag tag, string value) { node.Insert(new StringEmberLeaf(tag, value)); }
/// <summary> /// Inserts a new OCTET STRING leaf into this node's collection of children. /// </summary> /// <param name="tag">The tag of the leaf to create and insert.</param> /// <param name="value">The value of the leaf to create and insert.</param> public static void Insert(this EmberNode node, BerTag tag, byte[] value) { node.Insert(new OctetStringEmberLeaf(tag, value)); }
public StackElem(EmberNode oNode, int nIndent) { this.Node = oNode; this.Position = oNode.GetEnumerator(); this.Indent = nIndent; }
/// <summary> /// Inserts a new RELATIVE OBJECT leaf into this node's collection of children. /// </summary> /// <param name="tag">The tag of the leaf to create and insert.</param> /// <param name="value">The value of the leaf to create and insert.</param> public static void InsertRelativeOid(this EmberNode node, BerTag tag, int[] value) { node.Insert(new RelativeOidEmberLeaf(tag, value)); }
void WriteNodeDeep(EmberNode frame) { var nodeStack = new Stack<StackElem>(); nodeStack.Push(new StackElem(frame, 1)); Console.WriteLine(frame.ToString()); while(nodeStack.Count > 0) { var elem = nodeStack.Peek(); var node = elem.Node; var position = elem.Position; var indent = elem.Indent; var isPushed = false; while(position.MoveNext()) { var current = position.Current; Console.WriteLine(new String(' ', indent * 3) + current.ToString()); if(current is EmberContainer) { nodeStack.Push(new StackElem(current, indent + 1)); isPushed = true; break; } } if(isPushed == false) nodeStack.Pop(); } }
public RootReadyArgs(EmberNode root) { Root = root; }
/// <summary> /// Overridden to create DOM nodes from the decoded TLV. /// In derived classes, override the OnNewContainer(EmberNode) /// overload. /// </summary> protected override sealed void OnNewContainer() { Debug.Assert(_currentNode == null || _currentNode is EmberContainer); var container = EmberNode.FromReader(this, _application); Debug.Assert(container != null); if(IsRootReady) { Debug.Assert(_rootNode != null); Debug.Assert(_currentNode == null); _rootNode = null; IsRootReady = false; } if(_rootNode == null) { Debug.Assert(_currentNode == null); _rootNode = container; } else { Debug.Assert(_currentNode != null); _currentNode.InsertChildNode(container); } _currentNode = container; OnNewContainer(container); }
internal void RaiseValidationError(EmberNode node, string message) { OnValidationError(new ValidationErrorArgs(node, message)); }
string GetXml(EmberNode ember) { return GetXmlBody(writer => XmlExport.Export(ember, writer)); }