xref: /AOO42X/main/sw/source/filter/html/parcss1.hxx (revision 10a5df725afeb4846b5071dfeaf15b96a92b84a4)
1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 #ifndef _PARCSS1_HXX
23 #define _PARCSS1_HXX
24 
25 #include <tools/string.hxx>
26 
27 class Color;
28 
29 // Die Tokens des CSS1-Parsers
30 enum CSS1Token
31 {
32     CSS1_NULL,
33     CSS1_UNKOWN,
34 
35     CSS1_IDENT,
36     CSS1_STRING,
37     CSS1_NUMBER,
38     CSS1_PERCENTAGE,
39     CSS1_LENGTH,            // eine absolute Größe in 1/100 mm
40     CSS1_PIXLENGTH,         // eine Pixel-Größe
41     CSS1_EMS,
42     CSS1_EMX,
43     CSS1_HEXCOLOR,
44 
45     CSS1_DOT_W_WS,
46     CSS1_DOT_WO_WS,
47     CSS1_COLON,
48     CSS1_SLASH,
49     CSS1_PLUS,
50     CSS1_MINUS,
51     CSS1_OBRACE,
52     CSS1_CBRACE,
53     CSS1_SEMICOLON,
54     CSS1_COMMA,
55     CSS1_HASH,
56 
57     CSS1_IMPORT_SYM,
58 // Feature: PrintExt
59     CSS1_PAGE_SYM,
60 // /Feature: PrintExt
61 
62     CSS1_IMPORTANT_SYM,
63 
64     CSS1_URL,
65     CSS1_RGB
66 };
67 
68 
69 // die Zustände des Parsers
70 enum CSS1ParserState
71 {
72     CSS1_PAR_ACCEPTED = 0,
73     CSS1_PAR_WORKING,
74     CSS1_PAR_ERROR
75 };
76 
77 
78 enum CSS1SelectorType
79 {
80     CSS1_SELTYPE_ELEMENT,
81     CSS1_SELTYPE_ELEM_CLASS,
82     CSS1_SELTYPE_CLASS,
83     CSS1_SELTYPE_ID,
84     CSS1_SELTYPE_PSEUDO,
85 // Feature: PrintExt
86     CSS1_SELTYPE_PAGE
87 // /Feature: PrintExt
88 
89 };
90 
91 // Die folgende Klasse beschreibt einen Simple-Selector, also
92 // - einen HTML-Element-Namen
93 // - einen HTML-Element-Namen mit Klasse (durch '.' getrennt)
94 // - eine Klasse (ohne Punkt)
95 // - eine mit ID=xxx gesetzte ID aus einem HTML-Dokument
96 // oder
97 // - ein Pseudo-Element
98 //
99 // Die Simple-Sektoren werden in einer Liste zu vollständigen
100 // Selektoren verkettet
101 class CSS1Selector
102 {
103     CSS1SelectorType eType;     // Art des Selektors
104     String aSelector;           // der Selektor selbst
105     CSS1Selector *pNext;        // die nächste Komponente
106 
107 public:
108 
CSS1Selector(CSS1SelectorType eTyp,const String & rSel)109     CSS1Selector( CSS1SelectorType eTyp, const String &rSel )
110         : eType(eTyp), aSelector( rSel ), pNext( 0 )
111     {}
112 
113     ~CSS1Selector();
114 
GetType() const115     CSS1SelectorType GetType() const { return eType; }
GetString() const116     const String& GetString() const { return aSelector; }
117 
SetNext(CSS1Selector * pNxt)118     void SetNext( CSS1Selector *pNxt ) { pNext = pNxt; }
GetNext() const119     const CSS1Selector *GetNext() const { return pNext; }
120 };
121 
122 
123 // Die folgende 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 enthält das Vorzeichen für NUMBER und LENGTH
129 // - und dem Operator, mit dem er mit dem *Vorgänger*-Ausdruck
130 //   verknüpft ist.
131 //
132 struct CSS1Expression
133 {
134     sal_Unicode cOp;        // Art der Verknüpfung mit dem Vorgänger
135     CSS1Token eType;        // der Typ des Wertes
136     String aValue;          // und sein Wert als String
137     double nValue;          // und als Zahl (TWIPs für LENGTH)
138     CSS1Expression *pNext;  // die nächste Komponente
139 
140 public:
141 
CSS1ExpressionCSS1Expression142     CSS1Expression( CSS1Token eTyp, const String &rVal,
143                     double nVal, sal_Unicode cO = 0 )
144         : cOp(cO), eType(eTyp), aValue(rVal), nValue(nVal), pNext(0)
145     {}
146 
147     ~CSS1Expression();
148 
149     inline void Set( CSS1Token eTyp, const String &rVal, double nVal,
150                      sal_Unicode cO = 0 );
151 
GetTypeCSS1Expression152     CSS1Token GetType() const { return eType; }
GetStringCSS1Expression153     const String& GetString() const { return aValue; }
GetNumberCSS1Expression154     double GetNumber() const { return nValue; }
155     inline sal_uInt32 GetULength() const;
156     inline sal_Int32 GetSLength() const;
GetOpCSS1Expression157     sal_Unicode GetOp() const { return cOp; }
158 
159 
160     sal_Bool GetURL( String& rURL ) const;
161     sal_Bool GetColor( Color &rRGB ) const;
162 
SetNextCSS1Expression163     void SetNext( CSS1Expression *pNxt ) { pNext = pNxt; }
GetNextCSS1Expression164     const CSS1Expression *GetNext() const { return pNext; }
165 };
166 
Set(CSS1Token eTyp,const String & rVal,double nVal,sal_Unicode cO)167 inline void CSS1Expression::Set( CSS1Token eTyp, const String &rVal,
168                                  double nVal, sal_Unicode cO )
169 {
170     cOp = cO; eType = eTyp; aValue = rVal; nValue = nVal; pNext = 0;
171 }
172 
GetULength() const173 inline sal_uInt32 CSS1Expression::GetULength() const
174 {
175     return nValue < 0. ? 0UL : (sal_uInt32)(nValue + .5);
176 }
177 
GetSLength() const178 inline sal_Int32 CSS1Expression::GetSLength() const
179 {
180     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 Methoden SelectorParsed()
187 // und DeclarationParsed() an abgeleitete Parser übergeben. 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;                // nächstes 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 aktuelle 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 nächste Zeichen holen
220     sal_Unicode GetNextChar();
221 
222     // das nächste Token holen
223     CSS1Token GetNextToken();
224 
225     // arbeitet der Parser noch?
IsParserWorking() const226     sal_Bool IsParserWorking() const { return CSS1_PAR_WORKING == eState; }
227 
IsEOF() const228     sal_Bool IsEOF() const { return bEOF; }
229 
IncLineNr()230     sal_uInt32 IncLineNr() { return ++nlLineNr; }
IncLinePos()231     sal_uInt32 IncLinePos() { return ++nlLinePos; }
232     inline sal_uInt32 SetLineNr( sal_uInt32 nlNum );            // inline unten
233     inline sal_uInt32 SetLinePos( sal_uInt32 nlPos );           // inline unten
234 
235     // Parsen von Teilen der Grammatik
236     void ParseRule();
237     CSS1Selector *ParseSelector();
238     CSS1Expression *ParseDeclaration( String& rProperty );
239 
240 protected:
241 
242     void ParseStyleSheet();
243 
244     // Den Inhalt eines HTML-Style-Elements parsen.
245     // Für jeden Selektor und jede Deklaration wird
246     // SelectorParsed() bzw. DeclarationParsed() aufgerufen.
247     sal_Bool ParseStyleSheet( const String& rIn );
248 
249     // Den Inhalt einer HTML-Style-Option parsen.
250     // Für jede Deklaration wird DeclarationParsed() aufgerufen.
251     sal_Bool ParseStyleOption( const String& rIn );
252 
253     // Diese Methode wird aufgerufen, wenn ein Selektor geparsed wurde
254     // Wenn 'bFirst' gesetzt ist, beginnt mit dem Selektor eine neue
255     // Deklaration. Wird sal_True zurückgegeben, wird der Selektor
256     // gelöscht, sonst nicht.
257     // Die Implementierung dieser Methode gibt nur sal_True zurück.
258     virtual sal_Bool SelectorParsed( const CSS1Selector *pSelector,
259                                  sal_Bool bFirst );
260 
261     // Diese Methode wird für jede geparste Property aufgerufen. Wird
262     // sal_True zurückgegeben wird der Selektor gelöscht, sonst nicht.
263     // Die Implementierung dieser Methode gibt nur sal_True zurück.
264     virtual sal_Bool DeclarationParsed( const String& rProperty,
265                                     const CSS1Expression *pExpr );
266 
267 public:
268 
269     CSS1Parser();
270     virtual ~CSS1Parser();
271 
GetLineNr() const272     inline sal_uInt32   GetLineNr() const       { return nlLineNr; }
GetLinePos() const273     inline sal_uInt32   GetLinePos() const      { return nlLinePos; }
274 };
275 
SetLineNr(sal_uInt32 nlNum)276 inline sal_uInt32 CSS1Parser::SetLineNr( sal_uInt32 nlNum )
277 {
278     sal_uInt32 nlOld = nlLineNr;
279     nlLineNr = nlNum;
280     return nlOld;
281 }
282 
SetLinePos(sal_uInt32 nlPos)283 inline sal_uInt32 CSS1Parser::SetLinePos( sal_uInt32 nlPos )
284 {
285     sal_uInt32 nlOld = nlLinePos;
286     nlLinePos = nlPos;
287     return nlOld;
288 }
289 
290 
291 #endif
292 
293 /* vim: set noet sw=4 ts=4: */
294