xref: /AOO41X/main/sw/source/filter/html/parcss1.hxx (revision 1d2dbeb0b7301723c6d13094e87a8714ef81a328)
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 
23 
24 #ifndef _PARCSS1_HXX
25 #define _PARCSS1_HXX
26 
27 #include <tools/string.hxx>
28 
29 class Color;
30 
31 /*  */
32 
33 // Die Tokens des CSS1-Parsers
34 enum CSS1Token
35 {
36     CSS1_NULL,
37     CSS1_UNKOWN,
38 
39     CSS1_IDENT,
40     CSS1_STRING,
41     CSS1_NUMBER,
42     CSS1_PERCENTAGE,
43     CSS1_LENGTH,            // eine absolute Groesse in 1/100 MM
44     CSS1_PIXLENGTH,         // eine Pixel-Groesse
45     CSS1_EMS,
46     CSS1_EMX,
47     CSS1_HEXCOLOR,
48 
49     CSS1_DOT_W_WS,
50     CSS1_DOT_WO_WS,
51     CSS1_COLON,
52     CSS1_SLASH,
53     CSS1_PLUS,
54     CSS1_MINUS,
55     CSS1_OBRACE,
56     CSS1_CBRACE,
57     CSS1_SEMICOLON,
58     CSS1_COMMA,
59     CSS1_HASH,
60 
61     CSS1_IMPORT_SYM,
62 // Feature: PrintExt
63     CSS1_PAGE_SYM,
64 // /Feature: PrintExt
65 
66     CSS1_IMPORTANT_SYM,
67 
68     CSS1_URL,
69     CSS1_RGB
70 };
71 
72 
73 // die Zustaende des Parsers
74 enum CSS1ParserState
75 {
76     CSS1_PAR_ACCEPTED = 0,
77     CSS1_PAR_WORKING,
78     CSS1_PAR_ERROR
79 };
80 
81 
82 /*  */
83 
84 enum CSS1SelectorType
85 {
86     CSS1_SELTYPE_ELEMENT,
87     CSS1_SELTYPE_ELEM_CLASS,
88     CSS1_SELTYPE_CLASS,
89     CSS1_SELTYPE_ID,
90     CSS1_SELTYPE_PSEUDO,
91 // Feature: PrintExt
92     CSS1_SELTYPE_PAGE
93 // /Feature: PrintExt
94 
95 };
96 
97 // Die folegende Klasse beschreibt einen Simple-Selector, also
98 // - einen HTML-Element-Namen
99 // - einen HTML-Element-Namen mit Klasse (durch '.' getrennt)
100 // - eine Klasse (ohne Punkt)
101 // - eine mit ID=xxx gesetzte ID aus einem HTML-Dokument
102 // oder
103 // - ein Pseudo-Element
104 //
105 // Die Simple-Sektoren werden in einer Liste zu vollstaendigen
106 // Selektoren verkettet
107 class CSS1Selector
108 {
109     CSS1SelectorType eType;     // Art des Selektors
110     String aSelector;           // der Selektor selbst
111     CSS1Selector *pNext;        // die naechste Komponente
112 
113 public:
114 
CSS1Selector(CSS1SelectorType eTyp,const String & rSel)115     CSS1Selector( CSS1SelectorType eTyp, const String &rSel )
116         : eType(eTyp), aSelector( rSel ), pNext( 0 )
117     {}
118 
119     ~CSS1Selector();
120 
GetType() const121     CSS1SelectorType GetType() const { return eType; }
GetString() const122     const String& GetString() const { return aSelector; }
123 
SetNext(CSS1Selector * pNxt)124     void SetNext( CSS1Selector *pNxt ) { pNext = pNxt; }
GetNext() const125     const CSS1Selector *GetNext() const { return pNext; }
126 };
127 
128 
129 /*  */
130 
131 // Die folegende Klasse beschreibt einen Teil-Ausdruck einer
132 // CSS1-Deklaration sie besteht aus
133 //
134 // - dem Typ des Ausdrucks (entspricht dem Token)
135 // - dem eigentlichen Wert als String und ggf. double
136 //   der double-Wert enthaelt das Vorzeichen fuer NUMBER und LENGTH
137 // - und dem Operator, mit dem er mit dem *Vorganger*-Ausdruck
138 //   verknuepft ist.
139 //
140 struct CSS1Expression
141 {
142     sal_Unicode cOp;        // Art der Verkuepfung mit dem Vorgaenger
143     CSS1Token eType;        // der Typ des Wertes
144     String aValue;          // und sein Wert als String
145     double nValue;          // und als Zahl (TWIPs fuer LENGTH)
146     CSS1Expression *pNext;  // die naechste Komponente
147 
148 public:
149 
CSS1ExpressionCSS1Expression150     CSS1Expression( CSS1Token eTyp, const String &rVal,
151                     double nVal, sal_Unicode cO = 0 )
152         : cOp(cO), eType(eTyp), aValue(rVal), nValue(nVal), pNext(0)
153     {}
154 
155     ~CSS1Expression();
156 
157     inline void Set( CSS1Token eTyp, const String &rVal, double nVal,
158                      sal_Unicode cO = 0 );
159 
GetTypeCSS1Expression160     CSS1Token GetType() const { return eType; }
GetStringCSS1Expression161     const String& GetString() const { return aValue; }
GetNumberCSS1Expression162     double GetNumber() const { return nValue; }
163     inline sal_uInt32 GetULength() const;
164     inline sal_Int32 GetSLength() const;
GetOpCSS1Expression165     sal_Unicode GetOp() const { return cOp; }
166 
167 
168     sal_Bool GetURL( String& rURL ) const;
169     sal_Bool GetColor( Color &rRGB ) const;
170 
SetNextCSS1Expression171     void SetNext( CSS1Expression *pNxt ) { pNext = pNxt; }
GetNextCSS1Expression172     const CSS1Expression *GetNext() const { return pNext; }
173 };
174 
Set(CSS1Token eTyp,const String & rVal,double nVal,sal_Unicode cO)175 inline void CSS1Expression::Set( CSS1Token eTyp, const String &rVal,
176                                  double nVal, sal_Unicode cO )
177 {
178     cOp = cO; eType = eTyp; aValue = rVal; nValue = nVal; pNext = 0;
179 }
180 
GetULength() const181 inline sal_uInt32 CSS1Expression::GetULength() const
182 {
183     return nValue < 0. ? 0UL : (sal_uInt32)(nValue + .5);
184 }
185 
GetSLength() const186 inline sal_Int32 CSS1Expression::GetSLength() const
187 {
188     return (sal_Int32)(nValue + (nValue < 0. ? -.5 : .5 ));
189 }
190 
191 /*  */
192 
193 // Diese Klasse parst den Inhalt eines Style-Elements oder eine Style-Option
194 // und bereitet ihn ein wenig auf.
195 //
196 // Das Ergebnis des Parsers wird durch die Mehtoden SelectorParsed()
197 // und DeclarationParsed() an abgeleitete Parser uebergeben. Bsp:
198 //
199 // H1, H2 { font-weight: bold; text-align: right }
200 //  |  |                    |                  |
201 //  |  |                    |                  DeclP( 'text-align', 'right' )
202 //  |  |                    DeclP( 'font-weight', 'bold' )
203 //  |  SelP( 'H2', sal_False )
204 //  SelP( 'H1', sal_True )
205 //
206 class CSS1Parser
207 {
208     sal_Bool bWhiteSpace : 1;           // White-Space gelesen?
209     sal_Bool bEOF : 1;                  // Ende des "Files" ?
210 
211     sal_Unicode cNextCh;                // naechstes Zeichen
212 
213     xub_StrLen nInPos;                  // aktuelle Position im Input-String
214 
215     sal_uInt32 nlLineNr;                    // akt. Zeilen Nummer
216     sal_uInt32 nlLinePos;               // akt. Spalten Nummer
217 
218     double nValue;                  // der Wert des Tokens als Zahl
219 
220     CSS1ParserState eState;         // der akteulle Zustand der Parsers
221     CSS1Token nToken;               // das aktuelle Token
222 
223     String aIn;                     // der zu parsende String
224     String aToken;                  // das Token als String
225 
226     // Parsen vorbereiten
227     void InitRead( const String& rIn );
228 
229     // das naechste Zeichen holen
230     sal_Unicode GetNextChar();
231 
232     // das naechste Token holen
233     CSS1Token GetNextToken();
234 
235     // arbeitet der Parser noch?
IsParserWorking() const236     sal_Bool IsParserWorking() const { return CSS1_PAR_WORKING == eState; }
237 
IsEOF() const238     sal_Bool IsEOF() const { return bEOF; }
239 
IncLineNr()240     sal_uInt32 IncLineNr() { return ++nlLineNr; }
IncLinePos()241     sal_uInt32 IncLinePos() { return ++nlLinePos; }
242     inline sal_uInt32 SetLineNr( sal_uInt32 nlNum );            // inline unten
243     inline sal_uInt32 SetLinePos( sal_uInt32 nlPos );           // inline unten
244 
245     // Parsen von Teilen der Grammatik
246     void ParseRule();
247     CSS1Selector *ParseSelector();
248     CSS1Expression *ParseDeclaration( String& rProperty );
249 
250 protected:
251 
252     void ParseStyleSheet();
253 
254     // Den Inhalt eines HTML-Style-Elements parsen.
255     // Fuer jeden Selektor und jede Deklaration wird
256     // SelectorParsed() bzw. DeclarationParsed() aufgerufen.
257     sal_Bool ParseStyleSheet( const String& rIn );
258 
259     // Den Inhalt einer HTML-Style-Option parsen.
260     // F�r jede Deklaration wird DeclarationParsed() aufgerufen.
261     sal_Bool ParseStyleOption( const String& rIn );
262 
263     // Diese Methode wird aufgerufen, wenn ein Selektor geparsed wurde
264     // Wenn 'bFirst' gesetzt ist, beginnt mit dem Selektor eine neue
265     // Deklaration. Wird sal_True zurueckgegeben, wird der Selektor
266     // geloscht, sonst nicht.
267     // Die Implementierung dieser Methode gibt nur sal_True zuruck.
268     virtual sal_Bool SelectorParsed( const CSS1Selector *pSelector,
269                                  sal_Bool bFirst );
270 
271     // Diese Methode wird fuer jede geparsete Property aufgerufen. Wird
272     // sal_True zurueckgegeben wird der Selektor geloscht, sonst nicht.
273     // Die Implementierung dieser Methode gibt nur sal_True zuruck.
274     virtual sal_Bool DeclarationParsed( const String& rProperty,
275                                     const CSS1Expression *pExpr );
276 
277 public:
278 
279     CSS1Parser();
280     virtual ~CSS1Parser();
281 
GetLineNr() const282     inline sal_uInt32   GetLineNr() const       { return nlLineNr; }
GetLinePos() const283     inline sal_uInt32   GetLinePos() const      { return nlLinePos; }
284 };
285 
SetLineNr(sal_uInt32 nlNum)286 inline sal_uInt32 CSS1Parser::SetLineNr( sal_uInt32 nlNum )
287 {
288     sal_uInt32 nlOld = nlLineNr;
289     nlLineNr = nlNum;
290     return nlOld;
291 }
292 
SetLinePos(sal_uInt32 nlPos)293 inline sal_uInt32 CSS1Parser::SetLinePos( sal_uInt32 nlPos )
294 {
295     sal_uInt32 nlOld = nlLinePos;
296     nlLinePos = nlPos;
297     return nlOld;
298 }
299 
300 
301 #endif
302 
303 
304