vtm/quelldatein/helfer/xmlknoten.ts

161 lines
3.6 KiB
TypeScript
Raw Normal View History

2017-11-09 14:06:35 +01:00
/*
* Verrückte Turing-Maschinen A turing complete game
* Copyright (C) 2016 Christian Fraß <vidofnir@folksprak.org>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
2017-11-08 15:05:06 +01:00
module mod_vtm_helfer
{
/**
* @author kcf <vidofnir@folksprak.org>
*/
function einrueckung(tiefe : int, zeichen : string = "\t") : string
{
return ((tiefe === 0) ? "" : (zeichen + einrueckung(tiefe-1)));
}
/**
* @author kcf <vidofnir@folksprak.org>
*/
export interface schnittstelle_xmlknoten
{
/**
* @author kcf <vidofnir@folksprak.org>
*/
darstellen(tiefe ?: int) : string;
}
/**
* @author kcf <vidofnir@folksprak.org>
*/
export class klasse_xmlknoten_text
implements schnittstelle_xmlknoten
{
/**
* @author kcf <vidofnir@folksprak.org>
*/
private inhalt : string;
/**
* @author kcf <vidofnir@folksprak.org>
*/
public constructor(inhalt : string)
{
this.inhalt = inhalt;
}
/**
* @author kcf <vidofnir@folksprak.org>
* @implementation
*/
public darstellen(tiefe : int = 0) : string
{
return this.inhalt;
}
}
/**
* @author kcf <vidofnir@folksprak.org>
*/
export class klasse_xmlknoten_normal
implements schnittstelle_xmlknoten
{
/**
* @author kcf <vidofnir@folksprak.org>
*/
private name : string;
/**
* @author kcf <vidofnir@folksprak.org>
*/
private attribute : {[schluessel : string] : string} = {};
/**
* @author kcf <vidofnir@folksprak.org>
*/
private kinder : Array<schnittstelle_xmlknoten> = [];
/**
* @author kcf <vidofnir@folksprak.org>
*/
public constructor(name : string, attribute : {[schluessel : string] : string} = {}, kinder : Array<schnittstelle_xmlknoten> = [])
{
this.name = name;
this.attribute = attribute;
this.kinder = kinder;
}
/**
* @author kcf <vidofnir@folksprak.org>
* @implementation
*/
public darstellen(tiefe : int = 0) : string
{
let str : string = "";
// anfang
{
let str_anfang : string = "";
str_anfang += this.name;
// attribute
{
let str_attribute : string = "";
Object.keys(this.attribute).forEach
(
schluessel =>
{
let wert : string = this.attribute[schluessel];
str_attribute += (" " + schluessel + "=" + "\"" + wert + "\"");
}
)
;
str_anfang += str_attribute;
}
str_anfang = (einrueckung(tiefe) + "<" + str_anfang + ">" + "\n");
str += str_anfang;
}
// kinder
{
this.kinder.forEach(kind => (str += kind.darstellen(tiefe+1)));
}
// ende
{
let str_ende : string = "";
str_ende += this.name;
str_ende = (einrueckung(tiefe) + "<" + "/" + str_ende + ">" + "\n");
str += str_ende;
}
return str;
}
}
}