xmlobj.cpp
author insilmaril
Thu, 23 Apr 2009 12:15:31 +0000
changeset 755 ed5b407975b3
parent 721 12958f987bcf
child 791 f1006de05c54
permissions -rw-r--r--
more data in Tree, less in Map
insilmaril@621
     1
#include "xmlobj.h"
insilmaril@621
     2
insilmaril@621
     3
#include <QRegExp>
insilmaril@721
     4
#include <QString>
insilmaril@621
     5
insilmaril@621
     6
insilmaril@621
     7
// returns masked "<" ">" "&"
insilmaril@621
     8
QString quotemeta(const QString &s)
insilmaril@621
     9
{
insilmaril@621
    10
	QString r=s;
insilmaril@621
    11
    QRegExp  rx("&(?!amp;)");
insilmaril@621
    12
    r.replace ( rx,"&amp;");
insilmaril@621
    13
    rx.setPattern( ">");
insilmaril@621
    14
    r.replace ( rx,"&gt;");
insilmaril@621
    15
    rx.setPattern( "<");
insilmaril@621
    16
    r.replace ( rx,"&lt;");
insilmaril@621
    17
    rx.setPattern( "\"");
insilmaril@621
    18
    r.replace ( rx,"&quot;");
insilmaril@621
    19
    return r;
insilmaril@621
    20
}
insilmaril@621
    21
insilmaril@621
    22
insilmaril@621
    23
insilmaril@621
    24
int XMLObj::actindent=0;		// make instance of actindent
insilmaril@621
    25
insilmaril@621
    26
XMLObj::XMLObj()
insilmaril@621
    27
{
insilmaril@621
    28
    indentwidth=4;
insilmaril@621
    29
}
insilmaril@621
    30
insilmaril@621
    31
insilmaril@621
    32
// returns <s at />
insilmaril@621
    33
QString XMLObj::singleElement(QString s, QString at)
insilmaril@621
    34
{
insilmaril@621
    35
    return indent() + "<" + s +" " + at +" " + "/>\n";
insilmaril@621
    36
}
insilmaril@621
    37
insilmaril@621
    38
// returns <s>
insilmaril@621
    39
QString XMLObj::beginElement(QString s)
insilmaril@621
    40
{
insilmaril@621
    41
    return indent() + "<" + s + ">\n";
insilmaril@621
    42
}
insilmaril@621
    43
insilmaril@621
    44
// returns <s at>
insilmaril@621
    45
QString XMLObj::beginElement(QString s, QString at)
insilmaril@621
    46
{
insilmaril@621
    47
    return indent() + "<" + s + " " + at + ">\n";
insilmaril@621
    48
}
insilmaril@621
    49
insilmaril@621
    50
// returns </s>
insilmaril@621
    51
QString XMLObj::endElement(QString s)
insilmaril@621
    52
{
insilmaril@621
    53
    return indent() + "</" + s + ">\n";
insilmaril@621
    54
}
insilmaril@621
    55
insilmaril@621
    56
// returns  at="val"
insilmaril@621
    57
QString XMLObj::attribut(QString at, QString val)
insilmaril@621
    58
{
insilmaril@621
    59
    return " " + at + "=\"" + quotemeta (val) + "\""; 
insilmaril@621
    60
}
insilmaril@621
    61
insilmaril@621
    62
// returns <s> val </s>
insilmaril@621
    63
QString XMLObj::valueElement(QString el, QString val)
insilmaril@621
    64
{
insilmaril@621
    65
    return indent() + "<" + el + ">" + quotemeta(val) + "</" +el + ">\n";
insilmaril@621
    66
}
insilmaril@621
    67
insilmaril@621
    68
// returns <s at> val </s>
insilmaril@621
    69
QString XMLObj::valueElement(QString el, QString val, QString at)
insilmaril@621
    70
{
insilmaril@621
    71
    return indent() + "<" + el + " " + at + ">" + quotemeta(val) + "</" +el + ">\n";
insilmaril@621
    72
}
insilmaril@621
    73
insilmaril@621
    74
void XMLObj::incIndent()
insilmaril@621
    75
{
insilmaril@621
    76
    actindent++;
insilmaril@621
    77
}	
insilmaril@621
    78
insilmaril@621
    79
void XMLObj::decIndent()
insilmaril@621
    80
{
insilmaril@621
    81
    actindent--;
insilmaril@621
    82
    if (actindent<0) actindent=0;
insilmaril@621
    83
}	
insilmaril@621
    84
insilmaril@621
    85
QString XMLObj::indent()
insilmaril@621
    86
{
insilmaril@621
    87
    QString s;
insilmaril@621
    88
    int i;
insilmaril@621
    89
    for (i=0; i<actindent*indentwidth; i++)
insilmaril@621
    90
    {
insilmaril@621
    91
		s= s + " ";
insilmaril@621
    92
    }
insilmaril@621
    93
    return s;
insilmaril@621
    94
}	
insilmaril@621
    95