File: testxml.pl

package info (click to toggle)
cpm 0.32-1.6
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 1,644 kB
  • sloc: ansic: 7,380; makefile: 509; perl: 406; sh: 303
file content (303 lines) | stat: -rwxr-xr-x 8,729 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
#!/usr/bin/perl -w

# ##############################################################################
# this script creates test-xml output which can be used to validate libxml and
# the data handling in the application.
# ##############################################################################
# Copyright (C) 2005-2009 Harry Brueckner
#
# 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 2 of the License, or 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, write to the Free Software Foundation, Inc.,
# 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
#
# Contact: Harry Brueckner <harry_b@mm.st>
#          Muenchener Strasse 12a
#          85253 Kleinberghofen
#          Germany
# ##############################################################################

use strict;
use warnings;
use Getopt::Long;
use Time::localtime;


# ##############################################################################
@main::alphabet = ();

$main::config{'alphabet'}  = 'alphabet-ascii.txt';
$main::config{'depth-min'} = '1';
$main::config{'depth-max'} = '4';
$main::config{'nodes-min'} = '3';
$main::config{'nodes-max'} = '5';
$main::config{'text-min'}  = '10';
$main::config{'text-max'}  = '128';
$main::config{'users-min'} = '3';
$main::config{'users-max'} = '20';

$main::runtime{'help'} = 0;
$main::runtime{'users'} = 0;


# ##############################################################################
#
# Description     main function of the script which reads any commandline
#                 arguments and starts the XML output
# Author          Harry Brueckner
# Date            2005-06-01
# Arguments       void
# Return          void
#
sub main
  {
    my ($lmax, $users);


    if (!GetOptions(
          'alphabet=s'  => \$main::config{'alphabet'},
          'depth-min=i' => \$main::config{'depth-min'},
          'depth-max=i' => \$main::config{'depth-max'},
          'nodes-min=i' => \$main::config{'nodes-min'},
          'nodes-max=i' => \$main::config{'nodes-max'},
          'text-min=i'  => \$main::config{'text-min'},
          'text-max=i'  => \$main::config{'text-max'},
          'users-min=i' => \$main::config{'users-min'},
          'users-max=i' => \$main::config{'users-max'},
          ))
      { $main::runtime{'help'} = 1; }

    if ($main::runtime{'help'})
      {
        print STDERR <<EOF;
syntax: $0 --alphabet=FILE --depth-min=NR --depth-max=NR
                     --nodes-min=NR --nodes-max=NR
                     --text-min=NR --text-max=NR
                     --users-min=NR --users-max=NR

    --alphabet      specify the alphabet file
    --depth-min     minimum node depth
    --depth-max     maximum node depth
    --nodes-min     minimum sub-nodes per node
    --nodes-max     maximum sub-nodes per node
    --text-min      minimum text length
    --text-max      maximum text length
    --users-min     minimum users
    --users-max     maximum users

EOF
        exit(1);
      }

    readAlphabet($main::config{'alphabet'});

    $users = getRandom($main::config{'users-min'}, $main::config{'users-max'});
    $lmax = getRandom($main::config{'depth-min'}, $main::config{'depth-max'});

    xmlHeader($users);
    xmlNode($users, 1, $lmax);
    xmlFooter();
  }


# ##############################################################################
#
# Description     create a pseudo random number (of bad quality)
# Author          Harry Brueckner
# Date            2005-06-01
# Arguments       $min - minimum number to create
#                 $max - maximum number to create
# Return          integer random number
#
sub getRandom
  {
    my ($min, $max) = @_;

    return int(rand($max - $min) + $min + 1);
  }


# ##############################################################################
#
# Description     create a random string of the defined alphabet
# Author          Harry Brueckner
# Date            2005-06-01
# Arguments       void
# Return          string of the given alphabet
#
sub getString
  {
    my ($length, $maxalpha, $string);

    $maxalpha = $#main::alphabet;
    $length = getRandom($main::config{'text-min'}, $main::config{'text-max'});

    $string = '';
    for (my $i = 0; $i < $length; $i++)
      {
        $string .= $main::alphabet[getRandom(0, $maxalpha)];
      }

    return $string;
  }


# ##############################################################################
#
# Description     create a timestamp
# Author          Harry Brueckner
# Date            2005-06-01
# Arguments       void
# Return          timestamp format 'YYYY-MM-DD hh:mm:ss'
#
sub getTimestamp
  {
    return sprintf('%04d-%02d-%02d %02d:%02d:%02d',
        localtime -> year() + 1900,
        localtime -> mon() + 1,
        localtime -> mday(),
        localtime -> hour(),
        localtime -> min(),
        localtime -> sec());
  }


# ##############################################################################
#
# Description     read the alphabet file
# Author          Harry Brueckner
# Date            2005-06-01
# Arguments       $filename   - the filename to read
# Return          void; the alphabet is stored in @main::alphabet
#
sub readAlphabet
  {
    my ($filename) = @_;
    my ($line);

    if (!-f $filename)
      {
        die "file '" . $filename . "' does not exist.\n";
        exit(1);
      }

    open(FILE, $filename) ||
        die "can not read file '" . $filename . "'.\n";
    while (defined($line = <FILE>))
      {
        $line =~ s/[\r\n]$//g;
        push(@main::alphabet, $line);
      }
    close(FILE);
  }


# ##############################################################################
#
# Description     print the xml footer
# Author          Harry Brueckner
# Date            2005-06-01
# Arguments       void
# Return          void
#
sub xmlFooter
  {
    print <<EOF;
</root>
EOF
  }


# ##############################################################################
#
# Description     print the xml header
# Author          Harry Brueckner
# Date            2005-06-01
# Arguments       $users - maximum number of users
# Return          void
#
sub xmlHeader
  {
    my ($users) = @_;
    my ($tstamp) = getTimestamp();

    print <<EOF;
<?xml version="1.0" encoding="ISO-8859-1"?>
<root created-by="1" created-on="$tstamp" modified-by="1" modified-on="$tstamp">
  <template created-on="$tstamp"/>
  <editor created-on="$tstamp">
    <user uid="1" created-on="$tstamp">unknown</user>
EOF
    for (my $i = 2; $i < $users; $i++)
      {
        print sprintf('    <user uid="%d" created-on="%s">user %d</user>%s',
            $i, $tstamp, $i, "\n");
      }
    print sprintf('    <user uid="%d" created-on="%s">%s</user>%s',
        $users, $tstamp,
        'Console Password Manager (This is just a test key.) &lt;cpm@testdomain.org&gt;',
        "\n");
    print <<EOF;
  </editor>
EOF
  }


# ##############################################################################
#
# Description     print a xml node and all it's sub-nodes (recursively)
# Author          Harry Brueckner
# Date            2005-06-01
# Arguments       $users  - maximum number of possible users
#                 $lnow   - current level
#                 $lmax   - maximum possible level
# Return          void
#
sub xmlNode
  {
    my ($users, $lnow, $lmax) = @_;
    my ($nmax, $tstamp, $uid);

    $nmax = getRandom($main::config{'nodes-min'}, $main::config{'nodes-max'});
    $tstamp = getTimestamp();

    for (my $n = 0; $n < $nmax; $n++)
      {
        $uid = getRandom(1, $users);

        print " " x ($lnow * 2);
        print sprintf('<node label="%s" created-by="%d" created-on="%s">%s',
            getString(), $uid, $tstamp, "\n");

        if (getRandom(1, 100) < 50)
          {
            print " " x ($lnow * 2);
            print sprintf('  <comment created-by="%d" created-on="%s">%s</comment>%s',
                $uid,
                $tstamp,
                getString(),
                "\n");
          }

        if ($lnow < $lmax)
          { xmlNode($users, $lnow + 1, $lmax); }

        print " " x ($lnow * 2);
        print "</node>\n";
      }
  }


# ##############################################################################

main();


# ##############################################################################