Branch data Line data Source code
1 : : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : : /*************************************************************************
3 : : *
4 : : * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 : : *
6 : : * Copyright 2000, 2010 Oracle and/or its affiliates.
7 : : *
8 : : * OpenOffice.org - a multi-platform office productivity suite
9 : : *
10 : : * This file is part of OpenOffice.org.
11 : : *
12 : : * OpenOffice.org is free software: you can redistribute it and/or modify
13 : : * it under the terms of the GNU Lesser General Public License version 3
14 : : * only, as published by the Free Software Foundation.
15 : : *
16 : : * OpenOffice.org is distributed in the hope that it will be useful,
17 : : * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 : : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 : : * GNU Lesser General Public License version 3 for more details
20 : : * (a copy is included in the LICENSE file that accompanied this code).
21 : : *
22 : : * You should have received a copy of the GNU Lesser General Public License
23 : : * version 3 along with OpenOffice.org. If not, see
24 : : * <http://www.openoffice.org/license.html>
25 : : * for a copy of the LGPLv3 License.
26 : : *
27 : : ************************************************************************/
28 : :
29 : : #ifndef _PARCSS1_HXX
30 : : #define _PARCSS1_HXX
31 : :
32 : : // Die Tokens des CSS1-Parsers
33 : : enum CSS1Token
34 : : {
35 : : CSS1_NULL,
36 : : CSS1_UNKOWN,
37 : :
38 : : CSS1_IDENT,
39 : : CSS1_STRING,
40 : : CSS1_NUMBER,
41 : : CSS1_PERCENTAGE,
42 : : CSS1_LENGTH, // eine absolute Groesse in 1/100 MM
43 : : CSS1_PIXLENGTH, // eine Pixel-Groesse
44 : : CSS1_EMS,
45 : : CSS1_EMX,
46 : : CSS1_HEXCOLOR,
47 : :
48 : : CSS1_DOT_W_WS,
49 : : CSS1_DOT_WO_WS,
50 : : CSS1_COLON,
51 : : CSS1_SLASH,
52 : : CSS1_PLUS,
53 : : CSS1_MINUS,
54 : : CSS1_OBRACE,
55 : : CSS1_CBRACE,
56 : : CSS1_SEMICOLON,
57 : : CSS1_COMMA,
58 : : CSS1_HASH,
59 : :
60 : : CSS1_IMPORT_SYM,
61 : : // Feature: PrintExt
62 : : CSS1_PAGE_SYM,
63 : : // /Feature: PrintExt
64 : :
65 : : CSS1_IMPORTANT_SYM,
66 : :
67 : : CSS1_URL,
68 : : CSS1_RGB
69 : : };
70 : :
71 : : // die Zustaende des Parsers
72 : : enum CSS1ParserState
73 : : {
74 : : CSS1_PAR_ACCEPTED = 0,
75 : : CSS1_PAR_WORKING,
76 : : CSS1_PAR_ERROR
77 : : };
78 : :
79 : : enum CSS1SelectorType
80 : : {
81 : : CSS1_SELTYPE_ELEMENT,
82 : : CSS1_SELTYPE_ELEM_CLASS,
83 : : CSS1_SELTYPE_CLASS,
84 : : CSS1_SELTYPE_ID,
85 : : CSS1_SELTYPE_PSEUDO,
86 : : // Feature: PrintExt
87 : : CSS1_SELTYPE_PAGE
88 : : // /Feature: PrintExt
89 : :
90 : : };
91 : :
92 : : // Die folegende Klasse beschreibt einen Simple-Selector, also
93 : : // - einen HTML-Element-Namen
94 : : // - einen HTML-Element-Namen mit Klasse (durch '.' getrennt)
95 : : // - eine Klasse (ohne Punkt)
96 : : // - eine mit ID=xxx gesetzte ID aus einem HTML-Dokument
97 : : // oder
98 : : // - ein Pseudo-Element
99 : : //
100 : : // Die Simple-Sektoren werden in einer Liste zu vollstaendigen
101 : : // Selektoren verkettet
102 : : class CSS1Selector
103 : : {
104 : : CSS1SelectorType eType; // Art des Selektors
105 : : String aSelector; // der Selektor selbst
106 : : CSS1Selector *pNext; // die naechste Komponente
107 : :
108 : : public:
109 : :
110 : 0 : CSS1Selector( CSS1SelectorType eTyp, const String &rSel )
111 : 0 : : eType(eTyp), aSelector( rSel ), pNext( 0 )
112 : 0 : {}
113 : :
114 : : ~CSS1Selector();
115 : :
116 : 0 : CSS1SelectorType GetType() const { return eType; }
117 : 0 : const String& GetString() const { return aSelector; }
118 : :
119 : 0 : void SetNext( CSS1Selector *pNxt ) { pNext = pNxt; }
120 : 0 : const CSS1Selector *GetNext() const { return pNext; }
121 : : };
122 : :
123 : : // Die folegende Klasse beschreibt einen Teil-Ausdruck einer
124 : : // CSS1-Deklaration sie besteht aus
125 : : //
126 : : // - dem Typ des Ausdrucks (entspricht dem Token)
127 : : // - dem eigentlichen Wert als String und ggf. double
128 : : // der double-Wert enthaelt das Vorzeichen fuer NUMBER und LENGTH
129 : : // - und dem Operator, mit dem er mit dem *Vorganger*-Ausdruck
130 : : // verknuepft ist.
131 : : //
132 : : struct CSS1Expression
133 : : {
134 : : sal_Unicode cOp; // Art der Verkuepfung mit dem Vorgaenger
135 : : CSS1Token eType; // der Typ des Wertes
136 : : String aValue; // und sein Wert als String
137 : : double nValue; // und als Zahl (TWIPs fuer LENGTH)
138 : : CSS1Expression *pNext; // die naechste Komponente
139 : :
140 : : public:
141 : :
142 : 0 : CSS1Expression( CSS1Token eTyp, const String &rVal,
143 : : double nVal, sal_Unicode cO = 0 )
144 : 0 : : cOp(cO), eType(eTyp), aValue(rVal), nValue(nVal), pNext(0)
145 : 0 : {}
146 : :
147 : : ~CSS1Expression();
148 : :
149 : : inline void Set( CSS1Token eTyp, const String &rVal, double nVal,
150 : : sal_Unicode cO = 0 );
151 : :
152 : 0 : CSS1Token GetType() const { return eType; }
153 : 0 : const String& GetString() const { return aValue; }
154 : 0 : double GetNumber() const { return nValue; }
155 : : inline sal_uInt32 GetULength() const;
156 : : inline sal_Int32 GetSLength() const;
157 : 0 : sal_Unicode GetOp() const { return cOp; }
158 : :
159 : :
160 : : sal_Bool GetURL( String& rURL ) const;
161 : : sal_Bool GetColor( Color &rRGB ) const;
162 : :
163 : 0 : void SetNext( CSS1Expression *pNxt ) { pNext = pNxt; }
164 : 0 : const CSS1Expression *GetNext() const { return pNext; }
165 : : };
166 : :
167 : 0 : inline void CSS1Expression::Set( CSS1Token eTyp, const String &rVal,
168 : : double nVal, sal_Unicode cO )
169 : : {
170 : 0 : cOp = cO; eType = eTyp; aValue = rVal; nValue = nVal; pNext = 0;
171 : 0 : }
172 : :
173 : 0 : inline sal_uInt32 CSS1Expression::GetULength() const
174 : : {
175 [ # # ]: 0 : return nValue < 0. ? 0UL : (sal_uInt32)(nValue + .5);
176 : : }
177 : :
178 : 0 : inline sal_Int32 CSS1Expression::GetSLength() const
179 : : {
180 [ # # ]: 0 : return (sal_Int32)(nValue + (nValue < 0. ? -.5 : .5 ));
181 : : }
182 : :
183 : : // Diese Klasse parst den Inhalt eines Style-Elements oder eine Style-Option
184 : : // und bereitet ihn ein wenig auf.
185 : : //
186 : : // Das Ergebnis des Parsers wird durch die Mehtoden SelectorParsed()
187 : : // und DeclarationParsed() an abgeleitete Parser uebergeben. Bsp:
188 : : //
189 : : // H1, H2 { font-weight: bold; text-align: right }
190 : : // | | | |
191 : : // | | | DeclP( 'text-align', 'right' )
192 : : // | | DeclP( 'font-weight', 'bold' )
193 : : // | SelP( 'H2', sal_False )
194 : : // SelP( 'H1', sal_True )
195 : : //
196 : : class CSS1Parser
197 : : {
198 : : sal_Bool bWhiteSpace : 1; // White-Space gelesen?
199 : : sal_Bool bEOF : 1; // Ende des "Files" ?
200 : :
201 : : sal_Unicode cNextCh; // naechstes Zeichen
202 : :
203 : : xub_StrLen nInPos; // aktuelle Position im Input-String
204 : :
205 : : sal_uInt32 nlLineNr; // akt. Zeilen Nummer
206 : : sal_uInt32 nlLinePos; // akt. Spalten Nummer
207 : :
208 : : double nValue; // der Wert des Tokens als Zahl
209 : :
210 : : CSS1ParserState eState; // der akteulle Zustand der Parsers
211 : : CSS1Token nToken; // das aktuelle Token
212 : :
213 : : String aIn; // der zu parsende String
214 : : String aToken; // das Token als String
215 : :
216 : : // Parsen vorbereiten
217 : : void InitRead( const String& rIn );
218 : :
219 : : // das naechste Zeichen holen
220 : : sal_Unicode GetNextChar();
221 : :
222 : : // das naechste Token holen
223 : : CSS1Token GetNextToken();
224 : :
225 : : // arbeitet der Parser noch?
226 : 0 : sal_Bool IsParserWorking() const { return CSS1_PAR_WORKING == eState; }
227 : :
228 : 0 : sal_Bool IsEOF() const { return bEOF; }
229 : :
230 : 0 : sal_uInt32 IncLineNr() { return ++nlLineNr; }
231 : 0 : sal_uInt32 IncLinePos() { return ++nlLinePos; }
232 : : inline sal_uInt32 SetLinePos( sal_uInt32 nlPos ); // inline unten
233 : :
234 : : // Parsen von Teilen der Grammatik
235 : : void ParseRule();
236 : : CSS1Selector *ParseSelector();
237 : : CSS1Expression *ParseDeclaration( String& rProperty );
238 : :
239 : : protected:
240 : :
241 : : void ParseStyleSheet();
242 : :
243 : : // Den Inhalt eines HTML-Style-Elements parsen.
244 : : // Fuer jeden Selektor und jede Deklaration wird
245 : : // SelectorParsed() bzw. DeclarationParsed() aufgerufen.
246 : : sal_Bool ParseStyleSheet( const String& rIn );
247 : :
248 : : // Den Inhalt einer HTML-Style-Option parsen.
249 : : // F�r jede Deklaration wird DeclarationParsed() aufgerufen.
250 : : sal_Bool ParseStyleOption( const String& rIn );
251 : :
252 : : // Diese Methode wird aufgerufen, wenn ein Selektor geparsed wurde
253 : : // Wenn 'bFirst' gesetzt ist, beginnt mit dem Selektor eine neue
254 : : // Deklaration. Wird sal_True zurueckgegeben, wird der Selektor
255 : : // geloscht, sonst nicht.
256 : : // Die Implementierung dieser Methode gibt nur sal_True zuruck.
257 : : virtual bool SelectorParsed( CSS1Selector* pSelector, bool bFirst );
258 : :
259 : : // Diese Methode wird fuer jede geparsete Property aufgerufen. Wird
260 : : // sal_True zurueckgegeben wird der Selektor geloscht, sonst nicht.
261 : : // Die Implementierung dieser Methode gibt nur sal_True zuruck.
262 : : virtual sal_Bool DeclarationParsed( const String& rProperty,
263 : : const CSS1Expression *pExpr );
264 : :
265 : : public:
266 : :
267 : : CSS1Parser();
268 : : virtual ~CSS1Parser();
269 : :
270 : : inline sal_uInt32 GetLineNr() const { return nlLineNr; }
271 : : inline sal_uInt32 GetLinePos() const { return nlLinePos; }
272 : : };
273 : :
274 : 0 : inline sal_uInt32 CSS1Parser::SetLinePos( sal_uInt32 nlPos )
275 : : {
276 : 0 : sal_uInt32 nlOld = nlLinePos;
277 : 0 : nlLinePos = nlPos;
278 : 0 : return nlOld;
279 : : }
280 : :
281 : :
282 : : #endif
283 : :
284 : :
285 : : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|