using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using System.IO;
namespace SCJMapper_V2
{
///
/// Maintains an actionmap - something like:
///
///
///
///
///
///
///
///
///
///
class ActionMapCls : List
{
public String name { get; set; }
///
/// Merge the given Map with this Map
/// new ones are ignored - we don't learn from XML input for the time beeing
///
///
public void Merge( ActionMapCls newAcm )
{
// do we find all actions in the new list that are like the new ones in our list ?
foreach ( ActionCls newAc in newAcm ) {
ActionCls AC = this.Find( delegate( ActionCls ac ) {
return ac.key == newAc.key;
} );
if ( AC == null ) {
; // this.Add( newAc ); // no, add it
}
else {
AC.Merge( newAc ); // yes, merge it
}
}
}
///
/// Dump the actionmap as partial XML nicely formatted
///
/// the action as XML fragment
public String toXML( )
{
String r = String.Format( "\t\n", name );
foreach ( ActionCls ac in this ) {
String x = ac.toXML( );
if ( !String.IsNullOrEmpty(x) ) r += String.Format( "\t{0}", x);
}
r += String.Format( "\t\n");
return r;
}
///
/// Read an actionmap from XML - do some sanity check
///
/// the XML action fragment
/// True if an action was decoded
public Boolean fromXML( String xml )
{
XmlReaderSettings settings = new XmlReaderSettings( );
settings.ConformanceLevel = ConformanceLevel.Fragment;
settings.IgnoreWhitespace = true;
settings.IgnoreComments = true;
XmlReader reader = XmlReader.Create( new StringReader( xml ), settings );
reader.Read( );
if ( reader.Name == "actionmap" ) {
if ( reader.HasAttributes ) {
name = reader["name"];
}
else {
return false;
}
}
reader.Read( ); // move to next element
String x = reader.ReadOuterXml( );
while ( ! String.IsNullOrEmpty(x) ) {
ActionCls ac = new ActionCls( );
if ( ac.fromXML( x ) ) {
this.Add( ac ); // add to list
}
x=reader.ReadOuterXml();
}
return true;
}
}
}