summaryrefslogtreecommitdiff
path: root/buildscripts/PhpDocumentor/phpDocumentor/XMLpackagePageParser.inc
blob: 59a3f5572254b14dcf8229f8f7b2590e3af8fe43 (plain)
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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
<?php
//
// +------------------------------------------------------------------------+
// | phpDocumentor                                                          |
// +------------------------------------------------------------------------+
// | Copyright (c) 2000-2003 Joshua Eichorn, Gregory Beaver                 |
// | Email         jeichorn@phpdoc.org, cellog@phpdoc.org                   |
// | Web           http://www.phpdoc.org                                    |
// | Mirror        http://phpdocu.sourceforge.net/                          |
// | PEAR          http://pear.php.net/package-info.php?pacid=137           |
// +------------------------------------------------------------------------+
// | This source file is subject to version 3.00 of the PHP License,        |
// | that is available at http://www.php.net/license/3_0.txt.               |
// | If you did not receive a copy of the PHP license and are unable to     |
// | obtain it through the world-wide-web, please send a note to            |
// | license@php.net so we can mail you a copy immediately.                 |
// +------------------------------------------------------------------------+
//

/**
 * @package phpDocumentor
 * @subpackage Parsers
 * @author Greg Beaver <cellog@users.sourceforge.net>
 * @since 1.2
 */
/** when <programlisting> is found */
define('PHPDOCUMENTOR_PDP_EVENT_PROGRAMLISTING', 600);
/** when <programlisting> is found */
define('PHPDOCUMENTOR_PDP_STATE_PROGRAMLISTING', 700);
/** when a DocBook <tag> is found */
define('PHPDOCUMENTOR_PDP_EVENT_TAG', 601);
/** when a DocBook <tag> is found */
define('PHPDOCUMENTOR_PDP_STATE_TAG', 701);
/** when <![CDATA[ ]]> is found */
define('PHPDOCUMENTOR_PDP_EVENT_CDATA', 602);
/** when <![CDATA[ ]]> is found */
define('PHPDOCUMENTOR_PDP_STATE_CDATA', 702);
/** when tag attributes name="value" are found */
define('PHPDOCUMENTOR_PDP_EVENT_ATTRIBUTES', 603);
/** when tag attributes name="value" are found */
define('PHPDOCUMENTOR_PDP_STATE_ATTRIBUTES', 703);
/** when tag attributes name="value" are found */
define('PHPDOCUMENTOR_PDP_EVENT_ENTITY', 604);
/** when tag attributes name="value" are found */
define('PHPDOCUMENTOR_PDP_STATE_ENTITY', 704);

/**
 * Used to parse XML DocBook-based tutorials
 * @package phpDocumentor
 * @subpackage Parsers
 * @author Greg Beaver <cellog@users.sourceforge.net>
 * @since 1.2
 */
class XMLPackagePageParser extends Parser
{
    /** @var array */
    var $eventHandlers = array(
                               PHPDOCUMENTOR_PDP_EVENT_TAG => 'handleTag',
                               PHPDOCUMENTOR_PDP_EVENT_ATTRIBUTES => 'handleAttributes',
                               PHPDOCUMENTOR_PDP_EVENT_CDATA => 'handleCData',
                               PARSER_EVENT_NOEVENTS => 'defaultHandler',
                               PARSER_EVENT_COMMENTBLOCK => 'ignoreHandler',
                               PARSER_EVENT_OUTPHP => 'ignoreHandler',
                               PARSER_EVENT_QUOTE => 'handleQuote',
                               PHPDOCUMENTOR_PDP_EVENT_ENTITY => 'handleEntity',
                               );
    
    /**
     * @var array
     */
    var $pars = array();
    
    var $refsect1id = false;
    var $refsect2id = false;
    var $refsect3id = false;
    /**
     * @var array the tag stack
     */
    var $context;
    /**#@+ @access private */
    var $_gettoc = false;
    var $_toc = array();
    var $_cursection = 0;
    /**#@-*/
    /**
     * Set up the wordparser
     *
     * {@source}
     * @uses ObjectWordParser
     */
    function XMLPackagePageParser()
    {
        $this->wp = new ObjectWordParser(true);
    }
    /**
     * Parse a new file
     *
     * @param    string    $parse_data
     * @param    array    $tutorial for format, see {@link Io::getTutorials()}
     * @staticvar    integer    used for recursion limiting if a handler for an event is not found
     * @return    bool
     * @uses parserTutorial using {@link Publisher::PublishEvent()}, a new tutorial
     *                      is created from the file parsed, and passed to the
     *                      Intermediate Parser
     */
    function parse ($parse_data, $tutorial)
    {
        $tempparse = new ppageParser;
        $parse_data = $tempparse->parse($parse_data,true,$tutorial['package'],$tutorial['subpackage'],
                                        basename($tutorial['path']),$tutorial['category'], $tutorial['path']);
        unset($tempparse);
        static $endrecur = 0;
        if (!is_array($parse_data) || count($parse_data) == 0)
        {
            return false;
        }
        $this->setupStates();

        // initialize variables so E_ALL error_reporting doesn't complain
        $pevent = 0;
        $word = 0;
        $this->p_vars['start'] = true;
        $this->p_vars['event_stack'] = new EventStack;

        $this->wp->setup($parse_data,false);
        $this->wp->setWhitespace(true);
        $this->context = array();
        if (isset($this->curtag)) unset($this->curtag);

        do
        {
            $lpevent = $pevent;
            $pevent = $this->p_vars['event_stack']->getEvent();
            if ($lpevent != $pevent)
            {
                $this->p_vars['last_pevent'] = $lpevent;
            }

            if ($this->p_vars['last_pevent'] != $pevent)
            {
                // its a new event so the word parser needs to be reconfigured 
                $this->configWordParser($pevent);
            }


            $this->p_vars['last_word'] = $word;
            $word = $this->wp->getWord();

            if (0)//PHPDOCUMENTOR_DEBUG == true)
            {
                echo "----------------\n";
                echo "LAST: |" . $this->p_vars['last_word'] . "|\n";
//                echo "INDEX: ".$this->p_vars['curpar']."\n";
                echo "PEVENT: " . $this->getParserEventName($pevent) . "\n";
                echo "LASTPEVENT: " . $this->getParserEventName($this->p_vars['last_pevent']) . "\n";
                echo $this->wp->getPos() . " WORD: |$word|\n\n";
//                echo '"'.$this->p_vars['quote_data']."\"\n";
            }
            if (isset($this->eventHandlers[$pevent]))
            {
                $handle = $this->eventHandlers[$pevent];
                if ($word !== false) $this->$handle($word, $pevent);
            } else
            {
                debug('WARNING: possible error, no XMLPackagePageParser handler for event number '.$pevent);
                if ($endrecur++ == 25)
                {
                    die("FATAL ERROR, recursion limit reached");
                }
            }
            $this->p_vars['start'] = false;
        } while (!($word === false));
        if (count($this->_toc) && isset($this->p_vars['toc']))
        {
            $a = $this->curtag->getTOC($this->p_vars['toc']);
            $a->setTOC($this->_toc);
            $a->setPath($tutorial['path']);
            $this->curtag->setTOC($this->p_vars['toc'],$a);
        }
        $this->PublishEvent(PHPDOCUMENTOR_EVENT_TUTORIAL,new parserTutorial($this->curtag, $tutorial));
        return $this->curtag;
    }
    
    /**#@+
     * @access private
     * @param string|parserInlineTag token
     * @param integer parser event
     */
    function defaultHandler($word, $pevent)
    {
        if (is_string($word) && $this->checkEventPush($word, $pevent))
        {
            return;
        }
    }
    
    function ignoreHandler($word, $pevent)
    {
        $this->checkEventPop($word, $pevent);
    }
    
    /**
     * handler for QUOTE.
     * this handler recognizes strings defined with double quotation marks (") and handles them correctly
     * in any place that they legally appear in php code
     */
    
    function handleQuote($word, $pevent)
    {
        if ($this->p_flags['reset_quote_data'] === true)
        {
            $this->p_flags['reset_quote_data'] = false;
            $this->p_vars['quote_data'] = "";
        }
        if (!is_object($word)) $this->checkEventPush( $word, $pevent);
        if ($word != "\"")
        {
            if (is_object($word))
            {
                $this->p_vars['quote_data'] = $word;
            }
            else
            {
                if (!is_object($this->p_vars['quote_data']))
                $this->p_vars['quote_data'] .= $word;
            }
        }
        if (!is_object($word))
        {
            if ($word == '>')
            {
                if (is_object($this->p_vars['quote_data'])) $this->p_vars['quote_data'] = '{@id '.$this->p_vars['quote_data']->id.'}';
                addErrorDie(PDERROR_UNTERMINATED_ATTRIB,$this->curtag->name,$this->p_vars['attrname'],$this->p_vars['quote_data']);
            }
            if ($this->checkEventPop($word,$pevent))
            {
                $this->p_flags['reset_quote_data'] = true;
            }
        }
    }
    
    /**
     * Handles all XML DocBook tags
     */
    function handleTag($word, $pevent)
    {
        if (isset($this->curtag) && $this->curtag->hasTitle() && $this->_gettoc && $this->_gettoc->name == $this->curtag->name)
        {
            if (isset($this->_toc[$this->_cursection]))
            {
                $this->_toc[$this->_cursection]['title'] = $this->curtag->_title;
                $this->_cursection++;
            }
            $this->_gettoc = false;
        }
        if ($this->p_vars['last_word'] == '<')
        { // get tag name
            $this->p_flags['begin_tag'] = true;
            array_push($this->context,$word);
//            if (isset($this->curtag)) debug("pushed ".$this->curtag->name);
            if (isset($this->curtag))
            {
                array_push($this->pars,$this->curtag);
            }
            $this->curtag = new parserXMLDocBookTag($word);
        } elseif ($this->p_vars['last_word'] == '</' || $word == '/>')
        {
            $tag = array_pop($this->context);
            if ($word == '/>') {
                // all is OK
                $this->checkEventPop($word, $pevent);
                $word = $tag;
            }
            if ($tag != $word)
            {
                addErrorDie(PDERROR_UNMATCHED_TUTORIAL_TAG,$tag,$word,$this->curtag->getString());
            }
            if (in_array($this->curtag->name, array('refentry', 'refsect1', 'refsect2', 'refsect3')))
            {
                if (!isset($this->curtag->_id))
                {
                    $title = '';
                    if (isset($this->curtag->_title))
                    {
                        $title = $this->curtag->_title->getString();
                    }
                    addWarning(PDERROR_NO_DOCBOOK_ID, $this->curtag->name, $title);
                }
            }
            $this->p_flags['begin_tag'] = false;
            $curtag = @array_pop($this->pars);
//            debug("popped $tag ".$curtag->name.' I am '.$this->curtag->name);
            if ($curtag)
            {
                if ($this->curtag->name == 'refsect1') $this->refsect1id = false;
                if ($this->curtag->name == 'refsect2') $this->refsect2id = false;
                if ($this->curtag->name == 'refsect3') $this->refsect3id = false;
                $curtag->add($this->curtag);
//                debug("added ".$this->curtag->name." to ".$curtag->name.' '.$curtag->id);
                $this->curtag = $curtag;
            } else
            {
//                debug("here");
            }
        } elseif (is_string($word))
        {
            if (!($e = $this->checkEventPush($word, $pevent)))
            {
                if ($this->checkEventPop($word, $pevent))
                {
                    if ($this->p_flags['begin_tag'])
                    {
                        $this->p_vars['event_stack']->pushEvent(PHPDOCUMENTOR_PDP_EVENT_TAG);
                        $this->p_vars['event_stack']->pushEvent(PHPDOCUMENTOR_PDP_EVENT_CDATA);
                        $this->p_vars['last_tag'] = array_pop($this->context);
                        array_push($this->context,$this->p_vars['last_tag']);
                        $this->p_flags['in_cdata'] = false;
                    }
                    return;
                }
            } else
            {
                $this->p_flags['start_attr'] = true;
                $this->p_flags['end_attr'] = false;
            }
        } else addErrorDie(PDERROR_CANT_HAVE_INLINE_IN_TAGNAME);
    }
    
    /**
     * Handle CData sections
     */
    function handleCData($word, $pevent)
    {
        if ($this->curtag->name == 'refentry' && phpDocumentor_get_class($word) == 'parsertocinlinetag')
        {
            $this->p_vars['toc'] = $this->curtag->getTOC();
        }
        if (is_string($word) && !$this->p_flags['in_cdata'])
        {
            if ($this->checkEventPop($word, $pevent))
            {
                return;
            }
            if ($this->checkEventPush($word, $pevent))
            {
                return;
            }
        }
        if ($word == '<![CDATA[')
        {
            $this->curtag->startCData();
            $this->p_flags['in_cdata'] = true;
        } elseif ($this->p_flags['in_cdata'] && $word == ']]>')
        {
            $this->curtag->endCData();
            $this->p_flags['in_cdata'] = false;
        } else
        {
            if ($this->p_flags['in_cdata'])
            $this->curtag->addCData($word);
            else
            $this->curtag->add($word);
        }
    }
    
    /**
     * Handle Entities like &rdquo;
     */
    function handleEntity($word, $pevent)
    {
        if (!$word)
        {
            if (!isset($this->p_vars['entity_name']))
                $this->p_vars['entity_name'] = '';
            addErrorDie(PDERROR_UNTERMINATED_ENTITY,$this->p_vars['entity_name']);
        }
        $e = $this->checkEventPop($word, $pevent);
        if ($word && !$e) $this->p_vars['entity_name'] = $word;
        if ($e)
        {
            $entity = new parserEntity($this->p_vars['entity_name']);
            unset($this->p_vars['entity_name']);
            $this->curtag->add($entity);
        }
    }
    
    /**
     * Handle Tag attributes name="value"
     */
    function handleAttributes($word, $pevent)
    {
        if ($this->checkEventPush($word, $pevent)) return;
        if ($word == '=')
        {
            $this->p_flags['start_attr'] = false;
            $this->p_vars['end_attr'] = true;
        } else
        {
            if ($this->p_flags['start_attr'])
            {
                $this->p_vars['attrname'] = $word;
            } else
            {
                if (isset($this->p_vars['attrname']))
                {
                    $value = $this->p_vars['quote_data'];
                    if (phpDocumentor_get_class($value) == 'parseridinlinetag')
                    { // "inherit" the parent section's id, so
                      // <refsect1 id="{@id test"}> <!-- id is 'test' -->
                      // ...
                      //  <refsect2 id="{@id me}"> <!-- id is 'test.me' -->
                      //  ...
                      //   <refsect3 id="{@id out}"> <!-- id is 'test.me.out' -->
                      //    <example id="{@id withexample}"> <!-- id is 'test.me.out.withexample' -->
                        $a = ($this->refsect1id ? $this->refsect1id . '.' : '');
                        $a .= ($this->refsect2id ? $this->refsect2id . '.' : '');
                        $a .= ($this->refsect3id ? $this->refsect3id . '.' : '');
                        if ($this->curtag->name == 'refsect1')
                        {
                            $this->refsect1id = $value->id;
                        }
                        if ($this->curtag->name == 'refsect2')
                        {
                            $this->refsect2id = $value->id;
                        }
                        if ($this->curtag->name == 'refsect3')
                        {
                            $this->refsect3id = $value->id;
                        }
//                            debug($value->id.' is now '.$a.$value->id);
                        $value->id = $a . $value->id;
                        if ($value->id != '')
                        {
                            if (isset($this->_toc[$this->_cursection]))
                            {
                                $this->_cursection++;
                            }
                            $this->_toc[$this->_cursection]['id'] = $value;
                            $this->_toc[$this->_cursection]['tag'] = new parserXMLDocBookTag($this->curtag->name);
//                            debug("set gettoc to ".$this->curtag->name .' '. $value->id);
                            $this->_gettoc = $this->curtag;
                        }
                    }
                    $this->curtag->addAttribute($this->p_vars['attrname'],$value);
                    unset($this->p_vars['attrname']);
                    $this->p_flags['end_attr'] = false;
                }
            }
        }
        if (is_string($word) && $this->checkEventPop($word, $pevent))
        {
            $this->p_flags['start_attr'] = true;
            $this->p_flags['end_attr'] = false;
            $this->wp->setPos($this->wp->getPos() - strlen($word));
        }
    }
    /**#@-*/
    
    /**
     * setup the parser tokens, and the pushEvent/popEvent arrays
     * @see $tokens, $pushEvent, $popEvent
     */
    
    function setupStates()
    {
        $this->_gettoc = false;
        $this->_toc = array();
        $this->_cursection = 0;
        if (isset($this->p_vars['toc'])) unset($this->p_vars['toc']);
        
        $this->tokens[STATE_NOEVENTS]                     = array('</','<','<!--','<?');
        $this->tokens[STATE_COMMENTBLOCK]                 = array('-->');
        $this->tokens[STATE_OUTPHP]                       = array('?>');
        $this->tokens[STATE_QUOTE]                        = array("\\\"","\\\\","\"",'>');
        $this->tokens[STATE_ESCAPE]                       = false;// this tells the word parser to just cycle
        $this->tokens[PHPDOCUMENTOR_PDP_STATE_TAG]        = array('>',' ','/>');
        $this->tokens[PHPDOCUMENTOR_PDP_STATE_CDATA]      = array('&','<!--','</','<![CDATA[','<',']]>');
        $this->tokens[PHPDOCUMENTOR_PDP_STATE_ATTRIBUTES] = array('=','>','/>','"');
        $this->tokens[PHPDOCUMENTOR_PDP_STATE_ENTITY]     = array(';');

        // For each event word to event mapings
        $this->pushEvent[PARSER_EVENT_NOEVENTS] = 
            array(
                "<" => PHPDOCUMENTOR_PDP_EVENT_TAG,
                "</" => PHPDOCUMENTOR_PDP_EVENT_TAG,
                '<!--' => PARSER_EVENT_COMMENTBLOCK,
                '<?' => PARSER_EVENT_OUTPHP,
                '&' => PHPDOCUMENTOR_PDP_EVENT_ENTITY,
            );
##########################

        $this->pushEvent[PHPDOCUMENTOR_PDP_EVENT_TAG] =
            array(
                ' ' => PHPDOCUMENTOR_PDP_EVENT_ATTRIBUTES,
            );
         
        $this->popEvent[PHPDOCUMENTOR_PDP_EVENT_TAG] = array(">","/>");
##########################
        $this->pushEvent[PHPDOCUMENTOR_PDP_EVENT_ATTRIBUTES] =
            array(
                '"' => PARSER_EVENT_QUOTE,
            );

        $this->popEvent[PHPDOCUMENTOR_PDP_EVENT_ATTRIBUTES] = array(">","/>");
##########################

        $this->popEvent[PARSER_EVENT_COMMENTBLOCK] = array("-->");
##########################
        $this->pushEvent[PARSER_EVENT_QUOTE] = 
            array(
                "\\"    => PARSER_EVENT_ESCAPE
            );
        $this->popEvent[PARSER_EVENT_QUOTE] = array("\"");
##########################

        $this->popEvent[PARSER_EVENT_OUTPHP] = array("?>");
##########################

        $this->popEvent[PHPDOCUMENTOR_PDP_EVENT_ENTITY] = array(";");
##########################

        $this->pushEvent[PHPDOCUMENTOR_PDP_EVENT_CDATA] =
            array(
                "<" => PHPDOCUMENTOR_PDP_EVENT_TAG,
                '<!--' => PARSER_EVENT_COMMENTBLOCK,
                '<?' => PARSER_EVENT_OUTPHP,
                '&' => PHPDOCUMENTOR_PDP_EVENT_ENTITY,
            );
        $this->popEvent[PHPDOCUMENTOR_PDP_EVENT_CDATA] = array("</");
    }
    
    /**
     * debugging function
     *
     * {@source}
     * @static
     */
    function getParserEventName ($value)
    {    
        $lookup = array(
            PARSER_EVENT_NOEVENTS         => "PARSER_EVENT_NOEVENTS",
            PHPDOCUMENTOR_PDP_EVENT_TAG        => "PHPDOCUMENTOR_PDP_EVENT_TAG",
            PHPDOCUMENTOR_PDP_EVENT_ATTRIBUTES        => "PHPDOCUMENTOR_PDP_EVENT_ATTRIBUTES",
            PHPDOCUMENTOR_PDP_EVENT_CDATA        => "PHPDOCUMENTOR_PDP_EVENT_CDATA",
            PHPDOCUMENTOR_PDP_EVENT_LIST    => "PHPDOCUMENTOR_PDP_EVENT_LIST",
            PARSER_EVENT_QUOTE  => "PARSER_EVENT_QUOTE",
            PHPDOCUMENTOR_PDP_EVENT_ENTITY => "PHPDOCUMENTOR_PDP_EVENT_ENTITY",
        );
        if (isset($lookup[$value]))
        return $lookup[$value];
        else return $value;
    }
}
?>