Project

General

Profile

Statistics
| Branch: | Revision:

root / src / nedxml / saxparser.h @ 7c7d95e5

History | View | Annotate | Download (3.34 KB)

1 01873262 Georg Kunz
//==========================================================================
2
//  SAXPARSER.H - part of
3
//                     OMNeT++/OMNEST
4
//            Discrete System Simulation in C++
5
//
6
//==========================================================================
7
8
/*--------------------------------------------------------------*
9
  Copyright (C) 2002-2008 Andras Varga
10
  Copyright (C) 2006-2008 OpenSim Ltd.
11

12
  This file is distributed WITHOUT ANY WARRANTY. See the file
13
  `license' for details on this and other legal matters.
14
*--------------------------------------------------------------*/
15
16
17
18
#ifndef __SAXPARSER_H
19
#define __SAXPARSER_H
20
21
#include <stdio.h>
22
#include "nedxmldefs.h"
23
24
NAMESPACE_BEGIN
25
26
class SAXParser;
27
28
29
/**
30
 * Base class for SAX event handlers needed by SAXParser.
31
 * This is a simplified SAX handler interface.
32
 *
33
 * All event handlers provided by this class are empty, one must
34
 * subclass SAXHandler and redefine the event handler to make them
35
 * do something useful.
36
 *
37
 * @ingroup XMLParser
38
 */
39
class NEDXML_API SAXHandler
40
{
41
    friend class SAXParser;
42
  protected:
43
    SAXParser *parser;
44
45
    // internal: called from SAXParser::setHandler()
46
    virtual void setParser(SAXParser *p) {parser=p;}
47
48
  public:
49
    /**
50
     * Constructor
51
     */
52
    SAXHandler() {parser=0;}
53
54
    /**
55
     * Destructor
56
     */
57
    virtual ~SAXHandler() {}
58
59
    /**
60
     * Called by the parser on SAX StartElement events.
61
     */
62
    virtual void startElement(const char *name, const char **atts)  {}
63
64
    /**
65
     * Called by the parser on SAX EndElement events.
66
     */
67
    virtual void endElement(const char *name)  {}
68
69
    /**
70
     * Called by the parser on SAX CharacterData events.
71
     */
72
    virtual void characterData(const char *s, int len)  {}
73
74
    /**
75
     * Called by the parser on SAX ProcessingInstruction events.
76
     */
77
    virtual void processingInstruction(const char *target, const char *data)  {}
78
79
    /**
80
     * Called by the parser on SAX Comment events.
81
     */
82
    virtual void comment(const char *data)  {}
83
84
    /**
85
     * Called by the parser on SAX CDataStart events.
86
     */
87
    virtual void startCdataSection()  {}
88
89
    /**
90
     * Called by the parser on SAX CDataEnd events.
91
     */
92
    virtual void endCdataSection()  {}
93
};
94
95
96
/**
97
 * Wraps XML SAX parsers (Expat and LibXML are supported at the moment,
98
 * decided at compile-time. DTD validation is supported with LibXML.)
99
 *
100
 * One must provide a SAXHandler for this class to be useful.
101
 *
102
 * @ingroup XMLParser
103
 */
104
class NEDXML_API SAXParser
105
{
106
  protected:
107
    char errortext[512];
108
    SAXHandler *saxhandler;
109
    void *currentparser;
110
111
  public:
112
    /**
113
     * Constructor
114
     */
115
    SAXParser();
116
117
    /**
118
     * Install a SAX handler into the parser.
119
     */
120
    void setHandler(SAXHandler *sh);
121
122
    /**
123
     * Parse XML input read from the given file. Methods of the SAX handler
124
     * will be called as the parser processes the file. Returns false if
125
     * there was an error; in that case use getErrorMessage() to learn the
126
     * cause of the error.
127
     */
128
    bool parse(const char *filename);
129
130
    /**
131
     * Returns the current line number in the input. Can be called from SAX handler code.
132
     */
133
    int getCurrentLineNumber();
134
135
    /**
136
     * Can be called after parse() returned false. Returns the error description.
137
     */
138
    const char *getErrorMessage()  {return errortext;}
139
};
140
141
NAMESPACE_END
142
143
144
#endif