OpenPetra
Free Administration Software for Non-Profits
Ict.Common.IO.TYml2Xml Class Reference

TYml2Xml is able to parse a YML file and store it in an XmlDocument More...

Inheritance diagram for Ict.Common.IO.TYml2Xml:
Ict.Common.IO.TSimpleYmlParser

Public Member Functions

 TYml2Xml (string AFilename)
 constructor More...
 
 TYml2Xml (string[] ALines)
 constructor More...
 
XmlDocument ParseYML2XML ()
 loads an yml document into one xml document More...
 
XmlNode ParseYML2TaskListRoot ()
 loads an yml document into one xml document; then returns only the part meant to be passed directly to a TaskList constructor as the TaskList root. More...
 
XmlDocument ParseYML2XML (XmlDocument AMergeDoc, int ADepth)
 merges several yml documents into one xml document, and maintains their inheritance hierarchy for nodes that have the same name More...
 

Static Public Member Functions

static string GetElementName (XmlNode ANode)
 return either the name from the attribute, or the element name More...
 
static bool CheckName (string AElementName)
 check if the name can be used as an element name for XML More...
 
static string Xml2YmlGz (XmlDocument ADoc)
 format the XML into zipped YML and return as Base64 string More...
 
static string Xml2Yml (XmlDocument ADoc)
 format the XML into YML and return as string More...
 
static bool Xml2Yml (XmlDocument ADoc, string AOutYMLFile)
 format the XML into YML to increase readability and save to file More...
 
static bool ReadHeader (string AFilename, out string baseYamlOrClass)
 This method can be used to check if this is the correct file type, and also find out the baseyaml or baseclass More...
 
static SortedList ReferenceNodes (XmlDocument myDoc)
 fill sorted list which contains a reference to each node by name More...
 
static XmlDocument CreateXmlDocument ()
 create an empty xml document, which will be filled with the data from the yaml file More...
 
static bool Merge (ref XmlDocument AMergeDoc, XmlDocument ANewDoc, int ADepth)
 merge the specificDoc into the mergeDoc. merges 2 xml documents, maintains their inheritance hierarchy for nodes that have the same name. allows caching of base yaml files. More...
 
static List< XmlNode > GetChildren (XmlNode node, bool AConsiderBase)
 get all children nodes of a given node; depending on parameter, include the inherited nodes or not the nodes will be sorted by the order flag (AlwaysFirst, AlwaysLast) More...
 
static XmlNode Parent (XmlNode node)
 get the parent node More...
 
static void SetAttribute (XmlNode xmlNode, string name, string value)
 SetAttribute will never consider the base and the xml hierarchy; use Tag to move things to base. More...
 
static void ClearAttribute (XmlNode xmlNode, string name)
 remove attribute More...
 
static bool HasAttribute (XmlNode xmlNode, string name)
 check for the attribute; if the current node does not have it, check the base node More...
 
static string GetAttribute (XmlNode xmlNode, string name)
 get the attribute; if the current node does not have it, check the base node More...
 
static string GetAttributeRecursive (XmlNode xmlNode, string name)
 if the current node does not have the attribute, try the parent nodes More...
 
static bool HasAttributeRecursive (XmlNode xmlNode, string name)
 if the current node does not have the attribute, try the parent nodes More...
 
static SortedList< string, string > GetAttributes (XmlNode xmlNode)
 get all attributes, even from base node More...
 
static XmlNode GetChild (XmlNode node, string childName)
 get the child node with the given name; considers base nodes as well More...
 
static StringCollection GetElements (XmlNode node, string childName)
 overload for GetElements, with the main node and the name of the child node that has the elements More...
 
static StringCollection GetElements (XmlNode node)
 
static void Tag (XmlNode ANode)
 Tag will move all leafs to a new child element called base a leaf is an XmlNode that has no children, only attributes all attributes are moved to base can only be called once todo: what about sequences More...
 

Static Public Attributes

const string XMLLIST = "XmlList"
 the name used for elements in a list More...
 
const string XMLELEMENT = "XmlElement"
 the name used for elements in generated xml code More...
 
const string ROOTNODEINTERNAL = "RootNodeInternal"
 the name used for root node in generated xml code More...
 

Protected Member Functions

void ThrowException (string AMessage, Int32 lineNr)
 throw an exception and tell the current position while reading the yaml file More...
 
Int32 GetAbsoluteIndentation (Int32 lineNr)
 
Int32 GetAbsoluteIndentationNext (Int32 lineNr)
 
Int32 GetIndentationNext (Int32 lineNr)
 
string GetNextLine ()
 does not return comment lines More...
 
bool SplitNode (string line, out string nodeName, out string nodeContent)
 split a line into the node name and the node content More...
 
string StripQuotes (string s)
 strip the quotes from a string More...
 

Static Protected Member Functions

static XmlNode LoadChild (XmlNode parent, string nodeName, int ADepth)
 
static XmlNode GetBaseNode (XmlNode ANode)
 check if parent already has a base element More...
 

Protected Attributes

string[] lines = null
 contains the lines of the yml document More...
 
Int32 currentLine = -1
 the current line that we are parsing More...
 
string filename = ""
 the filename of the file that we are parsing More...
 

Detailed Description

TYml2Xml is able to parse a YML file and store it in an XmlDocument

Simple YAML to XML converter could have also used http://yaml-net-parser.sourceforge.net/default.html#intro But I prefer to write YAML files but to work with XML in the program. The yaml net parser would just be another interface to understand how to step through the document.

See also the spec http://yaml.org/spec/1.2/

We only support: indentation: http://yaml.org/spec/1.2/#id2577368 only use spaces, no tabs for indentation Each node must be indented further than its parent node. All sibling nodes must use the exact same indentation level.

comment lines start with #, can be indented separators are comma and colon, lists are defined by curly and square brackets {} and [] values can be quoted to escape the colon or comma

name and colon (name:) is converted into an XML element as a parent for the following indented lines indention only happens after node and colon without content scalar: name and colon and a literal is converted into an attribute of the current XML element sequence: [element1, element2, element3] are translated to <element name="element1"/><element name="element2"/> below the parent node mapping: name and value assignments {size=10, help=Test with spaces} are converted into attributes of the parent node

not supported: node names cannot be quoted to escape colons {params: size=10, help=Test with spaces}: leave out the params [=name, =id, address] sorting columns are not identified this way

This class supports the loading of several derived files, and overwriting the data of the base files; todo: make a tag so that everything is pushed into base, before the last file is loaded change the code: move only attributes into base tag add an attribute to elements that have been in base, base="yes" this way the order of elements can be maintained easily, but it is still known which elements and attributes have been added since the tag reactivate the check again, in ProcessXAML.cs: if (TXMLParser.GetAttribute(rootNode, "ClassType") != "abstract") todo: save the last file after modification have been done to the xml structure (eg. loaded from csharp file)

Constructor & Destructor Documentation

◆ TYml2Xml() [1/2]

Ict.Common.IO.TYml2Xml.TYml2Xml ( string  AFilename)
inline

constructor

Parameters
AFilename

◆ TYml2Xml() [2/2]

Ict.Common.IO.TYml2Xml.TYml2Xml ( string[]  ALines)
inline

constructor

Member Function Documentation

◆ CheckName()

static bool Ict.Common.IO.TYml2Xml.CheckName ( string  AElementName)
inlinestatic

check if the name can be used as an element name for XML

◆ ClearAttribute()

static void Ict.Common.IO.TYml2Xml.ClearAttribute ( XmlNode  xmlNode,
string  name 
)
inlinestatic

remove attribute

Parameters
xmlNode
name

◆ CreateXmlDocument()

static XmlDocument Ict.Common.IO.TYml2Xml.CreateXmlDocument ( )
inlinestatic

create an empty xml document, which will be filled with the data from the yaml file

◆ GetAbsoluteIndentation()

Int32 Ict.Common.IO.TYml2Xml.GetAbsoluteIndentation ( Int32  lineNr)
inlineprotected

returns -1 for comments or invalid line numbers, otherwise the number of spaces throws exception if there is a tab in the indentation

◆ GetAbsoluteIndentationNext()

Int32 Ict.Common.IO.TYml2Xml.GetAbsoluteIndentationNext ( Int32  lineNr)
inlineprotected
Returns
the absolute indentation of the next valid line (skipping comments)

◆ GetAttribute()

static string Ict.Common.IO.TYml2Xml.GetAttribute ( XmlNode  xmlNode,
string  name 
)
inlinestatic

get the attribute; if the current node does not have it, check the base node

◆ GetAttributeRecursive()

static string Ict.Common.IO.TYml2Xml.GetAttributeRecursive ( XmlNode  xmlNode,
string  name 
)
inlinestatic

if the current node does not have the attribute, try the parent nodes

◆ GetAttributes()

static SortedList< string, string > Ict.Common.IO.TYml2Xml.GetAttributes ( XmlNode  xmlNode)
inlinestatic

get all attributes, even from base node

Parameters
xmlNode
Returns

◆ GetBaseNode()

static XmlNode Ict.Common.IO.TYml2Xml.GetBaseNode ( XmlNode  ANode)
inlinestaticprotected

check if parent already has a base element

◆ GetChild()

static XmlNode Ict.Common.IO.TYml2Xml.GetChild ( XmlNode  node,
string  childName 
)
inlinestatic

get the child node with the given name; considers base nodes as well

Parameters
node
childName
Returns

◆ GetChildren()

static List< XmlNode > Ict.Common.IO.TYml2Xml.GetChildren ( XmlNode  node,
bool  AConsiderBase 
)
inlinestatic

get all children nodes of a given node; depending on parameter, include the inherited nodes or not the nodes will be sorted by the order flag (AlwaysFirst, AlwaysLast)

Parameters
node
AConsiderBaseinclude the inherited nodes
Returns

◆ GetElementName()

static string Ict.Common.IO.TYml2Xml.GetElementName ( XmlNode  ANode)
inlinestatic

return either the name from the attribute, or the element name

◆ GetElements() [1/2]

static StringCollection Ict.Common.IO.TYml2Xml.GetElements ( XmlNode  node)
inlinestatic

convert elements of a sequence into a string collection checks for duplicates, and removes names with a tilde character ~ in front

◆ GetElements() [2/2]

static StringCollection Ict.Common.IO.TYml2Xml.GetElements ( XmlNode  node,
string  childName 
)
inlinestatic

overload for GetElements, with the main node and the name of the child node that has the elements

Parameters
node
childName
Returns

◆ GetIndentationNext()

Int32 Ict.Common.IO.TYml2Xml.GetIndentationNext ( Int32  lineNr)
inlineprotected
Returns
+1 if next line is more indented than the current line (skipping comments), -1 if next line is less indented, or current line is last line 0 if current line has same identation than the next line

◆ GetNextLine()

string Ict.Common.IO.TYml2Xml.GetNextLine ( )
inlineprotected

does not return comment lines

Returns
null if no line available

◆ HasAttribute()

static bool Ict.Common.IO.TYml2Xml.HasAttribute ( XmlNode  xmlNode,
string  name 
)
inlinestatic

check for the attribute; if the current node does not have it, check the base node

◆ HasAttributeRecursive()

static bool Ict.Common.IO.TYml2Xml.HasAttributeRecursive ( XmlNode  xmlNode,
string  name 
)
inlinestatic

if the current node does not have the attribute, try the parent nodes

◆ LoadChild()

static XmlNode Ict.Common.IO.TYml2Xml.LoadChild ( XmlNode  parent,
string  nodeName,
int  ADepth 
)
inlinestaticprotected

TYml2Xml.LoadChild will either reuse an element, move an existing leaf from base to the main node, or create a new element

◆ Merge()

static bool Ict.Common.IO.TYml2Xml.Merge ( ref XmlDocument  AMergeDoc,
XmlDocument  ANewDoc,
int  ADepth 
)
inlinestatic

merge the specificDoc into the mergeDoc. merges 2 xml documents, maintains their inheritance hierarchy for nodes that have the same name. allows caching of base yaml files.

◆ Parent()

static XmlNode Ict.Common.IO.TYml2Xml.Parent ( XmlNode  node)
inlinestatic

get the parent node

◆ ParseYML2TaskListRoot()

XmlNode Ict.Common.IO.TYml2Xml.ParseYML2TaskListRoot ( )
inline

loads an yml document into one xml document; then returns only the part meant to be passed directly to a TaskList constructor as the TaskList root.

Returns

◆ ParseYML2XML() [1/2]

XmlDocument Ict.Common.IO.TYml2Xml.ParseYML2XML ( )
inline

loads an yml document into one xml document

Returns

◆ ParseYML2XML() [2/2]

XmlDocument Ict.Common.IO.TYml2Xml.ParseYML2XML ( XmlDocument  AMergeDoc,
int  ADepth 
)
inline

merges several yml documents into one xml document, and maintains their inheritance hierarchy for nodes that have the same name

Parameters
AMergeDoc
ADepth
Returns

◆ ReadHeader()

static bool Ict.Common.IO.TYml2Xml.ReadHeader ( string  AFilename,
out string  baseYamlOrClass 
)
inlinestatic

This method can be used to check if this is the correct file type, and also find out the baseyaml or baseclass

Returns
false if the yaml file cannot be read or interpreted

◆ ReferenceNodes()

static SortedList Ict.Common.IO.TYml2Xml.ReferenceNodes ( XmlDocument  myDoc)
inlinestatic

fill sorted list which contains a reference to each node by name

◆ SetAttribute()

static void Ict.Common.IO.TYml2Xml.SetAttribute ( XmlNode  xmlNode,
string  name,
string  value 
)
inlinestatic

SetAttribute will never consider the base and the xml hierarchy; use Tag to move things to base.

◆ SplitNode()

bool Ict.Common.IO.TYml2Xml.SplitNode ( string  line,
out string  nodeName,
out string  nodeContent 
)
inlineprotected

split a line into the node name and the node content

◆ StripQuotes()

string Ict.Common.IO.TYml2Xml.StripQuotes ( string  s)
inlineprotected

strip the quotes from a string

◆ Tag()

static void Ict.Common.IO.TYml2Xml.Tag ( XmlNode  ANode)
inlinestatic

Tag will move all leafs to a new child element called base a leaf is an XmlNode that has no children, only attributes all attributes are moved to base can only be called once todo: what about sequences

Parameters
ANode

◆ ThrowException()

void Ict.Common.IO.TYml2Xml.ThrowException ( string  AMessage,
Int32  lineNr 
)
inlineprotected

throw an exception and tell the current position while reading the yaml file

◆ Xml2Yml() [1/2]

static string Ict.Common.IO.TYml2Xml.Xml2Yml ( XmlDocument  ADoc)
inlinestatic

format the XML into YML and return as string

◆ Xml2Yml() [2/2]

static bool Ict.Common.IO.TYml2Xml.Xml2Yml ( XmlDocument  ADoc,
string  AOutYMLFile 
)
inlinestatic

format the XML into YML to increase readability and save to file

◆ Xml2YmlGz()

static string Ict.Common.IO.TYml2Xml.Xml2YmlGz ( XmlDocument  ADoc)
inlinestatic

format the XML into zipped YML and return as Base64 string

Member Data Documentation

◆ currentLine

Int32 Ict.Common.IO.TYml2Xml.currentLine = -1
protected

the current line that we are parsing

◆ filename

string Ict.Common.IO.TYml2Xml.filename = ""
protected

the filename of the file that we are parsing

◆ lines

string [] Ict.Common.IO.TYml2Xml.lines = null
protected

contains the lines of the yml document

◆ ROOTNODEINTERNAL

const string Ict.Common.IO.TYml2Xml.ROOTNODEINTERNAL = "RootNodeInternal"
static

the name used for root node in generated xml code

◆ XMLELEMENT

const string Ict.Common.IO.TYml2Xml.XMLELEMENT = "XmlElement"
static

the name used for elements in generated xml code

◆ XMLLIST

const string Ict.Common.IO.TYml2Xml.XMLLIST = "XmlList"
static

the name used for elements in a list


The documentation for this class was generated from the following file: