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 _PARHTML_HXX
30 : : #define _PARHTML_HXX
31 : :
32 : : #include "svtools/svtdllapi.h"
33 : : #include <tools/solar.h>
34 : : #include <tools/string.hxx>
35 : : #include <svtools/svparser.hxx>
36 : :
37 : : #include <boost/ptr_container/ptr_vector.hpp>
38 : :
39 : : namespace com { namespace sun { namespace star {
40 : : namespace document {
41 : : class XDocumentProperties;
42 : : }
43 : : } } }
44 : :
45 : : class Color;
46 : : class SvNumberFormatter;
47 : : class SvKeyValueIterator;
48 : :
49 : : #define HTMLFONTSZ1_DFLT 7
50 : : #define HTMLFONTSZ2_DFLT 10
51 : : #define HTMLFONTSZ3_DFLT 12
52 : : #define HTMLFONTSZ4_DFLT 14
53 : : #define HTMLFONTSZ5_DFLT 18
54 : : #define HTMLFONTSZ6_DFLT 24
55 : : #define HTMLFONTSZ7_DFLT 36
56 : :
57 : : enum HTMLTableFrame { HTML_TF_VOID, HTML_TF_ABOVE, HTML_TF_BELOW,
58 : : HTML_TF_HSIDES, HTML_TF_LHS, HTML_TF_RHS, HTML_TF_VSIDES, HTML_TF_BOX };
59 : :
60 : : enum HTMLTableRules { HTML_TR_NONE, HTML_TR_GROUPS, HTML_TR_ROWS,
61 : : HTML_TR_COLS, HTML_TR_ALL };
62 : :
63 : : enum HTMLInputType
64 : : {
65 : : HTML_IT_TEXT = 0x01,
66 : : HTML_IT_PASSWORD = 0x02,
67 : : HTML_IT_CHECKBOX = 0x03,
68 : : HTML_IT_RADIO = 0x04,
69 : : HTML_IT_RANGE = 0x05,
70 : : HTML_IT_SCRIBBLE = 0x06,
71 : : HTML_IT_FILE = 0x07,
72 : : HTML_IT_HIDDEN = 0x08,
73 : : HTML_IT_SUBMIT = 0x09,
74 : : HTML_IT_IMAGE = 0x0a,
75 : : HTML_IT_RESET = 0x0b,
76 : : HTML_IT_BUTTON = 0x0c
77 : : };
78 : :
79 : : enum HTMLScriptLanguage
80 : : {
81 : : HTML_SL_STARBASIC,
82 : : HTML_SL_JAVASCRIPT,
83 : : HTML_SL_UNKNOWN
84 : : };
85 : :
86 : : struct HTMLOptionEnum
87 : : {
88 : : const sal_Char *pName; // Wert einer HTML-Option
89 : : sal_uInt16 nValue; // und der dazugehoerige Wert eines Enums
90 : : };
91 : :
92 : : // Repraesentation einer HTML-Option (=Atrribut in einem Start-Tag)
93 : : // Die Werte der Optionen werden immer als String gespeichert.
94 : : // Die Methoden GetNumber, ... duerfen nur aufgerufen werden, wenn
95 : : // die Option auch numerisch, ... ist.
96 : :
97 [ + - ]: 20 : class SVT_DLLPUBLIC HTMLOption
98 : : {
99 : : String aValue; // der Wert der Option (immer als String)
100 : : String aToken; // der Name der Option als String
101 : : sal_uInt16 nToken; // und das entsprechende Token
102 : :
103 : : public:
104 : :
105 : : HTMLOption( sal_uInt16 nTyp, const String& rToken, const String& rValue );
106 : :
107 : : // der Name der Option ...
108 : 28 : sal_uInt16 GetToken() const { return nToken; } // ... als Enum
109 : 0 : const String& GetTokenString() const { return aToken; } // ... als String
110 : :
111 : : // der Wert der Option ...
112 : 28 : const String& GetString() const { return aValue; } // ... als String
113 : :
114 : : sal_uInt32 GetNumber() const; // ... als Zahl
115 : : sal_Int32 GetSNumber() const; // ... als Zahl
116 : : void GetNumbers( std::vector<sal_uInt32> &rNumbers, // ... als Zahlen
117 : : bool bSpaceDelim=false ) const;
118 : : void GetColor( Color& ) const; // ... als Farbe
119 : :
120 : : // ... als Enum pOptEnums ist ein HTMLOptionEnum-Array
121 : : sal_uInt16 GetEnum( const HTMLOptionEnum *pOptEnums,
122 : : sal_uInt16 nDflt=0 ) const;
123 : : bool GetEnum( sal_uInt16 &rEnum, const HTMLOptionEnum *pOptEnums ) const;
124 : :
125 : : // ... und als ein par spezielle Enums
126 : : HTMLInputType GetInputType() const; // <INPUT TYPE=...>
127 : : HTMLTableFrame GetTableFrame() const; // <TABLE FRAME=...>
128 : : HTMLTableRules GetTableRules() const; // <TABLE RULES=...>
129 : : //SvxAdjust GetAdjust() const; // <P,TH,TD ALIGN=>
130 : : };
131 : :
132 : : typedef ::boost::ptr_vector<HTMLOption> HTMLOptions;
133 : :
134 : : class SVT_DLLPUBLIC HTMLParser : public SvParser
135 : : {
136 : : private:
137 : : mutable HTMLOptions maOptions; // die Optionen des Start-Tags
138 : :
139 : : bool bNewDoc : 1; // neues Doc lesen ?
140 : : bool bIsInHeader : 1; // scanne Header-Bereich
141 : : bool bIsInBody : 1; // scanne Body-Bereich
142 : : bool bReadListing : 1; // Lese Listings
143 : : bool bReadXMP : 1; // Lese XMP
144 : : bool bReadPRE : 1; // Lese preformatted Text
145 : : bool bReadTextArea : 1; // Lese TEXTAREA
146 : : bool bReadScript : 1; // Lesen von <SCRIPT>
147 : : bool bReadStyle : 1; // Lesen von <STYLE>
148 : : bool bEndTokenFound : 1; // </SCRIPT> oder </STYLE> gefunden
149 : :
150 : : bool bPre_IgnoreNewPara : 1; // Flags fuers lesen von PRE-Absaetzen
151 : : bool bReadNextChar : 1; // true: NextChar nochmals lesen (JavaScript!)
152 : : bool bReadComment : 1; // true: NextChar nochmals lesen (JavaScript!)
153 : :
154 : : sal_uInt32 nPre_LinePos; // Pos in der Line im PRE-Tag
155 : :
156 : : String aEndToken;
157 : :
158 : : protected:
159 : : String sSaveToken; // das gelesene Tag als String
160 : :
161 : : int ScanText( const sal_Unicode cBreak = 0U );
162 : :
163 : : int _GetNextRawToken();
164 : :
165 : : // scanne das naechste Token,
166 : : virtual int _GetNextToken();
167 : :
168 : : virtual ~HTMLParser();
169 : :
170 : 2 : void FinishHeader( bool bBody ) { bIsInHeader = false; bIsInBody = bBody; }
171 : :
172 : : public:
173 : : HTMLParser( SvStream& rIn, bool bReadNewDoc = true );
174 : :
175 : : virtual SvParserState CallParser(); // Aufruf des Parsers
176 : :
177 : 38 : bool IsNewDoc() const { return bNewDoc; }
178 : 2 : bool IsInHeader() const { return bIsInHeader; }
179 : : bool IsInBody() const { return bIsInBody; }
180 : : bool IsValidSyntax() const { return true; }
181 : 0 : bool IsReadListing() const { return bReadListing; }
182 : 0 : bool IsReadXMP() const { return bReadXMP; }
183 : 90 : bool IsReadPRE() const { return bReadPRE; }
184 : 0 : bool IsReadScript() const { return bReadScript; }
185 : 0 : bool IsReadStyle() const { return bReadStyle; }
186 : :
187 : : void SetReadNextChar() { bReadNextChar = true; }
188 : :
189 : : // PRE-/LISTING oder XMP-Modus starten/beenden oder Tags entsprechend
190 : : // filtern
191 : : inline void StartPRE( bool bRestart=false );
192 : 0 : void FinishPRE() { bReadPRE = false; }
193 : : int FilterPRE( int nToken );
194 : :
195 : : inline void StartListing( bool bRestart=false );
196 : 0 : void FinishListing() { bReadListing = false; }
197 : : int FilterListing( int nToken );
198 : :
199 : : inline void StartXMP( bool bRestart=false );
200 : 0 : void FinishXMP() { bReadXMP = false; }
201 : : int FilterXMP( int nToken );
202 : :
203 : 0 : void FinishTextArea() { bReadTextArea = false; }
204 : :
205 : : // PRE-/LSITING- und XMP-Modus beenden
206 : 0 : void FinishPREListingXMP() { bReadPRE = bReadListing = bReadXMP = false; }
207 : :
208 : : // Das aktuelle Token dem aktuellen Modus (PRE, XMP, ...) entsprechend
209 : : // Filtern und die Flags setzen. Wird von Continue aufgerufen, bevor
210 : : // NextToken gerufen wird. Wer eigene Schleifen implementiert bzw.
211 : : // selbst NextToken aufruft, sollte diese Methode vorher rufen.
212 : : int FilterToken( int nToken );
213 : :
214 : : // Scannen eines Scripts beenden (sollte nur unmittelbar nach dem
215 : : // Lesen eines <SCRIPT> aufgerufen werden
216 : : void EndScanScript() { bReadScript = false; }
217 : :
218 : 0 : void ReadRawData( const sal_Char *pEndToken ) { aEndToken.AssignAscii(pEndToken); }
219 : :
220 : : // Token ohne \-Sequenzen
221 : : void UnescapeToken();
222 : :
223 : : // Ermitteln der Optionen. pNoConvertToken ist das optionale Token
224 : : // einer Option, fuer die CR/LFs nicht aus dem Wert der Option
225 : : // geloescht werden.
226 : : const HTMLOptions& GetOptions( sal_uInt16 *pNoConvertToken=0 ) const;
227 : :
228 : : // fuers asynchrone lesen aus dem SvStream
229 : : virtual void Continue( int nToken );
230 : :
231 : :
232 : : protected:
233 : :
234 : : static rtl_TextEncoding GetEncodingByMIME( const String& rMime );
235 : :
236 : : /// template method: called when ParseMetaOptions adds a user-defined meta
237 : : virtual void AddMetaUserDefined( ::rtl::OUString const & i_rMetaName );
238 : :
239 : : private:
240 : : /// parse meta options into XDocumentProperties and encoding
241 : : bool ParseMetaOptionsImpl( const ::com::sun::star::uno::Reference<
242 : : ::com::sun::star::document::XDocumentProperties>&,
243 : : SvKeyValueIterator*,
244 : : const HTMLOptions&,
245 : : rtl_TextEncoding& rEnc );
246 : :
247 : : public:
248 : : /// overriding method must call this implementation!
249 : : virtual bool ParseMetaOptions( const ::com::sun::star::uno::Reference<
250 : : ::com::sun::star::document::XDocumentProperties>&,
251 : : SvKeyValueIterator* );
252 : :
253 : : // Ist der uebergebene 0-terminierte String (vermutlich) der Anfang
254 : : // eines HTML-Files? Er sollte mind. 80 Zeichen lang sein.
255 : : // Mit Ausnahme des Falls, dass SwitchToUCS2==false und
256 : : // SVPAR_CS_DONTKNOW uebergeben wird muss der String mit zwei(!)
257 : : // 0-Bytes an einer geraden(!) Position terminiert sein.
258 : : static bool IsHTMLFormat( const sal_Char* pHeader,
259 : : bool bSwitchToUCS2 = false,
260 : : rtl_TextEncoding eEnc=RTL_TEXTENCODING_DONTKNOW );
261 : :
262 : : bool ParseScriptOptions( String& rLangString, const String&, HTMLScriptLanguage& rLang,
263 : : String& rSrc, String& rLibrary, String& rModule );
264 : :
265 : : // Einen Kommentar um den Inhalt von <SCRIPT> oder <STYLE> entfernen
266 : : // Bei 'bFull' wird ggf. die gesammte Zeile hinter einem "<!--"
267 : : // entfernt (fuer JavaSript)
268 : : static void RemoveSGMLComment( String &rString, sal_Bool bFull );
269 : :
270 : : static bool InternalImgToPrivateURL( String& rURL );
271 : : static rtl_TextEncoding GetEncodingByHttpHeader( SvKeyValueIterator *pHTTPHeader );
272 : : bool SetEncodingByHTTPHeader( SvKeyValueIterator *pHTTPHeader );
273 : : };
274 : :
275 : 0 : inline void HTMLParser::StartPRE( bool bRestart )
276 : : {
277 : 0 : bReadPRE = true;
278 : 0 : bPre_IgnoreNewPara = !bRestart;
279 : 0 : nPre_LinePos = 0UL;
280 : 0 : }
281 : :
282 : 0 : inline void HTMLParser::StartListing( bool bRestart )
283 : : {
284 : 0 : bReadListing = true;
285 : 0 : bPre_IgnoreNewPara = !bRestart;
286 : 0 : nPre_LinePos = 0UL;
287 : 0 : }
288 : :
289 : 0 : inline void HTMLParser::StartXMP( bool bRestart )
290 : : {
291 : 0 : bReadXMP = true;
292 : 0 : bPre_IgnoreNewPara = !bRestart;
293 : 0 : nPre_LinePos = 0UL;
294 : 0 : }
295 : :
296 : : #endif
297 : :
298 : : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|