]> git.kernelconcepts.de Git - karo-tx-redboot.git/blob - tools/src/tools/configtool/standalone/wxwin/htmlparser.h
unified MX27, MX25, MX37 trees
[karo-tx-redboot.git] / tools / src / tools / configtool / standalone / wxwin / htmlparser.h
1 //####COPYRIGHTBEGIN####
2 //                                                                          
3 // ----------------------------------------------------------------------------
4 // Copyright (C) 1998, 1999, 2000 Red Hat, Inc.
5 //
6 // This program is part of the eCos host tools.
7 //
8 // This program is free software; you can redistribute it and/or modify it 
9 // under the terms of the GNU General Public License as published by the Free 
10 // Software Foundation; either version 2 of the License, or (at your option) 
11 // any later version.
12 // 
13 // This program is distributed in the hope that it will be useful, but WITHOUT 
14 // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 
15 // FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for 
16 // more details.
17 // 
18 // You should have received a copy of the GNU General Public License along with
19 // this program; if not, write to the Free Software Foundation, Inc., 
20 // 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
21 //
22 // ----------------------------------------------------------------------------
23 //                                                                          
24 //####COPYRIGHTEND####
25 //===========================================================================
26 //#####DESCRIPTIONBEGIN####
27 //
28 // Author(s):   julians
29 // Contact(s):  julians
30 // Date:                2001/04/04
31 // Version:             0.01
32 // Purpose:     
33 // Description: HTML parser/HTML Help file generator
34 // Requires:    
35 // Provides:    
36 // See also:    
37 // Known bugs:  
38 // Usage:       
39 //
40 //####DESCRIPTIONEND####
41 //  
42 //===========================================================================
43
44 #ifndef _EC_HTMLPARSER_H_
45 #define _EC_HTMLPARSER_H_
46
47 #ifdef __GNUG__
48 #pragma interface "htmlparser.cpp"
49 #endif
50
51 #include "wx/module.h"
52 #include "wx/stream.h"
53
54 /*
55
56  So how are going to represent it: compare with my Latex parser.
57  This generates a hierarchy because it respects the hierarchical nature of the Latex
58  commands. However, we don't _have_ to do that, we can make it linear, e.g.
59
60  tag-with-attributes text-chunk end-tag-with-attributes tag-with-attributes text-chunk
61
62  Otherwise, we need knowledge about HTML tags to parse hierarchically. This wouldn't be hard.
63  Would need to specify which tags have open/close parts, which don't, and for which it's optional
64  (such as <P>).
65
66
67  */
68
69 /*
70  * wxSimpleHtmlAttribute
71  * Representation of an attribute
72  */
73
74 class wxSimpleHtmlAttribute
75 {
76     friend class wxSimpleHtmlTag;
77 public:
78     wxSimpleHtmlAttribute(const wxString& name, const wxString& value)
79     {
80         m_name = name; m_value = value; m_next = NULL;
81     }
82 //// Operations
83
84     // Write this attribute
85     void Write(wxOutputStream& stream);
86
87 //// Accessors
88     const wxString& GetName() const { return m_name; }
89     const wxString& GetValue() const { return m_value; }
90
91     wxSimpleHtmlAttribute* GetNextAttribute() { return m_next; }
92     void SetNextAttribute(wxSimpleHtmlAttribute* attr) { m_next = attr; }
93
94     bool HasName(const wxString& name) const { return (0 == m_name.CmpNoCase(name)); }
95     bool HasValue(const wxString& val) const { return (0 == m_value.CmpNoCase(val)); }
96
97 private:
98     wxString                m_name;
99     wxString                m_value;
100     wxSimpleHtmlAttribute*  m_next;
101 };
102
103
104 /*
105  * wxSimpleHtmlTag
106  * Representation of a tag or chunk of text
107  */
108
109 enum { wxSimpleHtmlTag_Text, wxSimpleHtmlTag_TopLevel, wxSimpleHtmlTag_Open, wxSimpleHtmlTag_Close, wxSimpleHtmlTag_Directive  };
110
111 class wxSimpleHtmlTag
112 {
113 public:
114     wxSimpleHtmlTag(const wxString& tagName, int tagType);
115     ~wxSimpleHtmlTag();
116
117 //// Operations
118     void ClearAttributes();
119     wxSimpleHtmlAttribute* FindAttribute(const wxString& name) const ;
120     void AppendAttribute(const wxString& name, const wxString& value);
121     void ClearChildren();
122     void AppendTag(wxSimpleHtmlTag* tag);
123     // Write this tag
124     void Write(wxOutputStream& stream);
125
126     // Gets the text from this tag and its descendants
127     wxString GetTagText();
128
129 //// Accessors
130     const wxString& GetName() const { return m_name; }
131     void SetName(const wxString& name) { m_name = name; }
132
133     int GetType() const { return m_type; }
134     void SetType(int t) { m_type = t; }
135
136     // If type is wxSimpleHtmlTag_Text, m_text will contain some text.
137     const wxString& GetText() const { return m_text; }
138     void SetText(const wxString& text) { m_text = text; }
139
140     wxSimpleHtmlAttribute* GetFirstAttribute() { return m_attributes; }
141     void SetFirstAttribute(wxSimpleHtmlAttribute* attr) { m_attributes = attr; }
142
143     int GetAttributeCount() const ;
144     wxSimpleHtmlAttribute* GetAttribute(int i) const ;
145
146     wxSimpleHtmlTag* GetChildren() const { return m_children; }
147     void SetChildren(wxSimpleHtmlTag* children) { m_children = children; }
148
149     wxSimpleHtmlTag* GetParent() const { return m_parent; }
150     void SetParent(wxSimpleHtmlTag* parent) { m_parent = parent; }
151     int GetChildCount() const;
152     wxSimpleHtmlTag*    GetChild(int i) const;
153     wxSimpleHtmlTag*    GetNext() const { return m_next; }
154
155 //// Convenience accessors & search functions
156     bool NameIs(const wxString& name) { return (m_name.CmpNoCase(name) == 0); }
157     bool HasAttribute(const wxString& name, const wxString& value) const;
158     bool HasAttribute(const wxString& name) const;
159     bool GetAttributeValue(wxString& value, const wxString& attrName);
160
161     // Search forward from this tag until we find a tag with this name & attribute 
162     wxSimpleHtmlTag* FindTag(const wxString& tagName, const wxString& attrName);
163
164     // Gather the text until we hit the given close tag
165     bool FindTextUntilTagClose(wxString& text, const wxString& tagName);
166
167 private:
168     wxString                m_name;
169     int                     m_type;
170     wxString                m_text;
171     wxSimpleHtmlAttribute*  m_attributes;
172
173     // List of children
174     wxSimpleHtmlTag*        m_children;
175     wxSimpleHtmlTag*        m_next; // Next sibling
176     wxSimpleHtmlTag*        m_parent;
177 };
178
179 /*
180  * wxSimpleHtmlParser
181  * Simple HTML parser, for such tasks as scanning HTML for keywords, contents, etc.
182  */
183
184 class wxSimpleHtmlParser : public wxObject
185 {
186     
187 public:
188     wxSimpleHtmlParser();
189     ~wxSimpleHtmlParser();
190
191 //// Operations
192     bool ParseFile(const wxString& filename);
193     bool ParseString(const wxString& str);
194     void Clear();
195     // Write this file
196     void Write(wxOutputStream& stream);
197     bool WriteFile(wxString& filename);
198
199 //// Helpers
200
201     // Main recursive parsing function
202     bool ParseHtml(wxSimpleHtmlTag* parent);
203
204     wxSimpleHtmlTag* ParseTagHeader();
205     wxSimpleHtmlTag* ParseTagClose();
206     bool ParseAttributes(wxSimpleHtmlTag* tag);
207     wxSimpleHtmlTag* ParseDirective(); // e.g. <!DOCTYPE ....>
208     bool ParseComment(); // Throw away comments
209     // Plain text, up until an angled bracket
210     bool ParseText(wxString& text);
211
212     bool EatWhitespace(); // Throw away whitespace
213     bool EatWhitespace(int& pos); // Throw away whitespace: using 'pos'
214     bool ReadString(wxString& str, bool eatIt = FALSE);
215     bool ReadWord(wxString& str, bool eatIt = FALSE);
216     bool ReadNumber(wxString& str, bool eatIt = FALSE);
217     // Could be number, string, whatever, but read up until whitespace.
218     bool ReadLiteral(wxString& str, bool eatIt = FALSE);
219
220     bool IsDirective();
221     bool IsComment();
222     bool IsString();
223     bool IsWord();
224     bool IsTagClose();
225     bool IsTagStartBracket(int ch);
226     bool IsTagEndBracket(int ch);
227     bool IsWhitespace(int ch);
228     bool IsAlpha(int ch);
229     bool IsWordChar(int ch);
230     bool IsNumeric(int ch);
231
232     // Matches this string (case insensitive)
233     bool Matches(const wxString& tok, bool eatIt = FALSE) ;
234     bool Eof() const { return (m_pos >= m_length); }
235     bool Eof(int pos) const { return (pos >= m_length); }
236
237     void SetPosition(int pos) { m_pos = pos; }
238
239
240 //// Accessors
241     wxSimpleHtmlTag* GetTopLevelTag() const { return m_topLevel; }
242
243     // Safe way of getting a character
244     int GetChar(size_t i) const;
245     
246 private:
247
248     wxSimpleHtmlTag*    m_topLevel;
249     int                 m_pos;    // Position in string
250     int                 m_length; // Length of string
251     wxString            m_text;   // The actual text
252
253 };
254
255 /*
256  * wxSimpleHtmlTagSpec
257  * Describes a tag, and what type it is.
258  * wxSimpleHtmlModule will initialise/cleanup a list of these, one per tag type
259  */
260
261 #if 0
262 class wxSimpleHtmlTagSpec : public wxObject
263 {
264     
265 public:
266     wxSimpleHtmlTagSpec(const wxString& name, int type);
267
268 //// Operations
269     static void AddTagSpec(wxSimpleHtmlTagSpec* spec);
270     static void Clear();
271
272 //// Accessors
273     const wxString& GetName() const { return m_name; }
274     int GetType() const { return m_type; }
275
276 private:
277
278     wxString    m_name;
279     int         m_type;
280
281     static wxList* sm_tagSpecs;
282 };
283
284 /*
285  * wxSimpleHtmlModule
286  * Responsible for init/cleanup of appropriate data structures
287  */
288
289 class wxSimpleHtmlModule : public wxModule
290 {
291 DECLARE_DYNAMIC_CLASS(wxSimpleHtmlModule)
292
293 public:
294     wxSimpleHtmlModule() {};
295
296     bool OnInit() ;
297     void OnExit() ;
298 };
299 #endif
300
301 #endif